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?
There is a function alexScanTokens :: String -> [token]
which you can use.
It's defined in the file templates/wrappers.hs
Here's a monadic version I found here:
alexScanTokens :: String -> Either String [Keyword]
alexScanTokens inp = runAlex inp gather
where
gather = do
t <- alexMonadScan
case trace (show t) t of
EOF -> return [EOF]
_ -> (t:) `liftM` gather
链接地址: http://www.djcxy.com/p/65638.html
上一篇: 什么原因导致快乐抛出一个解析错误?
下一篇: 我如何单元测试Alex代码?