Skip to content

Commit 8938237

Browse files
authored
Comptime tests (#8)
Enable all tests at comptime, except test_serialization which requires some fixings in nim-ser to work
1 parent 6ad43c6 commit 8938237

9 files changed

Lines changed: 149 additions & 142 deletions

tests/test_bigints.nim

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -11,57 +11,57 @@ import
1111
unittest2, bigints, ./utils, ../cbor_serialization, ../cbor_serialization/pkg/bigints
1212

1313
suite "Test BigInt":
14-
test "Spec unsigned bignum tag encode":
14+
dualTest "Spec unsigned bignum tag encode":
1515
let val = "18446744073709551616".initBigInt
1616
let cbor = Cbor.encode(val)
1717
check cbor == "0xc249010000000000000000".unhex
1818

19-
test "Spec negative bignum tag encode":
19+
dualTest "Spec negative bignum tag encode":
2020
let val = "-18446744073709551617".initBigInt
2121
let cbor = Cbor.encode(val)
2222
check cbor == "0xc349010000000000000000".unhex
2323

24-
test "Spec unsigned bignum tag decode":
24+
dualTest "Spec unsigned bignum tag decode":
2525
let val = "0xc249010000000000000000".unhex
2626
let cbor = Cbor.decode(val, BigInt)
2727
check cbor == "18446744073709551616".initBigInt
2828

29-
test "Spec negative bignum tag decode":
29+
dualTest "Spec negative bignum tag decode":
3030
let val = "0xc349010000000000000000".unhex
3131
let cbor = Cbor.decode(val, BigInt)
3232
check cbor == "-18446744073709551617".initBigInt
3333

34-
test "Spec unsigned int encode":
34+
dualTest "Spec unsigned int encode":
3535
let val = "18446744073709551615".initBigInt
3636
let cbor = Cbor.encode(val)
3737
check cbor == "0x1bffffffffffffffff".unhex
3838

39-
test "Spec negative int encode":
39+
dualTest "Spec negative int encode":
4040
let val = "-18446744073709551616".initBigInt
4141
let cbor = Cbor.encode(val)
4242
check cbor == "0x3bffffffffffffffff".unhex
4343

44-
test "Spec unsigned int decode":
44+
dualTest "Spec unsigned int decode":
4545
let val = "0x1bffffffffffffffff".unhex
4646
let cbor = Cbor.decode(val, BigInt)
4747
check cbor == "18446744073709551615".initBigInt
4848

49-
test "Spec negative int decode":
49+
dualTest "Spec negative int decode":
5050
let val = "0x3bffffffffffffffff".unhex
5151
let cbor = Cbor.decode(val, BigInt)
5252
check cbor == "-18446744073709551616".initBigInt
5353

54-
test "Spec small int encode":
54+
dualTest "Spec small int encode":
5555
let val = "1".initBigInt
5656
let cbor = Cbor.encode(val)
5757
check cbor == "0x01".unhex
5858

59-
test "Spec small int decode":
59+
dualTest "Spec small int decode":
6060
let val = "0x01".unhex
6161
let cbor = Cbor.decode(val, BigInt)
6262
check cbor == "1".initBigInt
6363

64-
test "Tag Bignum bigNumBytesLimit":
64+
dualTest "Tag Bignum bigNumBytesLimit":
6565
let val = (initBigInt(1) shl 128) - initBigInt(1)
6666
let cbor = Cbor.encode(val)
6767
check:
@@ -70,21 +70,21 @@ suite "Test BigInt":
7070
expect UnexpectedValueError:
7171
discard Cbor.decode(cbor, BigInt, conf = CborReaderConf(bigNumBytesLimit: 15))
7272

73-
test "Tag negative Bignum bigNumBytesLimit":
74-
var val = (initBigInt(1) shl 128) - initBigInt(1)
75-
val *= -1.initBigInt
76-
let cbor = Cbor.encode(val)
77-
check:
78-
Cbor.decode(cbor, BigInt) == val
79-
Cbor.decode(cbor, BigInt, conf = CborReaderConf(bigNumBytesLimit: 16)) == val
80-
expect UnexpectedValueError:
81-
discard Cbor.decode(cbor, BigInt, conf = CborReaderConf(bigNumBytesLimit: 15))
73+
dualTest "Tag negative Bignum bigNumBytesLimit":
74+
var val = (initBigInt(1) shl 128) - initBigInt(1)
75+
val *= -1.initBigInt
76+
let cbor = Cbor.encode(val)
77+
check:
78+
Cbor.decode(cbor, BigInt) == val
79+
Cbor.decode(cbor, BigInt, conf = CborReaderConf(bigNumBytesLimit: 16)) == val
80+
expect UnexpectedValueError:
81+
discard Cbor.decode(cbor, BigInt, conf = CborReaderConf(bigNumBytesLimit: 15))
8282

83-
test "Tag negative Bignum bigNumBytesLimit":
84-
# this triggers the limit check for negativeTag
85-
var val = (initBigInt(1) shl 128) #- initBigInt(1)
86-
val *= -1.initBigInt
87-
let cbor = Cbor.encode(val)
88-
check Cbor.decode(cbor, BigInt) == val
89-
expect UnexpectedValueError:
90-
discard Cbor.decode(cbor, BigInt, conf = CborReaderConf(bigNumBytesLimit: 16))
83+
dualTest "Tag negative Bignum bigNumBytesLimit":
84+
# this triggers the limit check for negativeTag
85+
var val = (initBigInt(1) shl 128) #- initBigInt(1)
86+
val *= -1.initBigInt
87+
let cbor = Cbor.encode(val)
88+
check Cbor.decode(cbor, BigInt) == val
89+
expect UnexpectedValueError:
90+
discard Cbor.decode(cbor, BigInt, conf = CborReaderConf(bigNumBytesLimit: 16))

tests/test_cbor_flavor.nim

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ Container.useDefaultSerializationIn NullyFields
9292
ListOnly.useDefaultSerializationIn NullyFields
9393

9494
suite "Test CborFlavor":
95-
test "basic test":
95+
dualTest "basic test":
9696
let c = Container(name: "c", x: -10, y: 20, list: @[1'i64, 2, 25])
9797
let encoded = StringyCbor.encode(c)
9898
# {"name":"c","x":"-10","y":"20","list":["1","2","25"]}
@@ -102,7 +102,7 @@ suite "Test CborFlavor":
102102
let decoded = StringyCbor.decode(encoded, Container)
103103
check decoded == Container(name: "c", x: -10, y: 20, list: @[1, 2, 25])
104104

105-
test "optional fields":
105+
dualTest "optional fields":
106106
let a = OptionalFields(one: Opt.some("hello"))
107107
let b = OptionalFields(two: some(567))
108108
let c = OptionalFields(one: Opt.some("burn"), two: some(333))
@@ -119,7 +119,7 @@ suite "Test CborFlavor":
119119
check cc.hex == "0xa2636f6e65646275726e6374776f19014d" # {"one":"burn","two":333}
120120
checkCbor cc, c
121121

122-
test "Write special types":
122+
dualTest "Write special types":
123123
let vv = Cbor.decode(cborText.unhex, SpecialTypes)
124124
let xx = Cbor.encode(vv)
125125
var ww = Cbor.decode(xx, SpecialTypes)
@@ -129,7 +129,7 @@ suite "Test CborFlavor":
129129
xx.hex ==
130130
"0xA36374776F3903146574687265651903E764666F7572A2656170706C658301F56574687265656662616E616E61A36463686970187B617AF66176F4".toLowerAscii
131131

132-
test "object with null fields":
132+
dualTest "object with null fields":
133133
expect CborReaderError:
134134
let x = Cbor.decode(cborTextWithNullFields.unhex, Container)
135135
discard x
@@ -141,7 +141,8 @@ suite "Test CborFlavor":
141141
let y = NullyFields.decode(cborTextWithNullFields.unhex, ListOnly)
142142
check y.list.len == 0
143143

144-
test "Enum value representation primitives":
144+
dualTest "Enum value representation primitives":
145+
NullyFields.flavorEnumRep(EnumAsString)
145146
when NullyFields.flavorEnumRep() == EnumAsString:
146147
check true
147148
elif NullyFields.flavorEnumRep() == EnumAsNumber:
@@ -165,7 +166,7 @@ suite "Test CborFlavor":
165166
elif NullyFields.flavorEnumRep() == EnumAsStringifiedNumber:
166167
check true
167168

168-
test "Enum value representation of custom flavor":
169+
dualTest "Enum value representation of custom flavor":
169170
type ExoticFruits = enum
170171
DragonFruit
171172
SnakeFruit
@@ -186,7 +187,7 @@ suite "Test CborFlavor":
186187
check w.hex == "0x6132"
187188
checkCbor w, "2"
188189

189-
test "EnumAsString of custom flavor":
190+
dualTest "EnumAsString of custom flavor":
190191
type Fruit = enum
191192
Banana = "BaNaNa"
192193
Apple = "ApplE"
@@ -218,7 +219,7 @@ suite "Test CborFlavor":
218219
check z.hex == "0x00"
219220
checkCbor z, 0
220221

221-
test "custom writer that uses stream":
222+
dualTest "custom writer that uses stream":
222223
let value = @[@[byte 0, 1], @[byte 2, 3]]
223224
let cbor = StringyCbor.encode(value)
224225
check cbor.hex == "0x8264303030316430323033"

tests/test_cbor_raw.nim

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,45 +19,45 @@ proc specData(): seq[string] =
1919
doAssert result.len > 0
2020

2121
suite "Test CborBytes":
22-
test "decode spec vectors":
22+
dualTest "decode spec vectors":
2323
let cborData = specData()
2424
check cborData.len > 0
2525
for val in cborData:
2626
let cbor = Cbor.decode(val.unhex, CborBytes)
2727
check cbor.toBytes().toHex() == val
2828

29-
test "encode spec vectors":
29+
dualTest "encode spec vectors":
3030
let cborData = specData()
3131
check cborData.len > 0
3232
for val in cborData:
3333
let raw = val.unhex.CborBytes
3434
check Cbor.encode(raw).toHex() == val
3535

36-
test "decode string":
36+
dualTest "decode string":
3737
let val = "0x6161".unhex
3838
check Cbor.decode(val, CborBytes) == val
3939

40-
test "encode string":
40+
dualTest "encode string":
4141
let val = "0x6161".unhex
4242
check Cbor.encode(val.CborBytes) == val
4343

44-
test "Object nestedDepthLimit":
44+
dualTest "Object nestedDepthLimit":
4545
let cbor = Cbor.encode((x: (y: (z: "a"))))
4646
check:
4747
Cbor.decode(cbor, CborBytes) == cbor
4848
Cbor.decode(cbor, CborBytes, conf = CborReaderConf(nestedDepthLimit: 3)) == cbor
4949
expect UnexpectedValueError:
5050
discard Cbor.decode(cbor, CborBytes, conf = CborReaderConf(nestedDepthLimit: 2))
5151

52-
test "Array nestedDepthLimit":
52+
dualTest "Array nestedDepthLimit":
5353
let cbor = Cbor.encode(@[@[@["a", "b"], @["c", "d"]], @[@["e", "f"]]])
5454
check:
5555
Cbor.decode(cbor, CborBytes) == cbor
5656
Cbor.decode(cbor, CborBytes, conf = CborReaderConf(nestedDepthLimit: 3)) == cbor
5757
expect UnexpectedValueError:
5858
discard Cbor.decode(cbor, CborBytes, conf = CborReaderConf(nestedDepthLimit: 2))
5959

60-
test "Tag nestedDepthLimit":
60+
dualTest "Tag nestedDepthLimit":
6161
type
6262
Tag1 = CborTag[Tag2]
6363
Tag2 = CborTag[Tag3]
@@ -71,31 +71,31 @@ suite "Test CborBytes":
7171
expect UnexpectedValueError:
7272
discard Cbor.decode(cbor, Tag1, conf = CborReaderConf(nestedDepthLimit: 2))
7373

74-
test "Array arrayElementsLimit":
74+
dualTest "Array arrayElementsLimit":
7575
let cbor = Cbor.encode(@["a", "b", "c"])
7676
check:
7777
Cbor.decode(cbor, CborBytes) == cbor
7878
Cbor.decode(cbor, CborBytes, conf = CborReaderConf(arrayElementsLimit: 3)) == cbor
7979
expect UnexpectedValueError:
8080
discard Cbor.decode(cbor, CborBytes, conf = CborReaderConf(arrayElementsLimit: 2))
8181

82-
test "Object objectMembersLimit":
82+
dualTest "Object objectMembersLimit":
8383
let cbor = Cbor.encode((a: "a", b: "b", c: "c"))
8484
check:
8585
Cbor.decode(cbor, CborBytes) == cbor
8686
Cbor.decode(cbor, CborBytes, conf = CborReaderConf(objectMembersLimit: 3)) == cbor
8787
expect UnexpectedValueError:
8888
discard Cbor.decode(cbor, CborBytes, conf = CborReaderConf(objectMembersLimit: 2))
8989

90-
test "String stringLengthLimit":
90+
dualTest "String stringLengthLimit":
9191
let cbor = Cbor.encode("abc")
9292
check:
9393
Cbor.decode(cbor, CborBytes) == cbor
9494
Cbor.decode(cbor, CborBytes, conf = CborReaderConf(stringLengthLimit: 3)) == cbor
9595
expect UnexpectedValueError:
9696
discard Cbor.decode(cbor, CborBytes, conf = CborReaderConf(stringLengthLimit: 2))
9797

98-
test "ByteString byteStringLengthLimit":
98+
dualTest "ByteString byteStringLengthLimit":
9999
let cbor = Cbor.encode(@[1.byte, 2, 3])
100100
check:
101101
Cbor.decode(cbor, CborBytes) == cbor

0 commit comments

Comments
 (0)