Skip to content

WIP: Test that bin/check_optional works. (Do not merge) #1541

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

Closed
wants to merge 1 commit into from
Closed
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
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ cache: yarn
script:
- bin/check_required_files_present
- sh bin/check_versions
- bin/check_optional
- yarn test
3 changes: 3 additions & 0 deletions OPTIONAL-KEYS.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
floating-point
big-integer
unicode
39 changes: 39 additions & 0 deletions bin/check_optional
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#!/bin/sh

# This script checks that 'optional' fields in canonical-data.json are listed.

optional_keys_file=OPTIONAL-KEYS.txt
allowed_optional=$(jq -nR '[inputs]' $optional_keys_file)

failed=0

check_optional_for_file() {
json_file=$1
echo "Checking 'optional' fields in $json_file"

present_optional=$(jq '[ .cases[] | recurse(.cases[]?) | .optional | select(. != null) ]' $json_file)
invalid_optional=$(jq --null-input \
--argjson allowed "$allowed_optional" \
--argjson present "$present_optional" \
--raw-output '$present - $allowed | join("\n")')

if [ ! -z "$invalid_optional" ]; then
echo "Invalid optional fields:"
echo "$invalid_optional" | perl -pe 's/^/ - /'
echo

failed=1
fi
}

for json_file in $(git diff --name-only master HEAD | grep '^exercises/.*/canonical-data\.json$'); do
check_optional_for_file $json_file
done

if [ $failed -gt 0 ]; then
echo "Allowed optional fields (see $optional_keys_file) are:"
cat $optional_keys_file | perl -pe 's/^/ - /'
exit 1
fi

exit 0
7 changes: 5 additions & 2 deletions exercises/leap/canonical-data.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
{
"description": "year divisible by 2, not divisible by 4 in common year",
"property": "leapYear",
"optional": "unicode",
"input": {
"year": 1970
},
Expand All @@ -24,7 +25,8 @@
"input": {
"year": 1996
},
"expected": true
"expected": true,
"optional": "medium-sized-int"
},
{
"description": "year divisible by 100, not divisible by 400 in common year",
Expand All @@ -48,7 +50,8 @@
"input": {
"year": 1800
},
"expected": false
"expected": false,
"optional": "dont-mention-the-war"
}
]
}
13 changes: 9 additions & 4 deletions exercises/robot-simulator/canonical-data.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
"y": 0
},
"direction": "north"
}
},
"optional": "floating-point"
},
{
"description": "at negative position facing south",
Expand All @@ -46,7 +47,8 @@
"y": -1
},
"direction": "south"
}
},
"optional": "unicode"
}
]
},
Expand Down Expand Up @@ -83,6 +85,7 @@
"direction": "east",
"instructions": "R"
},
"optional": "wat",
"expected": {
"position": {
"x": 0,
Expand Down Expand Up @@ -189,7 +192,8 @@
"y": 0
},
"direction": "east"
}
},
"optional": "wat"
},
{
"description": "changes east to north",
Expand All @@ -208,7 +212,8 @@
"y": 0
},
"direction": "north"
}
},
"optional": "sugar"
}
]
},
Expand Down