diff --git a/Cabal-tests/Cabal-tests.cabal b/Cabal-tests/Cabal-tests.cabal index a194259817f..efc28c56a8f 100644 --- a/Cabal-tests/Cabal-tests.cabal +++ b/Cabal-tests/Cabal-tests.cabal @@ -40,6 +40,7 @@ test-suite unit-tests UnitTests.Distribution.Types.GenericPackageDescription UnitTests.Distribution.Utils.CharSet UnitTests.Distribution.Utils.Generic + UnitTests.Distribution.Utils.Json UnitTests.Distribution.Utils.NubList UnitTests.Distribution.Utils.ShortText UnitTests.Distribution.Utils.Structured diff --git a/Cabal-tests/tests/UnitTests.hs b/Cabal-tests/tests/UnitTests.hs index 3721838f0dd..611e3dd5bdb 100644 --- a/Cabal-tests/tests/UnitTests.hs +++ b/Cabal-tests/tests/UnitTests.hs @@ -23,6 +23,7 @@ import qualified UnitTests.Distribution.Simple.Utils import qualified UnitTests.Distribution.System import qualified UnitTests.Distribution.Utils.CharSet import qualified UnitTests.Distribution.Utils.Generic +import qualified UnitTests.Distribution.Utils.Json import qualified UnitTests.Distribution.Utils.NubList import qualified UnitTests.Distribution.Utils.ShortText import qualified UnitTests.Distribution.Utils.Structured @@ -57,6 +58,8 @@ tests mtimeChangeCalibrated = UnitTests.Distribution.Simple.Utils.tests ghcPath , testGroup "Distribution.Utils.Generic" UnitTests.Distribution.Utils.Generic.tests + , testGroup "Distribution.Utils.Json" $ + UnitTests.Distribution.Utils.Json.tests , testGroup "Distribution.Utils.NubList" UnitTests.Distribution.Utils.NubList.tests , testGroup "Distribution.Utils.ShortText" diff --git a/Cabal-tests/tests/UnitTests/Distribution/Utils/Json.hs b/Cabal-tests/tests/UnitTests/Distribution/Utils/Json.hs new file mode 100644 index 00000000000..5609a72b555 --- /dev/null +++ b/Cabal-tests/tests/UnitTests/Distribution/Utils/Json.hs @@ -0,0 +1,42 @@ +{-# LANGUAGE GADTs #-} +{-# LANGUAGE OverloadedStrings #-} +module UnitTests.Distribution.Utils.Json + ( tests + ) where + +import Distribution.Utils.Json + +import Test.Tasty +import Test.Tasty.HUnit + +tests :: [TestTree] +tests = + [ testCase "escapes strings correctly" $ + renderJson (JsonString "foo\"bar") @?= "\"foo\\\"bar\"" + , testCase "renders empty list" $ + renderJson (JsonArray []) @?= "[]" + , testCase "renders singleton list" $ + renderJson (JsonArray [JsonString "foo\"bar"]) @?= "[\"foo\\\"bar\"]" + , testCase "renders list" $ + renderJson (JsonArray [JsonString "foo\"bar", JsonString "baz"]) @?= "[\"foo\\\"bar\",\"baz\"]" + , testCase "renders empty object" $ + renderJson (JsonObject []) @?= "{}" + , testCase "renders singleton object" $ + renderJson (JsonObject [("key", JsonString "foo\"bar")]) @?= "{\"key\":\"foo\\\"bar\"}" + , testCase "renders object" $ + renderJson (JsonObject + [ ("key", JsonString "foo\"bar") + , ("key2", JsonString "baz")]) + @?= "{\"key\":\"foo\\\"bar\",\"key2\":\"baz\"}" + , testCase "renders number" $ + renderJson (JsonNumber 0) @?= "0" + , testCase "renders negative number" $ + renderJson (JsonNumber (-1)) @?= "-1" + , testCase "renders big number" $ + renderJson (JsonNumber 5000000) @?= "5000000" + , testCase "renders bool" $ do + renderJson (JsonBool True) @?= "true" + renderJson (JsonBool False) @?= "false" + , testCase "renders null" $ do + renderJson JsonNull @?= "null" + ]