Skip to content

Commit 16b2177

Browse files
authored
Use snapshots in a few more key places (#393)
* Use snapshots in a few more key places * Revise version
1 parent 5c89f23 commit 16b2177

File tree

2 files changed

+114
-93
lines changed

2 files changed

+114
-93
lines changed

tests/testthat/_snaps/register.md

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,109 @@
1+
# get_call_entries: returns an empty string for packages with .Call entries and NAMESPACE files
2+
3+
Code
4+
call_entries
5+
Output
6+
[1] "/* .Call calls */"
7+
[2] "extern SEXP bar(void);"
8+
[3] ""
9+
[4] "static const R_CallMethodDef CallEntries[] = {"
10+
[5] " {\"bar\", (DL_FUNC) &bar, 0},"
11+
[6] " {NULL, NULL, 0}"
12+
[7] "};"
13+
14+
# get_call_entries: works with multiple register functions.
15+
16+
Code
17+
cat(read_file(cpp_bindings))
18+
Output
19+
// Generated by cpp11: do not edit by hand
20+
// clang-format off
21+
22+
23+
#include "cpp11/declarations.hpp"
24+
#include <R_ext/Visibility.h>
25+
26+
// multiple.cpp
27+
int foo();
28+
extern "C" SEXP _testPkg_foo() {
29+
BEGIN_CPP11
30+
return cpp11::as_sexp(foo());
31+
END_CPP11
32+
}
33+
// multiple.cpp
34+
double bar(bool run);
35+
extern "C" SEXP _testPkg_bar(SEXP run) {
36+
BEGIN_CPP11
37+
return cpp11::as_sexp(bar(cpp11::as_cpp<cpp11::decay_t<bool>>(run)));
38+
END_CPP11
39+
}
40+
// multiple.cpp
41+
bool baz(bool run, int value);
42+
extern "C" SEXP _testPkg_baz(SEXP run, SEXP value) {
43+
BEGIN_CPP11
44+
return cpp11::as_sexp(baz(cpp11::as_cpp<cpp11::decay_t<bool>>(run), cpp11::as_cpp<cpp11::decay_t<int>>(value)));
45+
END_CPP11
46+
}
47+
48+
extern "C" {
49+
static const R_CallMethodDef CallEntries[] = {
50+
{"_testPkg_bar", (DL_FUNC) &_testPkg_bar, 1},
51+
{"_testPkg_baz", (DL_FUNC) &_testPkg_baz, 2},
52+
{"_testPkg_foo", (DL_FUNC) &_testPkg_foo, 0},
53+
{NULL, NULL, 0}
54+
};
55+
}
56+
57+
extern "C" attribute_visible void R_init_testPkg(DllInfo* dll){
58+
R_registerRoutines(dll, NULL, CallEntries, NULL, NULL);
59+
R_useDynamicSymbols(dll, FALSE);
60+
R_forceSymbols(dll, TRUE);
61+
}
62+
63+
# cpp_register: works with a package that registers a single c++ function
64+
65+
Code
66+
cat(read_file(r_bindings))
67+
Output
68+
# Generated by cpp11: do not edit by hand
69+
70+
foo <- function() {
71+
.Call(`_testPkg_foo`)
72+
}
73+
74+
---
75+
76+
Code
77+
cat(read_file(cpp_bindings))
78+
Output
79+
// Generated by cpp11: do not edit by hand
80+
// clang-format off
81+
82+
83+
#include "cpp11/declarations.hpp"
84+
#include <R_ext/Visibility.h>
85+
86+
// single.cpp
87+
int foo();
88+
extern "C" SEXP _testPkg_foo() {
89+
BEGIN_CPP11
90+
return cpp11::as_sexp(foo());
91+
END_CPP11
92+
}
93+
94+
extern "C" {
95+
static const R_CallMethodDef CallEntries[] = {
96+
{"_testPkg_foo", (DL_FUNC) &_testPkg_foo, 0},
97+
{NULL, NULL, 0}
98+
};
99+
}
100+
101+
extern "C" attribute_visible void R_init_testPkg(DllInfo* dll){
102+
R_registerRoutines(dll, NULL, CallEntries, NULL, NULL);
103+
R_useDynamicSymbols(dll, FALSE);
104+
R_forceSymbols(dll, TRUE);
105+
}
106+
1107
# cpp_register: can be run with messages
2108

3109
Code

tests/testthat/test-register.R

Lines changed: 8 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -50,25 +50,18 @@ describe("get_call_entries", {
5050
})
5151

5252
it("returns an empty string for packages with .Call entries and NAMESPACE files", {
53-
5453
# tools::package_native_routine_registration_skeleton is not available before R 3.4
55-
skip_if(getRversion() < "3.4")
54+
# R added `(void)` to the signature after R 4.3.0
55+
skip_if(getRversion() < "4.3.0")
5656

5757
pkg <- local_package()
5858
path <- pkg_path(pkg)
5959
dir.create(file.path(path, "R"))
60+
6061
writeLines('foo <- function() .Call("bar")', file.path(path, "R", "foo.R"))
6162
call_entries <- get_call_entries(path, get_funs(path)$name, get_package_name(path))
62-
# R added `(void)` to the signature after R 4.2.1
63-
expect_match(call_entries[2], "extern SEXP bar[(](void)?[)]")
64-
expect_equal(
65-
call_entries[4:7],
66-
c("static const R_CallMethodDef CallEntries[] = {",
67-
" {\"bar\", (DL_FUNC) &bar, 0},",
68-
" {NULL, NULL, 0}",
69-
"};"
70-
)
71-
)
63+
64+
expect_snapshot(call_entries)
7265
})
7366
it("works with multiple register functions.", {
7467
pkg <- local_package()
@@ -78,51 +71,7 @@ describe("get_call_entries", {
7871

7972
cpp_register(p)
8073
cpp_bindings <- file.path(p, "src", "cpp11.cpp")
81-
expect_equal(read_file(cpp_bindings),
82-
"// Generated by cpp11: do not edit by hand
83-
// clang-format off
84-
85-
86-
#include \"cpp11/declarations.hpp\"
87-
#include <R_ext/Visibility.h>
88-
89-
// multiple.cpp
90-
int foo();
91-
extern \"C\" SEXP _testPkg_foo() {
92-
BEGIN_CPP11
93-
return cpp11::as_sexp(foo());
94-
END_CPP11
95-
}
96-
// multiple.cpp
97-
double bar(bool run);
98-
extern \"C\" SEXP _testPkg_bar(SEXP run) {
99-
BEGIN_CPP11
100-
return cpp11::as_sexp(bar(cpp11::as_cpp<cpp11::decay_t<bool>>(run)));
101-
END_CPP11
102-
}
103-
// multiple.cpp
104-
bool baz(bool run, int value);
105-
extern \"C\" SEXP _testPkg_baz(SEXP run, SEXP value) {
106-
BEGIN_CPP11
107-
return cpp11::as_sexp(baz(cpp11::as_cpp<cpp11::decay_t<bool>>(run), cpp11::as_cpp<cpp11::decay_t<int>>(value)));
108-
END_CPP11
109-
}
110-
111-
extern \"C\" {
112-
static const R_CallMethodDef CallEntries[] = {
113-
{\"_testPkg_bar\", (DL_FUNC) &_testPkg_bar, 1},
114-
{\"_testPkg_baz\", (DL_FUNC) &_testPkg_baz, 2},
115-
{\"_testPkg_foo\", (DL_FUNC) &_testPkg_foo, 0},
116-
{NULL, NULL, 0}
117-
};
118-
}
119-
120-
extern \"C\" attribute_visible void R_init_testPkg(DllInfo* dll){
121-
R_registerRoutines(dll, NULL, CallEntries, NULL, NULL);
122-
R_useDynamicSymbols(dll, FALSE);
123-
R_forceSymbols(dll, TRUE);
124-
}
125-
")
74+
expect_snapshot(cat(read_file(cpp_bindings)))
12675
})
12776
})
12877

@@ -569,44 +518,11 @@ describe("cpp_register", {
569518

570519
r_bindings <- file.path(p, "R", "cpp11.R")
571520
expect_true(file.exists(r_bindings))
572-
expect_equal(read_file(r_bindings),
573-
"# Generated by cpp11: do not edit by hand
521+
expect_snapshot(cat(read_file(r_bindings)))
574522

575-
foo <- function() {
576-
.Call(`_testPkg_foo`)
577-
}
578-
")
579523
cpp_bindings <- file.path(p, "src", "cpp11.cpp")
580524
expect_true(file.exists(cpp_bindings))
581-
expect_equal(read_file(cpp_bindings),
582-
"// Generated by cpp11: do not edit by hand
583-
// clang-format off
584-
585-
586-
#include \"cpp11/declarations.hpp\"
587-
#include <R_ext/Visibility.h>
588-
589-
// single.cpp
590-
int foo();
591-
extern \"C\" SEXP _testPkg_foo() {
592-
BEGIN_CPP11
593-
return cpp11::as_sexp(foo());
594-
END_CPP11
595-
}
596-
597-
extern \"C\" {
598-
static const R_CallMethodDef CallEntries[] = {
599-
{\"_testPkg_foo\", (DL_FUNC) &_testPkg_foo, 0},
600-
{NULL, NULL, 0}
601-
};
602-
}
603-
604-
extern \"C\" attribute_visible void R_init_testPkg(DllInfo* dll){
605-
R_registerRoutines(dll, NULL, CallEntries, NULL, NULL);
606-
R_useDynamicSymbols(dll, FALSE);
607-
R_forceSymbols(dll, TRUE);
608-
}
609-
")
525+
expect_snapshot(cat(read_file(cpp_bindings)))
610526
})
611527

612528
it("can be run without messages", {
@@ -617,7 +533,6 @@ extern \"C\" attribute_visible void R_init_testPkg(DllInfo* dll){
617533
expect_silent(cpp_register(p, quiet = TRUE))
618534
})
619535

620-
621536
it("can be run with messages", {
622537
local_reproducible_output()
623538
pkg <- local_package()

0 commit comments

Comments
 (0)