How can I unit test Alex's code?

I am writing lexer in Alex with a monad cover. It doesn't behave the way I expect and 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 dont know how to check that the string "foo bar" gets lexed before [TokenFoo, TokenBar]

.

Considering that Token

is my token, I need a type function runAlex

that has a type String -> Alex [Token] -> Either String [Token]

, but I don't know how to convert alexMonadScan

to have a type Alex [Token]

, not Alex Token

.

I tried

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

      

which appears to be of the correct type, but returns Right [TokenEOF]

, seemingly lowering the markers it saw along the way.

How can I achieve this?

+3


source to share


1 answer


There is a function alexScanTokens :: String -> [token]

that you can use.

It is defined in the file templates/wrappers.hs



Here in the 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

      

+4


source







All Articles