@@ -3,23 +3,25 @@ module Test.BasicsSpec where
33import Prelude
44
55import Data.Array.NonEmpty as NEA
6−import Data.BigInt (BigInt)
76import Data.Either (Either(..))
87import Data.Foldable (traverse_)
98import Data.JSDate as JSDate
109import Data.List as List
1110import Data.List.Lazy as LazyList
1211import Data.Map as Map
13−import Data.Maybe (Maybe(..), fromMaybe')
12+import Data.Maybe (Maybe(..), fromJust, fromMaybe')
1413import Data.Newtype (class Newtype, un)
1514import Data.Nullable as Nullable
1615import Data.Tuple (Tuple(..))
1716import Data.Tuple.Nested ((/\))
1817import Data.Variant (Variant, inj)
18+import Debug (spy)
1919import Effect.Class (liftEffect)
2020import Effect.Now (nowDateTime)
2121import Foreign.Object as Object
22−import Partial.Unsafe (unsafeCrashWith)
22+import Js.BigInt.BigInt (BigInt)
23+import Js.BigInt.BigInt as BigInt
24+import Partial.Unsafe (unsafeCrashWith, unsafePartial)
2325import Test.Spec (Spec, describe, it)
2426import Test.Spec.Assertions (shouldEqual)
2527import Test.Util (roundtrips)
@@ -27,7 +29,7 @@ import Type.Proxy (Proxy(..))
2729import Yoga.JSON (class ReadForeign, class WriteForeign, readJSON, writeJSON)
2830import Yoga.JSON.Variant (TaggedVariant(..), UntaggedVariant(..))
2931
30−spec :: Spec Unit
32+spec ∷ Spec Unit
3133spec = describe "En- and decoding" $ do
3234
3335 describe "works on primitive types" do
@@ -40,63 +42,63 @@ spec = describe "En- and decoding" $ do
4042 it "roundtrips Maybe" do
4143 roundtrips (Just 3)
4244 -- just roundtrips Nothing doesn't work when rendering the JSON
43− roundtrips { empty: Nothing :: Maybe Int }
44− it "roundtrips Nullable" $ traverse_ roundtrips [Nullable.notNull 3, Nullable.null]
45+ roundtrips { empty: Nothing ∷ Maybe Int }
46+ it "roundtrips Nullable" $ traverse_ roundtrips [ Nullable.notNull 3, Nullable.null ]
4547 it "roundtrips Either" do
46− roundtrips ((Left 3) :: Either Int Int)
47− roundtrips ((Right 3) :: Either String Int)
48− writeJSON (Right 3 :: Either Int Int) `shouldEqual` """{"value":3,"type":"right"}"""
49− writeJSON (Left true :: Either Boolean Int) `shouldEqual` """{"value":true,"type":"left"}"""
48+ roundtrips ((Left 3) ∷ Either Int Int)
49+ roundtrips ((Right 3) ∷ Either String Int)
50+ writeJSON (Right 3 ∷ Either Int Int) `shouldEqual` """{"value":3,"type":"right"}"""
51+ writeJSON (Left true ∷ Either Boolean Int) `shouldEqual` """{"value":true,"type":"left"}"""
5052 it "roundtrips Tuple" $ do
5153 roundtrips (Tuple 3 4)
5254 roundtrips ("4" /\ 8 /\ Just 4)
53− it "roundtrips Array" $ traverse_ roundtrips [["A", "B"],[]]
54− it "roundtrips LazyList" $ traverse_ roundtrips (LazyList.fromFoldable [["A", "B"],[]])
55− it "roundtrips List" $ traverse_ roundtrips (List.fromFoldable [["A", "B"],[]])
56− it "roundtrips NonEmptyArray" $ roundtrips (NEA.cons' "A" ["B"])
55+ it "roundtrips Array" $ traverse_ roundtrips [ [ "A", "B" ], [] ]
56+ it "roundtrips LazyList" $ traverse_ roundtrips (LazyList.fromFoldable [ [ "A", "B" ], [] ])
57+ it "roundtrips List" $ traverse_ roundtrips (List.fromFoldable [ [ "A", "B" ], [] ])
58+ it "roundtrips NonEmptyArray" $ roundtrips (NEA.cons' "A" [ "B" ])
5759 it "roundtrips Object" $ roundtrips (Object.fromHomogeneous { a: 12, b: 54 })
58− it "roundtrips String Map" $ roundtrips (Map.fromFoldable [("A" /\ 8),("C" /\ 7)])
59− it "roundtrips Int Map" $ roundtrips (Map.fromFoldable [(4 /\ "B"),(8 /\ "D")])
60+ it "roundtrips String Map" $ roundtrips (Map.fromFoldable [ ("A" /\ 8), ("C" /\ 7) ])
61+ it "roundtrips Int Map" $ roundtrips (Map.fromFoldable [ (4 /\ "B"), (8 /\ "D") ])
6062 it "roundtrips Map with String newtype keys"
61− $ roundtrips (Map.fromFoldable [(Stringy "A" /\ "B"),(Stringy "C" /\ "D")])
63+ $ roundtrips (Map.fromFoldable [ (Stringy "A" /\ "B"), (Stringy "C" /\ "D") ])
6264 it "roundtrips Map with Int newtype keys"
63− $ roundtrips (Map.fromFoldable [(Inty 4 /\ "B"),(Inty 8 /\ "D")])
65+ $ roundtrips (Map.fromFoldable [ (Inty 4 /\ "B"), (Inty 8 /\ "D") ])
6466 it "roundtrips BigInt" do
6567 let
6668 inputStr = """{ "number":1, "big": 18014398509481982, "mediumBig": 1652955871799, "smallBig": 10 }"""
6769 expected = """{"smallBig":"10","number":1,"mediumBig":"1652955871799","big":"18014398509481982"}"""
68− parsed ∷ _ ({ number ∷ Int, mediumBig :: BigInt, big ∷ BigInt, smallBig ∷ BigInt })
70+
71+ parsed ∷ _ ({ number ∷ Int, mediumBig ∷ BigInt, big ∷ BigInt, smallBig ∷ BigInt })
6972 parsed = readJSON inputStr
7073 stringified = parsed <#> writeJSON
7174 stringified `shouldEqual` (Right expected)
72− -- [TODO] Comment in as soon as bug in big-integers is fixed
73− -- @sigma-andex: What bug? Can we link this???
74− -- it "roundtrips BigInt (2)" do
75− -- let
76− -- smallBig = BigInt.fromInt 10
77− -- big = unsafePartial $ fromJust $ BigInt.fromString "18014398509481982"
7875
79− -- expected = { big, smallBig }
80− -- json = spy "json" $ writeJSON expected
76+ it "roundtrips BigInt (2)" do
77+ let
78+ smallBig = BigInt.fromInt 10
79+ big = unsafePartial $ fromJust $ BigInt.fromString "18014398509481982"
80+
81+ expected = { big, smallBig }
82+ json = writeJSON expected
8183
82− -- parsed ∷ _ ({ big ∷ BigInt, smallBig ∷ BigInt })
83− -- parsed = readJSON json
84− -- (spy "parsed" parsed) `shouldEqual` (Right expected)
84+ parsed ∷ _ ({ big ∷ BigInt, smallBig ∷ BigInt })
85+ parsed = readJSON json
86+ parsed `shouldEqual` (Right expected)
8587
8688 describe "works with JSDate" do
8789 it "roundtrips" do
88− now <- JSDate.now # liftEffect
90+ now ← JSDate.now # liftEffect
8991 roundtrips now
90− someDate <- JSDate.parse "2022-01-01:00:00:00Z" # liftEffect
92+ someDate ← JSDate.parse "2022-01-01:00:00:00Z" # liftEffect
9193 let result = writeJSON someDate
9294 let expected = show "2022-01-01T00:00:00.000Z"
9395 result `shouldEqual` expected
9496
9597 describe "works with DateTime" do
9698 it "roundtrips" do
97− now <- nowDateTime # liftEffect
99+ now ← nowDateTime # liftEffect
98100 roundtrips now
99− someDate <- JSDate.parse "2022-01-01:00:00:00Z" <#> (JSDate.toDateTime >>> fromMaybe' \_ -> unsafeCrashWith "nope") # liftEffect
101+ someDate ← JSDate.parse "2022-01-01:00:00:00Z" <#> (JSDate.toDateTime >>> fromMaybe' \_ → unsafeCrashWith "nope") # liftEffect
100102 let result = writeJSON someDate
101103 let expected = show "2022-01-01T00:00:00.000Z"
102104 result `shouldEqual` expected
@@ -112,41 +114,43 @@ spec = describe "En- and decoding" $ do
112114
113115 describe "works on variant types" do
114116 it "roundtrips" do
115− roundtrips (inj (Proxy :: _ "erwin") "e" :: Variant ("erwin" :: String))
116− roundtrips (inj (Proxy :: _ "jackie") 7 :: Variant ExampleVariant)
117+ roundtrips (inj (Proxy ∷ _ "erwin") "e" ∷ Variant ("erwin" ∷ String))
118+ roundtrips (inj (Proxy ∷ _ "jackie") 7 ∷ Variant ExampleVariant)
117119
118120 describe "works on tagged variant types" do
119121 it "roundtrips" do
120− roundtrips (TaggedVariant (erwin "e") :: TaggedVariant "super" "hans" (Erwin ()))
122+ roundtrips (TaggedVariant (erwin "e") ∷ TaggedVariant "super" "hans" (Erwin ()))
121123 let bareVariant = erwin "e"
122− let res = writeJSON (TaggedVariant bareVariant :: TaggedVariant "type" "value" ExampleVariant)
124+ let res = writeJSON (TaggedVariant bareVariant ∷ TaggedVariant "type" "value" ExampleVariant)
123125 let expected = """{"value":"e","type":"erwin"}"""
124126 res `shouldEqual` expected
125127 let
126− parsed :: _ (TaggedVariant "type" "value" ExampleVariant)
128+ parsed ∷ _ (TaggedVariant "type" "value" ExampleVariant)
127129 parsed = readJSON expected
128130 (un TaggedVariant <$> parsed) `shouldEqual` Right bareVariant
129131
130132 describe "works on untagged variant types" do
131133 it "roundtrips" do
132− roundtrips (UntaggedVariant (erwin "e") :: UntaggedVariant (Erwin ()))
134+ roundtrips (UntaggedVariant (erwin "e") ∷ UntaggedVariant (Erwin ()))
133135 let bareVariant = erwin "e"
134− let res = writeJSON (UntaggedVariant bareVariant :: UntaggedVariant ExampleVariant)
136+ let res = writeJSON (UntaggedVariant bareVariant ∷ UntaggedVariant ExampleVariant)
135137 let expected = """"e""""
136138 res `shouldEqual` expected
137139 let
138− parsed :: _ (UntaggedVariant ExampleVariant)
140+ parsed ∷ _ (UntaggedVariant ExampleVariant)
139141 parsed = readJSON expected
140142 (un UntaggedVariant <$> parsed) `shouldEqual` Right bareVariant
141143
142−type ExampleVariant = ("erwin" :: String, "jackie" :: Int)
144+type ExampleVariant = ("erwin" ∷ String, "jackie" ∷ Int)
143145type ExampleTaggedVariant t v = TaggedVariant t v ExampleVariant
144146
145−erwin ∷ ∀ a r. a → Variant ( erwin ∷ a | r )
146−erwin = inj (Proxy :: Proxy "erwin")
147−type Erwin r = (erwin :: String | r)
147+erwin ∷ ∀ a r. a → Variant (erwin ∷ a | r)
148+erwin = inj (Proxy ∷ Proxy "erwin")
149+
150+type Erwin r = (erwin ∷ String | r)
148151
149152newtype Stringy = Stringy String
153+
150154derive instance Newtype Stringy _
151155derive newtype instance Show Stringy
152156derive newtype instance Eq Stringy
@@ -155,6 +159,7 @@ derive newtype instance WriteForeign Stringy
155159derive newtype instance ReadForeign Stringy
156160
157161newtype Inty = Inty Int
162+
158163derive instance Newtype Inty _
159164derive newtype instance Show Inty
160165derive newtype instance Eq Inty