Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

testthat 3e #405

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ Suggests:
glue,
knitr,
lobstr,
mockery,
progress,
rmarkdown,
scales,
Expand Down
6 changes: 6 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# cpp11 (development version)

# cpp11 0.5.1.9000

* Uses testthat 3e style tests (#402).
* Removes the mockery dependence.
* The vendoring function accepts a custom path (i.e., to use the GitHub version of the package)
*
# cpp11 0.5.1

* cpp11 now requires R >=4.0.0, in line with the
Expand Down
11 changes: 7 additions & 4 deletions R/source.R
Original file line number Diff line number Diff line change
Expand Up @@ -163,12 +163,15 @@ generate_cpp_name <- function(name, loaded_dlls = c("cpp11", names(getLoadedDLLs
sprintf("%s.%s", new_name, ext)
}



generate_include_paths <- function(packages) {
generate_include_paths <- function(packages, custom_path = NULL) {
out <- character(length(packages))
for (i in seq_along(packages)) {
path <- system.file(package = packages[[i]], "include")
if (!is.null(custom_path)) {
path <- custom_path
} else {
path <- system.file(package = packages[[i]], "include")
}

if (is_windows()) {
path <- utils::shortPathName(path)
}
Expand Down
11 changes: 7 additions & 4 deletions R/vendor.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
#' code until you run `cpp_vendor()` again.
#'
#' @inheritParams cpp_register
#' @param headers The path to the cpp11 headers to vendor. By default this is
#' the path where R installed the cpp11 package. You can change this to
#' use a different version of cpp11, such as as the development version
#' from GitHub.
#' @return The file path to the vendored code (invisibly).
#' @export
#' @examples
Expand All @@ -30,7 +34,7 @@
#'
#' # cleanup
#' unlink(dir, recursive = TRUE)
cpp_vendor <- function(path = ".") {
cpp_vendor <- function(path = ".", headers = system.file("include", "cpp11", package = "cpp11")) {
new <- file.path(path, "inst", "include", "cpp11")

if (dir.exists(new)) {
Expand All @@ -39,16 +43,15 @@ cpp_vendor <- function(path = ".") {

dir.create(new , recursive = TRUE, showWarnings = FALSE)

current <- system.file("include", "cpp11", package = "cpp11")
if (!nzchar(current)) {
if (!nzchar(headers)) {
stop("cpp11 is not installed", call. = FALSE)
}

cpp11_version <- utils::packageVersion("cpp11")

cpp11_header <- sprintf("// cpp11 version: %s\n// vendored on: %s", cpp11_version, Sys.Date())

files <- list.files(current, full.names = TRUE)
files <- list.files(headers, full.names = TRUE)

writeLines(
c(cpp11_header, readLines(system.file("include", "cpp11.hpp", package = "cpp11"))),
Expand Down
10 changes: 9 additions & 1 deletion man/cpp_vendor.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions tests/testthat.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
# This file is part of the standard setup for testthat.
# It is recommended that you do not modify it.
#
# Where should you do additional test configuration?
# Learn more about the roles of various files in:
# * https://r-pkgs.org/testing-design.html#sec-tests-files-overview
# * https://testthat.r-lib.org/articles/special-files.html

library(testthat)
library(cpp11)

Expand Down
8 changes: 4 additions & 4 deletions tests/testthat/_snaps/register.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# get_call_entries: returns an empty string for packages with .Call entries and NAMESPACE files
# get_call_entries returns an empty string for packages with .Call entries and NAMESPACE files

Code
call_entries
Expand All @@ -11,7 +11,7 @@
[6] " {NULL, NULL, 0}"
[7] "};"

# get_call_entries: works with multiple register functions.
# get_call_entries works with multiple register functions

Code
cat(read_file(cpp_bindings))
Expand Down Expand Up @@ -60,7 +60,7 @@
R_forceSymbols(dll, TRUE);
}

# cpp_register: works with a package that registers a single c++ function
# cpp_register works with a package that registers a single C++ function

Code
cat(read_file(r_bindings))
Expand Down Expand Up @@ -104,7 +104,7 @@
R_forceSymbols(dll, TRUE);
}

# cpp_register: can be run with messages
# cpp_register can be run with messages

Code
cpp_register(p, quiet = FALSE)
Expand Down
38 changes: 18 additions & 20 deletions tests/testthat/test-knitr.R
Original file line number Diff line number Diff line change
@@ -1,24 +1,22 @@
describe("eng_cpp11", {
it("works when code is not evaluated", {
skip_on_os("solaris")
opts <- knitr::opts_chunk$get()
opts <- utils::modifyList(opts, list(eval = FALSE, engine = "cpp11", code = "1 + 1"))
test_that("eng_cpp11 works when code is not evaluated", {
skip_on_os("solaris")
opts <- knitr::opts_chunk$get()
opts <- utils::modifyList(opts, list(eval = FALSE, engine = "cpp11", code = "1 + 1"))

expect_equal(
eng_cpp11(opts),
"1 + 1"
)
})
expect_equal(
eng_cpp11(opts),
"1 + 1"
)
})

it("works when code is evaluated", {
skip_on_os("solaris")
opts <- knitr::opts_chunk$get()
code <- "[[cpp11::register]] int foo() { return 0; }"
opts <- utils::modifyList(opts, list(eval = TRUE, engine = "cpp11", code = code, quiet = TRUE))
test_that("eng_cpp11 works when code is evaluated", {
skip_on_os("solaris")
opts <- knitr::opts_chunk$get()
code <- "[[cpp11::register]] int foo() { return 0; }"
opts <- utils::modifyList(opts, list(eval = TRUE, engine = "cpp11", code = code, quiet = TRUE))

expect_equal(
eng_cpp11(opts),
code
)
})
expect_equal(
eng_cpp11(opts),
code
)
})
Loading
Loading