packages feed

skeletest-0.3.6: test/Skeletest/AssertionsSpec.hs

{-# LANGUAGE CPP #-}
{-# LANGUAGE OverloadedRecordDot #-}

module Skeletest.AssertionsSpec (spec) where

import Skeletest
import Skeletest.Predicate qualified as P
import Skeletest.TestUtils.CallStack (sanitizeTraceback)
import Skeletest.TestUtils.Integration

spec :: Spec
spec = do
  describe "shouldBe" $ do
    it "should pass" $
      1 `shouldBe` (1 :: Int)

    integration . it "should show helpful failure" $ do
      runner <- getFixture @TestRunner
      runner.addTestFile "ExampleSpec.hs" $
        [ "module ExampleSpec (spec) where"
        , ""
        , "import Skeletest"
        , ""
        , "spec = it \"should fail\" $ 1 `shouldBe` (2 :: Int)"
        ]

      (stdout, stderr) <- expectFailure runner.runTests
      stderr `shouldBe` ""
      stdout `shouldSatisfy` P.matchesSnapshot

  describe "shouldNotBe" $ do
    it "should pass" $
      1 `shouldNotBe` (2 :: Int)

    integration . it "should show helpful failure" $ do
      runner <- getFixture @TestRunner
      runner.addTestFile "ExampleSpec.hs" $
        [ "module ExampleSpec (spec) where"
        , ""
        , "import Skeletest"
        , ""
        , "spec = it \"should fail\" $ 1 `shouldNotBe` (1 :: Int)"
        ]

      (stdout, stderr) <- expectFailure runner.runTests
      stderr `shouldBe` ""
      stdout `shouldSatisfy` P.matchesSnapshot

  describe "shouldSatisfy" $ do
    it "should pass" $
      1 `shouldSatisfy` P.gt (0 :: Int)

    integration . it "should show helpful failure" $ do
      runner <- getFixture @TestRunner
      runner.addTestFile "ExampleSpec.hs" $
        [ "module ExampleSpec (spec) where"
        , ""
        , "import Skeletest"
        , "import qualified Skeletest.Predicate as P"
        , ""
        , "spec = it \"should fail\" $ (-1) `shouldSatisfy` P.gt (0 :: Int)"
        ]

      (stdout, stderr) <- expectFailure runner.runTests
      stderr `shouldBe` ""
      stdout `shouldSatisfy` P.matchesSnapshot

  describe "shouldNotSatisfy" $ do
    it "should pass" $
      (-1) `shouldNotSatisfy` P.gt (0 :: Int)

    integration . it "should show helpful failure" $ do
      runner <- getFixture @TestRunner
      runner.addTestFile "ExampleSpec.hs" $
        [ "module ExampleSpec (spec) where"
        , ""
        , "import Skeletest"
        , "import qualified Skeletest.Predicate as P"
        , ""
        , "spec = it \"should fail\" $ 1 `shouldNotSatisfy` P.gt (0 :: Int)"
        ]

      (stdout, stderr) <- expectFailure runner.runTests
      stderr `shouldBe` ""
      stdout `shouldSatisfy` P.matchesSnapshot

  describe "shouldReturn" $ do
    it "should pass" $
      pure 1 `shouldReturn` (1 :: Int)

    integration . it "should show helpful failure" $ do
      runner <- getFixture @TestRunner
      runner.addTestFile "ExampleSpec.hs" $
        [ "module ExampleSpec (spec) where"
        , ""
        , "import Skeletest"
        , ""
        , "spec = it \"should fail\" $ pure 1 `shouldReturn` (2 :: Int)"
        ]

      (stdout, stderr) <- expectFailure runner.runTests
      stderr `shouldBe` ""
      stdout `shouldSatisfy` P.matchesSnapshot

  describe "context" $ do
    integration . it "should show failure context" $ do
      runner <- getFixture @TestRunner
      runner.addTestFile "ExampleSpec.hs" $
        [ "module ExampleSpec (spec) where"
        , ""
        , "import Skeletest"
        , ""
        , "spec = it \"should fail\" $ do"
        , "  context \"hello\" . context \"world\" $"
        , "    1 `shouldBe` (2 :: Int)"
        ]

      (stdout, stderr) <- expectFailure runner.runTests
      stderr `shouldBe` ""
      stdout `shouldSatisfy` P.matchesSnapshot

  describe "failTest" $ do
    integration . it "should show failure" $ do
      runner <- getFixture @TestRunner
      runner.addTestFile "ExampleSpec.hs" $
        [ "module ExampleSpec (spec) where"
        , ""
        , "import Skeletest"
        , ""
        , "spec = it \"should fail\" $ failTest \"error message\""
        ]

      (stdout, stderr) <- expectFailure runner.runTests
      stderr `shouldBe` ""
      stdout `shouldSatisfy` P.matchesSnapshot

  integration . it "shows backtrace of failed assertions" $ do
    runner <- getFixture @TestRunner
    runner.addTestFile "ExampleSpec.hs" $
      [ "module ExampleSpec (spec) where"
      , ""
      , "import Skeletest"
      , "import qualified Skeletest.Predicate as P"
      , ""
      , "spec = it \"should fail\" $ expectPositive (-1)"
      , ""
      , "expectPositive :: HasCallStack => Int -> IO ()"
      , "expectPositive = expectGT 0"
      , ""
      , "expectGT :: HasCallStack => Int -> Int -> IO ()"
      , "expectGT x actual = actual `shouldSatisfy` P.gt x"
      ]

    (stdout, stderr) <- expectFailure runner.runTests
    stderr `shouldBe` ""
    stdout `shouldSatisfy` P.matchesSnapshot

  integration . it "shows helpful error on pattern match fail" $ do
    runner <- getFixture @TestRunner
    runner.addTestFile "ExampleSpec.hs" $
      [ "module ExampleSpec (spec) where"
      , ""
      , "import Skeletest"
      , "import qualified Skeletest.Predicate as P"
      , ""
      , "spec = it \"should fail\" $ do"
      , "  Just x <- pure Nothing"
      , "  x `shouldBe` True"
      ]

    (stdout, stderr) <- expectFailure runner.runTests
    stderr `shouldBe` ""
    stdout `shouldSatisfy` P.matchesSnapshot

  integration . it "shows unrecognized exceptions" $ do
    runner <- getFixture @TestRunner
    runner.addTestFile "ExampleSpec.hs" $
      [ "module ExampleSpec (spec) where"
      , ""
      , "import Skeletest"
      , "import qualified Skeletest.Predicate as P"
      , ""
      , "spec = it \"should fail\" $ do"
      , "  _ <- readFile \"unknown-file.txt\""
      , "  pure ()"
      ]

    (stdout, stderr) <- expectFailure runner.runTests
    stderr `shouldBe` ""
    sanitizeTraceback stdout `shouldSatisfy` P.matchesSnapshot

  integration . it "shows source code when running from different directory" $ do
    runner <- getFixture @TestRunner
    runner.addTestFile "ExampleSpec.hs" $
      [ "module ExampleSpec (spec) where"
      , "import Skeletest"
      , "spec = it \"should fail\" $ failTest \"failure\""
      ]

    (stdout, stderr) <- expectFailure $ runner.runTestsWith def{cwd = Just "/"}
    stderr `shouldBe` ""
    stdout `shouldSatisfy` P.matchesSnapshot