-
Notifications
You must be signed in to change notification settings - Fork 343
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
Changes from all commits
f34111f
6d42518
546436e
aac4ad6
99dde78
a9e9319
09482ab
2c12010
18d248c
3b17e83
b8c1942
a9d4463
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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 | ||
| ) | ||
|
Comment on lines
+88
to
+94
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. Open to better ideas here. I'm also not entirely sure how to trigger this case. |
||
| } | ||
| }) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| 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 | ||
|
|
||


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