[Haskell-beginners] QuickCheck for testing a parsing function

Thomas Bach thbach at students.uni-mainz.de
Sun Feb 16 09:45:21 UTC 2014


Hi there,

I have the following snippet:

data Volume = Volume { name  :: String
                     , mount :: String
                     , size  :: String
                     , fs    :: String
                     , mbr   :: Bool
                     } deriving (Show, Eq)

defaultVolume :: Volume
defaultVolume = Volume { name  = ""
                       , mount = ""
                       , size  = ""
                       , fs    = "ext4"
                       , mbr   = False
                       }

parseOptVols :: String -> Volume
parseOptVols s = parseChain (defaultVolume, s)
  where
    parseChain = parseMBR . parseFS . parseSize . parseMount . parseName
    parseName  (vol, s) = (vol {name  = getThis s}, getNext s)
    parseMount (vol, s) = (vol {mount = getThis s}, getNext s)
    parseSize  (vol, s) = (vol {size  = getThis s}, getNext s)
    -- fs and mbr are optional
    parseFS (vol, "")    = (vol, "")
    parseFS (vol, ',':s) = (vol, s)
    parseFS (vol, s)     = (vol {fs = getThis s}, getNext s)
    parseMBR (vol, "t") = vol {mbr = True}
    parseMBR (vol, _  ) = vol
    getThis = takeWhile (',' /=)
    getNext = saveTail . dropWhile (',' /=)

saveTail :: [a] -> [a]
saveTail     [] = []
saveTail (_:xs) = xs

What it basically does is parse a string (handed over from the
command line) like "boot,/boot,256M,ext3,t" and populates a Volume
instance with this. The last two fields are optional.

I like testing and coming from an object-oriented background unit
testing is the most comfortable to me. But I wonder if there is some
smart way to make QuickCheck produce test cases. I would need some
generator for this which produces a pattern like
"name,mount,size,fs,mbr" eventually leaving "fs" and/or "mbr" empty and
sometimes omitting both commas or just one of them. And I also need
access to these fields in order to be able to compare them with the
resulting Volume instance. I can see how to achieve the former somehow,
but I don't have any clue how to achieve the latter.

Any hints? Or is this simply not a nail for the QuickCheck-hammer?

Regards,

        Thomas Bach.

PS: Any advice on how to improve the code above is highly appreciated as
well.


More information about the Beginners mailing list