Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
68a1e47
add node stuff to gitignore
shmish111 May 10, 2019
736a8dd
rebase upstream master and add spago
shmish111 Aug 14, 2020
090a0c9
add List decode/encode
shmish111 Aug 14, 2020
f34d0c3
encode/decode tuple using array as this is what is normally done
shmish111 Aug 14, 2020
9fcc6ee
encode/decode Map using array of tuples, not sure there is another way
shmish111 Aug 14, 2020
fd7f49f
encode/decode Set using array
shmish111 Aug 14, 2020
d356fd2
upgrade purs
shmish111 Aug 17, 2020
db3685a
use spago in travis
shmish111 Aug 17, 2020
e9064e2
Adding a roundtrip test for Map.
krisajenkins Sep 3, 2020
26bc66f
More flexible Map decoding.
krisajenkins Sep 3, 2020
401b78b
Merge pull request #1 from krisajenkins/fix-map
shmish111 Sep 3, 2020
16989ec
Adding encoders/decoders for Either.
krisajenkins Sep 3, 2020
c3c6431
Merge pull request #2 from krisajenkins/add-either
shmish111 Sep 3, 2020
1e0b95f
Adding Data.BigInt support.
krisajenkins Sep 3, 2020
3d70577
Merge pull request #3 from shmish111/add-big-ints
shmish111 Sep 3, 2020
ecd14ae
Adding a shell.nix file, for a repeatable env.
krisajenkins Sep 3, 2020
b01fe37
Merge pull request #4 from shmish111/nix-shell
shmish111 Sep 3, 2020
b77bf99
Adding extra map-decoding tests.
krisajenkins Sep 4, 2020
4963d86
Improving the resiliency of BigInt decoding.
krisajenkins Sep 4, 2020
bd0a108
Improved encoding/decoding for big integers.
krisajenkins Sep 8, 2020
1f6a328
Moving to a more formal test suite.
krisajenkins Sep 11, 2020
a2c5a0d
BigInteger support.
krisajenkins Sep 10, 2020
695e30b
Refactoring the test suite.
krisajenkins Sep 17, 2020
f5b1c43
Fixing a bug with aeson-style encoding on nested sum types.
krisajenkins Sep 17, 2020
57692ed
Simplifying a fromMaybe clause.
krisajenkins Sep 17, 2020
c7cb623
add node stuff to gitignore
shmish111 May 10, 2019
9054539
rebase upstream master and add spago
shmish111 Aug 14, 2020
57539e7
add List decode/encode
shmish111 Aug 14, 2020
209e040
encode/decode tuple using array as this is what is normally done
shmish111 Aug 14, 2020
33f2a7f
encode/decode Map using array of tuples, not sure there is another way
shmish111 Aug 14, 2020
60a7a0f
encode/decode Set using array
shmish111 Aug 14, 2020
0b95402
upgrade purs
shmish111 Aug 17, 2020
58a9b33
use spago in travis
shmish111 Aug 17, 2020
0377029
Adding a roundtrip test for Map.
krisajenkins Sep 3, 2020
961aa70
More flexible Map decoding.
krisajenkins Sep 3, 2020
652a993
Adding encoders/decoders for Either.
krisajenkins Sep 3, 2020
538d5e8
Adding Data.BigInt support.
krisajenkins Sep 3, 2020
b0584ad
Adding a shell.nix file, for a repeatable env.
krisajenkins Sep 3, 2020
bcd6e20
Adding extra map-decoding tests.
krisajenkins Sep 4, 2020
f840a43
Improving the resiliency of BigInt decoding.
krisajenkins Sep 4, 2020
2d2e351
Improved encoding/decoding for big integers.
krisajenkins Sep 8, 2020
cd8ed03
Moving to a more formal test suite.
krisajenkins Sep 11, 2020
040060d
BigInteger support.
krisajenkins Sep 10, 2020
2a62593
Refactoring the test suite.
krisajenkins Sep 17, 2020
22aac68
Fixing a bug with aeson-style encoding on nested sum types.
krisajenkins Sep 17, 2020
cc4519d
Simplifying a fromMaybe clause.
krisajenkins Sep 17, 2020
bd412a1
Merge pull request #7 from shmish111/rebase-upstream
shmish111 Oct 26, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Refactoring the test suite.
  • Loading branch information
krisajenkins committed Sep 17, 2020
commit 695e30b4b841df540dcc26d13b2f48e01750ea9a
132 changes: 40 additions & 92 deletions test/Main.purs
Original file line number Diff line number Diff line change
@@ -1,27 +1,23 @@
module Test.Main where

import Prelude
import TestUtils (testGenericRoundTrip, testOption, testRoundTrip)
import BigIntegerTests as BigIntegerTests
import Control.Monad.Except (runExcept)
import Data.Bifunctor (bimap)
import Data.BigInteger as BigInteger
import Data.Either (Either(..))
import Data.Generic.Rep (class Generic)
import Data.Map (Map)
import Data.Map as Map
import Data.Maybe (Maybe(..), isNothing)
import Data.String (toLower, toUpper)
import Data.Tuple (Tuple(..))
import Effect (Effect)
import Foreign (isNull, unsafeToForeign)
import Foreign.Generic (class Decode, class Encode, class GenericDecode, class GenericEncode, Options, decode, encode, defaultOptions, decodeJSON, encodeJSON, genericDecodeJSON, genericEncodeJSON)
import Foreign.Generic.EnumEncoding (class GenericDecodeEnum, class GenericEncodeEnum, GenericEnumOptions, genericDecodeEnum, genericEncodeEnum)
import Foreign.Generic (decode, decodeJSON, defaultOptions, encode, encodeJSON)
import Foreign.Index (readProp)
import Foreign.JSON (parseJSON)
import Foreign.Object as Object
import Global.Unsafe (unsafeStringify)
import Test.Assert (assert')
import Test.Types (Fruit(..), IntList(..), RecordTest(..), Tree(..), TupleArray(..), UndefinedTest(..), SumWithRecord(..))
import Test.Types (Fruit(..), IntList(..), RecordTest(..), SumWithRecord(..), Tree(..), TupleArray(..), UndefinedTest(..))
import Test.Unit (TestSuite, failure, success, suite, test)
import Test.Unit.Assert (equal)
import Test.Unit.Main (runTest)
Expand All @@ -35,55 +31,6 @@ buildTree f n a = Branch $ buildTree (bimap f f) (n - 1) (f a)
makeTree :: Int -> Tree Int
makeTree n = buildTree (\i -> TupleArray (Tuple (2 * i) (2 * i + 1))) n 0

throw :: String -> Effect Unit
throw = flip assert' false

testRoundTrip ::
∀ a.
Eq a =>
Show a =>
Decode a =>
Encode a =>
a ->
TestSuite
testRoundTrip x =
test ("RoundTrip " <> show x) do
equal (Right x) (runExcept (decodeJSON (encodeJSON x)))

testGenericRoundTrip ::
∀ a r.
Eq a =>
Show a =>
Generic a r =>
GenericDecode r =>
GenericEncode r =>
Options ->
a ->
TestSuite
testGenericRoundTrip opts x =
test ("Generic roundtrip " <> show x) do
equal (Right x) (runExcept (genericDecodeJSON opts (genericEncodeJSON opts x)))

testOption ::
∀ a rep.
Eq a =>
Show a =>
Generic a rep =>
GenericEncodeEnum rep =>
GenericDecodeEnum rep =>
GenericEnumOptions ->
String ->
a ->
TestSuite
testOption options string value =
test "testOption" do
let
json = unsafeStringify $ genericEncodeEnum options value
equal (Right value) (runExcept (decode' json))
equal (Right value) (runExcept (decode' string))
where
decode' = genericDecodeEnum options <=< parseJSON

testUnaryConstructorLiteral :: TestSuite
testUnaryConstructorLiteral = do
testOption (makeCasingOptions toUpper) "\"FRIKANDEL\"" Frikandel
Expand All @@ -98,7 +45,8 @@ testUnaryConstructorLiteral = do
testNothingToNull :: TestSuite
testNothingToNull =
test "Nothing to Null" do
let json = encode (UndefinedTest { a: Nothing })
let
json = encode (UndefinedTest { a: Nothing })
case runExcept (pure json >>= readProp "contents" >>= readProp "a") of
Right val ->
if (isNull val) then
Expand Down Expand Up @@ -134,38 +82,38 @@ main =

roundTripTests :: TestSuite
roundTripTests =
suite "RoundTrips" do
testRoundTrip (RecordTest { foo: 1, bar: "test", baz: 'a' })
testRoundTrip NoArgs
testRoundTrip (SomeArg "some argument")
testRoundTrip (ManyArgs "fst" "snd")
testRoundTrip (RecordArgs { foo: 1, bar: "test", baz: 'a' })
testRoundTrip (Cons 1 (Cons 2 (Cons 3 Nil)))
testRoundTrip (UndefinedTest { a: Just "test" })
testRoundTrip (UndefinedTest { a: Nothing })
testRoundTrip [ Just "test" ]
testRoundTrip [ Nothing :: Maybe String ]
testRoundTrip (Apple)
testRoundTrip (makeTree 0)
testRoundTrip (makeTree 5)
testRoundTrip (Object.fromFoldable [ Tuple "one" 1, Tuple "two" 2 ])
testRoundTrip (Map.fromFoldable [ Tuple "one" 1, Tuple "two" 2 ])
test "Maps" do
equal (Right (Map.fromFoldable [ Tuple "foo" 5 ]))
(runExcept (decodeJSON "{\"foo\": 5}"))
equal (Right (Map.empty :: Map String Int))
(runExcept (decodeJSON "null"))
testRoundTrip [ Left 5, Right "Test" ]
testRoundTrip (BigInteger.fromString ("9055784127882682410409638")) -- 2^60. Anything over 2^32 would baffle JavaScript.
test "BigInteger" do
equal (Right (BigInteger.fromInt 50))
(runExcept (decodeJSON "50"))
equal (Right {a: (BigInteger.fromInt 50)})
(runExcept (decodeJSON "{\"a\": 50}"))
testUnaryConstructorLiteral
let
opts = defaultOptions { fieldTransform = toUpper }
pure unit
testGenericRoundTrip opts (RecordTest { foo: 1, bar: "test", baz: 'a' })
testNothingToNull
testNothingFromMissing
suite "RoundTrips" do
testRoundTrip (RecordTest { foo: 1, bar: "test", baz: 'a' })
testRoundTrip NoArgs
testRoundTrip (SomeArg "some argument")
testRoundTrip (ManyArgs "fst" "snd")
testRoundTrip (RecordArgs { foo: 1, bar: "test", baz: 'a' })
testRoundTrip (Cons 1 (Cons 2 (Cons 3 Nil)))
testRoundTrip (UndefinedTest { a: Just "test" })
testRoundTrip (UndefinedTest { a: Nothing })
testRoundTrip [ Just "test" ]
testRoundTrip [ Nothing :: Maybe String ]
testRoundTrip (Apple)
testRoundTrip (makeTree 0)
testRoundTrip (makeTree 5)
testRoundTrip (Object.fromFoldable [ Tuple "one" 1, Tuple "two" 2 ])
testRoundTrip (Map.fromFoldable [ Tuple "one" 1, Tuple "two" 2 ])
test "Maps" do
equal (Right (Map.fromFoldable [ Tuple "foo" 5 ]))
(runExcept (decodeJSON "{\"foo\": 5}"))
equal (Right (Map.empty :: Map String Int))
(runExcept (decodeJSON "null"))
testRoundTrip [ Left 5, Right "Test" ]
testRoundTrip (BigInteger.fromString ("9055784127882682410409638")) -- 2^60. Anything over 2^32 would baffle JavaScript.
test "BigInteger" do
equal (Right (BigInteger.fromInt 50))
(runExcept (decodeJSON "50"))
equal (Right { a: (BigInteger.fromInt 50) })
(runExcept (decodeJSON "{\"a\": 50}"))
testUnaryConstructorLiteral
let
opts = defaultOptions { fieldTransform = toUpper }
pure unit
testGenericRoundTrip opts (RecordTest { foo: 1, bar: "test", baz: 'a' })
testNothingToNull
testNothingFromMissing
63 changes: 63 additions & 0 deletions test/TestUtils.purs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
module TestUtils where

import Prelude
import Control.Monad.Except (runExcept)
import Data.Either (Either(..))
import Data.Generic.Rep (class Generic)
import Effect (Effect)
import Foreign.Generic (class Decode, class Encode, class GenericDecode, class GenericEncode, Options, decodeJSON, encodeJSON, genericDecodeJSON, genericEncodeJSON)
import Foreign.Generic.EnumEncoding (class GenericDecodeEnum, class GenericEncodeEnum, GenericEnumOptions, genericDecodeEnum, genericEncodeEnum)
import Foreign.JSON (parseJSON)
import Global.Unsafe (unsafeStringify)
import Test.Assert (assert')
import Test.Unit (TestSuite, test)
import Test.Unit.Assert (equal)

throw :: String -> Effect Unit
throw = flip assert' false

testRoundTrip ::
∀ a.
Eq a =>
Show a =>
Decode a =>
Encode a =>
a ->
TestSuite
testRoundTrip x =
test ("RoundTrip " <> show x) do
equal (Right x) (runExcept (decodeJSON (encodeJSON x)))

testGenericRoundTrip ::
∀ a r.
Eq a =>
Show a =>
Generic a r =>
GenericDecode r =>
GenericEncode r =>
Options ->
a ->
TestSuite
testGenericRoundTrip opts x =
test ("Generic roundtrip " <> show x) do
equal (Right x) (runExcept (genericDecodeJSON opts (genericEncodeJSON opts x)))

testOption ::
∀ a rep.
Eq a =>
Show a =>
Generic a rep =>
GenericEncodeEnum rep =>
GenericDecodeEnum rep =>
GenericEnumOptions ->
String ->
a ->
TestSuite
testOption options string value =
test "testOption" do
let
json = unsafeStringify $ genericEncodeEnum options value
equal (Right value) (runExcept (decode' json))
equal (Right value) (runExcept (decode' string))
where
decode' = genericDecodeEnum options <=< parseJSON