mardi 4 août 2015

How can I unit test Alex code?

I'm writing a lexer in Alex with the monad wrapper. It's not behaving as I expect, and I would like to write some unit tests for it. I can write unit tests for lexing a single token by doing:

runAlex "foo" alexMonadScan `shouldBe` Right TokenFoo

but I don't know how to test that the string "foo bar" gets lexed to [TokenFoo, TokenBar].

Given that Token is my token type, I'd need a function like runAlex that has the type String -> Alex [Token] -> Either String [Token], but I don't know how to transform alexMonadScan so that it has the type Alex [Token] rather than Alex Token.

I tried

runAlex "foo bar" (liftM (:[]) alexMonadScan) `shouldBe` [TokenFoo, TokenBar]

which seems to have the right type, but it returns Right [TokenEOF], apparently dropping the tokens it saw along the way.

How can I achieve this?

Aucun commentaire:

Enregistrer un commentaire