Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
dev,test: Use jqtest code from jqjq for jq tests
- Loading branch information
Showing
10 changed files
with
234 additions
and
154 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# basic usage | ||
_args_parse(.args; .opts) | ||
{"args": ["-a", "123", "b"], "opts": {"a": {"short": "-a", "long": "--abc", "description": "Set abc", "string": true}}} | ||
{"parsed": {"a": "123"}, "rest": ["b"]} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
map(expr_to_path | path_to_expr) | ||
[".", ".a", ".a[0]", ".a[123].bb", ".[123].a", ".[123][123].a", ".\"b b\"", ".\"a \\\\ b\"", ".\"a \\\" b\""] | ||
[".", ".a", ".a[0]", ".a[123].bb", ".[123].a", ".[123][123].a", ".\"b b\"", ".\"a \\\\ b\"", ".\"a \\\" b\""] | ||
|
||
.[] | group | ||
[[1], [1,2], [1,2,2], [1,2,2,3]] | ||
[[1]] | ||
[[1],[2]] | ||
[[1],[2,2]] | ||
[[1],[2,2],[3]] | ||
|
||
|
||
.[] | count | ||
[[1], [1,2], [1,2,2,2], [1,2,2,2,3]] | ||
[[1,1]] | ||
[[1,1],[2,1]] | ||
[[1,1],[2,3]] | ||
[[1,1],[2,3],[3,1]] | ||
|
||
.[] | count_by(.%2) | ||
[[1], [1,2], [1,2,2,2], [1,2,2,2,3]] | ||
[[1,1]] | ||
[[0,1],[1,1]] | ||
[[0,3],[1,1]] | ||
[[0,3],[1,2]] | ||
|
||
.[] | streaks | ||
[[], [1], [1,1], [1,1,2], [1,1,2,2], [1,2,2,1]] | ||
[] | ||
[[1]] | ||
[[1,1]] | ||
[[1,1],[2]] | ||
[[1,1],[2,2]] | ||
[[1],[2,2],[1]] | ||
|
||
.[] | delta | ||
[[], [1], [1,2], [1,2,2,2], [1,2,2,2,3]] | ||
[] | ||
[] | ||
[1] | ||
[1,0,0] | ||
[1,0,0,1] | ||
|
||
.[] | delta_by(.a+.b) | ||
[[], [1], [1,2], [1,2,2,2], [1,2,2,2,3]] | ||
[] | ||
[] | ||
[3] | ||
[3,4,4] | ||
[3,4,4,5] | ||
|
||
streaks_by(.a) | ||
[{"a":1},{"a":1},{"a":2}] | ||
[[{"a":1},{"a":1}],[{"a":2}]] | ||
|
||
.[] as [$input, $size] | $input | chunk($size) | ||
[[[], 1], [[], 2], [[1], 1], [[1], 2], [[1,2], 1], [[1,2], 2], [[1,2,3,4], 2], [[1,2,3,4], 3], ["", 1], ["", 2], ["1", 1], ["1", 2], ["12", 1], ["12", 2], ["1234", 2], ["1234", 3]] | ||
[] | ||
[] | ||
[[1]] | ||
[[1]] | ||
[[1],[2]] | ||
[[1,2]] | ||
[[1,2],[3,4]] | ||
[[1,2,3],[4]] | ||
[] | ||
[] | ||
["1"] | ||
["1"] | ||
["1","2"] | ||
["12"] | ||
["12","34"] | ||
["123","4"] |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
# read jq test format: | ||
# # comment | ||
# expr | ||
# input | ||
# output* | ||
# <blank>+ | ||
# ... | ||
# <next test> | ||
def fromjqtest: | ||
[ foreach (split("\n")[], "") as $l ( | ||
{ current_line: 0 | ||
, nr: 1 | ||
, emit: true | ||
}; | ||
( .current_line += 1 | ||
| if .emit then | ||
( .expr = null | ||
| .input = null | ||
| .output = [] | ||
| .fail = null | ||
| .emit = null | ||
| .error = null | ||
) | ||
else . | ||
end | ||
| if $l | test("^\\s*#") then . | ||
elif $l | test("^\\s*$") then | ||
if .expr then | ||
( .emit = | ||
{ line | ||
, nr | ||
, expr | ||
, input | ||
, output | ||
, fail | ||
, error | ||
} | ||
| .nr += 1 | ||
) | ||
else . | ||
end | ||
elif $l | test("^\\s*%%FAIL") then | ||
.fail = $l | ||
else | ||
if .expr == null then | ||
( .line = .current_line | ||
| .expr = $l | ||
) | ||
elif .fail and .error == null then .error = $l | ||
elif .input == null then .input = $l | ||
else .output += [$l] | ||
end | ||
end | ||
); | ||
if .emit then .emit | ||
else empty | ||
end | ||
) | ||
]; | ||
|
||
def run_tests: | ||
def _f: | ||
( fromjqtest[] | ||
| . as $c | ||
| try | ||
if .error | not then | ||
( ( .input | ||
, .output[] | ||
) |= fromjson | ||
) | ||
else . | ||
end | ||
catch | ||
( . as $err | ||
| $c | ||
| .fromjson_error = $err | ||
) | ||
| select(.fromjson_error | not) | ||
| "\(.nr) (line \(.line)): [\(.input | tojson) | \(.expr)] -> \(.output | tojson)" as $test_name | ||
| . as $test | ||
| try | ||
( $test.input | ||
| [ eval($test.expr)] as $actual_output | ||
| if $test.output == $actual_output then | ||
( empty # "OK: \($test_name)" | ||
, {ok: true} | ||
) | ||
else | ||
( "DIFF: \($test_name)" | ||
, " Expected: \($test.output | tojson)" | ||
, " Actual: \($actual_output | tojson)" | ||
, {error: true} | ||
) | ||
end | ||
) | ||
catch | ||
if $test.fail then | ||
if . == $test.error then | ||
( empty #"OK: \($test_name)" | ||
, {ok: true} | ||
) | ||
else | ||
( "FAIL DIFF: \($test_name)" | ||
, " Expected: \($test.error)" | ||
, " Actual: \(.)" | ||
, {error: true} | ||
) | ||
end | ||
else | ||
( "ERROR: \($test_name)" | ||
, " \(.)" | ||
, {error: true} | ||
) | ||
end | ||
); | ||
# this mess make it possible to run all tests and exit with non-zero if any test failed | ||
( foreach (_f, {end: true}) as $l ( | ||
{ errors: 0 | ||
, oks: 0 | ||
}; | ||
if ($l | type) == "object" then | ||
( .line = false | ||
| if $l.error then .errors +=1 | ||
elif $l.ok then .oks += 1 | ||
elif $l.end then .end = true | ||
else . | ||
end | ||
) | ||
else . + {line: $l} | ||
end; | ||
. | ||
) | ||
| if .end then | ||
( "\(.oks) of \(.oks + .errors) tests passed" | ||
, if .errors > 0 then null | halt_error(1) else empty end | ||
) | ||
elif .line then .line | ||
else empty | ||
end | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
.[] | _query_fromstring | _query_pipe_last | _query_tostring | ||
["", ".", "a", "1, 2", "1 | 2", "1 | 2 | 3", "(1 | 2) | 3", "1 | (2 | 3)", "1 as $_ | 2", "def f: 1; 1", "def f: 1; 1 | 2"] | ||
"." | ||
"." | ||
"a" | ||
"1, 2" | ||
"2" | ||
"3" | ||
"3" | ||
"(2 | 3)" | ||
"2" | ||
"def f: 1; 1" | ||
"2" |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.