-
Notifications
You must be signed in to change notification settings - Fork 339
Rework run_cpp_tests() to avoid testthat internals
#2315
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
Merged
+104
−95
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
f34111f
Bump `the$test_expectations`
DavisVaughan 6d42518
Move towards less manual bookkeeping
DavisVaughan 546436e
Fix `with_description_push()` thinko
DavisVaughan aac4ad6
Use `test_code()` to avoid more internals! This also gets the `test_d…
DavisVaughan 99dde78
Recognize that `with_description_push()` + `test_code()` is exactly `…
DavisVaughan a9e9319
Recognize that we can now use `fail()`
DavisVaughan 09482ab
You really do need `<<-` here!
DavisVaughan 2c12010
Use `conditionMessage()` just in case
DavisVaughan 18d248c
Justify usage of `expectation()`
DavisVaughan 3b17e83
NEWS bullet
DavisVaughan b8c1942
Refactor into a series of parsers
DavisVaughan a9d4463
Remove unused variable
DavisVaughan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -49,124 +49,131 @@ run_cpp_tests <- function(package) { | |
| run_testthat_tests <- get_routine(package, "run_testthat_tests") | ||
|
|
||
| output <- "" | ||
| tests_passed <- TRUE | ||
| catch_error <- NULL | ||
|
|
||
| catch_error <- FALSE | ||
| tryCatch( | ||
| { | ||
| output <- capture_output_lines( | ||
| tests_passed <- .Call(run_testthat_tests, TRUE) | ||
| .Call(run_testthat_tests, TRUE) | ||
| ) | ||
| }, | ||
| error = function(e) { | ||
| catch_error <- TRUE | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| reporter <- get_reporter() | ||
|
|
||
| context_start("Catch") | ||
| reporter$start_test(context = "Catch", test = "Catch") | ||
| reporter$add_result( | ||
| context = "Catch", | ||
| test = "Catch", | ||
| result = new_expectation("failure", e$message) | ||
| ) | ||
| reporter$end_test(context = "Catch", test = "Catch") | ||
| catch_error <<- e | ||
| } | ||
| ) | ||
|
|
||
| if (catch_error) { | ||
| if (!is.null(catch_error)) { | ||
| context_start("Catch") | ||
| test_that("Catch", { | ||
| fail(conditionMessage(catch_error)) | ||
| }) | ||
| return() | ||
| } | ||
|
|
||
| report <- xml2::read_xml(paste(output, collapse = "\n")) | ||
|
|
||
| contexts <- xml2::xml_find_all(report, "//TestCase") | ||
| output <- paste(output, collapse = "\n") | ||
| contexts <- parse_catch_contexts(output) | ||
|
|
||
| for (context in contexts) { | ||
| context_name <- sub(" [|][^|]+$", "", xml2::xml_attr(context, "name")) | ||
| context_start(context$name) | ||
|
|
||
| for (test in context$tests) { | ||
| test_that(test$name, { | ||
| for (i in seq_len(test$n_successes)) { | ||
| pass() | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This now makes it very clear why order doesn't matter. |
||
| } | ||
| for (failure in test$failures) { | ||
| fail(message = failure$message, srcref = failure$srcref) | ||
| } | ||
| for (exception in test$exceptions) { | ||
| # There is no `fail()` equivalent for an error. | ||
| # We could use `stop()`, but we want to pass through a `srcref`. | ||
| expectation( | ||
| type = "error", | ||
| message = exception$message, | ||
| srcref = exception$srcref | ||
| ) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| context_start(context_name) | ||
| parse_catch_contexts <- function(text) { | ||
| xml <- xml2::read_xml(text) | ||
|
|
||
| tests <- xml2::xml_find_all(context, "./Section") | ||
| for (test in tests) { | ||
| test_name <- xml2::xml_attr(test, "name") | ||
| contexts <- xml2::xml_find_all(xml, "//TestCase") | ||
| contexts <- map(contexts, parse_catch_context) | ||
|
|
||
| result <- xml2::xml_find_first(test, "./OverallResults") | ||
| successes <- as.integer(xml2::xml_attr(result, "successes")) | ||
| contexts | ||
| } | ||
|
|
||
| get_reporter()$start_test(context = context_name, test = test_name) | ||
| parse_catch_context <- function(context) { | ||
| name <- sub(" [|][^|]+$", "", xml2::xml_attr(context, "name")) | ||
| tests <- xml2::xml_find_all(context, "./Section") | ||
| tests <- map(tests, parse_catch_test) | ||
| list(name = name, tests = tests) | ||
| } | ||
|
|
||
| for (i in seq_len(successes)) { | ||
| exp <- new_expectation("success", "") | ||
| exp$test <- test_name | ||
| get_reporter()$add_result( | ||
| context = context_name, | ||
| test = test_name, | ||
| result = exp | ||
| ) | ||
| } | ||
| parse_catch_test <- function(test) { | ||
| name <- xml2::xml_attr(test, "name") | ||
|
|
||
| failures <- xml2::xml_find_all(test, "./Expression") | ||
| for (failure in failures) { | ||
| org <- xml2::xml_find_first(failure, "Original") | ||
| org_text <- xml2::xml_text(org, trim = TRUE) | ||
|
|
||
| filename <- xml2::xml_attr(failure, "filename") | ||
| type <- xml2::xml_attr(failure, "type") | ||
|
|
||
| type_msg <- switch( | ||
| type, | ||
| "CATCH_CHECK_FALSE" = "isn't false.", | ||
| "CATCH_CHECK_THROWS" = "did not throw an exception.", | ||
| "CATCH_CHECK_THROWS_AS" = "threw an exception with unexpected type.", | ||
| "isn't true." | ||
| ) | ||
|
|
||
| org_text <- paste(org_text, type_msg) | ||
|
|
||
| line <- xml2::xml_attr(failure, "line") | ||
| failure_srcref <- srcref( | ||
| srcfile(file.path("src", filename)), | ||
| c(line, line, 1, 1) | ||
| ) | ||
|
|
||
| exp <- new_expectation("failure", org_text, srcref = failure_srcref) | ||
| exp$test <- test_name | ||
|
|
||
| get_reporter()$add_result( | ||
| context = context_name, | ||
| test = test_name, | ||
| result = exp | ||
| ) | ||
| } | ||
| overall_results <- xml2::xml_find_first(test, "./OverallResults") | ||
| n_successes <- as.integer(xml2::xml_attr(overall_results, "successes")) | ||
|
|
||
| exceptions <- xml2::xml_find_all(test, "./Exception") | ||
| for (exception in exceptions) { | ||
| exception_text <- xml2::xml_text(exception, trim = TRUE) | ||
| filename <- xml2::xml_attr(exception, "filename") | ||
| line <- xml2::xml_attr(exception, "line") | ||
|
|
||
| exception_srcref <- srcref( | ||
| srcfile(file.path("src", filename)), | ||
| c(line, line, 1, 1) | ||
| ) | ||
|
|
||
| exp <- new_expectation( | ||
| "error", | ||
| exception_text, | ||
| srcref = exception_srcref | ||
| ) | ||
| exp$test <- test_name | ||
|
|
||
| get_reporter()$add_result( | ||
| context = context_name, | ||
| test = test_name, | ||
| result = exp | ||
| ) | ||
| } | ||
| failures <- xml2::xml_find_all(test, "./Expression") | ||
| failures <- map(failures, parse_catch_failure) | ||
|
|
||
| get_reporter()$end_test(context = context_name, test = test_name) | ||
| } | ||
| } | ||
| exceptions <- xml2::xml_find_all(test, "./Exception") | ||
| exceptions <- map(exceptions, parse_catch_exception) | ||
|
|
||
| list( | ||
| name = name, | ||
| n_successes = n_successes, | ||
| failures = failures, | ||
| exceptions = exceptions | ||
| ) | ||
| } | ||
|
|
||
| parse_catch_failure <- function(failure) { | ||
| type <- switch( | ||
| xml2::xml_attr(failure, "type"), | ||
| "CATCH_CHECK_FALSE" = "isn't false.", | ||
| "CATCH_CHECK_THROWS" = "did not throw an exception.", | ||
| "CATCH_CHECK_THROWS_AS" = "threw an exception with unexpected type.", | ||
| "isn't true." | ||
| ) | ||
|
|
||
| message <- xml2::xml_find_first(failure, "Original") | ||
| message <- xml2::xml_text(message, trim = TRUE) | ||
| message <- paste(message, type) | ||
|
|
||
| filename <- xml2::xml_attr(failure, "filename") | ||
| line <- xml2::xml_attr(failure, "line") | ||
| srcref <- srcref( | ||
| srcfile(file.path("src", filename)), | ||
| c(line, line, 1, 1) | ||
| ) | ||
|
|
||
| list( | ||
| message = message, | ||
| srcref = srcref | ||
| ) | ||
| } | ||
|
|
||
| parse_catch_exception <- function(exception) { | ||
| message <- xml2::xml_text(exception, trim = TRUE) | ||
|
|
||
| filename <- xml2::xml_attr(exception, "filename") | ||
| line <- xml2::xml_attr(exception, "line") | ||
| srcref <- srcref( | ||
| srcfile(file.path("src", filename)), | ||
| c(line, line, 1, 1) | ||
| ) | ||
|
|
||
| list( | ||
| message = message, | ||
| srcref = srcref | ||
| ) | ||
| } | ||
|
|
||
| #' Use Catch for C++ unit testing | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.


Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was a useful claude experiment to me because even though
was very much the wrong solution for this, that very quickly helped me isolate what the actual problem was (that would have taken me an hour by myself, or more, probably)
And I also used Claude for the first draft of the
pass()/fail()solution, which was pretty helpful even though it also wasn't great yet.But overall it gave me momentum on a bug that I definitely would have just given up on otherwise, because testthat is just too unfamiliar to me.
I feel like there's this unexplored or unnamed usage of Claude of "just find the bug and explain it to me, I'll be the one to work on the solution". Which is still quite useful in saving a bunch of upfront time.
Here's my whole Claude conversation
Details
My two prompts were