|
| 1 | +module Test.Hspec.Formatters.CodewarsSpec (spec) where |
| 2 | + |
| 3 | +import Helper (captureLines, normalizeSummary) |
| 4 | +import Test.Hspec |
| 5 | +import Test.Hspec.Core.Formatters.V2 (formatterToFormat) |
| 6 | +import qualified Test.Hspec.Core.Runner as H |
| 7 | +import qualified Test.Hspec.Core.Spec as H |
| 8 | +import Test.Hspec.Formatters.Codewars (newFormatter) |
| 9 | + |
| 10 | +runSpecWithCodewars :: Spec -> IO [String] |
| 11 | +runSpecWithCodewars s = |
| 12 | + do |
| 13 | + codewars <- newFormatter |
| 14 | + let config = H.defaultConfig {H.configFormat = Just $ formatterToFormat codewars} |
| 15 | + captureLines $ H.hspecWithResult config s |
| 16 | + >>= (return . normalizeSummary) |
| 17 | + |
| 18 | +spec :: Spec |
| 19 | +spec = do |
| 20 | + describe "Codewars Formatter" $ do |
| 21 | + it "handles passing tests" $ do |
| 22 | + r <- runSpecWithCodewars $ do |
| 23 | + H.describe "Example" $ do |
| 24 | + H.it "success" (H.Result "" H.Success) |
| 25 | + r |
| 26 | + `shouldBe` [ "", |
| 27 | + "<DESCRIBE::>Example", |
| 28 | + "", |
| 29 | + "<IT::>success", |
| 30 | + "", |
| 31 | + "<PASSED::>Test Passed", |
| 32 | + "", |
| 33 | + "<COMPLETEDIN::>0.000", |
| 34 | + "", |
| 35 | + "<COMPLETEDIN::>0.000" |
| 36 | + ] |
| 37 | + |
| 38 | + it "handles failing tests" $ do |
| 39 | + r <- runSpecWithCodewars $ do |
| 40 | + H.describe "parsing" $ do |
| 41 | + H.it "can parse integers and fail" $ do |
| 42 | + read "10" `shouldBe` (11 :: Int) |
| 43 | + H.it "can parse floating-point numbers and fail" $ do |
| 44 | + read "2.5" `shouldBe` (2.6 :: Float) |
| 45 | + r |
| 46 | + `shouldBe` [ "", |
| 47 | + "<DESCRIBE::>parsing", |
| 48 | + "", |
| 49 | + "<IT::>can parse integers and fail", |
| 50 | + "", |
| 51 | + "<FAILED::>expected: 11<:LF:> but got: 10", |
| 52 | + "", |
| 53 | + "<COMPLETEDIN::>0.000", |
| 54 | + "", |
| 55 | + "<IT::>can parse floating-point numbers and fail", |
| 56 | + "", |
| 57 | + "<FAILED::>expected: 2.6<:LF:> but got: 2.5", |
| 58 | + "", |
| 59 | + "<COMPLETEDIN::>0.000", |
| 60 | + "", |
| 61 | + "<COMPLETEDIN::>0.000" |
| 62 | + ] |
| 63 | + |
| 64 | + it "handles crashing tests" $ do |
| 65 | + r <- runSpecWithCodewars $ do |
| 66 | + H.it "crash" $ do |
| 67 | + read (last $ tail $ ["10"]) `shouldBe` (11 :: Int) |
| 68 | + r |
| 69 | + `shouldBe` [ "", |
| 70 | + "<IT::>crash", |
| 71 | + "", |
| 72 | + "<ERROR::>uncaught exception: ErrorCall<:LF:>Prelude.last: empty list", |
| 73 | + "", |
| 74 | + "<COMPLETEDIN::>0.000" |
| 75 | + ] |
| 76 | + |
| 77 | + it "handles pending tests" $ do |
| 78 | + r <- runSpecWithCodewars $ do |
| 79 | + H.it "not implemented" $ do |
| 80 | + H.pending |
| 81 | + H.it "not implemented, with a message" $ do |
| 82 | + H.pendingWith "Not implemented yet" |
| 83 | + |
| 84 | + r |
| 85 | + `shouldBe` [ "", |
| 86 | + "<IT::>not implemented", |
| 87 | + "", |
| 88 | + "<FAILED::>Test pending: no reason given", |
| 89 | + "", |
| 90 | + "<COMPLETEDIN::>0.000", |
| 91 | + "", |
| 92 | + "<IT::>not implemented, with a message", |
| 93 | + "", |
| 94 | + "<FAILED::>Test pending: Not implemented yet", |
| 95 | + "", |
| 96 | + "<COMPLETEDIN::>0.000" |
| 97 | + ] |
| 98 | + |
| 99 | + it "handles multiline names" $ do |
| 100 | + r <- runSpecWithCodewars $ do |
| 101 | + H.describe "Can present\nmultiline titles\nof groups" $ do |
| 102 | + H.it "Can present\nmultiline titles\nof items" $ do |
| 103 | + read "10" `shouldBe` (10 :: Int) |
| 104 | + r |
| 105 | + `shouldBe` [ "", |
| 106 | + "<DESCRIBE::>Can present<:LF:>multiline titles<:LF:>of groups", |
| 107 | + "", |
| 108 | + "<IT::>Can present<:LF:>multiline titles<:LF:>of items", |
| 109 | + "", |
| 110 | + "<PASSED::>Test Passed", |
| 111 | + "", |
| 112 | + "<COMPLETEDIN::>0.000", |
| 113 | + "", |
| 114 | + "<COMPLETEDIN::>0.000" |
| 115 | + ] |
| 116 | + |
| 117 | + it "handles multiline assertions" $ do |
| 118 | + r <- runSpecWithCodewars $ do |
| 119 | + H.it "present multiline assertions" $ do |
| 120 | + "10" `shouldBe` "10, but\nbroken into\nmultiple lines" |
| 121 | + r |
| 122 | + `shouldBe` [ "", |
| 123 | + "<IT::>present multiline assertions", |
| 124 | + "", |
| 125 | + "<FAILED::>expected: \"10, but\\nbroken into\\nmultiple lines\"<:LF:> but got: \"10\"", |
| 126 | + "", |
| 127 | + "<COMPLETEDIN::>0.000" |
| 128 | + ] |
0 commit comments