Skip to content
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

When the expected result is an object, use jq to determine object equality #196

Merged
merged 1 commit into from
Nov 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 44 additions & 10 deletions bin/generate_tests
Original file line number Diff line number Diff line change
Expand Up @@ -142,31 +142,42 @@ END_PREAMBLE
;

def generate_test:
(.expected | type == "object") as $isobj
| (.expected | type == "array") as $isarr
| ($isobj and (.expected | has("error"))) as $iserr
| (if ($isobj or $isarr)
then ($cmd | sub("jq -r";"jq -c"))
else $cmd
end) as $command
|
"\n"
+ "@test \(.description | @sh) {\n"
+ " "
+ "[[ $BATS_RUN_SKIPPED == \"true\" ]] || skip\n"
+ "\n"
+ " run \($cmd) << \($Q)END_INPUT\($Q)"
+ " run \($command) << \($Q)END_INPUT\($Q)"
, if $property then {property, input} else .input end
, "END_INPUT\n"
, ( if (.expected | type == "object" and has("error"))
, ( if $iserr
then
" assert_failure\n"
+ " expected=\(.expected.error | @sh)\n"
+ " expected=\(.expected.error | @sh)"
else
" assert_success"
, ( .expected
| if type | IN("array","object")
then
" expected=$(cat << \($Q)END_EXPECTED\($Q)"
, .
, "END_EXPECTED\n)"
| if ($isobj or $isarr)
then " expected=\(@json | @sh)"
else " expected=\(@sh)"
end
)
end
, " assert_equal \"$output\" \"$expected\""
,
if ($isobj and ($iserr | not))
then
" assert_objects_equal \"$output\" \"$expected\""
else
" assert_equal \"$output\" \"$expected\""
end
)
, "}"
;
Expand All @@ -177,14 +188,37 @@ END_PREAMBLE
' "$canonicalData"
} \
| awk '
# skip the first test
# un-skip the first test
/BATS_RUN_SKIPPED/ && !seen {
sub(/[[]/, "#[")
seen = 1
}
# indent the input
/^END_INPUT$/ {indent = 0}
indent {sub(/^/, " ")}
/<< .END_INPUT/ {indent = 1}
1
' \
| tee "$testFile"

if grep -q assert_objects_equal "$testFile"; then
local t=$(mktemp)
local func=$(cat <<'END_FUNC'
assert_objects_equal() {
local result=$(
jq -n --argjson actual "$1" \\
--argjson expected "$2" \\
'$actual == $expected'
)
[[ $result == "true" ]]
}
END_FUNC
)
awk -v fn="$func" '
{print}
/load bats-extra/ { print ""; print fn }
' "$testFile" > "$t" && mv "$t" "$testFile"
fi
}

[[ -d ./exercises/practice ]] || die "Run me from the repo root."
Expand Down
20 changes: 14 additions & 6 deletions exercises/practice/etl/test-etl.bats
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
#!/usr/bin/env bats
# generated on 2022-11-02T20:59:06Z
# generated on 2023-11-07T18:49:21Z
load bats-extra

assert_objects_equal() {
local result=$(
jq -n --argjson actual "$1" \
--argjson expected "$2" \
'$actual == $expected'
)
[[ $result == "true" ]]
}

@test 'single letter' {
#[[ $BATS_RUN_SKIPPED == "true" ]] || skip

Expand All @@ -17,7 +26,7 @@ END_INPUT

assert_success
expected='{"a":1}'
assert_equal "$output" "$expected"
assert_objects_equal "$output" "$expected"
}

@test 'single score with multiple letters' {
Expand All @@ -39,7 +48,7 @@ END_INPUT

assert_success
expected='{"a":1,"e":1,"i":1,"o":1,"u":1}'
assert_equal "$output" "$expected"
assert_objects_equal "$output" "$expected"
}

@test 'multiple scores with multiple letters' {
Expand All @@ -62,7 +71,7 @@ END_INPUT

assert_success
expected='{"a":1,"d":2,"e":1,"g":2}'
assert_equal "$output" "$expected"
assert_objects_equal "$output" "$expected"
}

@test 'multiple scores with differing numbers of letters' {
Expand Down Expand Up @@ -117,6 +126,5 @@ END_INPUT

assert_success
expected='{"a":1,"b":3,"c":3,"d":2,"e":1,"f":4,"g":2,"h":4,"i":1,"j":8,"k":5,"l":1,"m":3,"n":1,"o":1,"p":3,"q":10,"r":1,"s":1,"t":1,"u":1,"v":4,"w":4,"x":8,"y":4,"z":10}'
assert_equal "$output" "$expected"
assert_objects_equal "$output" "$expected"
}

20 changes: 14 additions & 6 deletions exercises/practice/nucleotide-count/test-nucleotide-count.bats
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
#!/usr/bin/env bats
# generated on 2022-11-02T20:59:33Z
# generated on 2023-11-07T18:49:21Z
load bats-extra

assert_objects_equal() {
local result=$(
jq -n --argjson actual "$1" \
--argjson expected "$2" \
'$actual == $expected'
)
[[ $result == "true" ]]
}

@test 'empty strand' {
#[[ $BATS_RUN_SKIPPED == "true" ]] || skip

Expand All @@ -13,7 +22,7 @@ END_INPUT

assert_success
expected='{"A":0,"C":0,"G":0,"T":0}'
assert_equal "$output" "$expected"
assert_objects_equal "$output" "$expected"
}

@test 'can count one nucleotide in single-character input' {
Expand All @@ -27,7 +36,7 @@ END_INPUT

assert_success
expected='{"A":0,"C":0,"G":1,"T":0}'
assert_equal "$output" "$expected"
assert_objects_equal "$output" "$expected"
}

@test 'strand with repeated nucleotide' {
Expand All @@ -41,7 +50,7 @@ END_INPUT

assert_success
expected='{"A":0,"C":0,"G":7,"T":0}'
assert_equal "$output" "$expected"
assert_objects_equal "$output" "$expected"
}

@test 'strand with multiple nucleotides' {
Expand All @@ -55,7 +64,7 @@ END_INPUT

assert_success
expected='{"A":20,"C":12,"G":17,"T":21}'
assert_equal "$output" "$expected"
assert_objects_equal "$output" "$expected"
}

@test 'strand with invalid nucleotides' {
Expand All @@ -71,4 +80,3 @@ END_INPUT
expected='Invalid nucleotide in strand'
assert_equal "$output" "$expected"
}

18 changes: 13 additions & 5 deletions exercises/practice/satellite/test-satellite.bats
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
#!/usr/bin/env bats
# generated on 2022-11-02T20:59:46Z
# generated on 2023-11-07T18:49:22Z
load bats-extra

assert_objects_equal() {
local result=$(
jq -n --argjson actual "$1" \
--argjson expected "$2" \
'$actual == $expected'
)
[[ $result == "true" ]]
}

@test 'Empty tree' {
#[[ $BATS_RUN_SKIPPED == "true" ]] || skip

Expand All @@ -14,7 +23,7 @@ END_INPUT

assert_success
expected='{}'
assert_equal "$output" "$expected"
assert_objects_equal "$output" "$expected"
}

@test 'Tree with one item' {
Expand All @@ -33,7 +42,7 @@ END_INPUT

assert_success
expected='{"v":"a","l":{},"r":{}}'
assert_equal "$output" "$expected"
assert_objects_equal "$output" "$expected"
}

@test 'Tree with many items' {
Expand All @@ -60,7 +69,7 @@ END_INPUT

assert_success
expected='{"v":"a","l":{"v":"i","l":{},"r":{}},"r":{"v":"x","l":{"v":"f","l":{},"r":{}},"r":{"v":"r","l":{},"r":{}}}}'
assert_equal "$output" "$expected"
assert_objects_equal "$output" "$expected"
}

@test 'Reject traversals of different length' {
Expand Down Expand Up @@ -130,4 +139,3 @@ END_INPUT
expected='traversals must contain unique items'
assert_equal "$output" "$expected"
}

26 changes: 17 additions & 9 deletions exercises/practice/two-bucket/test-two-bucket.bats
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
#!/usr/bin/env bats
# generated on 2022-11-02T20:59:53Z
# generated on 2023-11-07T18:49:23Z
load bats-extra

assert_objects_equal() {
local result=$(
jq -n --argjson actual "$1" \
--argjson expected "$2" \
'$actual == $expected'
)
[[ $result == "true" ]]
}

@test 'Measure using bucket one of size 3 and bucket two of size 5 - start with bucket one' {
#[[ $BATS_RUN_SKIPPED == "true" ]] || skip

Expand All @@ -16,7 +25,7 @@ END_INPUT

assert_success
expected='{"moves":4,"goalBucket":"one","otherBucket":5}'
assert_equal "$output" "$expected"
assert_objects_equal "$output" "$expected"
}

@test 'Measure using bucket one of size 3 and bucket two of size 5 - start with bucket two' {
Expand All @@ -33,7 +42,7 @@ END_INPUT

assert_success
expected='{"moves":8,"goalBucket":"two","otherBucket":3}'
assert_equal "$output" "$expected"
assert_objects_equal "$output" "$expected"
}

@test 'Measure using bucket one of size 7 and bucket two of size 11 - start with bucket one' {
Expand All @@ -50,7 +59,7 @@ END_INPUT

assert_success
expected='{"moves":14,"goalBucket":"one","otherBucket":11}'
assert_equal "$output" "$expected"
assert_objects_equal "$output" "$expected"
}

@test 'Measure using bucket one of size 7 and bucket two of size 11 - start with bucket two' {
Expand All @@ -67,7 +76,7 @@ END_INPUT

assert_success
expected='{"moves":18,"goalBucket":"two","otherBucket":7}'
assert_equal "$output" "$expected"
assert_objects_equal "$output" "$expected"
}

@test 'Measure one step using bucket one of size 1 and bucket two of size 3 - start with bucket two' {
Expand All @@ -84,7 +93,7 @@ END_INPUT

assert_success
expected='{"moves":1,"goalBucket":"two","otherBucket":0}'
assert_equal "$output" "$expected"
assert_objects_equal "$output" "$expected"
}

@test 'Measure using bucket one of size 2 and bucket two of size 3 - start with bucket one and end with bucket two' {
Expand All @@ -101,7 +110,7 @@ END_INPUT

assert_success
expected='{"moves":2,"goalBucket":"two","otherBucket":2}'
assert_equal "$output" "$expected"
assert_objects_equal "$output" "$expected"
}

@test 'Not possible to reach the goal' {
Expand Down Expand Up @@ -135,7 +144,7 @@ END_INPUT

assert_success
expected='{"moves":10,"goalBucket":"two","otherBucket":0}'
assert_equal "$output" "$expected"
assert_objects_equal "$output" "$expected"
}

@test 'Goal larger than both buckets is impossible' {
Expand All @@ -154,4 +163,3 @@ END_INPUT
expected='impossible'
assert_equal "$output" "$expected"
}