Skip to content

Commit 6c7ad0c

Browse files
Parse test description (#10)
The Factor word `description` outputs ###DESC### and the test description. Before exercism/factor#219 the track's practice exercises printed a description between tests, with no prefix, and the test runner was incorrectly reporting this as part of the output of the preceeding test.
1 parent 27dad81 commit 6c7ad0c

23 files changed

Lines changed: 107 additions & 13 deletions

File tree

bin/run.sh

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ src_tests=$(awk "${awk_json}"'
9393
' "${tmp_dir}/${slug}/${slug}-tests.factor")
9494

9595
# 2. Parse Factor stdout into NDJSON segments and failures:
96-
# segments: {"type":"segment","idx":N,"failed":bool,"output":"..."}
96+
# segments: {"type":"segment","idx":N,"failed":bool,"name":"...","output":"..."}
9797
# failures: {"type":"failure","line_no":N,"message":"..."}
9898
parsed=$(printf '%s\n' "${raw_output}" | awk "${awk_json}"'
9999
function close_segment( out, i) {
@@ -102,8 +102,8 @@ parsed=$(printf '%s\n' "${raw_output}" | awk "${awk_json}"'
102102
for (i = 1; i <= seg_n; i++) out = out (i > 1 ? "\n" : "") seg[i]
103103
sub(/^\n+/, "", out)
104104
sub(/\n+$/, "", out)
105-
printf "{\"type\":\"segment\",\"idx\":%d,\"failed\":%s,\"output\":%s}\n",
106-
idx, (seg_failed ? "true" : "false"), json_str(out)
105+
printf "{\"type\":\"segment\",\"idx\":%d,\"failed\":%s,\"name\":%s,\"output\":%s}\n",
106+
idx, (seg_failed ? "true" : "false"), json_str(cur_name), json_str(out)
107107
}
108108
function close_failure( body, i) {
109109
body = ""
@@ -126,6 +126,9 @@ parsed=$(printf '%s\n' "${raw_output}" | awk "${awk_json}"'
126126
BEGIN {
127127
state = "inline"
128128
idx = 0
129+
pending_name = ""
130+
cur_name = ""
131+
desc_re = "^###DESC### "
129132
header_re = "^(Unit Test|Unit Test~|Unit Test V~|Long Unit Test|Must Fail|Must Fail With|Must Not Fail|Must Infer|Must Infer As): "
130133
}
131134
state == "inline" && $0 ~ header_re {
@@ -134,6 +137,18 @@ parsed=$(printf '%s\n' "${raw_output}" | awk "${awk_json}"'
134137
seg_failed = 0
135138
seg_n = 0
136139
delete seg
140+
cur_name = pending_name
141+
pending_name = ""
142+
next
143+
}
144+
# A generated "###DESC### <description>" line labels the test that
145+
# follows it (it is printed before that test'"'"'s "Unit Test:" header).
146+
# Stash it as the upcoming segment'"'"'s name; never add it to any
147+
# segment'"'"'s output. Concept exercises emit no such lines.
148+
state == "inline" && $0 ~ desc_re {
149+
line = $0
150+
sub(desc_re, "", line)
151+
pending_name = line
137152
next
138153
}
139154
state == "inline" && $0 == "###FAIL_BEGIN###" {
@@ -188,7 +203,7 @@ failures=$(printf '%s\n' "${parsed}" | awk '/"type":"failure"/' || true)
188203

189204
# 3. If no segments emitted, surface a top-level error from the raw output.
190205
if [[ -z "${segments}" ]]; then
191-
cleaned=$(printf '%s\n' "${raw_output}" | awk '/^\([UO]\) /{exit} {print}' \
206+
cleaned=$(printf '%s\n' "${raw_output}" | awk '/^\([UO]\) /{exit} !/^###DESC### /{print}' \
192207
| awk '
193208
NF {
194209
print
@@ -229,8 +244,10 @@ jq -n \
229244
else "fail" end
230245
else "pass" end
231246
) as $status
247+
| ($seg.name // "") as $label
232248
| {
233-
name: ("Test " + ((.key + 1) | tostring)),
249+
name: (if ($label | length) > 0 then $label
250+
else ("Test " + ((.key + 1) | tostring)) end),
234251
status: $status,
235252
test_code: ($src.test_code // ""),
236253
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
USING: all-fail exercism-tools tools.test ;
22
IN: all-fail.tests
33

4+
"greet returns hello" description
45
{ "hello" } [ greet ] unit-test
56

67
STOP-HERE
78

9+
"greet returns world" description
810
{ "world" } [ greet ] unit-test

tests/all-fail/exercism-tools/exercism-tools.factor

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ SYNTAX: STOP-HERE
99
SYNTAX: TASK:
1010
lexer get next-line ;
1111

12+
! Label the test that follows with its description. The marker lets the
13+
! wrapper strip this line from captured output and attach it to the next
14+
! test as a name, rather than leaving it in the previous test's output.
15+
: description ( str -- )
16+
"###DESC### " write print ;
17+
1218
! Print one failure block in a stable, parser-friendly form. Bracketed by
1319
! markers so a wrapper can split the stream reliably and avoid Factor's
1420
! noisy callstack output (which is interleaved with subsequent failures).

tests/all-fail/expected_results.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
"status": "fail",
44
"tests": [
55
{
6-
"name": "Test 1",
6+
"name": "greet returns hello",
77
"status": "fail",
88
"test_code": "{ \"hello\" } [ greet ] unit-test",
99
"message": "=== Expected:\n\"hello\"\n=== Got:\n\"wrong\""
1010
},
1111
{
12-
"name": "Test 2",
12+
"name": "greet returns world",
1313
"status": "fail",
1414
"test_code": "{ \"world\" } [ greet ] unit-test",
1515
"message": "=== Expected:\n\"world\"\n=== Got:\n\"wrong\""

tests/concept-concurrency/exercism-tools/exercism-tools.factor

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ SYNTAX: STOP-HERE
99
SYNTAX: TASK:
1010
lexer get next-line ;
1111

12+
! Label the test that follows with its description. The marker lets the
13+
! wrapper strip this line from captured output and attach it to the next
14+
! test as a name, rather than leaving it in the previous test's output.
15+
: description ( str -- )
16+
"###DESC### " write print ;
17+
1218
! Print one failure block in a stable, parser-friendly form. Bracketed by
1319
! markers so a wrapper can split the stream reliably and avoid Factor's
1420
! noisy callstack output (which is interleaved with subsequent failures).

tests/concept-not-parsing/exercism-tools/exercism-tools.factor

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ SYNTAX: STOP-HERE
99
SYNTAX: TASK:
1010
lexer get next-line ;
1111

12+
! Label the test that follows with its description. The marker lets the
13+
! wrapper strip this line from captured output and attach it to the next
14+
! test as a name, rather than leaving it in the previous test's output.
15+
: description ( str -- )
16+
"###DESC### " write print ;
17+
1218
! Print one failure block in a stable, parser-friendly form. Bracketed by
1319
! markers so a wrapper can split the stream reliably and avoid Factor's
1420
! noisy callstack output (which is interleaved with subsequent failures).

tests/concept-partial-fail/exercism-tools/exercism-tools.factor

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ SYNTAX: STOP-HERE
99
SYNTAX: TASK:
1010
lexer get next-line ;
1111

12+
! Label the test that follows with its description. The marker lets the
13+
! wrapper strip this line from captured output and attach it to the next
14+
! test as a name, rather than leaving it in the previous test's output.
15+
: description ( str -- )
16+
"###DESC### " write print ;
17+
1218
! Print one failure block in a stable, parser-friendly form. Bracketed by
1319
! markers so a wrapper can split the stream reliably and avoid Factor's
1420
! noisy callstack output (which is interleaved with subsequent failures).

tests/concept-stub/exercism-tools/exercism-tools.factor

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ SYNTAX: STOP-HERE
99
SYNTAX: TASK:
1010
lexer get next-line ;
1111

12+
! Label the test that follows with its description. The marker lets the
13+
! wrapper strip this line from captured output and attach it to the next
14+
! test as a name, rather than leaving it in the previous test's output.
15+
: description ( str -- )
16+
"###DESC### " write print ;
17+
1218
! Print one failure block in a stable, parser-friendly form. Bracketed by
1319
! markers so a wrapper can split the stream reliably and avoid Factor's
1420
! noisy callstack output (which is interleaved with subsequent failures).

tests/concept-success/exercism-tools/exercism-tools.factor

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ SYNTAX: STOP-HERE
99
SYNTAX: TASK:
1010
lexer get next-line ;
1111

12+
! Label the test that follows with its description. The marker lets the
13+
! wrapper strip this line from captured output and attach it to the next
14+
! test as a name, rather than leaving it in the previous test's output.
15+
: description ( str -- )
16+
"###DESC### " write print ;
17+
1218
! Print one failure block in a stable, parser-friendly form. Bracketed by
1319
! markers so a wrapper can split the stream reliably and avoid Factor's
1420
! noisy callstack output (which is interleaved with subsequent failures).
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
USING: empty-file exercism-tools tools.test ;
2+
IN: empty-file.tests
3+
4+
"greet returns hello" description
25
{ "hello" } [ greet ] unit-test

0 commit comments

Comments
 (0)