@@ -43,40 +43,33 @@ extract_test_lines <- function(exprs, line, error_call = caller_env()) {
4343 check_number_whole(line , min = 1 , call = error_call )
4444
4545 srcrefs <- attr(exprs , " srcref" )
46-
47- # Focus on srcrefs before the selected line
48- keep <- start_line(srcrefs ) < = line
49- exprs <- exprs [keep ]
50- srcrefs <- srcrefs [keep ]
51-
52- # We first capture the prequel, all code outside of tests
5346 is_subtest <- map_lgl(exprs , is_subtest )
54- if (any(! is_subtest )) {
55- prequel <- c(
56- comment_header(" prequel" ),
57- map_chr(srcrefs [! is_subtest ], as.character ),
58- " "
59- )
60- } else {
61- prequel <- NULL
62- }
6347
64- # Now we extract the contents of the last test
65- if (! any(is_subtest )) {
48+ # First we find the test
49+ is_test <- is_subtest &
50+ start_line(srcrefs ) < = line &
51+ end_line(srcrefs ) > = line
52+ if (! any(is_test )) {
6653 cli :: cli_abort(" Failed to find test at line {line}." , call = error_call )
6754 }
68- test_idx <- rev(which(is_subtest ))[[1 ]]
69- call <- exprs [[test_idx ]]
70- check_test_call(call , error_call = error_call )
71-
55+ call <- exprs [[which(is_test )[[1 ]]]]
7256 test_contents <- attr(call [[3 ]], " srcref" )[- 1 ] # drop `{`
7357 keep <- start_line(test_contents ) < = line
74- test <- c(
75- comment_header(" test" ),
76- map_chr(test_contents [keep ], as.character )
77- )
58+ test <- srcref_to_character(test_contents [keep ])
59+
60+ # We first find the prequel, all non-test code before the test
61+ is_prequel <- ! is_subtest & start_line(srcrefs ) < line
62+ if (! any(is_prequel )) {
63+ return (test )
64+ }
7865
79- c(prequel , test )
66+ c(
67+ " # prequel ---------------------------------------------------------------" ,
68+ srcref_to_character(srcrefs [is_prequel ]),
69+ " " ,
70+ " # test ------------------------------------------------------------------" ,
71+ test
72+ )
8073}
8174
8275# Helpers ---------------------------------------------------------------------
@@ -92,27 +85,20 @@ parse_file <- function(path, error_call = caller_env()) {
9285 parse(path , keep.source = TRUE )
9386}
9487
95- check_test_call <- function (expr , error_call = caller_env()) {
96- if (! is_call(expr , n = 2 )) {
97- cli :: cli_abort(
98- " test call has unexpected number of arguments" ,
99- internal = TRUE ,
100- call = error_call
101- )
102- }
103- if (! is_call(expr [[3 ]], " {" )) {
104- cli :: cli_abort(
105- " test call doesn't use `{`" ,
106- internal = TRUE ,
107- call = error_call
108- )
109- }
110- }
88+ parse_text <- function (text ) {
89+ text <- sub(" ^\n " , " " , text )
90+ indent <- regmatches(text , regexpr(" ^ *" , text ))
91+ text <- gsub(paste0(" (?m)^" , indent ), " " , text , perl = TRUE )
11192
112- comment_header <- function (x ) {
113- paste0(" # " , x , " " , strrep(" -" , 80 - nchar(x ) - 3 ))
93+ parse(text = text , keep.source = TRUE )
11494}
11595
96+ srcref_to_character <- function (x ) {
97+ unlist(map(x , as.character ))
98+ }
11699start_line <- function (srcrefs ) {
117100 map_int(srcrefs , \(x ) x [[1 ]])
118101}
102+ end_line <- function (srcrefs ) {
103+ map_int(srcrefs , \(x ) x [[3 ]])
104+ }
0 commit comments