Skip to content

Commit 6ebc5b4

Browse files
committed
Refactoring and bugfix in Show instance
1 parent 2c21c63 commit 6ebc5b4

File tree

3 files changed

+13
-14
lines changed

3 files changed

+13
-14
lines changed

shelltestrunner.cabal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ executable shelltest
4646
Import
4747
Parse
4848
Preprocessor
49+
Print
4950
Types
5051
Utils
5152
Utils.Debug

src/Types.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ instance Show Matcher where show = showMatcherTrimmed
4444
showMatcherTrimmed :: Matcher -> String
4545
showMatcherTrimmed (PositiveRegex r) = "/"++(trim r)++"/"
4646
showMatcherTrimmed (NegativeRegex r) = "!/"++(trim r)++"/"
47-
showMatcherTrimmed (Numeric s) = trim (show s)
48-
showMatcherTrimmed (NegativeNumeric s) = "!"++ trim (show s)
47+
showMatcherTrimmed (Numeric s) = trim s
48+
showMatcherTrimmed (NegativeNumeric s) = "!"++ trim s
4949
showMatcherTrimmed (Lines _ s) = trim s
5050

5151
showMatcher :: Matcher -> String

src/shelltest.hs

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ main = do
147147

148148

149149
printShellTestsWithResults :: Args -> Either ParseError [ShellTest] -> IO ()
150-
printShellTestsWithResults args (Right ts) = mapM_ (prepareShellTest args True) ts
150+
printShellTestsWithResults args (Right ts) = mapM_ (prepareShellTest args) ts
151151
printShellTestsWithResults _ (Left e) = putStrLn $ "*** parse error in " ++ (sourceName $ errorPos e)
152152

153153
-- | Additional argument checking.
@@ -157,20 +157,18 @@ checkArgs args = do
157157
warn $ printf "Please specify at least one test file or directory, eg: %s tests" progname
158158
when (isJust (actual args) && not (isJust (print_ args))) $
159159
warn "Option --actual can only be used with --print."
160-
when (fromMaybe "v3" (print_ args) /= "v3") $
161-
warn "Currently, --print only supports test format v3."
162160
return args
163161

164162
-- running tests
165163

166164
testFileParseToHUnitTest :: Args -> Either ParseError [ShellTest] -> Test.HUnit.Test
167-
testFileParseToHUnitTest args (Right ts) = TestList $ map (\t -> testname t ~: prepareShellTest args False t) ts
165+
testFileParseToHUnitTest args (Right ts) = TestList $ map (\t -> testname t ~: prepareShellTest args t) ts
168166
testFileParseToHUnitTest _ (Left e) = ("parse error in " ++ (sourceName $ errorPos e)) ~: (assertFailure :: (String -> IO ())) $ show e
169167

170168
-- | Prepare test as IO action and optionally print it (as specified in args).
171-
prepareShellTest :: Args -> Bool -> ShellTest -> IO ()
172-
prepareShellTest args printTests st@ShellTest{testname=n,command=c,stdin=i,stdoutExpected=o_expected,
173-
stderrExpected=e_expected,exitCodeExpected=x_expected,lineNumber=ln} =
169+
prepareShellTest :: Args -> ShellTest -> IO ()
170+
prepareShellTest args st@ShellTest{testname=n,command=c,stdin=i,stdoutExpected=o_expected,
171+
stderrExpected=e_expected,exitCodeExpected=x_expected,lineNumber=ln} =
174172
do
175173
let e = with args
176174
cmd = case (e,c) of (_:_, ReplaceableCommand s) -> e ++ " " ++ dropWhile (/=' ') s
@@ -188,11 +186,11 @@ prepareShellTest args printTests st@ShellTest{testname=n,command=c,stdin=i,stdou
188186
let outputMatch = maybe True (o_actual `matches`) o_expected
189187
let errorMatch = maybe True (e_actual `matches`) e_expected
190188
let exitCodeMatch = show x_actual `matches` x_expected
191-
if printTests
192-
then printShellTest (actual args) st (mkEither outputMatch o_actual) (mkEither errorMatch e_actual) (mkEither exitCodeMatch x_actual)
193-
else if (x_actual == 127) -- catch bad executable - should work on posix systems at least
194-
then ioError $ userError $ unwords $ filter (not . null) [e_actual, printf "Command: '%s' Exit code: %i" cmd x_actual] -- XXX still a test failure; should be an error
195-
else assertString $ concat $ filter (not . null) [
189+
case print_ args of
190+
Just format -> printShellTest format (actual args) st (mkEither outputMatch o_actual) (mkEither errorMatch e_actual) (mkEither exitCodeMatch x_actual)
191+
Nothing -> if (x_actual == 127) -- catch bad executable - should work on posix systems at least
192+
then ioError $ userError $ unwords $ filter (not . null) [e_actual, printf "Command: '%s' Exit code: %i" cmd x_actual] -- XXX still a test failure; should be an error
193+
else assertString $ concat $ filter (not . null) [
196194
if any not [outputMatch, errorMatch, exitCodeMatch]
197195
then printf "Command (at line %s):\n%s\n" (show ln) cmd
198196
else ""

0 commit comments

Comments
 (0)