From 8ba4966248836facb5838ff5e9c800d05f33d57d Mon Sep 17 00:00:00 2001 From: Reed Everis Date: Mon, 18 Mar 2024 23:46:48 +0000 Subject: [PATCH] test: isolate tests from each other --- Closes #103 --- .../can-print-json-ast/prints-json-ast.bats | 19 +++++++++++-------- tests/plcc/compiles-obj/compiles-obj.bats | 15 ++++++++++++--- tests/plcc/detects-illegal-tokens.bats | 15 ++++++++++----- tests/plcc/parses.bats | 15 ++++++++++----- tests/plcc/scans-tokens.bats | 17 +++++++++++------ 5 files changed, 54 insertions(+), 27 deletions(-) diff --git a/tests/plcc/can-print-json-ast/prints-json-ast.bats b/tests/plcc/can-print-json-ast/prints-json-ast.bats index 7f1e0d31..6c58bce9 100644 --- a/tests/plcc/can-print-json-ast/prints-json-ast.bats +++ b/tests/plcc/can-print-json-ast/prints-json-ast.bats @@ -1,19 +1,22 @@ #!/usr/bin/env bats +setup() { + mkdir ${BATS_TMPDIR}/json-test +} + +teardown() { + cd ../.. + rm -rf ${BATS_TMPDIR}/json-test +} + @test "PLCC can print JSON AST." { FILES="expected.json given-grammar.lang given-program.lang" for f in $FILES ; do - cp "${BATS_TEST_DIRNAME}/${f}" "${BATS_TMPDIR}" + cp -R "${BATS_TEST_DIRNAME}/${f}" "${BATS_TMPDIR}/json-test" done - cp "${BATS_TEST_DIRNAME}"/* "$BATS_TMPDIR/" - cd "${BATS_TMPDIR}" + cd "${BATS_TMPDIR}/json-test" plccmk --json_ast given-grammar.lang parse -n --json_ast < given-program.lang > result.json diff expected.json result.json - - for f in $FILES ; do - rm "${BATS_TMPDIR}/${f}" - done - rm "${BATS_TMPDIR}/result.json" } diff --git a/tests/plcc/compiles-obj/compiles-obj.bats b/tests/plcc/compiles-obj/compiles-obj.bats index 76aec037..bf38be7d 100644 --- a/tests/plcc/compiles-obj/compiles-obj.bats +++ b/tests/plcc/compiles-obj/compiles-obj.bats @@ -1,11 +1,20 @@ #!/usr/bin/env bats +setup() { + mkdir ${BATS_TMPDIR}/obj-test +} + +teardown() { + cd ../../../.. + rm -rf ${BATS_TMPDIR}/obj-test +} + @test "PLCC compiles the OBJ language." { - # Copy the OBJ language to the temp directory for this test - cp -R "${BATS_TEST_DIRNAME}/OBJ" "${BATS_TMPDIR}" + # Copy the OBJ language to the temporary directory for this test + cp -R "${BATS_TEST_DIRNAME}/OBJ" "${BATS_TMPDIR}/obj-test" # Change into the temporary OBJ directory. - cd "${BATS_TMPDIR}/OBJ" + cd "${BATS_TMPDIR}/obj-test/OBJ" # Use plcc to generate the Java files from the OBJ grammar file. plcc "grammar" diff --git a/tests/plcc/detects-illegal-tokens.bats b/tests/plcc/detects-illegal-tokens.bats index a54bbd99..1de1c712 100644 --- a/tests/plcc/detects-illegal-tokens.bats +++ b/tests/plcc/detects-illegal-tokens.bats @@ -1,13 +1,21 @@ #!/usr/bin/env bats +setup() { + mkdir ${BATS_TMPDIR}/illegal-tokens-test +} + +teardown() { + rm -rf ${BATS_TMPDIR}/illegal-tokens-test +} + @test "PLCC detects illegal tokens." { # GIVEN a grammar file with a bad token name - cat << EOF > "$BATS_TMPDIR/grammar" + cat << EOF > "${BATS_TMPDIR}/illegal-tokens-test/grammar" token bad_token_name '.' EOF # WHEN plcc is ran on that grammar file - run plcc "$BATS_TMPDIR/grammar" + run plcc "${BATS_TMPDIR}/illegal-tokens-test/grammar" # print stdout and stderr for debugging echo "OUTPUT: $output" @@ -18,7 +26,4 @@ EOF # AND plcc reports that there is an "illegal token name" [[ "$output" = *"illegal token name"* ]] - - # cleanup - rm "$BATS_TMPDIR/grammar" } diff --git a/tests/plcc/parses.bats b/tests/plcc/parses.bats index 65556d8b..39e77e6e 100644 --- a/tests/plcc/parses.bats +++ b/tests/plcc/parses.bats @@ -1,7 +1,15 @@ #!/usr/bin/env bats +setup() { + mkdir ${BATS_TMPDIR}/parse-test +} + +teardown() { + rm -rf ${BATS_TMPDIR}/parse-test +} + @test "PLCC parses." { - cat << EOF > "$BATS_TMPDIR/grammar" + cat << EOF > "${BATS_TMPDIR}/parse-test/grammar" A 'A' B 'B' skip OTHER '.' @@ -10,11 +18,8 @@ skip OTHER '.' **= A EOF - RESULT="$(cd "$BATS_TMPDIR" && plccmk -c grammar > /dev/null && echo "A asdf A fdsa A B" | parse -n)" + RESULT="$(cd "${BATS_TMPDIR}/parse-test" && plccmk -c grammar > /dev/null && echo "A asdf A fdsa A B" | parse -n)" echo "RESULT: $RESULT" [[ "$RESULT" =~ .*OK.* ]] - - rm -rf "$BATS_TMPDIR/grammar" - rm -rf "$BATS_TMPDIR/Java" } diff --git a/tests/plcc/scans-tokens.bats b/tests/plcc/scans-tokens.bats index e28280ba..f5b84921 100644 --- a/tests/plcc/scans-tokens.bats +++ b/tests/plcc/scans-tokens.bats @@ -1,7 +1,15 @@ #!/usr/bin/env bats +setup() { + mkdir ${BATS_TMPDIR}/scan-test +} + +teardown() { + rm -rf ${BATS_TMPDIR}/scan-test +} + @test "PLCC scans." { - cat << EOF > "$BATS_TMPDIR/grammar" + cat << EOF > "${BATS_TMPDIR}/scan-test/grammar" skip WHITESPACE '\s' token FOO 'foo' token BAR 'bar' @@ -11,8 +19,8 @@ EOF IN="foo bar \n foobar" TOKENS="$( - cd "$BATS_TMPDIR" && - plccmk -c "$BATS_TMPDIR/grammar" && + cd "${BATS_TMPDIR}/scan-test" && + plccmk -c "grammar" && OUT="$(echo "$IN" | scan)" && echo "$OUT" )" @@ -20,7 +28,4 @@ EOF [[ "$TOKENS" =~ "FOO 'foo'" ]] [[ "$TOKENS" =~ "BAR 'bar'" ]] [[ "$TOKENS" =~ "ID 'foobar'" ]] - - rm -rf "$BATS_TMPDIR/grammar" - rm -rf "$BATS_TMPDIR/Java" }