Skip to content

Commit

Permalink
Write tests for Cabal's JSON lib
Browse files Browse the repository at this point in the history
  • Loading branch information
fendor committed Aug 15, 2021
1 parent cdf1984 commit 2e6d0db
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cabal-tests/Cabal-tests.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions Cabal-tests/tests/UnitTests.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"
Expand Down
42 changes: 42 additions & 0 deletions Cabal-tests/tests/UnitTests/Distribution/Utils/Json.hs
Original file line number Diff line number Diff line change
@@ -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"
]

0 comments on commit 2e6d0db

Please sign in to comment.