-
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 10 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 |
|---|---|---|
|
|
@@ -51,29 +51,24 @@ run_cpp_tests <- function(package) { | |
| output <- "" | ||
| tests_passed <- TRUE | ||
|
|
||
| catch_error <- FALSE | ||
| catch_error <- NULL | ||
|
|
||
| tryCatch( | ||
| { | ||
| output <- capture_output_lines( | ||
| tests_passed <- .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() | ||
| } | ||
|
|
||
|
|
@@ -83,88 +78,67 @@ run_cpp_tests <- function(package) { | |
|
|
||
| for (context in contexts) { | ||
| context_name <- sub(" [|][^|]+$", "", xml2::xml_attr(context, "name")) | ||
|
|
||
| context_start(context_name) | ||
|
|
||
| tests <- xml2::xml_find_all(context, "./Section") | ||
|
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. All of the xml2 stuff in this file remains untouched. It's just the setup and reporting of pass/fail that has changed. |
||
| for (test in tests) { | ||
| test_name <- xml2::xml_attr(test, "name") | ||
|
|
||
| result <- xml2::xml_find_first(test, "./OverallResults") | ||
| successes <- as.integer(xml2::xml_attr(result, "successes")) | ||
|
|
||
| get_reporter()$start_test(context = context_name, test = test_name) | ||
|
|
||
| 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 | ||
| ) | ||
| } | ||
|
|
||
| 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 | ||
| ) | ||
| } | ||
|
|
||
| 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 | ||
| ) | ||
| } | ||
|
|
||
| get_reporter()$end_test(context = context_name, test = test_name) | ||
| for (test in tests) { | ||
| test_description <- xml2::xml_attr(test, "name") | ||
|
|
||
| test_that(test_description, { | ||
|
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. "For each test in a context, set up an R level
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. You should consider if you should use
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. I tried that at first, see 99dde78 The problem was that we need to record the I had come up with some solution of a new |
||
| result <- xml2::xml_find_first(test, "./OverallResults") | ||
|
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. Probably out of scope for this PR, but it would probably be better to do: result <- xml2::xml_find_first(test, "./OverallResults|./Expression|./Exception")in order preserve the original ordering of successes, failures, and errors.
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. I've determined that I don't think that will actually help that much
from which we extract but we don't know where the successes are in relation to the 1 a single test_that block on the cpp11 side might look like but Catch just tells us this i.e. we see 5 successes, 1 failure, and we can get info about that failure from the I'm also not sure it matters much. Whether we call The one case that might be kinda important is emitting
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. Ok, makes sense. |
||
| successes <- as.integer(xml2::xml_attr(result, "successes")) | ||
| for (i in seq_len(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. |
||
| } | ||
|
|
||
| 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) | ||
| ) | ||
|
|
||
| fail(org_text, srcref = failure_srcref) | ||
| } | ||
|
|
||
| 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) | ||
| ) | ||
|
|
||
| # 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_text, | ||
| 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. |
||
| } | ||
| }) | ||
| } | ||
| } | ||
| } | ||
|
|
||


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