packages feed

hspec-expectations-json 1.0.0.6 → 1.0.0.7

raw patch · 6 files changed

+110/−11 lines, 6 filesPVP: minor bump suggested

API additions: PVP suggests at least a minor version bump

API changes (from Hackage documentation)

+ Test.Hspec.Expectations.Json.Color: Green :: Color
+ Test.Hspec.Expectations.Json.Color: Red :: Color
+ Test.Hspec.Expectations.Json.Color: Reset :: Color
+ Test.Hspec.Expectations.Json.Color: data Color
+ Test.Hspec.Expectations.Json.Color: getColorize :: MonadIO m => m (Color -> String -> String)
+ Test.Hspec.Expectations.Json.Lifted: shouldBeJson :: (HasCallStack, MonadIO m) => Value -> Value -> m ()
+ Test.Hspec.Expectations.Json.Lifted: shouldBeUnorderedJson :: (HasCallStack, MonadIO m) => Value -> Value -> m ()
+ Test.Hspec.Expectations.Json.Lifted: shouldMatchJson :: (HasCallStack, MonadIO m) => Value -> Value -> m ()
+ Test.Hspec.Expectations.Json.Lifted: shouldMatchOrderedJson :: (HasCallStack, MonadIO m) => Value -> Value -> m ()

Files

CHANGELOG.md view
@@ -1,6 +1,10 @@-## [_Unreleased_](https://github.com/freckle/hspec-expectations-json/compare/v1.0.0.6...main)+## [_Unreleased_](https://github.com/freckle/hspec-expectations-json/compare/v1.0.1.0...main) -None+## [v1.0.1.0](https://github.com/freckle/hspec-expectations-json/compare/v1.0.0.6...v1.0.1.0)++- Colorize diff in expectation failure, if connected to a terminal or running on+  GitHub Actions.+- Add a `.Lifted` module with `MonadIO` versions  ## [v1.0.0.6](https://github.com/freckle/hspec-expectations-json/compare/v1.0.0.5...v1.0.0.6) 
hspec-expectations-json.cabal view
@@ -1,6 +1,6 @@ cabal-version:   1.18 name:            hspec-expectations-json-version:         1.0.0.6+version:         1.0.0.7 license:         MIT license-file:    LICENSE copyright:       2020 Freckle Education@@ -42,7 +42,9 @@ library     exposed-modules:         Test.Hspec.Expectations.Json+        Test.Hspec.Expectations.Json.Color         Test.Hspec.Expectations.Json.Internal+        Test.Hspec.Expectations.Json.Lifted      hs-source-dirs:     library     other-modules:      Paths_hspec_expectations_json
+ library/Test/Hspec/Expectations/Json/Color.hs view
@@ -0,0 +1,30 @@+module Test.Hspec.Expectations.Json.Color+  ( Color(..)+  , getColorize+  ) where++import Prelude++import Control.Monad.IO.Class (MonadIO(..))+import System.Environment (lookupEnv)+import System.IO (hIsTerminalDevice, stdout)++data Color = Reset | Red | Green++getColorize :: MonadIO m => m (Color -> String -> String)+getColorize = do+  -- The stdout handle will not appear as a terminal on GitHub Actions, but it+  -- does support color escapes.+  shouldColorize <-+    liftIO $ (||) <$> isGitHubActions <*> hIsTerminalDevice stdout++  pure $ if shouldColorize+    then \c x -> escape Reset <> escape c <> x <> escape Reset+    else \_ x -> x+  where isGitHubActions = (== Just "true") <$> lookupEnv "GITHUB_ACTIONS"++escape :: Color -> String+escape = \case+  Reset -> "\ESC[0m"+  Red -> "\ESC[0;31m"+  Green -> "\ESC[0;32m"
library/Test/Hspec/Expectations/Json/Internal.hs view
@@ -43,20 +43,22 @@ import qualified Data.Vector as V import GHC.Stack (HasCallStack) import qualified Test.HUnit as HUnit+import Test.Hspec.Expectations.Json.Color  -- So we can call HashMap KeyMap in older aeson {-# ANN module ("HLint: ignore Avoid restricted qualification" :: String) #-}  assertBoolWithDiff :: HasCallStack => Bool -> Text -> Text -> IO ()-assertBoolWithDiff asserting expected got =-  flip HUnit.assertBool asserting . unlines . map addSign $ getDiff+assertBoolWithDiff asserting expected got = do+  colorize <- getColorize+  flip HUnit.assertBool asserting . unlines . map (addSign colorize) $ getDiff     (lines (T.unpack expected))     (lines (T.unpack got))  where-  addSign = \case-    Both _ s -> "   " ++ s-    First s -> "---" ++ s-    Second s -> "+++" ++ s+  addSign colorize = \case+    Both _ s -> colorize Reset $ "   " ++ s+    First s -> colorize Red $ "---" ++ s+    Second s -> colorize Green $ "+++" ++ s  newtype Superset = Superset Value 
+ library/Test/Hspec/Expectations/Json/Lifted.hs view
@@ -0,0 +1,25 @@+module Test.Hspec.Expectations.Json.Lifted+  ( shouldBeJson+  , shouldBeUnorderedJson+  , shouldMatchJson+  , shouldMatchOrderedJson+  ) where++import Prelude++import Control.Monad.IO.Class (MonadIO(..))+import Data.Aeson+import GHC.Stack+import qualified Test.Hspec.Expectations.Json as E++shouldBeJson :: (HasCallStack, MonadIO m) => Value -> Value -> m ()+shouldBeJson x = liftIO . E.shouldBeJson x++shouldBeUnorderedJson :: (HasCallStack, MonadIO m) => Value -> Value -> m ()+shouldBeUnorderedJson x = liftIO . E.shouldBeUnorderedJson x++shouldMatchJson :: (HasCallStack, MonadIO m) => Value -> Value -> m ()+shouldMatchJson x = liftIO . E.shouldMatchJson x++shouldMatchOrderedJson :: (HasCallStack, MonadIO m) => Value -> Value -> m ()+shouldMatchOrderedJson x = liftIO . E.shouldMatchOrderedJson x
tests/Test/Hspec/Expectations/JsonSpec.hs view
@@ -2,8 +2,7 @@  module Test.Hspec.Expectations.JsonSpec   ( spec-  )-where+  ) where  import Prelude @@ -111,3 +110,40 @@         |]        a `shouldMatchJson` b++    -- it "is an example failure, to checking how they're printed" $ do+    --   let+    --     a = [aesonQQ|+    --       [ { "shortName": "B"+    --         , "subSkills":+    --           [ { "shortName": "1"+    --             , "uspId": "68fa5xxxxxxxx332f7c88884e5c40e"+    --             }+    --           ]+    --         }+    --       , { "shortName": "A"+    --         , "subSkills":+    --              [ { "shortName": "a"+    --                , "uspId": "71839723561ba1a49cf2a789dbe50302"+    --                }+    --              , { "shortName": "b"+    --                , "uspId": "4096d2cfebcab73438971ae2304544ee"+    --                }+    --              ]+    --         }+    --       ]+    --     |]+    --     b = [aesonQQ|+    --       [ { "subSkills":+    --           [ { "uspId": "71839723561ba1a49cf2a789dbe50302" }+    --           , { "uspId": "4096d2cfebcab73438971ae2304544ee" }+    --           ]+    --         }+    --       , { "subSkills":+    --           [ { "uspId": "68fa57ddbc1e9aa332f7c88884e5c40e" }+    --           ]+    --         }+    --       ]+    --     |]++    --   a `shouldMatchJson` b