parsec解析器的美味Hunit测试?

时间:2016-07-21 19:56:59

标签: unit-testing haskell parsec

我需要设置一个测试套装,让我确定我在parsec解析器中所做的更改是否会破坏其他任何内容。

我正在使用tasty单元测试,这就是我所拥有的:

simpleLabels :: TestTree
simpleLabels = testGroup "Simple label searches"
    [ testCase "List comparison (same length)" $ -- just a test to make sure that the @?= works like it should
      [1, 2, 3] `compare` [1,2,2] @?= LT
      , 
      testCase "Phonetic = a " $ 
        parse umeQuery "Source" "Phonetic = a " @?= Right ( LabelInLabelType "Phonetic" ["a"] "=")  
    ]

现在,当我在REPL中运行它时,解析器正确解析了这个流:

> parse umeQuery "something"  (pack  "Phonetic = a ")
Right (LabelInLabelType "Phonetic" ["a"] "=")

这就像它应该的那样,因此我在上面的测试中设置了它。

现在,测试套件根本没有构建,给出错误:

TestQueryParser.hs:29:55:
    No instance for (Eq ParseError) arising from a use of ‘@?=’
    In the second argument of ‘($)’, namely
      ‘parse umeQuery "Ume Query : " "Phonetic = a "
       @?= Right (LabelInLabelType "Phonetic" ["a"] "=")’
    In the expression:
      testCase "Phonetic = a "
      $ parse umeQuery "Ume Query : " "Phonetic = a "
        @?= Right (LabelInLabelType "Phonetic" ["a"] "=")
    In the second argument of ‘testGroup’, namely
      ‘[testCase "List comparison (same length)"
        $ [1, 2, ....] `compare` [1, 2, ....] @?= LT,
        testCase "Phonetic = a "
        $ parse umeQuery "Ume Query : " "Phonetic = a "
          @?= Right (LabelInLabelType "Phonetic" ["a"] "=")]’

为什么我需要这个,考虑到成功/正确解析应该导致Right a值?

为parsec解析器设置单元测试的最佳方法是什么?

1 个答案:

答案 0 :(得分:2)

感谢您提供所有有用的评论。

确实,正如@ user2407038指出的那样,需要更好地处理错误消息。

simpleLabels :: TestTree
simpleLabels = testGroup "Simple label searches"
    [
    testCase "Equality matching simple label" $ let 
        res = case (parse umeQuery "Source" "Phonetic = a ") of
            Right a -> a
            Left a -> error "Something"
    in res @?=  LabelInLabelType "Phonetic" ["a"] "="
    ]

的工作原理。上例中的错误消息显然不是最佳的,但它适用于当前示例。