/
ApiSpec.hs
114 lines (108 loc) · 5.4 KB
/
ApiSpec.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
{-# LANGUAGE OverloadedLists #-}
module Language.Marlowe.Runtime.Transaction.ApiSpec
( spec
) where
import Language.Marlowe.Runtime.Transaction.Api (CIP25Metadata(..), CIP25MetadataLabel(..))
import qualified Data.Aeson as Aeson
import qualified Data.ByteString.Lazy.Char8 as BL
import qualified Data.Maybe as Maybe
import Test.Hspec (Spec, it, shouldBe)
import qualified Test.Hspec as Hspec
import Test.Hspec.QuickCheck (prop)
import Test.QuickCheck (Gen)
import qualified Test.QuickCheck as Gen
{-
DOING Ensure CIP-25 version 2 is supported
- version 1 may be supported but don't waste effort on this
- ignore <other_properties> for now
DONE plan out test suite
DOING write tests
TODO piggy back on existing tests to check that FromJSON and ToJSON are inverses
TODO piggy back on existing tests to check serialization forms correct structure
TODO CIP25Metadata -> Metadata procedure
TODO Reorganizes strings into lists of strings such that they are each no longer than 64 characters long
-}
version1Gen :: Gen (Maybe Int)
version1Gen = Gen.elements [Nothing, Just 1]
spec :: Spec
spec = do
Hspec.fdescribe "CIP-25 Metadata" do
Hspec.describe "version 1" do
prop "Parses PolicyId as UTF-8 string" do
version1 <- version1Gen
let input :: BL.ByteString
input = Aeson.encode $
Aeson.Object [("721", Aeson.Object
if Maybe.isJust version1
then [("53756e", Aeson.Object []), ("version", Aeson.Number 1)]
else [("53756e", Aeson.Object [])])]
actual :: Either String CIP25Metadata
actual = Aeson.eitherDecode' input
expected :: Either String CIP25Metadata
expected = Right $ CIP25Metadata $ CIP25MetadataLabel [("53756e", [])] version1
pure $ actual `shouldBe` expected
prop "Serializes PolicyId as UTF-8 string" do
version1 <- version1Gen
let input :: CIP25Metadata
input = CIP25Metadata $ CIP25MetadataLabel [("53756e", [])] version1
actual :: BL.ByteString
actual = Aeson.encode input
expected :: BL.ByteString
expected = Aeson.encode $
Aeson.Object [("721", Aeson.Object
if Maybe.isJust version1
then [("53756e", Aeson.Object []), ("version", Aeson.Number 1)]
else [("53756e", Aeson.Object [])])]
pure $ actual `shouldBe` expected
it "Parses TokenName as UTF-8 string" Hspec.pending
it "Serializes TokenName as UTF-8 string" Hspec.pending
it "Interprets lack of version as version 1" Hspec.pending
it "Interprets version 1 as version 1" Hspec.pending
it "Rejects PolicyId longer than 64 characters" Hspec.pending
it "Rejects TokenName longer than 64 characters" Hspec.pending
Hspec.describe "version 2" do
it "Parses PolicyId as raw byte string" do
let input :: BL.ByteString
input = Aeson.encode $
Aeson.Object [("721", Aeson.Object
[ ("Sun", Aeson.Object [])
, ("version", Aeson.Number 2)
])]
actual :: Either String CIP25Metadata
actual = Aeson.eitherDecode' input
expected :: Either String CIP25Metadata
expected = Right $ CIP25Metadata $ CIP25MetadataLabel [("53756e", [])] (Just 2)
actual `shouldBe` expected
it "Serializes PolicyId as raw byte string" do
let input :: CIP25Metadata
input = CIP25Metadata $ CIP25MetadataLabel [("53756e", [])] (Just 2)
actual :: BL.ByteString
actual = Aeson.encode input
expected :: BL.ByteString
expected = Aeson.encode $
Aeson.Object [("721", Aeson.Object
[ ("Sun", Aeson.Object [])
, ("version", Aeson.Number 2)
])]
actual `shouldBe` expected
it "Parses TokenName as raw byte string" Hspec.pending
it "Serializes TokenName as raw byte string" Hspec.pending
it "Interprets version 2 as version 2" Hspec.pending
it "Rejects PolicyId longer than 64 bytes" Hspec.pending
it "Rejects TokenName longer than 64 bytes" Hspec.pending
Hspec.describe "version 1 & 2 common" do
it "Ignores <other_properties>" Hspec.pending
it "Reorganizes strings into maximum 64 length strings" Hspec.pending
it "Rejects invalid version values" Hspec.pending
it "Serializes 0 files_details.src into [] representation" Hspec.pending
it "Serializes 1 files_details.src into 1 representation" Hspec.pending
it "Serializes * files_details.src into [*] representation" Hspec.pending
it "Serializes 0 metadata_details.image into [] representation" Hspec.pending
it "Serializes 1 metadata_details.image into 1 representation" Hspec.pending
it "Serializes * metadata_details.image into [*] representation" Hspec.pending
it "Serializes 0 metadata_details.description into no representation" Hspec.pending
it "Serializes 1 metadata_details.description into 1 compact representation" Hspec.pending
it "Serializes * metadata_details.description into * compact representation" Hspec.pending
it "Serializes 0 metadata_details.files into compact no representation" Hspec.pending
it "Serializes 1 metadata_details.files into compact 1 representation" Hspec.pending
it "Serializes * metadata_details.files into compact * representation" Hspec.pending