Skip to content

Commit b8c1942

Browse files
committed
Refactor into a series of parsers
1 parent 3b17e83 commit b8c1942

1 file changed

Lines changed: 89 additions & 54 deletions

File tree

R/test-compiled-code.R

Lines changed: 89 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -72,77 +72,112 @@ run_cpp_tests <- function(package) {
7272
return()
7373
}
7474

75-
report <- xml2::read_xml(paste(output, collapse = "\n"))
76-
77-
contexts <- xml2::xml_find_all(report, "//TestCase")
75+
output <- paste(output, collapse = "\n")
76+
contexts <- parse_catch_contexts(output)
7877

7978
for (context in contexts) {
80-
context_name <- sub(" [|][^|]+$", "", xml2::xml_attr(context, "name"))
81-
context_start(context_name)
82-
83-
tests <- xml2::xml_find_all(context, "./Section")
84-
85-
for (test in tests) {
86-
test_description <- xml2::xml_attr(test, "name")
79+
context_start(context$name)
8780

88-
test_that(test_description, {
89-
result <- xml2::xml_find_first(test, "./OverallResults")
90-
successes <- as.integer(xml2::xml_attr(result, "successes"))
91-
for (i in seq_len(successes)) {
81+
for (test in context$tests) {
82+
test_that(test$name, {
83+
for (i in seq_len(test$n_successes)) {
9284
pass()
9385
}
94-
95-
failures <- xml2::xml_find_all(test, "./Expression")
96-
for (failure in failures) {
97-
org <- xml2::xml_find_first(failure, "Original")
98-
org_text <- xml2::xml_text(org, trim = TRUE)
99-
100-
filename <- xml2::xml_attr(failure, "filename")
101-
type <- xml2::xml_attr(failure, "type")
102-
103-
type_msg <- switch(
104-
type,
105-
"CATCH_CHECK_FALSE" = "isn't false.",
106-
"CATCH_CHECK_THROWS" = "did not throw an exception.",
107-
"CATCH_CHECK_THROWS_AS" = "threw an exception with unexpected type.",
108-
"isn't true."
109-
)
110-
111-
org_text <- paste(org_text, type_msg)
112-
113-
line <- xml2::xml_attr(failure, "line")
114-
failure_srcref <- srcref(
115-
srcfile(file.path("src", filename)),
116-
c(line, line, 1, 1)
117-
)
118-
119-
fail(org_text, srcref = failure_srcref)
86+
for (failure in test$failures) {
87+
fail(message = failure$message, srcref = failure$srcref)
12088
}
121-
122-
exceptions <- xml2::xml_find_all(test, "./Exception")
123-
for (exception in exceptions) {
124-
exception_text <- xml2::xml_text(exception, trim = TRUE)
125-
filename <- xml2::xml_attr(exception, "filename")
126-
line <- xml2::xml_attr(exception, "line")
127-
128-
exception_srcref <- srcref(
129-
srcfile(file.path("src", filename)),
130-
c(line, line, 1, 1)
131-
)
132-
89+
for (exception in test$exceptions) {
13390
# There is no `fail()` equivalent for an error.
13491
# We could use `stop()`, but we want to pass through a `srcref`.
13592
expectation(
13693
type = "error",
137-
message = exception_text,
138-
srcref = exception_srcref
94+
message = exception$message,
95+
srcref = exception$srcref
13996
)
14097
}
14198
})
14299
}
143100
}
144101
}
145102

103+
parse_catch_contexts <- function(text) {
104+
xml <- xml2::read_xml(text)
105+
106+
contexts <- xml2::xml_find_all(xml, "//TestCase")
107+
contexts <- map(contexts, parse_catch_context)
108+
109+
contexts
110+
}
111+
112+
parse_catch_context <- function(context) {
113+
name <- sub(" [|][^|]+$", "", xml2::xml_attr(context, "name"))
114+
tests <- xml2::xml_find_all(context, "./Section")
115+
tests <- map(tests, parse_catch_test)
116+
list(name = name, tests = tests)
117+
}
118+
119+
parse_catch_test <- function(test) {
120+
name <- xml2::xml_attr(test, "name")
121+
122+
overall_results <- xml2::xml_find_first(test, "./OverallResults")
123+
n_successes <- as.integer(xml2::xml_attr(overall_results, "successes"))
124+
125+
failures <- xml2::xml_find_all(test, "./Expression")
126+
failures <- map(failures, parse_catch_failure)
127+
128+
exceptions <- xml2::xml_find_all(test, "./Exception")
129+
exceptions <- map(exceptions, parse_catch_exception)
130+
131+
list(
132+
name = name,
133+
n_successes = n_successes,
134+
failures = failures,
135+
exceptions = exceptions
136+
)
137+
}
138+
139+
parse_catch_failure <- function(failure) {
140+
type <- switch(
141+
xml2::xml_attr(failure, "type"),
142+
"CATCH_CHECK_FALSE" = "isn't false.",
143+
"CATCH_CHECK_THROWS" = "did not throw an exception.",
144+
"CATCH_CHECK_THROWS_AS" = "threw an exception with unexpected type.",
145+
"isn't true."
146+
)
147+
148+
message <- xml2::xml_find_first(failure, "Original")
149+
message <- xml2::xml_text(message, trim = TRUE)
150+
message <- paste(message, type)
151+
152+
filename <- xml2::xml_attr(failure, "filename")
153+
line <- xml2::xml_attr(failure, "line")
154+
srcref <- srcref(
155+
srcfile(file.path("src", filename)),
156+
c(line, line, 1, 1)
157+
)
158+
159+
list(
160+
message = message,
161+
srcref = srcref
162+
)
163+
}
164+
165+
parse_catch_exception <- function(exception) {
166+
message <- xml2::xml_text(exception, trim = TRUE)
167+
168+
filename <- xml2::xml_attr(exception, "filename")
169+
line <- xml2::xml_attr(exception, "line")
170+
srcref <- srcref(
171+
srcfile(file.path("src", filename)),
172+
c(line, line, 1, 1)
173+
)
174+
175+
list(
176+
message = message,
177+
srcref = srcref
178+
)
179+
}
180+
146181
#' Use Catch for C++ unit testing
147182
#'
148183
#' Add the necessary infrastructure to enable C++ unit testing

0 commit comments

Comments
 (0)