tasty-discoverを使ってみた

参考ページ

何それ?

 Haskellプロジェクトのtest-suiteのmain-isなるソースを書かずに、test-suiteプロジェクトのディレクトリを再帰的に見て、テスト項目を自動抽出してくれるやつ。

良かったところ

 完全にいいのですが、裏技があって、(unit|prop|scprop|spec|test)_function_nameとなるfunction_nameをsneak_caseにしておくと、テスト実行時に項目名にしてくれます。

Step1Test.hs

test_parser_and_printer_converts_left_to_right :: [TestTree]
test_parser_and_printer_converts_left_to_right =
  [ testCase "'123' -> '123'" $ "123" `isConvertedTo` "123"
  , testCase "'abc' -> 'abc'" $ "abc" `isConvertedTo` "abc"
  , testCase "'(123 456)' -> '(123 456)'" $ "(123 456)" `isConvertedTo` "(123 456)"
  , testCase "'( 123 456 789 )' -> '(123 456 789)'" $ "( 123 456 789 )" `isConvertedTo` "(123 456 789)"
  , testCase "'( + 2 (* 3 4) )' -> '(+ 2 (* 3 4))'" $ "( + 2 (* 3 4) )" `isConvertedTo` "(+ 2 (* 3 4))"
  ]
  where
    isConvertedTo :: Text -> Text -> Assertion
    isConvertedTo code expected =
      case Parser.parse code of
        Left  e     -> assertFailure $ "The parse is failed: " ++ Parser.parseErrorPretty e
        Right sexpr -> MT.toSyntax sexpr @?= expected

出力

Progress: 1/2tasty-discover
  parser and printer converts left to right
    '123' -> '123':                       OK
    'abc' -> 'abc':                       OK
    '(123 456)' -> '(123 456)':           OK
    '( 123 456 789 )' -> '(123 456 789)': OK
    '( + 2 (* 3 4) )' -> '(+ 2 (* 3 4))': OK

All 5 tests passed (0.00s)

Completed 2 action(s).

筆者プロフィール

my-latest-logo

aiya000(あいや)

せつラボ 〜圏論の基本〜」 「せつラボ2~雲と天使と関手圏~」 「矢澤にこ先輩といっしょに代数!」を書いています!

強い静的型付けとテストを用いて、バグを防ぐのが好き。Haskell・TypeScript。