Skip to content

Commit 4574e7e

Browse files
committed
Fix, add tests
1 parent c7f490a commit 4574e7e

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

src/Data/Codec/Argonaut/Generic.purs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,12 @@ instance nullarySumCodecCtor ∷ IsSymbol name ⇒ NullarySumCodec (Constructor
7070
tagRaw = reflectSymbol (Proxy Proxy name)
7171
tag = encoding.mapTag tagRaw
7272
in
73-
J.fromString $ tag
73+
J.fromString tag
7474
nullarySumDecode encoding name j = do
75-
tagRaw ← note (CA.Named name (CA.TypeMismatch "String")) (J.toString j)
76-
let tag = encoding.mapTag tagRaw
77-
if tag /= reflectSymbol (Proxy Proxy name) then
75+
actualTag ← note (CA.Named name (CA.TypeMismatch "String")) (J.toString j)
76+
let expectedTagRaw = reflectSymbol (Proxy Proxy name)
77+
let expectedTag = encoding.mapTag expectedTagRaw
78+
if expectedTag /= actualTag then
7879
Left (CA.Named name (CA.UnexpectedValue j))
7980
else
8081
Right (Constructor NoArguments)

test/Test/Generic.purs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@ module Test.Generic where
22

33
import Prelude
44

5-
import Data.Codec.Argonaut.Generic (nullarySum)
5+
import Data.Codec.Argonaut.Generic (nullarySum, nullarySumWith)
66
import Data.Generic.Rep (class Generic)
77
import Data.Show.Generic (genericShow)
88
import Effect (Effect)
99
import Effect.Console (log)
1010
import Test.QuickCheck (quickCheck)
1111
import Test.QuickCheck.Arbitrary (genericArbitrary)
1212
import Test.QuickCheck.Gen (Gen)
13+
import Test.Sum (check)
1314
import Test.Util (propCodec)
1415

1516
data MySum = Ctor1 | Ctor2 | MoarCtors
@@ -27,3 +28,9 @@ main ∷ Effect Unit
2728
main = do
2829
log "Check nullarySum"
2930
quickCheck (propCodec genMySum (nullarySum "MySum"))
31+
32+
let opts = { mapTag: \tag → "My" <> tag }
33+
34+
check (nullarySumWith opts "MySum") Ctor1 "\"MyCtor1\""
35+
check (nullarySumWith opts "MySum") MoarCtors "\"MyMoarCtors\""
36+

0 commit comments

Comments
 (0)