Capstone A: testing the parser

A table of cases

The parser of the capstone is the part with the most cases, so it gets a test runner: a table of a line and the result it must give, a check that prints one line per case, and an exit status of 0 only when every case passed.

check :: (String, Either String (String, Int, Int)) -> IO Bool check (line, expected) = let actual = parseLine line in do putStrLn ((if actual == expected then "ok " else "FAIL ") ++ line) pure (actual == expected)

The table has a good row, a row with a bad number and a row with too few fields, which are the three branches of the parser.

The runner

module Main (main) where import System.Exit (exitFailure, exitSuccess) parseLine :: String -> Either String (String, Int, Int) parseLine line = case break (== ',') line of (item, ',' : rest) -> case break (== ',') rest of (qty, ',' : pence) -> case (reads qty, reads pence) of ([(q, "")], [(p, "")]) -> Right (item, q, p) _ -> Left "bad number" _ -> Left "expected item,qty,pence" _ -> Left "expected item,qty,pence" check :: (String, Either String (String, Int, Int)) -> IO Bool check (line, expected) = let actual = parseLine line in do putStrLn ((if actual == expected then "ok " else "FAIL ") ++ line) pure (actual == expected) main :: IO () main = do results <- mapM check [ ("tea,3,250", Right ("tea", 3, 250)) , ("cake,1,400", Right ("cake", 1, 400)) , ("tea", Left "expected item,qty,pence") , ("tea,x,250", Left "bad number") ] if and results then exitSuccess else exitFailure

ghc -Wall -O1 -o tests tests.hs
./tests
echo "status: $?"

ok tea,3,250
ok cake,1,400
ok tea
ok tea,x,250
status: 0

What the runner gives

A failure in this runner is a line with FAIL and a status of 1; the next capstone builds the same table for a parser with precedence and an evaluator.

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. Why does the runner exit with a status?

    Answer

    The status is the verdict a build tool or a CI job reads

    exitSuccess and exitFailure turn the results into the one signal a tool uses.

  2. Why are all cases run instead of stopping at the first failure?

    Answer

    One run reports every failure

    The checks are values in a list and the summary decides at the end.

  3. What does the table cover?

    Answer

    A good row, a bad number and a short row

    The three branches of the parser are the three cases.

  4. What does this program print? module Main (main) where main :: IO () main = print (1 + 1 :: Int)

    Answer

    2

    The sum is printed without quotes.

  5. What does a FAIL line mean?

    Answer

    The actual result differs from the expected one

    The check compares the two values and prints the label it was given.

  6. What does this program print? module Main (main) where main :: IO () main = print (and [True, True, False])

    Answer

    False

    and is false when one element is false.

  7. What does the status of a passing run mean to Cabal?

    Answer

    The suite passed

    exitcode-stdio suites are read by their status.

  8. What does this program print? module Main (main) where main :: IO () main = print (null [1 :: Int])

    Answer

    False

    The list has an element, so null is false.

  9. What is the expected value for the row tea?

    Answer

    A Left with the message about the fields

    One field cannot make a row of three, so the parser returns the error.

  10. What does this program print? module Main (main) where main :: IO () main = print (length [1, 2, 3 :: Int])

    Answer

    3

    The list has three elements.

The code on this page was built with GHC 9.10.3 and run on Alpine Linux 3.24.3 (x86_64) in a Docker container.

Article author: Arthur Isaev