Skip to content

Commit 2ddc0dc

Browse files
authored
Deploy updated bats jq (#290)
1 parent 6c30330 commit 2ddc0dc

File tree

88 files changed

+4593
-203
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

88 files changed

+4593
-203
lines changed

exercises/concept/assembly-line/bats-jq.bash

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
# - "Printing to the terminal", https://bats-core.readthedocs.io/en/stable/writing-tests.html#printing-to-the-terminal
1111
# - "File descriptor 3", https://bats-core.readthedocs.io/en/stable/writing-tests.html#file-descriptor-3-read-this-if-bats-hangs
1212

13-
1413
jq() {
1514
local output stderr rc line
1615
stderr=$(mktemp)
@@ -27,3 +26,64 @@ jq() {
2726
echo "$output"
2827
return "$rc"
2928
}
29+
30+
#############################################################
31+
# These are extra assert functions for use in tests.
32+
33+
# Assert two JSON objects are equal.
34+
# https://jqlang.org/manual/v1.7/#==-!=
35+
#
36+
# assert_objects_equal '{"a": 1, "b": 2}' '{"b":2,"a":1}'
37+
# # => true
38+
#
39+
assert_objects_equal() {
40+
local result=$(
41+
jq -n --argjson actual "$1" \
42+
--argjson expected "$2" \
43+
'$actual == $expected'
44+
)
45+
[[ $result == "true" ]]
46+
}
47+
48+
# Assert 2 floating-point values are "close enough".
49+
#
50+
# # are they the same to 2 decimal places?
51+
# assert_float 1.993 1.995 # => true
52+
#
53+
# # are they the same to 3 decimal places?
54+
# assert_float -d 3 1.993 1.995 # => false
55+
#
56+
assert_float() {
57+
local OPTIND OPTARG
58+
local decimals=2 actual expected
59+
while getopts :d: opt; do
60+
case $opt in
61+
d) decimals=$OPTARG ;;
62+
*) return 2 ;;
63+
esac
64+
done
65+
shift $((OPTIND - 1))
66+
# bash can't do floating point: use awk
67+
read -r actual expected < <(
68+
awk -v d="$decimals" -v a="$1" -v e="$2" '
69+
BEGIN {
70+
m = 10 ^ d
71+
print int(a * m)/m, int(e * m)/m
72+
}
73+
'
74+
)
75+
# now call a bats-assert command to get the desired output
76+
assert_equal "$actual" "$expected"
77+
}
78+
79+
# Assert that an object's value of a given key is the expected value.
80+
# This uses the bats `output` variable.
81+
#
82+
# run jq -f ...
83+
# assert_key_value 'the_key' 'the_expected_value'
84+
#
85+
assert_key_value() {
86+
local key=$1 expected=$2 actual
87+
actual=$(jq -rc --arg key "$key" '.[$key]' <<< "$output")
88+
assert_equal "$actual" "$expected"
89+
}

exercises/concept/assembly-line/test-assembly-line.bats

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,6 @@
22
load bats-extra
33
load bats-jq
44

5-
assert_float() {
6-
local OPTIND OPTARG
7-
local decimals=2 actual expected
8-
while getopts :d: opt; do
9-
case $opt in
10-
d) decimals=$OPTARG ;;
11-
*) return 2 ;;
12-
esac
13-
done
14-
shift $((OPTIND - 1))
15-
# bash can't do floating point: use awk (also uses IEEE754 numbers)
16-
read -r actual expected < <(
17-
awk -v d="$decimals" -v a="$1" -v e="$2" '
18-
BEGIN {
19-
m = 10 ^ d
20-
print int(a * m)/m, int(e * m)/m
21-
}
22-
'
23-
)
24-
# now call a bats-assert command to get the desired output
25-
assert_equal "$actual" "$expected"
26-
}
27-
285
@test production_rate_per_hour_for_speed_zero {
296
## task 1
307
run jq -f assembly-line.jq <<< '{"speed": 0}'

exercises/concept/bird-count/bats-jq.bash

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
# - "Printing to the terminal", https://bats-core.readthedocs.io/en/stable/writing-tests.html#printing-to-the-terminal
1111
# - "File descriptor 3", https://bats-core.readthedocs.io/en/stable/writing-tests.html#file-descriptor-3-read-this-if-bats-hangs
1212

13-
1413
jq() {
1514
local output stderr rc line
1615
stderr=$(mktemp)
@@ -27,3 +26,64 @@ jq() {
2726
echo "$output"
2827
return "$rc"
2928
}
29+
30+
#############################################################
31+
# These are extra assert functions for use in tests.
32+
33+
# Assert two JSON objects are equal.
34+
# https://jqlang.org/manual/v1.7/#==-!=
35+
#
36+
# assert_objects_equal '{"a": 1, "b": 2}' '{"b":2,"a":1}'
37+
# # => true
38+
#
39+
assert_objects_equal() {
40+
local result=$(
41+
jq -n --argjson actual "$1" \
42+
--argjson expected "$2" \
43+
'$actual == $expected'
44+
)
45+
[[ $result == "true" ]]
46+
}
47+
48+
# Assert 2 floating-point values are "close enough".
49+
#
50+
# # are they the same to 2 decimal places?
51+
# assert_float 1.993 1.995 # => true
52+
#
53+
# # are they the same to 3 decimal places?
54+
# assert_float -d 3 1.993 1.995 # => false
55+
#
56+
assert_float() {
57+
local OPTIND OPTARG
58+
local decimals=2 actual expected
59+
while getopts :d: opt; do
60+
case $opt in
61+
d) decimals=$OPTARG ;;
62+
*) return 2 ;;
63+
esac
64+
done
65+
shift $((OPTIND - 1))
66+
# bash can't do floating point: use awk
67+
read -r actual expected < <(
68+
awk -v d="$decimals" -v a="$1" -v e="$2" '
69+
BEGIN {
70+
m = 10 ^ d
71+
print int(a * m)/m, int(e * m)/m
72+
}
73+
'
74+
)
75+
# now call a bats-assert command to get the desired output
76+
assert_equal "$actual" "$expected"
77+
}
78+
79+
# Assert that an object's value of a given key is the expected value.
80+
# This uses the bats `output` variable.
81+
#
82+
# run jq -f ...
83+
# assert_key_value 'the_key' 'the_expected_value'
84+
#
85+
assert_key_value() {
86+
local key=$1 expected=$2 actual
87+
actual=$(jq -rc --arg key "$key" '.[$key]' <<< "$output")
88+
assert_equal "$actual" "$expected"
89+
}

exercises/concept/bird-count/test-bird-count.bats

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,6 @@
22
load bats-extra
33
load bats-jq
44

5-
assert_key_value() {
6-
local key=$1 expected=$2 actual
7-
actual=$(jq -rc --arg key "$key" '.[$key]' <<< "$output")
8-
assert_equal "$actual" "$expected"
9-
}
10-
115
@test last_week {
126
## task 1
137
run jq -f bird-count.jq bird-counting-data.json

exercises/concept/grade-stats/bats-jq.bash

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
# - "Printing to the terminal", https://bats-core.readthedocs.io/en/stable/writing-tests.html#printing-to-the-terminal
1111
# - "File descriptor 3", https://bats-core.readthedocs.io/en/stable/writing-tests.html#file-descriptor-3-read-this-if-bats-hangs
1212

13-
1413
jq() {
1514
local output stderr rc line
1615
stderr=$(mktemp)
@@ -27,3 +26,64 @@ jq() {
2726
echo "$output"
2827
return "$rc"
2928
}
29+
30+
#############################################################
31+
# These are extra assert functions for use in tests.
32+
33+
# Assert two JSON objects are equal.
34+
# https://jqlang.org/manual/v1.7/#==-!=
35+
#
36+
# assert_objects_equal '{"a": 1, "b": 2}' '{"b":2,"a":1}'
37+
# # => true
38+
#
39+
assert_objects_equal() {
40+
local result=$(
41+
jq -n --argjson actual "$1" \
42+
--argjson expected "$2" \
43+
'$actual == $expected'
44+
)
45+
[[ $result == "true" ]]
46+
}
47+
48+
# Assert 2 floating-point values are "close enough".
49+
#
50+
# # are they the same to 2 decimal places?
51+
# assert_float 1.993 1.995 # => true
52+
#
53+
# # are they the same to 3 decimal places?
54+
# assert_float -d 3 1.993 1.995 # => false
55+
#
56+
assert_float() {
57+
local OPTIND OPTARG
58+
local decimals=2 actual expected
59+
while getopts :d: opt; do
60+
case $opt in
61+
d) decimals=$OPTARG ;;
62+
*) return 2 ;;
63+
esac
64+
done
65+
shift $((OPTIND - 1))
66+
# bash can't do floating point: use awk
67+
read -r actual expected < <(
68+
awk -v d="$decimals" -v a="$1" -v e="$2" '
69+
BEGIN {
70+
m = 10 ^ d
71+
print int(a * m)/m, int(e * m)/m
72+
}
73+
'
74+
)
75+
# now call a bats-assert command to get the desired output
76+
assert_equal "$actual" "$expected"
77+
}
78+
79+
# Assert that an object's value of a given key is the expected value.
80+
# This uses the bats `output` variable.
81+
#
82+
# run jq -f ...
83+
# assert_key_value 'the_key' 'the_expected_value'
84+
#
85+
assert_key_value() {
86+
local key=$1 expected=$2 actual
87+
actual=$(jq -rc --arg key "$key" '.[$key]' <<< "$output")
88+
assert_equal "$actual" "$expected"
89+
}

exercises/concept/grade-stats/test-grade-stats.bats

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,6 @@
22
load bats-extra
33
load bats-jq
44

5-
assert_key_value() {
6-
local key=$1 expected=$2 actual
7-
actual=$(jq -rc --arg key "$key" '.[$key]' <<< "$output")
8-
assert_equal "$actual" "$expected"
9-
}
10-
115
@test 'number to letter grade A' {
126
## task 1
137
run jq -nc '

exercises/concept/high-score-board/bats-jq.bash

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
# - "Printing to the terminal", https://bats-core.readthedocs.io/en/stable/writing-tests.html#printing-to-the-terminal
1111
# - "File descriptor 3", https://bats-core.readthedocs.io/en/stable/writing-tests.html#file-descriptor-3-read-this-if-bats-hangs
1212

13-
1413
jq() {
1514
local output stderr rc line
1615
stderr=$(mktemp)
@@ -27,3 +26,64 @@ jq() {
2726
echo "$output"
2827
return "$rc"
2928
}
29+
30+
#############################################################
31+
# These are extra assert functions for use in tests.
32+
33+
# Assert two JSON objects are equal.
34+
# https://jqlang.org/manual/v1.7/#==-!=
35+
#
36+
# assert_objects_equal '{"a": 1, "b": 2}' '{"b":2,"a":1}'
37+
# # => true
38+
#
39+
assert_objects_equal() {
40+
local result=$(
41+
jq -n --argjson actual "$1" \
42+
--argjson expected "$2" \
43+
'$actual == $expected'
44+
)
45+
[[ $result == "true" ]]
46+
}
47+
48+
# Assert 2 floating-point values are "close enough".
49+
#
50+
# # are they the same to 2 decimal places?
51+
# assert_float 1.993 1.995 # => true
52+
#
53+
# # are they the same to 3 decimal places?
54+
# assert_float -d 3 1.993 1.995 # => false
55+
#
56+
assert_float() {
57+
local OPTIND OPTARG
58+
local decimals=2 actual expected
59+
while getopts :d: opt; do
60+
case $opt in
61+
d) decimals=$OPTARG ;;
62+
*) return 2 ;;
63+
esac
64+
done
65+
shift $((OPTIND - 1))
66+
# bash can't do floating point: use awk
67+
read -r actual expected < <(
68+
awk -v d="$decimals" -v a="$1" -v e="$2" '
69+
BEGIN {
70+
m = 10 ^ d
71+
print int(a * m)/m, int(e * m)/m
72+
}
73+
'
74+
)
75+
# now call a bats-assert command to get the desired output
76+
assert_equal "$actual" "$expected"
77+
}
78+
79+
# Assert that an object's value of a given key is the expected value.
80+
# This uses the bats `output` variable.
81+
#
82+
# run jq -f ...
83+
# assert_key_value 'the_key' 'the_expected_value'
84+
#
85+
assert_key_value() {
86+
local key=$1 expected=$2 actual
87+
actual=$(jq -rc --arg key "$key" '.[$key]' <<< "$output")
88+
assert_equal "$actual" "$expected"
89+
}

exercises/concept/high-score-board/test-high-score-board.bats

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,6 @@
22
load bats-extra
33
load bats-jq
44

5-
assert_key_value() {
6-
local key=$1 expected=$2 actual
7-
actual=$(jq -rc --arg key "$key" '.[$key]' <<< "$output")
8-
assert_equal "$actual" "$expected"
9-
}
10-
115
@test creates_a_new_board_with_a_test_entry {
126
## task 1
137
run jq -n -c '

0 commit comments

Comments
 (0)