Skip to content

Commit 89838b3

Browse files
committed
test-example: behave differently based on test type
* success: builds and passes the tests. * fail: builds, but fails the tests. * error: doesn't build. Closes #397
1 parent beb05ce commit 89838b3

File tree

2 files changed

+44
-6
lines changed

2 files changed

+44
-6
lines changed

README.md

+8
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,14 @@ The first condition is always enforced and cannot be circumvented.
127127
### Example solution
128128
The example solution could be inspiration for other language implementors. It doesn't need to be perfect or very elegant. But it should be efficient enough for the test suite to finish in only a few seconds.
129129

130+
Examples are named `<type>-<name>`.
131+
There are three possible types of examples:
132+
133+
* success: The example is expected to pass the tests.
134+
There should be at least one of these per exercise.
135+
* fail: The example is expected to build, but fail the tests.
136+
* error: The example is expected to fail to build.
137+
130138
### Test suite
131139
The test suite should be derived from the respective `x-common/exercises/<exercise-name>/canonical-data.json` and comply to some formatting and coding standards (to get an idea you may look at some of the existing tests).
132140

bin/test-example

+36-6
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,39 @@ examplecache="${cachedir}/.foldercache/${exercisename}/${examplename}/.stack-wor
3535
mkdir -p "$examplecache"
3636
ln -f -s "$examplecache"
3737

38-
echo "testing ${exampledir}"
39-
# SET_RESOLVER passed by .travis.yml - sets --resolver if not current.
40-
stack test ${SET_RESOLVER} `# Select the correct resolver. `\
41-
--install-ghc `# Download GHC if not in cache.`\
42-
--no-terminal `# Terminal detection is broken.`\
43-
--pedantic `# Enable -Wall and -Werror. `
38+
exampletype=$(echo "$examplename" | cut -d- -f1)
39+
40+
runstack () {
41+
# SET_RESOLVER passed by .travis.yml - sets --resolver if not current.
42+
stack "$@" ${SET_RESOLVER} `# Select the correct resolver. `\
43+
--install-ghc `# Download GHC if not in cache.`\
44+
--no-terminal `# Terminal detection is broken.`\
45+
--pedantic `# Enable -Wall and -Werror. `
46+
}
47+
48+
if [ "$exampletype" = "success" ]; then
49+
echo "testing ${exampledir} - should succeed"
50+
# Implicit exit value: this is last thing in this path,
51+
# so the script's exit value = stack's exit value.
52+
# If statements are reordered, please preserve this property.
53+
runstack "test"
54+
elif [ "$exampletype" = "fail" ]; then
55+
echo "testing ${exampledir} - should build, but fail tests"
56+
if ! runstack "build"; then
57+
echo "${exampledir} build failed unexpectedly"
58+
exit 1
59+
fi
60+
if runstack "test"; then
61+
echo "${exampledir} test succeeded unexpectedly"
62+
exit 1
63+
fi
64+
elif [ "$exampletype" = "error" ]; then
65+
echo "testing ${exampledir} - should fail to build"
66+
if runstack "test" "--no-run-tests"; then
67+
echo "${exampledir} build succeeded unexpectedly"
68+
exit 1
69+
fi
70+
else
71+
echo "unknown example type: ${exampledir}"
72+
exit 1
73+
fi

0 commit comments

Comments
 (0)