Interpret shell failures as exceptions; improve internal tests.
catseye
13 years ago
0 | 0 |
module Test.Falderal.Formatter.Shell (format) where
|
1 | 1 |
|
2 | 2 |
--
|
3 | |
-- Test.Falderal.Formatter.Shell -- Shell-script compiler for Falderal
|
|
3 |
-- Test.Falderal.Formatter.Shell -- Bourne shell script compiler for Falderal
|
4 | 4 |
-- Copyright (c)2011 Cat's Eye Technologies. All rights reserved.
|
5 | 5 |
--
|
6 | 6 |
-- Redistribution and use in source and binary forms, with or without
|
|
71 | 71 |
\rm -f input.txt output.txt\n"
|
72 | 72 |
|
73 | 73 |
testExecution cmd id =
|
74 | |
cmd ++ " <input.txt >output.txt\n\
|
75 | |
\echo \"output\"\n\
|
|
74 |
cmd ++ " <input.txt >output.txt 2>&1\n\
|
|
75 |
\if [ $? != 0 ]; then\n\
|
|
76 |
\ echo \"exception\"\n\
|
|
77 |
\else\n\
|
|
78 |
\ echo \"output\"\n\
|
|
79 |
\fi\n\
|
76 | 80 |
\echo " ++ (show id) ++ "\n\
|
77 | 81 |
\echo `wc -l output.txt`\n\
|
78 | 82 |
\cat output.txt\n"
|
|
0 |
-> encoding: UTF-8
|
|
1 |
|
|
2 |
-> Functionality "Count lines" is implemented by
|
|
3 |
-> Haskell function Erroneous:countLines
|
|
4 |
|
|
5 |
-> Tests for functionality "Count lines"
|
|
6 |
|
|
7 |
| These are eight words
|
|
8 |
| that span two lines.
|
|
9 |
= 2
|
|
10 |
|
|
11 |
| These are eight words
|
|
12 |
| that span
|
|
13 |
| three lines.
|
|
14 |
= 3
|
|
15 |
|
|
16 |
-> Functionality "Raise exception" is implemented by shell command
|
|
17 |
-> "./exception.sh"
|
|
18 |
|
|
19 |
-> Tests for functionality "Raise exception"
|
|
20 |
|
|
21 |
When a shell script returns a non-zero exit code, that should be interpreted
|
|
22 |
as an exception by Falderal, which can be matched with an expected exception
|
|
23 |
block.
|
|
24 |
|
|
25 |
| whatever
|
|
26 |
? gello
|
|
0 |
module Erroneous where
|
|
1 |
|
|
2 |
-- This Haskell module contains some errors which will cause the results
|
|
3 |
-- generator generated for it to not even compile. Test.Falderal should
|
|
4 |
-- handle this in some way where it's obvious something went wrong.
|
|
5 |
|
|
6 |
countLines str = show $ length $ likes str
|
|
0 |
#!/bin/sh
|
|
1 |
|
|
2 |
# This Bourne shell script should fail when run, unless you have some really
|
|
3 |
# weirdly-named executables on your $PATH. Test.Falderal should interpret
|
|
4 |
# this failure as an exception.
|
|
5 |
|
|
6 |
echo 1>&2 gello
|
|
7 |
exit 1
|
4 | 4 |
# installed via Cabal first:
|
5 | 5 |
# $ cabal clean && cabal install --prefix=$HOME --user
|
6 | 6 |
|
|
7 |
echo 'Testing formatting...'
|
|
8 |
|
7 | 9 |
falderal format identity eg/LiterateHaskellDemo.lhs >formatted.txt
|
8 | 10 |
diff -u eg/LiterateHaskellDemo.lhs formatted.txt
|
9 | 11 |
E1=$?
|
10 | 12 |
rm -f formatted.txt
|
|
13 |
|
|
14 |
echo 'Testing LiterateHaskellDemo...'
|
11 | 15 |
|
12 | 16 |
cat >expected.txt <<EOF
|
13 | 17 |
--------------------------------
|
|
68 | 72 |
E2=$?
|
69 | 73 |
rm -f expected.txt actual.txt
|
70 | 74 |
|
71 | |
if [ $E1 != 0 -o $E2 != 0 ]
|
|
75 |
echo 'Testing Erroneous.falderal...'
|
|
76 |
|
|
77 |
cat >expected.txt <<EOF
|
|
78 |
--------------------------------
|
|
79 |
Total tests: 3, failures: 2
|
|
80 |
--------------------------------
|
|
81 |
|
|
82 |
NOT RUN : (#2)
|
|
83 |
Input:
|
|
84 |
These are eight words
|
|
85 |
that span two lines.
|
|
86 |
Expected: Output "2"
|
|
87 |
|
|
88 |
NOT RUN : (#3)
|
|
89 |
Input:
|
|
90 |
These are eight words
|
|
91 |
that span
|
|
92 |
three lines.
|
|
93 |
Expected: Output "3"
|
|
94 |
|
|
95 |
EOF
|
|
96 |
cd eg
|
|
97 |
falderal -m test Erroneous.falderal >../actual.txt 2>/dev/null
|
|
98 |
cd ..
|
|
99 |
diff -u expected.txt actual.txt
|
|
100 |
E3=$?
|
|
101 |
rm -f expected.txt actual.txt
|
|
102 |
|
|
103 |
if [ $E1 != 0 -o $E2 != 0 -o $E3 != 0 ]
|
72 | 104 |
then
|
|
105 |
echo "Internal tests failed!"
|
73 | 106 |
exit 1
|
74 | 107 |
else
|
|
108 |
echo "All tests passed."
|
75 | 109 |
exit 0
|
76 | 110 |
fi
|