Skip to content

Commit

Permalink
Add BitInt tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jhbertra committed Oct 27, 2021
1 parent 24427cd commit 2f02fa3
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 10 deletions.
11 changes: 9 additions & 2 deletions src/Data/BigInt/Argonaut.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,20 @@ exports.restoreJson = function (original) {
}

exports.decodeBigInt = function (fail, succ, json) {
if (typeof json === "number" || typeof json === "bigint") {
if (Number.isInteger(json) || typeof json === "bigint") {
return succ(bigInt(json));
} else {
return fail;
}
}

exports.encodeBigInt = function (a) {
return bigInt(a);
if (JSON.stringify !== JSONbig.stringify) {
console.warn(
"Tried to encode BitInt without patching JSON.stringify. Wrap your app in Data.BigInt.Argonaut.withJsonPatch."
);
return a.toJSNumber();
} else {
return a.value;
}
}
54 changes: 54 additions & 0 deletions test/Data/BigInt/Argonaut/Spec.purs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
module Data.BigInt.Argonaut.Spec
( bigIntSpec
) where

import Prologue

import Data.Argonaut.Decode (JsonDecodeError)
import Data.Argonaut.Extra (encodeStringifyJson, parseDecodeJson)
import Data.BigInt as BI
import Data.BigInt.Argonaut (BigInt, fromInt, withJsonPatch)
import Data.Newtype (unwrap)
import Data.String (codePointFromChar, dropWhile, null)
import Data.String.Gen (genDigitString)
import Test.QuickCheck (class Arbitrary, (===))
import Test.QuickCheck.Gen (suchThat)
import Test.Spec (Spec, around_, describe, it)
import Test.Spec.Assertions (shouldNotEqual)
import Test.Spec.QuickCheck (quickCheck)

newtype DigitString = DigitString String

instance arbitraryDigitString :: Arbitrary DigitString where
arbitrary =
map DigitString
$ flip suchThat (not null)
$ map (dropWhile (eq (codePointFromChar '0')))
$ genDigitString

bigIntSpec :: Spec Unit
bigIntSpec =
describe "Data.BigInt.Argonaut" do
around_ withJsonPatch $ it "obeys the roundtrip law under withJsonPatch" do
quickCheck \(DigitString s) ->
let
decoded :: Either JsonDecodeError BigInt
decoded = parseDecodeJson s
in
encodeStringifyJson <$> decoded === Right s
it "does not always obey the roundtrip law when not under withJsonPatch" do
let
s = "23457890122347890123478901234123409871234"

decoded :: Either JsonDecodeError BigInt
decoded = parseDecodeJson s
(encodeStringifyJson <$> decoded) `shouldNotEqual` Right s
around_ withJsonPatch $ it "decodes a valid BigInt" do
quickCheck \(DigitString s) ->
let
decoded :: Either JsonDecodeError BigInt
decoded = parseDecodeJson s
in
(unwrap <<< add (fromInt 10))
<$> decoded === (add (BI.fromInt 10) <<< unwrap)
<$> decoded
16 changes: 8 additions & 8 deletions test/Main.purs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module Test.Main where
import Prologue

import Data.Array.Extra.Spec (arrayExtraSpec)
import Data.BigInt.Argonaut (withJsonPatch)
import Data.BigInt.Argonaut.Spec (bigIntSpec)
import Data.Cursor.Spec (cursorSpec)
import Data.Foldable.Extra.Spec (foldableExtraSpec)
import Data.String.Extra.Spec (stringExtraSpec)
Expand All @@ -16,10 +16,10 @@ import Test.Spec.Runner (runSpec)
main :: Effect Unit
main =
launchAff_ do
withJsonPatch do
runSpec [ consoleReporter ] do
cursorSpec
arrayExtraSpec
foldableExtraSpec
stringExtraSpec
assocMapSpec
runSpec [ consoleReporter ] do
cursorSpec
arrayExtraSpec
foldableExtraSpec
stringExtraSpec
assocMapSpec
bigIntSpec

0 comments on commit 2f02fa3

Please sign in to comment.