Random thoughts, daily life, being a student, quantum computing.

2012-01-19

Approaching HUnit

My January goals for LQPL include using TDD practices to create the code for the LQPL server. I am doing that, and at this point, I am starting to see some of the "warts" with the way I am tackling the TDD. For example, to be able to parse a set of five input text commands, I've created 56 cases (assertions).  Each case is a single line, but that is a lot of reading. For the "error out" cases, I created a function that improved the readability quite a bit.

    
    expectLeftString :: Either String a -> Bool
    expectLeftString (Left _)  = True
    expectLeftString _ = False
    
    makeBoolParseError :: String -> Assertion
    makeBoolParseError s = assertBool ("'"++s++"' returns error") (expectLeftString (getCommand s))
This gets used in the test creation like this:  

    "parseL3 " ~: makeBoolParseError "load"


For the positive cases, however, it becomes quite a bit messier. For example:

    "parseL2aSpace " ~: "'load     /x/xxx addint.qpo' returns QCLoad" ~:
               Right (QCLoad "/a/bc with space.qpo") @=? (getCommand "load /a/bc with space.qpo") 


Multiple lines of this become rather difficult to parse.

One solution would be to create a helper function for creating the positive assertions as well. I'll give that a shot and see how it looks!

No comments: