@helios-lang/effect 0.1.4 → 0.1.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (113) hide show
  1. package/bun.lock +60 -0
  2. package/dist/Ledger/NetworkParams.js +40 -0
  3. package/dist/Ledger/NetworkParams.js.map +1 -0
  4. package/dist/Ledger/index.js +1 -0
  5. package/dist/Ledger/index.js.map +1 -1
  6. package/package.json +1 -1
  7. package/tsconfig.build.tsbuildinfo +1 -0
  8. package/tsconfig.check.tsbuildinfo +1 -0
  9. package/tsconfig.tsbuildinfo +1 -0
  10. package/types/Address.d.ts +5 -0
  11. package/types/Address.d.ts.map +1 -0
  12. package/types/Bech32.d.ts +30 -0
  13. package/types/Bech32.d.ts.map +1 -0
  14. package/types/Cbor.d.ts +430 -0
  15. package/types/Cbor.d.ts.map +1 -0
  16. package/types/Ledger/Address.d.ts +109 -0
  17. package/types/Ledger/Address.d.ts.map +1 -0
  18. package/types/Ledger/AssetClass.d.ts +101 -0
  19. package/types/Ledger/AssetClass.d.ts.map +1 -0
  20. package/types/Ledger/Assets.d.ts +70 -0
  21. package/types/Ledger/Assets.d.ts.map +1 -0
  22. package/types/Ledger/Credential.d.ts +26 -0
  23. package/types/Ledger/Credential.d.ts.map +1 -0
  24. package/types/Ledger/DatumHash.d.ts +40 -0
  25. package/types/Ledger/DatumHash.d.ts.map +1 -0
  26. package/types/Ledger/IsMainnet.d.ts +6 -0
  27. package/types/Ledger/IsMainnet.d.ts.map +1 -0
  28. package/types/Ledger/MintingPolicy.d.ts +39 -0
  29. package/types/Ledger/MintingPolicy.d.ts.map +1 -0
  30. package/types/Ledger/NetworkParams.d.ts +40 -0
  31. package/types/Ledger/NetworkParams.d.ts.map +1 -0
  32. package/types/Ledger/PubKeyHash.d.ts +40 -0
  33. package/types/Ledger/PubKeyHash.d.ts.map +1 -0
  34. package/types/Ledger/TxId.d.ts +10 -0
  35. package/types/Ledger/TxId.d.ts.map +1 -0
  36. package/types/Ledger/TxInput.d.ts +55 -0
  37. package/types/Ledger/TxInput.d.ts.map +1 -0
  38. package/types/Ledger/TxOutput.d.ts +63 -0
  39. package/types/Ledger/TxOutput.d.ts.map +1 -0
  40. package/types/Ledger/TxOutputDatum.d.ts +41 -0
  41. package/types/Ledger/TxOutputDatum.d.ts.map +1 -0
  42. package/types/Ledger/TxOutputId.d.ts +14 -0
  43. package/types/Ledger/TxOutputId.d.ts.map +1 -0
  44. package/types/Ledger/ValidatorHash.d.ts +40 -0
  45. package/types/Ledger/ValidatorHash.d.ts.map +1 -0
  46. package/types/Ledger/index.d.ts +16 -0
  47. package/types/Ledger/index.d.ts.map +1 -0
  48. package/types/Uplc/Cek.d.ts +72 -0
  49. package/types/Uplc/Cek.d.ts.map +1 -0
  50. package/types/Uplc/Data.d.ts +530 -0
  51. package/types/Uplc/Data.d.ts.map +1 -0
  52. package/types/Uplc/DataSchema.d.ts +227 -0
  53. package/types/Uplc/DataSchema.d.ts.map +1 -0
  54. package/types/Uplc/Primitive.d.ts +26 -0
  55. package/types/Uplc/Primitive.d.ts.map +1 -0
  56. package/types/Uplc/index.d.ts +3 -0
  57. package/types/Uplc/index.d.ts.map +1 -0
  58. package/types/index.d.ts +5 -0
  59. package/types/index.d.ts.map +1 -0
  60. package/types/internal/Base32.d.ts +49 -0
  61. package/types/internal/Base32.d.ts.map +1 -0
  62. package/types/internal/BigEndian.d.ts +22 -0
  63. package/types/internal/BigEndian.d.ts.map +1 -0
  64. package/types/internal/Bits.d.ts +123 -0
  65. package/types/internal/Bits.d.ts.map +1 -0
  66. package/types/internal/Bytes.d.ts +88 -0
  67. package/types/internal/Bytes.d.ts.map +1 -0
  68. package/types/internal/Flat.d.ts +71 -0
  69. package/types/internal/Flat.d.ts.map +1 -0
  70. package/types/internal/Float.d.ts +38 -0
  71. package/types/internal/Float.d.ts.map +1 -0
  72. package/types/internal/Utf8.d.ts +24 -0
  73. package/types/internal/Utf8.d.ts.map +1 -0
  74. package/src/Bech32.test.ts +0 -117
  75. package/src/Bech32.ts +0 -198
  76. package/src/Cbor.test.ts +0 -1610
  77. package/src/Cbor.ts +0 -1704
  78. package/src/Ledger/Address.ts +0 -248
  79. package/src/Ledger/AssetClass.ts +0 -90
  80. package/src/Ledger/Assets.ts +0 -164
  81. package/src/Ledger/Credential.ts +0 -29
  82. package/src/Ledger/DatumHash.ts +0 -36
  83. package/src/Ledger/IsMainnet.ts +0 -6
  84. package/src/Ledger/MintingPolicy.ts +0 -57
  85. package/src/Ledger/PubKeyHash.ts +0 -36
  86. package/src/Ledger/TxId.ts +0 -31
  87. package/src/Ledger/TxInput.test.ts +0 -21
  88. package/src/Ledger/TxInput.ts +0 -66
  89. package/src/Ledger/TxOutput.ts +0 -166
  90. package/src/Ledger/TxOutputDatum.ts +0 -64
  91. package/src/Ledger/TxOutputId.ts +0 -63
  92. package/src/Ledger/ValidatorHash.ts +0 -36
  93. package/src/Ledger/index.ts +0 -14
  94. package/src/Uplc/Cek.ts +0 -92
  95. package/src/Uplc/Data.test.ts +0 -321
  96. package/src/Uplc/Data.ts +0 -657
  97. package/src/Uplc/Primitive.ts +0 -56
  98. package/src/Uplc/index.ts +0 -2
  99. package/src/index.ts +0 -4
  100. package/src/internal/Base32.test.ts +0 -219
  101. package/src/internal/Base32.ts +0 -341
  102. package/src/internal/BigEndian.test.ts +0 -79
  103. package/src/internal/BigEndian.ts +0 -67
  104. package/src/internal/Bits.test.ts +0 -300
  105. package/src/internal/Bits.ts +0 -398
  106. package/src/internal/Bytes.test.ts +0 -369
  107. package/src/internal/Bytes.ts +0 -343
  108. package/src/internal/Flat.test.ts +0 -29
  109. package/src/internal/Flat.ts +0 -387
  110. package/src/internal/Float.test.ts +0 -51
  111. package/src/internal/Float.ts +0 -190
  112. package/src/internal/Utf8.test.ts +0 -69
  113. package/src/internal/Utf8.ts +0 -58
package/src/Cbor.test.ts DELETED
@@ -1,1610 +0,0 @@
1
- import { describe, expect, it } from "bun:test"
2
- import { runSync, succeed } from "effect/Effect"
3
- import { makeStream, toArray, toHex } from "./internal/Bytes.js"
4
- import * as Cbor from "./Cbor.js"
5
-
6
- const FALSE_CBOR_BYTE = 0xf4
7
- const TRUE_CBOR_BYTE = 0xf5
8
-
9
- describe("Cbor.decodeBool", () => {
10
- it(`returns false for [${FALSE_CBOR_BYTE}]`, () => {
11
- expect(runSync(Cbor.decodeBool([FALSE_CBOR_BYTE]))).toBe(false)
12
- })
13
-
14
- it(`returns true for [${TRUE_CBOR_BYTE}]`, () => {
15
- expect(runSync(Cbor.decodeBool([TRUE_CBOR_BYTE]))).toBe(true)
16
- })
17
-
18
- it("fails for [0xf6]", () => {
19
- expect(() => runSync(Cbor.decodeBool([0xf6]))).toThrow()
20
- })
21
-
22
- it("fails for [0xf3]", () => {
23
- expect(() => runSync(Cbor.decodeBool([0xf3]))).toThrow()
24
- })
25
-
26
- it("fails for empty bytes", () => {
27
- expect(() => runSync(Cbor.decodeBool([]))).toThrow()
28
- })
29
- })
30
-
31
- describe("Cbor.encodeBool", () => {
32
- it(`returns [${FALSE_CBOR_BYTE}] for false`, () => {
33
- expect(Cbor.encodeBool(false)).toEqual([FALSE_CBOR_BYTE])
34
- })
35
-
36
- it(`returns [${TRUE_CBOR_BYTE}] for true`, () => {
37
- expect(Cbor.encodeBool(true)).toEqual([TRUE_CBOR_BYTE])
38
- })
39
- })
40
-
41
- describe("Cbor.isBool", () => {
42
- it("fails for empty bytes", () => {
43
- expect(() => runSync(Cbor.isBool([]))).toThrow()
44
- })
45
-
46
- it(`returns true for [${FALSE_CBOR_BYTE}]`, () => {
47
- expect(runSync(Cbor.isBool([FALSE_CBOR_BYTE]))).toBe(true)
48
- })
49
-
50
- it(`returns true for [${TRUE_CBOR_BYTE}]`, () => {
51
- expect(runSync(Cbor.isBool([TRUE_CBOR_BYTE]))).toBe(true)
52
- })
53
-
54
- it("doesn't change stream pos", () => {
55
- const stream = makeStream([TRUE_CBOR_BYTE])
56
-
57
- expect(runSync(Cbor.isBool(stream))).toBe(true)
58
- expect(stream.pos).toBe(0)
59
- })
60
-
61
- it("doesn't change stream pos if not a bool", () => {
62
- const stream = makeStream(Cbor.encodeInt(0))
63
-
64
- expect(runSync(Cbor.isBool(stream))).toBe(false)
65
- expect(stream.pos).toBe(0)
66
- })
67
- })
68
-
69
- describe("Cbor.decodeBytes()", () => {
70
- it("returns [] for [0x40]", () => {
71
- expect(runSync(Cbor.decodeBytes([0x40]))).toEqual([])
72
- })
73
-
74
- it("returns [1,2,3,4] for #4401020304", () => {
75
- expect(runSync(Cbor.decodeBytes("4401020304"))).toEqual([1, 2, 3, 4])
76
- })
77
-
78
- it("returns #4d01000033222220051200120011 for #4e4d01000033222220051200120011", () => {
79
- expect(runSync(Cbor.decodeBytes("4e4d01000033222220051200120011"))).toEqual(
80
- toArray("4d01000033222220051200120011")
81
- )
82
- })
83
-
84
- it("fails when trying to decode a list", () => {
85
- expect(() => {
86
- runSync(Cbor.decodeBytes(Cbor.encodeDefList([Cbor.encodeInt(0)])))
87
- }).toThrow()
88
- })
89
- })
90
-
91
- describe("Cbor.encodeBytes()", () => {
92
- it("returns #4e4d01000033222220051200120011 for #4d01000033222220051200120011", () => {
93
- expect(Cbor.encodeBytes("4d01000033222220051200120011")).toEqual(
94
- toArray("4e4d01000033222220051200120011")
95
- )
96
- })
97
- })
98
-
99
- describe("Cbor.encodeBytes()/Cbor.decodeBytes() roundtrip", () => {
100
- const testVector = []
101
-
102
- for (let i = 0; i < 100; i++) {
103
- testVector.push(
104
- new Array(i).fill(0).map(() => Math.floor(256 * Math.random()) % 256)
105
- )
106
- }
107
-
108
- testVector.forEach((v, i) => {
109
- const split = i % 2 == 0
110
-
111
- it(`ok for ${toHex(v)}`, () => {
112
- expect(runSync(Cbor.decodeBytes(Cbor.encodeBytes(v, split)))).toEqual(v)
113
- })
114
- })
115
- })
116
-
117
- describe("Cbor.isBytes()", () => {
118
- it("fails for empty bytes", () => {
119
- expect(() => runSync(Cbor.isBytes([]))).toThrow()
120
- })
121
-
122
- it("returns false for [0]", () => {
123
- expect(runSync(Cbor.isBytes([0]))).toBe(false)
124
- })
125
-
126
- it("returns true for #4e4d01000033222220051200120011", () => {
127
- expect(runSync(Cbor.isBytes("4e4d01000033222220051200120011"))).toBe(true)
128
- })
129
-
130
- it("doesn't change stream pos", () => {
131
- const stream = makeStream("4e4d01000033222220051200120011")
132
-
133
- expect(runSync(Cbor.isBytes(stream))).toBe(true)
134
- expect(stream.pos).toBe(0)
135
- })
136
-
137
- it("doesn't change stream pos if not bytes", () => {
138
- const stream = makeStream(Cbor.encodeInt(0))
139
-
140
- expect(runSync(Cbor.isBytes(stream))).toBe(false)
141
- expect(stream.pos).toBe(0)
142
- })
143
- })
144
-
145
- describe("Cbor.isDefBytes()", () => {
146
- it("fails for empty bytes", () => {
147
- expect(() => runSync(Cbor.isDefBytes([]))).toThrow()
148
- })
149
-
150
- it("returns false for [0]", () => {
151
- expect(runSync(Cbor.isDefBytes([0]))).toBe(false)
152
- })
153
-
154
- it("returns false for indef bytes", () => {
155
- expect(runSync(Cbor.isDefBytes([2 * 32 + 31]))).toBe(false)
156
- })
157
-
158
- it("returns true for #4e4d01000033222220051200120011", () => {
159
- expect(runSync(Cbor.isDefBytes("4e4d01000033222220051200120011"))).toBe(
160
- true
161
- )
162
- })
163
- })
164
-
165
- describe("Cbor.decodeConstr", () => {
166
- // test vectors taken from https://github.com/input-output-hk/plutus/blob/master/plutus-core/plutus-core/test/CBOR/DataStability.hs#L83
167
- describe("returns [0, [#bd99a373075d42fe4ac9109515e46303d0940cb9620bf058b87986a9, [0, []]]]", () => {
168
- const expected = [
169
- 0,
170
- [
171
- toArray("bd99a373075d42fe4ac9109515e46303d0940cb9620bf058b87986a9"),
172
- [0, []]
173
- ]
174
- ] as [number, [number[], [number, []]]]
175
-
176
- const testVectors = [
177
- "d87982581cbd99a373075d42fe4ac9109515e46303d0940cb9620bf058b87986a9d87980",
178
- "d8799f581cbd99a373075d42fe4ac9109515e46303d0940cb9620bf058b87986a9d87980ff"
179
- ]
180
-
181
- testVectors.forEach((v) => {
182
- it(`ok for ${v}`, () => {
183
- const actual = runSync(
184
- Cbor.decodeConstr([Cbor.decodeBytes, Cbor.decodeConstr([])])(v)
185
- )
186
-
187
- expect(actual).toEqual(expected)
188
- })
189
-
190
- it(`fails for ${v} too few field decoders`, () => {
191
- expect(() =>
192
- runSync(Cbor.decodeConstr([Cbor.decodeBytes])(v))
193
- ).toThrow()
194
- })
195
-
196
- it(`fails for ${v} too many field decoders`, () => {
197
- expect(() =>
198
- runSync(
199
- Cbor.decodeConstr([
200
- Cbor.decodeBytes,
201
- Cbor.decodeConstr([]),
202
- Cbor.decodeInt
203
- ])(v)
204
- )
205
- ).toThrow()
206
- })
207
- })
208
- })
209
-
210
- it("fails for []", () => {
211
- expect(() => runSync(Cbor.decodeConstr([])([]))).toThrow()
212
- })
213
-
214
- it("fails for [0]", () => {
215
- expect(() => runSync(Cbor.decodeConstr([])([0]))).toThrow()
216
- })
217
-
218
- describe(`returns [0, [[0,[[[0, [[], [[0, [[], 2123n]]]]]], [[0, [[], [[0, [[], 2223n]]]]]]]]]]`, () => {
219
- const expected: [
220
- number,
221
- [
222
- [
223
- number,
224
- [
225
- [number, [number[], [number, [number[], bigint]][]]][],
226
- [number, [number[], [number, [number[], bigint]][]]][]
227
- ]
228
- ]
229
- ]
230
- ] = [
231
- 0,
232
- [[0, [[[0, [[], [[0, [[], 2123n]]]]]], [[0, [[], [[0, [[], 2223n]]]]]]]]]
233
- ]
234
-
235
- const testVectors = [
236
- "d87981d8798281d879824081d879824019084b81d879824081d87982401908af",
237
- "d8799fd8799f9fd8799f409fd8799f4019084bffffffff9fd8799f409fd8799f401908afffffffffffff"
238
- ]
239
-
240
- testVectors.forEach((v) => {
241
- it(`ok for ${v}`, () => {
242
- const actual = runSync(
243
- Cbor.decodeConstr([
244
- Cbor.decodeConstr([
245
- Cbor.decodeList(
246
- Cbor.decodeConstr([
247
- Cbor.decodeBytes,
248
- Cbor.decodeList(
249
- Cbor.decodeConstr([Cbor.decodeBytes, Cbor.decodeInt])
250
- )
251
- ])
252
- ),
253
- Cbor.decodeList(
254
- Cbor.decodeConstr([
255
- Cbor.decodeBytes,
256
- Cbor.decodeList(
257
- Cbor.decodeConstr([Cbor.decodeBytes, Cbor.decodeInt])
258
- )
259
- ])
260
- )
261
- ])
262
- ])(v)
263
- )
264
-
265
- expect(actual).toEqual(expected)
266
- })
267
- })
268
- })
269
- })
270
-
271
- describe("Cbor.encodeConstr()", () => {
272
- it("returns #d8799f581cbd99a373075d42fe4ac9109515e46303d0940cb9620bf058b87986a9d87980ff for [0, [#bd99a373075d42fe4ac9109515e46303d0940cb9620bf058b87986a9, [0, []]]]", () => {
273
- expect(
274
- Cbor.encodeConstr(0, [
275
- Cbor.encodeBytes(
276
- "bd99a373075d42fe4ac9109515e46303d0940cb9620bf058b87986a9"
277
- ),
278
- Cbor.encodeConstr(0, [])
279
- ])
280
- ).toEqual(
281
- toArray(
282
- "d8799f581cbd99a373075d42fe4ac9109515e46303d0940cb9620bf058b87986a9d87980ff"
283
- )
284
- )
285
- })
286
-
287
- it("returns #d8799fd8799f9fd8799f409fd8799f4019084bffffffff9fd8799f409fd8799f401908afffffffffffff for [0, [[0,[[[0, [[], [[0, [[], 2123n]]]]]], [[0, [[], [[0, [[], 2223n]]]]]]]]]]", () => {
288
- expect(
289
- Cbor.encodeConstr(0, [
290
- Cbor.encodeConstr(0, [
291
- Cbor.encodeList([
292
- Cbor.encodeConstr(0, [
293
- Cbor.encodeBytes([]),
294
- Cbor.encodeList([
295
- Cbor.encodeConstr(0, [
296
- Cbor.encodeBytes([]),
297
- Cbor.encodeInt(2123n)
298
- ])
299
- ])
300
- ])
301
- ]),
302
- Cbor.encodeList([
303
- Cbor.encodeConstr(0, [
304
- Cbor.encodeBytes([]),
305
- Cbor.encodeList([
306
- Cbor.encodeConstr(0, [
307
- Cbor.encodeBytes([]),
308
- Cbor.encodeInt(2223n)
309
- ])
310
- ])
311
- ])
312
- ])
313
- ])
314
- ])
315
- ).toEqual(
316
- toArray(
317
- "d8799fd8799f9fd8799f409fd8799f4019084bffffffff9fd8799f409fd8799f401908afffffffffffff"
318
- )
319
- )
320
- })
321
- })
322
-
323
- describe("bad constr tags", () => {
324
- it("fails for a negative tag", () => {
325
- expect(() => Cbor.encodeConstr(-1, [])).toThrow()
326
- })
327
-
328
- it("fails for a non-whole number tag", () => {
329
- expect(() => Cbor.encodeConstr(3.14, [])).toThrow()
330
- })
331
-
332
- const badEncodedTags = [101, 103, 120, 128, 1279, 1401, 2000]
333
-
334
- badEncodedTags.forEach((t) => {
335
- it(`fails decoding tag ${t}`, () => {
336
- expect(() =>
337
- runSync(
338
- Cbor.decodeConstr([])(
339
- Cbor.encodeDefHead(6, BigInt(t)).concat(Cbor.encodeList([]))
340
- )
341
- )
342
- ).toThrow()
343
- })
344
- })
345
-
346
- it("fails for tag 102 with bad second header", () => {
347
- expect(() =>
348
- runSync(
349
- Cbor.decodeConstr([])(
350
- Cbor.encodeDefHead(6, 102n)
351
- .concat(Cbor.encodeDefHead(0, 0n))
352
- .concat(Cbor.encodeInt(0n))
353
- .concat(Cbor.encodeList([]))
354
- )
355
- )
356
- ).toThrow()
357
- })
358
- })
359
-
360
- describe("Cbor.encodeConstr()/Cbor.decodeConstr() roundtrip homogenous field type", () => {
361
- it(`ok for [0, 1, 2, 3]`, () => {
362
- const tag = 0
363
- const fields = [0n, 1n, 2n, 3n]
364
-
365
- const actual = runSync(
366
- Cbor.decodeConstr(Cbor.decodeInt)(
367
- Cbor.encodeConstr(
368
- tag,
369
- fields.map((item) => Cbor.encodeInt(item))
370
- )
371
- )
372
- )
373
-
374
- expect(actual).toEqual([tag, fields])
375
- })
376
- })
377
-
378
- const tagsTestVector = [
379
- 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110,
380
- 120, 130, 140, 150, 160, 170, 180, 190, 200, 1000, 2000, 3000, 4000, 5000
381
- ]
382
-
383
- describe("Cbor.encodeConstr()/Cbor.decodeConstr() tag only roundtrip", () => {
384
- tagsTestVector.forEach((t) => {
385
- it(`ok for ${t}`, () => {
386
- expect(runSync(Cbor.decodeConstr([])(Cbor.encodeConstr(t, [])))).toEqual([
387
- t,
388
- []
389
- ])
390
- })
391
- })
392
- })
393
-
394
- describe("Cbor.isConstr()", () => {
395
- it("returns true for #d87982581cbd99a373075d42fe4ac9109515e46303d0940cb9620bf058b87986a9d87980", () => {
396
- expect(
397
- runSync(
398
- Cbor.isConstr(
399
- "d87982581cbd99a373075d42fe4ac9109515e46303d0940cb9620bf058b87986a9d87980"
400
- )
401
- )
402
- ).toBe(true)
403
- })
404
-
405
- tagsTestVector.forEach((t) => {
406
- it(`returns true for encoded ${t}`, () => {
407
- expect(runSync(Cbor.isConstr(Cbor.encodeConstr(t, [])))).toBe(true)
408
- })
409
- })
410
-
411
- it("returns false for [0]]", () => {
412
- expect(runSync(Cbor.isConstr([0]))).toBe(false)
413
- })
414
-
415
- it("fails for []]", () => {
416
- expect(() => runSync(Cbor.isConstr([])))
417
- })
418
-
419
- it("doesn't change stream pos", () => {
420
- const stream = makeStream(
421
- "d87982581cbd99a373075d42fe4ac9109515e46303d0940cb9620bf058b87986a9d87980"
422
- )
423
-
424
- expect(runSync(Cbor.isConstr(stream))).toBe(true)
425
- expect(stream.pos).toBe(0)
426
- })
427
-
428
- it("doesn't change stream pos if not a constr", () => {
429
- const stream = makeStream(Cbor.encodeInt(0))
430
-
431
- expect(runSync(Cbor.isConstr(stream))).toBe(false)
432
- expect(stream.pos).toBe(0)
433
- })
434
- })
435
-
436
- /**
437
- * Taken from https://github.com/cbor/test-vectors/blob/master/appendix_a.json
438
- */
439
- const floatTestVector: [number[], number][] = [
440
- [[0xf9, 0x00, 0x00], 0.0],
441
- [[0xf9, 0x80, 0x00], -0.0],
442
- [[0xf9, 0x3c, 0x00], 1.0],
443
- [[0xfb, 0x3f, 0xf1, 0x99, 0x99, 0x99, 0x99, 0x99, 0x9a], 1.1],
444
- [[0xf9, 0x3e, 0x00], 1.5],
445
- [[0xf9, 0x7b, 0xff], 65504.0],
446
- [[0xfa, 0x47, 0xc3, 0x50, 0x00], 100000.0],
447
- [[0xfa, 0x7f, 0x7f, 0xff, 0xff], 3.4028234663852886e38],
448
- [[0xfb, 0x7e, 0x37, 0xe4, 0x3c, 0x88, 0x00, 0x75, 0x9c], 1.0e300],
449
- [[0xf9, 0x00, 0x01], 5.960464477539063e-8],
450
- [[0xf9, 0x04, 0x00], 6.103515625e-5],
451
- [[0xf9, 0xc4, 0x00], -4.0],
452
- [[0xfb, 0xc0, 0x10, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66], -4.1],
453
- [[0xf9, 0x7c, 0x00], Number.POSITIVE_INFINITY],
454
- [[0xf9, 0x7e, 0x00], Number.NaN],
455
- [[0xf9, 0xfc, 0x00], Number.NEGATIVE_INFINITY],
456
- [[0xfa, 0x7f, 0x80, 0x00, 0x00], Number.POSITIVE_INFINITY],
457
- [[0xfa, 0x7f, 0xc0, 0x00, 0x00], Number.NaN],
458
- [[0xfa, 0xff, 0x80, 0x00, 0x00], Number.NEGATIVE_INFINITY],
459
- [
460
- [0xfb, 0x7f, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00],
461
- Number.POSITIVE_INFINITY
462
- ],
463
- [[0xfb, 0x7f, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00], Number.NaN],
464
- [
465
- [0xfb, 0xff, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00],
466
- Number.NEGATIVE_INFINITY
467
- ]
468
- ]
469
-
470
- describe("Cbor.decodeFloat()", () => {
471
- floatTestVector.forEach(([bytes, f]) => {
472
- it(`decodes #${toHex(bytes)} as ${f}`, () => {
473
- expect(runSync(Cbor.decodeFloat(bytes))).toBe(f)
474
- })
475
- })
476
-
477
- it("fails for non-float cbor", () => {
478
- expect(() => {
479
- runSync(Cbor.decodeFloat([0]))
480
- }).toThrow()
481
- })
482
- })
483
-
484
- describe("Cbor.decodeFloat16()", () => {
485
- floatTestVector
486
- .filter(([bytes, f]) => bytes.length == 3 && !Number.isNaN(f))
487
- .forEach(([bytes, f]) => {
488
- expect(runSync(Cbor.decodeFloat16(bytes))).toBe(f)
489
- expect(() => {
490
- runSync(Cbor.decodeFloat32(bytes))
491
- }).toThrow()
492
- expect(() => {
493
- runSync(Cbor.decodeFloat64(bytes))
494
- }).toThrow()
495
- })
496
- })
497
-
498
- describe("Cbor.decodeFloat32()", () => {
499
- // NaN has a variety of representations, so we won't test that here
500
- floatTestVector
501
- .filter(([bytes, f]) => bytes.length == 5 && !Number.isNaN(f))
502
- .forEach(([bytes, f]) => {
503
- expect(() => {
504
- runSync(Cbor.decodeFloat16(bytes))
505
- }).toThrow()
506
- expect(runSync(Cbor.decodeFloat32(bytes))).toBe(f)
507
- expect(() => {
508
- runSync(Cbor.decodeFloat64(bytes))
509
- }).toThrow()
510
- })
511
- })
512
-
513
- describe("Cbor.decodeFloat64()", () => {
514
- // NaN has a variety of representations, so we won't test that here
515
- floatTestVector
516
- .filter(([bytes, f]) => bytes.length == 9 && !Number.isNaN(f))
517
- .forEach(([bytes, f]) => {
518
- expect(() => {
519
- runSync(Cbor.decodeFloat16(bytes))
520
- }).toThrow()
521
- expect(() => {
522
- runSync(Cbor.decodeFloat32(bytes))
523
- }).toThrow()
524
- expect(runSync(Cbor.decodeFloat64(bytes))).toBe(f)
525
- })
526
- })
527
-
528
- describe("Cbor.encodeFloat16() & Cbor.isFloat<n>()", () => {
529
- // NaN has a variety of representations, so will won't test that here
530
- floatTestVector
531
- .filter(([bytes, f]) => bytes.length == 3 && !Number.isNaN(f))
532
- .forEach(([bytes, f]) => {
533
- it(`encodes ${f} as #${toHex(bytes)}`, () => {
534
- expect(Cbor.encodeFloat16(f)).toEqual(bytes)
535
- expect(runSync(Cbor.isFloat(bytes))).toBe(true)
536
- expect(runSync(Cbor.isFloat16(bytes))).toBe(true)
537
- expect(runSync(Cbor.isFloat32(bytes))).toBe(false)
538
- expect(runSync(Cbor.isFloat64(bytes))).toBe(false)
539
- })
540
- })
541
- })
542
-
543
- describe("Cbor.encodeFloat32() & Cbor.isFloat<n>()", () => {
544
- // NaN has a variety of representations, so we won't test that here
545
- floatTestVector
546
- .filter(([bytes, f]) => bytes.length == 5 && !Number.isNaN(f))
547
- .forEach(([bytes, f]) => {
548
- it(`encodes ${f} as #${toHex(bytes)}`, () => {
549
- expect(Cbor.encodeFloat32(f)).toEqual(bytes)
550
- expect(runSync(Cbor.isFloat(bytes))).toBe(true)
551
- expect(runSync(Cbor.isFloat16(bytes))).toBe(false)
552
- expect(runSync(Cbor.isFloat32(bytes))).toBe(true)
553
- expect(runSync(Cbor.isFloat64(bytes))).toBe(false)
554
- })
555
- })
556
- })
557
-
558
- describe("Cbor.encodeFloat64() & Cbor.isFloat<n>()", () => {
559
- // NaN has a variety of representations, so we don't test that here
560
- floatTestVector
561
- .filter(([bytes, f]) => bytes.length == 9 && !Number.isNaN(f))
562
- .forEach(([bytes, f]) => {
563
- it(`encodes ${f} as #${toHex(bytes)}`, () => {
564
- expect(Cbor.encodeFloat64(f)).toEqual(bytes)
565
- expect(runSync(Cbor.isFloat(bytes))).toBe(true)
566
- expect(runSync(Cbor.isFloat16(bytes))).toBe(false)
567
- expect(runSync(Cbor.isFloat32(bytes))).toBe(false)
568
- expect(runSync(Cbor.isFloat64(bytes))).toBe(true)
569
- })
570
- })
571
- })
572
-
573
- const intTestVectors: [number | bigint, number[]][] = [
574
- [0, [0]],
575
- [1, [1]],
576
- [10, [10]],
577
- [23, [23]],
578
- [24n, [24, 24]],
579
- [25n, [24, 25]],
580
- [100, [24, 100]],
581
- [1000, [25, 3, 232]],
582
- [1000000n, [0x1a, 0, 0x0f, 0x42, 0x40]],
583
- [1000000000000n, [0x1b, 0, 0, 0, 0xe8, 0xd4, 0xa5, 0x10, 0]],
584
- [
585
- 18446744073709551615n,
586
- [0x1b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff]
587
- ],
588
- [18446744073709551616n, [0xc2, 0x49, 1, 0, 0, 0, 0, 0, 0, 0, 0]],
589
- [
590
- -18446744073709551616n,
591
- [0x3b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff]
592
- ],
593
- [-18446744073709551617n, [0xc3, 0x49, 1, 0, 0, 0, 0, 0, 0, 0, 0]],
594
- [-1, [0x20]],
595
- [-10, [0x29]],
596
- [-100, [0x38, 0x63]],
597
- [-1000, [0x39, 0x03, 0xe7]]
598
- ]
599
-
600
- describe("Cbor.decodeInt()", () => {
601
- intTestVectors.forEach(([x, bs]) => {
602
- it(`returns ${x} for #${toHex(bs)}`, () => {
603
- expect(runSync(Cbor.decodeInt(bs))).toBe(BigInt(x))
604
- })
605
- })
606
-
607
- it("fails for [24]", () => {
608
- expect(() => runSync(Cbor.decodeInt([24]))).toThrow()
609
- })
610
-
611
- it("fails for empty bytes", () => {
612
- expect(() => runSync(Cbor.decodeInt([]))).toThrow()
613
- })
614
- })
615
-
616
- describe("Cbor.encodeInt()", () => {
617
- intTestVectors.forEach(([x, bs]) => {
618
- it(`returns #${toHex(bs)} for ${x}`, () => {
619
- expect(Cbor.encodeInt(x)).toEqual(bs)
620
- })
621
- })
622
- })
623
-
624
- describe("Cbor.isInt()", () => {
625
- intTestVectors.forEach(([, bs]) => {
626
- it(`returns true for #${toHex(bs)}`, () => {
627
- expect(runSync(Cbor.isInt(bs))).toBe(true)
628
- })
629
- })
630
-
631
- it("returns false for #6161", () => {
632
- expect(runSync(Cbor.isInt([0x61, 0x61]))).toBe(false)
633
- })
634
-
635
- it("fails for []", () => {
636
- expect(() => runSync(Cbor.isInt([]))).toThrow()
637
- })
638
-
639
- it("doesn't change stream pos", () => {
640
- const stream = makeStream(Cbor.encodeInt(0))
641
-
642
- expect(runSync(Cbor.isInt(stream))).toBe(true)
643
- expect(stream.pos).toBe(0)
644
- })
645
-
646
- it("doesn't change stream pos if not an int", () => {
647
- const stream = makeStream([0x61, 0x61])
648
-
649
- expect(runSync(Cbor.isInt(stream))).toBe(false)
650
- expect(stream.pos).toBe(0)
651
- })
652
- })
653
-
654
- describe("Cbor.decodeList()", () => {
655
- const unusedItemDecoder = () => succeed(0)
656
-
657
- it("fails for []", () => {
658
- expect(() => runSync(Cbor.decodeList(unusedItemDecoder)([]))).toThrow()
659
- })
660
-
661
- it("fails for [0]", () => {
662
- expect(() => runSync(Cbor.decodeList(unusedItemDecoder)([0]))).toThrow()
663
- })
664
-
665
- it("returns [] for [0x80]", () => {
666
- expect(runSync(Cbor.decodeList(unusedItemDecoder)([0x80]))).toEqual([])
667
- })
668
-
669
- it("returns [] for [0x9f, 0xff]", () => {
670
- expect(runSync(Cbor.decodeList(unusedItemDecoder)([0x9f, 0xff]))).toEqual(
671
- []
672
- )
673
- })
674
-
675
- it("returns [1n,2n,3n] for #83010203", () => {
676
- expect(runSync(Cbor.decodeList(Cbor.decodeInt)("83010203"))).toEqual([
677
- 1n,
678
- 2n,
679
- 3n
680
- ])
681
- })
682
-
683
- describe("returns [1n,2n,3n,4n, ..., 25n]", () => {
684
- const variants = [
685
- "98190102030405060708090a0b0c0d0e0f101112131415161718181819",
686
- "9f0102030405060708090a0b0c0d0e0f101112131415161718181819ff"
687
- ]
688
-
689
- const expected = new Array(25).fill(0).map((_, i) => BigInt(i + 1))
690
-
691
- for (const v of variants) {
692
- it(`decodes #${v}`, () => {
693
- expect(runSync(Cbor.decodeList(Cbor.decodeInt)(v))).toEqual(expected)
694
- })
695
- }
696
- })
697
- })
698
-
699
- describe("Cbor.decodeListLazy()", () => {
700
- it("fails for []", () => {
701
- expect(() => runSync(Cbor.decodeListLazy([]))).toThrow()
702
- })
703
-
704
- it("fails for [0]", () => {
705
- expect(() => runSync(Cbor.decodeListLazy([0]))).toThrow()
706
- })
707
-
708
- it("succeeds when not calling the callback for [0x80] (i.e. empty list)", () => {
709
- runSync(Cbor.decodeListLazy([0x80]))
710
- })
711
-
712
- it("fails when calling the callback for [0x80] (i.e. empty list)", () => {
713
- const callback = runSync(Cbor.decodeListLazy([0x80]))
714
-
715
- expect(() => {
716
- runSync(callback(Cbor.decodeInt))
717
- }).toThrow(/end-of-list/)
718
- })
719
-
720
- it("succeeds when not calling callback for [0x9f, 0xff] (i.e. empty list)", () => {
721
- runSync(Cbor.decodeListLazy([0x9f, 0xff]))
722
- })
723
-
724
- it("fails when calling the callback for [0x9f, 0xff] (i.e. empty list)", () => {
725
- const callback = runSync(Cbor.decodeListLazy([0x9f, 0xff]))
726
-
727
- expect(() => {
728
- runSync(callback(Cbor.decodeInt))
729
- }).toThrow(/end-of-list/)
730
- })
731
-
732
- it("returns [1n,2n,3n] for #83010203", () => {
733
- const callback = runSync(Cbor.decodeListLazy("83010203"))
734
-
735
- expect(runSync(callback(Cbor.decodeInt))).toBe(1n)
736
- expect(runSync(callback(Cbor.decodeInt))).toBe(2n)
737
- expect(runSync(callback(Cbor.decodeInt))).toBe(3n)
738
-
739
- expect(() => {
740
- runSync(callback(Cbor.decodeInt))
741
- }).toThrow(/end-of-list/)
742
- })
743
-
744
- describe("returns [1n,2n,3n,4n, ..., 25n]", () => {
745
- const variants = [
746
- "98190102030405060708090a0b0c0d0e0f101112131415161718181819",
747
- "9f0102030405060708090a0b0c0d0e0f101112131415161718181819ff"
748
- ]
749
-
750
- for (const v of variants) {
751
- it(`decodes #${v}`, () => {
752
- const callback = runSync(Cbor.decodeListLazy(v))
753
-
754
- for (let i = 1; i <= 25; i++) {
755
- expect(runSync(callback(Cbor.decodeInt))).toBe(BigInt(i))
756
- }
757
-
758
- expect(() => {
759
- runSync(callback(Cbor.decodeInt))
760
- }).toThrow(/end-of-list/)
761
- })
762
- }
763
- })
764
- })
765
-
766
- describe("Cbor.decodeListLazyOption()", () => {
767
- it("fails for []", () => {
768
- expect(() => runSync(Cbor.decodeListLazyOption([]))).toThrow()
769
- })
770
-
771
- it("fails for [0]", () => {
772
- expect(() => runSync(Cbor.decodeListLazyOption([0]))).toThrow()
773
- })
774
-
775
- it("succeeds when not calling the callback for [0x80] (i.e. empty list)", () => {
776
- runSync(Cbor.decodeListLazy([0x80]))
777
- })
778
-
779
- it("returns undefined when calling the callback for [0x80] (i.e. empty list)", () => {
780
- const callback = runSync(Cbor.decodeListLazyOption([0x80]))
781
-
782
- expect(runSync(callback(Cbor.decodeInt))).toBe(undefined)
783
- })
784
-
785
- it("succeeds when not calling callback for [0x9f, 0xff] (i.e. empty list)", () => {
786
- runSync(Cbor.decodeListLazyOption([0x9f, 0xff]))
787
- })
788
-
789
- it("returns undefined when calling the callback for [0x9f, 0xff] (i.e. empty list)", () => {
790
- const callback = runSync(Cbor.decodeListLazyOption([0x9f, 0xff]))
791
-
792
- expect(runSync(callback(Cbor.decodeInt))).toBe(undefined)
793
- })
794
-
795
- it("returns [1n,2n,3n] for #83010203", () => {
796
- const callback = runSync(Cbor.decodeListLazyOption("83010203"))
797
-
798
- expect(runSync(callback(Cbor.decodeInt))).toBe(1n)
799
- expect(runSync(callback(Cbor.decodeInt))).toBe(2n)
800
- expect(runSync(callback(Cbor.decodeInt))).toBe(3n)
801
- expect(runSync(callback(Cbor.decodeInt))).toBe(undefined)
802
- })
803
-
804
- describe("returns [1n,2n,3n,4n, ..., 25n]", () => {
805
- const variants = [
806
- "98190102030405060708090a0b0c0d0e0f101112131415161718181819",
807
- "9f0102030405060708090a0b0c0d0e0f101112131415161718181819ff"
808
- ]
809
-
810
- for (const v of variants) {
811
- it(`decodes #${v}`, () => {
812
- const callback = runSync(Cbor.decodeListLazyOption(v))
813
-
814
- for (let i = 1; i <= 25; i++) {
815
- expect(runSync(callback(Cbor.decodeInt))).toBe(BigInt(i))
816
- }
817
-
818
- expect(runSync(callback(Cbor.decodeInt))).toBe(undefined)
819
- })
820
- }
821
- })
822
- })
823
-
824
- describe("Cbor.encodeList()", () => {
825
- // see https://github.com/well-typed/cborg/blob/4bdc818a1f0b35f38bc118a87944630043b58384/serialise/src/Codec/Serialise/Class.hs#L181
826
- it("returns [0x80] for []", () => {
827
- expect(Cbor.encodeList([])).toEqual([0x80])
828
- })
829
- })
830
-
831
- describe("Cbor.isList()", () => {
832
- it("returns true for [0x80]", () => {
833
- expect(runSync(Cbor.isList([0x80]))).toBe(true)
834
- })
835
-
836
- it("returns false for [0x61, 0x61]", () => {
837
- expect(runSync(Cbor.isList([0x61, 0x61]))).toBe(false)
838
- })
839
-
840
- it("returns false for [0]", () => {
841
- expect(runSync(Cbor.isList([0]))).toBe(false)
842
- })
843
-
844
- it("fails for []", () => {
845
- expect(() => runSync(Cbor.isList([]))).toThrow()
846
- })
847
-
848
- it("doesn't change stream pos", () => {
849
- const stream = makeStream([0x80])
850
-
851
- expect(runSync(Cbor.isList(stream))).toBe(true)
852
- expect(stream.pos).toBe(0)
853
- })
854
-
855
- it("doesn't change stream pos if not a list", () => {
856
- const stream = makeStream([0x61, 0x61])
857
-
858
- expect(runSync(Cbor.isList(stream))).toBe(false)
859
- expect(stream.pos).toBe(0)
860
- })
861
- })
862
-
863
- describe("Cbor.decodeMap()", () => {
864
- const unusedDecoder = () => succeed(0)
865
-
866
- it("fails for []", () => {
867
- expect(() =>
868
- runSync(Cbor.decodeMap(unusedDecoder, unusedDecoder)([]))
869
- ).toThrow()
870
- })
871
-
872
- it("fails for [0]", () => {
873
- expect(() =>
874
- runSync(Cbor.decodeMap(unusedDecoder, unusedDecoder)([0]))
875
- ).toThrow()
876
- })
877
-
878
- it("returns [] for [0xa0]", () => {
879
- expect(
880
- runSync(Cbor.decodeMap(unusedDecoder, unusedDecoder)([0xa0]))
881
- ).toEqual([])
882
- })
883
-
884
- it("returns [[1n, 2n], [3n, 4n]] for #a201020304", () => {
885
- expect(
886
- runSync(Cbor.decodeMap(Cbor.decodeInt, Cbor.decodeInt)("a201020304"))
887
- ).toEqual([
888
- [1n, 2n],
889
- [3n, 4n]
890
- ])
891
- })
892
-
893
- it("doesn't fail for int -> string map with 31 entries", () => {
894
- const cborHex =
895
- "b81f18736a6765745f6d696e746564187c683c7377697463683e1887683c7377697463683e188a683c7377697463683e1894683c7377697463683e1897683c7377697463683e18a1683c7377697463683e18a4683c7377697463683e18ae683c7377697463683e18b2683c7377697463683e18bc683c7377697463683e18c0683c7377697463683e18ca683c7377697463683e18ce683c7377697463683e18d8683c7377697463683e18dc683c7377697463683e18e6683c7377697463683e18e9683c61737369676e3e18f2683c7377697463683e18fc683c7377697463683e190100683c7377697463683e19010a683c7377697463683e19010e683c7377697463683e190118683c7377697463683e19011c683c7377697463683e190126683c7377697463683e190129683c7377697463683e190133683c7377697463683e190136683c7377697463683e19013b6f696e6469726563745f706f6c69637919013d683c61737369676e3e"
896
-
897
- runSync(Cbor.decodeMap(Cbor.decodeInt, Cbor.decodeString)(cborHex))
898
- })
899
- })
900
-
901
- describe("Cbor.encodeDefMap()", () => {
902
- it("encoding a def map with 31 entries should be decodeable", () => {
903
- const cbor = Cbor.encodeDefMap(
904
- (new Array(31).fill(0) as number[]).map((i) => [
905
- Cbor.encodeInt(i),
906
- Cbor.encodeString(i.toString())
907
- ])
908
- )
909
-
910
- runSync(Cbor.decodeMap(Cbor.decodeInt, Cbor.decodeString)(cbor))
911
- })
912
- })
913
-
914
- describe("Cbor.encodeMap()/Cbor.decodeMap() roundtrip", () => {
915
- const testVectors: [string, string][][] = [
916
- [],
917
- [["a", "A"]],
918
- [
919
- ["a", "A"],
920
- ["b", "B"]
921
- ],
922
- [
923
- ["a", "A"],
924
- ["b", "B"],
925
- ["c", "C"]
926
- ],
927
- [
928
- ["a", "A"],
929
- ["b", "B"],
930
- ["c", "C"],
931
- ["d", "D"]
932
- ],
933
- [
934
- ["a", "A"],
935
- ["b", "B"],
936
- ["c", "C"],
937
- ["d", "D"],
938
- ["e", "E"]
939
- ],
940
- [
941
- ["a", "A"],
942
- ["b", "B"],
943
- ["c", "C"],
944
- ["d", "D"],
945
- ["e", "E"],
946
- ["f", "F"]
947
- ]
948
- ]
949
-
950
- testVectors.forEach((v) => {
951
- it(`ok for ${JSON.stringify(v)}`, () => {
952
- expect(
953
- runSync(
954
- Cbor.decodeMap(
955
- Cbor.decodeString,
956
- Cbor.decodeString
957
- )(
958
- Cbor.encodeMap(
959
- v.map(([k, v]) => [Cbor.encodeString(k), Cbor.encodeString(v)])
960
- )
961
- )
962
- )
963
- ).toEqual(v)
964
- })
965
-
966
- it(`ok for ${JSON.stringify(v)} (indef encoding)`, () => {
967
- expect(
968
- runSync(
969
- Cbor.decodeMap(
970
- Cbor.decodeString,
971
- Cbor.decodeString
972
- )(
973
- Cbor.encodeIndefMap(
974
- v.map(([k, v]) => [Cbor.encodeString(k), Cbor.encodeString(v)])
975
- )
976
- )
977
- )
978
- ).toEqual(v)
979
- })
980
- })
981
- })
982
-
983
- describe("Cbor.isMap()", () => {
984
- it("returns true for [0xa0]", () => {
985
- expect(runSync(Cbor.isMap([0xa0]))).toBe(true)
986
- })
987
-
988
- it("returns false for [0]", () => {
989
- expect(runSync(Cbor.isMap([0]))).toBe(false)
990
- })
991
-
992
- it("fails for []", () => {
993
- expect(() => runSync(Cbor.isMap([]))).toThrow()
994
- })
995
-
996
- it("doesn't change stream pos", () => {
997
- const stream = makeStream([0xa0])
998
-
999
- expect(runSync(Cbor.isMap(stream))).toBe(true)
1000
- expect(stream.pos).toBe(0)
1001
- })
1002
-
1003
- it("doesn't change stream pos if not a map", () => {
1004
- const stream = makeStream(Cbor.encodeInt(0))
1005
-
1006
- expect(runSync(Cbor.isMap(stream))).toBe(false)
1007
- expect(stream.pos).toBe(0)
1008
- })
1009
- })
1010
-
1011
- const NULL_CBOR_BYTE = 0xf6
1012
-
1013
- describe("Cbor.decodeNull()", () => {
1014
- it("fails for empty bytes", () => {
1015
- expect(() => runSync(Cbor.decodeNull([]))).toThrow()
1016
- })
1017
-
1018
- it("fails for [0]", () => {
1019
- expect(() => runSync(Cbor.decodeNull([0]))).toThrow()
1020
- })
1021
-
1022
- it(`returns null for [${NULL_CBOR_BYTE}]`, () => {
1023
- expect(runSync(Cbor.decodeNull([NULL_CBOR_BYTE]))).toBe(null)
1024
- })
1025
- })
1026
-
1027
- describe("Cbor.encodeNull()", () => {
1028
- it(`returns [${NULL_CBOR_BYTE}]`, () => {
1029
- expect(Cbor.encodeNull()).toEqual([NULL_CBOR_BYTE])
1030
- })
1031
-
1032
- it(`returns [${NULL_CBOR_BYTE}] when called with null arg`, () => {
1033
- expect(Cbor.encodeNull(null)).toEqual([NULL_CBOR_BYTE])
1034
- })
1035
- })
1036
-
1037
- describe("Cbor.isNull()", () => {
1038
- it(`returns true for [${NULL_CBOR_BYTE}]`, () => {
1039
- expect(runSync(Cbor.isNull([NULL_CBOR_BYTE]))).toBe(true)
1040
- })
1041
-
1042
- it(`fails for empty bytes`, () => {
1043
- expect(() => runSync(Cbor.isNull([]))).toThrow()
1044
- })
1045
-
1046
- it("doesn't change stream pos", () => {
1047
- const stream = makeStream([NULL_CBOR_BYTE])
1048
-
1049
- expect(runSync(Cbor.isNull(stream))).toBe(true)
1050
- expect(stream.pos).toBe(0)
1051
- })
1052
-
1053
- it("doesn't change stream pos if not null", () => {
1054
- const stream = makeStream(Cbor.encodeInt(0))
1055
-
1056
- expect(runSync(Cbor.isNull(stream))).toBe(false)
1057
- expect(stream.pos).toBe(0)
1058
- })
1059
- })
1060
-
1061
- describe("Cbor.decodeObjectIKey()", () => {
1062
- it("returns {1: 2n, 3: 4n} for #a201020304", () => {
1063
- const actual = runSync(
1064
- Cbor.decodeObjectIKey({
1065
- 1: Cbor.decodeInt,
1066
- 3: Cbor.decodeInt
1067
- })("a201020304")
1068
- )
1069
-
1070
- expect(actual).toEqual({ 1: 2n, 3: 4n })
1071
- })
1072
- })
1073
-
1074
- describe("Cbor.encodeObjectIKey()", () => {
1075
- it("returns #a201020304 for {1: 2n, 3: 4n}", () => {
1076
- expect(
1077
- Cbor.encodeObjectIKey({
1078
- 1: Cbor.encodeInt(2n),
1079
- 3: Cbor.encodeInt(4n)
1080
- })
1081
- ).toEqual(toArray("a201020304"))
1082
- })
1083
-
1084
- it("returns #a201020304 for Map({1: 2n, 3: 4n})", () => {
1085
- expect(
1086
- Cbor.encodeObjectIKey(
1087
- new Map([
1088
- [1, Cbor.encodeInt(2n)],
1089
- [3, Cbor.encodeInt(4n)]
1090
- ])
1091
- )
1092
- ).toEqual(toArray("a201020304"))
1093
- })
1094
- })
1095
-
1096
- describe("Cbor.decodeObjectSKey()", () => {
1097
- it("returns {a: 1n, b: [2n, 3n]} for #a26161016162820203", () => {
1098
- const actual = runSync(
1099
- Cbor.decodeObjectSKey({
1100
- a: Cbor.decodeInt,
1101
- b: Cbor.decodeList(Cbor.decodeInt)
1102
- })("a26161016162820203")
1103
- )
1104
-
1105
- expect(actual).toEqual({ a: 1n, b: [2n, 3n] })
1106
- })
1107
-
1108
- it('returns {a: "A", b: "B", c: "C", d: "D", e: "E"} for #a56161614161626142616361436164614461656145', () => {
1109
- const actual = runSync(
1110
- Cbor.decodeObjectSKey({
1111
- a: Cbor.decodeString,
1112
- b: Cbor.decodeString,
1113
- c: Cbor.decodeString,
1114
- d: Cbor.decodeString,
1115
- e: Cbor.decodeString
1116
- })("a56161614161626142616361436164614461656145")
1117
- )
1118
-
1119
- expect(actual).toEqual({
1120
- a: "A",
1121
- b: "B",
1122
- c: "C",
1123
- d: "D",
1124
- e: "E"
1125
- })
1126
- })
1127
-
1128
- it("returns {Fun: true, Amt: -2} for #bf6346756ef563416d7421ff", () => {
1129
- const actual = runSync(
1130
- Cbor.decodeObjectSKey({
1131
- Fun: Cbor.decodeBool,
1132
- Amt: Cbor.decodeInt
1133
- })("bf6346756ef563416d7421ff")
1134
- )
1135
-
1136
- expect(actual).toEqual({ Fun: true, Amt: -2n })
1137
- })
1138
-
1139
- it("fails for #bf6346756ef563416d7421ff if Amt decoder isn't specified", () => {
1140
- expect(() =>
1141
- runSync(
1142
- Cbor.decodeObjectSKey({
1143
- Fun: Cbor.decodeBool
1144
- })("bf6346756ef563416d7421ff")
1145
- )
1146
- ).toThrow()
1147
- })
1148
- })
1149
-
1150
- describe("Cbor.encodeObjectSKey()", () => {
1151
- it('returns #a56161614161626142616361436164614461656145 for {a: "A", b: "B", c: "C", d: "D", e: "E"}', () => {
1152
- expect(
1153
- Cbor.encodeObjectSKey({
1154
- a: Cbor.encodeString("A"),
1155
- b: Cbor.encodeString("B"),
1156
- c: Cbor.encodeString("C"),
1157
- d: Cbor.encodeString("D"),
1158
- e: Cbor.encodeString("E")
1159
- })
1160
- ).toEqual(toArray("a56161614161626142616361436164614461656145"))
1161
- })
1162
-
1163
- it('returns #a56161614161626142616361436164614461656145 for Map({a: "A", b: "B", c: "C", d: "D", e: "E"})', () => {
1164
- expect(
1165
- Cbor.encodeObjectSKey(
1166
- new Map([
1167
- ["a", Cbor.encodeString("A")],
1168
- ["b", Cbor.encodeString("B")],
1169
- ["c", Cbor.encodeString("C")],
1170
- ["d", Cbor.encodeString("D")],
1171
- ["e", Cbor.encodeString("E")]
1172
- ])
1173
- )
1174
- ).toEqual(toArray("a56161614161626142616361436164614461656145"))
1175
- })
1176
- })
1177
-
1178
- describe("Cbor.isObject()", () => {
1179
- it("fails for []", () => {
1180
- expect(() => runSync(Cbor.isObject([]))).toThrow()
1181
- })
1182
-
1183
- it("returns false for [0]", () => {
1184
- expect(runSync(Cbor.isObject([0]))).toBe(false)
1185
- })
1186
-
1187
- it("returns true for #a201020304", () => {
1188
- expect(runSync(Cbor.isObject("a201020304"))).toBe(true)
1189
- })
1190
-
1191
- it("doesn't change stream pos", () => {
1192
- const stream = makeStream("a201020304")
1193
-
1194
- expect(runSync(Cbor.isObject(stream))).toBe(true)
1195
- expect(stream.pos).toBe(0)
1196
- })
1197
-
1198
- it("doesn't change stream pos if not an object", () => {
1199
- const stream = makeStream(Cbor.encodeInt(0))
1200
-
1201
- expect(runSync(Cbor.isObject(stream))).toBe(false)
1202
- expect(stream.pos).toBe(0)
1203
- })
1204
- })
1205
-
1206
- describe("Cbor.decodeString()", () => {
1207
- it('returns "" for [0x60]', () => {
1208
- expect(runSync(Cbor.decodeString([0x60]))).toBe("")
1209
- })
1210
-
1211
- it('returns "a" for [0x61, 0x61]', () => {
1212
- expect(runSync(Cbor.decodeString([0x61, 0x61]))).toBe("a")
1213
- })
1214
-
1215
- it('returns "IETF" for #6449455446', () => {
1216
- expect(runSync(Cbor.decodeString("6449455446"))).toBe("IETF")
1217
- })
1218
-
1219
- it('returns ""\\" for #62225c', () => {
1220
- expect(runSync(Cbor.decodeString("62225c"))).toBe('"\\')
1221
- })
1222
-
1223
- it('returns "ü" for #62c3bc', () => {
1224
- expect(runSync(Cbor.decodeString("62c3bc"))).toBe("ü")
1225
- })
1226
-
1227
- it('returns "水" for #63e6b0b4', () => {
1228
- expect(runSync(Cbor.decodeString("63e6b0b4"))).toBe("水")
1229
- })
1230
-
1231
- it('returns "𐅑" for #64f0908591', () => {
1232
- expect(runSync(Cbor.decodeString("64f0908591"))).toBe("𐅑")
1233
- })
1234
-
1235
- it("fails for []", () => {
1236
- expect(() => runSync(Cbor.decodeString([]))).toThrow()
1237
- })
1238
-
1239
- it("fails for [0]", () => {
1240
- expect(() => runSync(Cbor.decodeString([0]))).toThrow()
1241
- })
1242
- })
1243
-
1244
- describe("Cbor.encodeString()", () => {
1245
- it('returns [0x60] for ""', () => {
1246
- expect(Cbor.encodeString("")).toEqual([0x60])
1247
- })
1248
-
1249
- it('returns [0x61, 0x61] for "a"', () => {
1250
- expect(Cbor.encodeString("a")).toEqual([0x61, 0x61])
1251
- })
1252
-
1253
- it('returns #6449455446 for "IETF"', () => {
1254
- expect(Cbor.encodeString("IETF")).toEqual(toArray("6449455446"))
1255
- })
1256
- })
1257
-
1258
- describe("Cbor.encodeString()/Cbor.decodeString() roundtrip", () => {
1259
- const testVector = [
1260
- "天",
1261
- "地玄",
1262
- "黃宇宙",
1263
- "洪荒。蓋",
1264
- "此身髮四大",
1265
- "五常。都邑華",
1266
- "夏東西二京。治",
1267
- "本於農務茲稼穡。",
1268
- "耽讀玩市寓目囊箱。",
1269
- "布射僚丸嵇琴阮嘯。日",
1270
- "月盈昃辰宿列張。恭惟鞠",
1271
- "養豈敢毀傷。背邙面洛浮渭",
1272
- "據涇。俶載南畝我藝黍稷。易",
1273
- "輶攸畏屬耳垣牆。恬筆倫紙鈞巧",
1274
- "任釣。寒來暑往,秋收冬藏。女慕",
1275
- "貞絜男效才良。宮殿盤郁樓觀飛驚。稅",
1276
- "熟貢新勸賞黜陟。具膳餐飯適口充腸。釋",
1277
- "紛利俗竝皆佳妙。閏餘成歲律呂調陽。知過",
1278
- "必改得能莫忘。圖寫禽獸畫彩仙靈。孟軻敦素",
1279
- "史魚秉直。飽飫烹宰飢厭糟糠。毛施淑姿工顰妍",
1280
- "笑。雲騰致雨露結為霜。罔談彼短靡恃己長。丙舍",
1281
- "傍啟甲帳對楹。庶幾中庸勞謙謹敕。親戚故舊老少異",
1282
- "糧。年矢每催曦暉朗曜。金生麗水玉出崑岡。信使可覆",
1283
- "器欲難量。肆筵設席鼓瑟吹笙。聆音察理鑒貌辨色。妾御",
1284
- "績紡侍巾帷房。璿璣懸斡晦魄環照。劍號巨闕珠稱夜光。墨",
1285
- "悲絲染詩讚羔羊。升階納陛弁轉疑星。貽厥嘉猷勉其祗植。紈",
1286
- "扇圓潔銀燭煒煌。指薪修祜永綏吉劭。果珍李柰菜重芥薑。景行",
1287
- "維賢克念作聖。右通廣內左達承明。省躬譏誡寵增抗極。晝眠夕寐",
1288
- "藍筍象床。矩步引領俯仰廊廟。海鹹河淡,鱗潛羽翔。德建名立形端",
1289
- "表正。既集墳典亦聚群英。殆辱近恥林皋幸即。弦歌酒宴接杯舉觴。束",
1290
- "帶矜庄徘徊瞻眺。空谷傳聲虛堂習聽。杜稿鍾隸漆書壁經。兩疏見機解組",
1291
- "誰逼。矯手頓足,悅豫且康。孤陋寡聞,愚蒙等誚。龍師火帝,鳥官人皇。",
1292
- " 禍因惡積,福緣善慶。 府羅將相,路俠槐卿。 索居閒處,沉默寂寥。",
1293
- "嫡後嗣續,祭祀烝嘗。 謂語助者,焉哉乎也。 始製文字,乃服衣裳",
1294
- "尺璧非寶,寸陰是競。 戶封八縣,家給千兵。 求古尋論,散慮逍遙。稽顙",
1295
- "再拜,悚懼恐惶。 推位讓國,有虞陶唐。 資父事君,曰嚴與敬。",
1296
- " 高冠陪輦,驅轂振纓。 欣奏累遣,慼謝歡招。 箋牒簡要,顧答審詳。 ",
1297
- "弔民伐罪,周發殷湯。 孝當竭力,忠則盡命。 世祿侈富,車駕肥輕。 渠荷",
1298
- "的歷,園莽抽條。 骸垢想浴,執熱願涼。 坐朝問道,垂拱平章。 ",
1299
- "臨深履薄,夙興溫凊。 策功茂實,勒碑刻銘。 枇杷晚翠,梧桐蚤凋。 驢騾犢特",
1300
- ",駭躍超驤。 愛育黎首,臣伏戎羌。 似蘭斯馨,如松之盛。 磻溪伊尹,佐時阿衡",
1301
- "。 陳根委翳,落葉飄搖。 誅斬賊盜,捕獲叛亡。 遐邇一體,率賓歸王。",
1302
- " 川流不息,淵澄取映。 奄宅曲阜,微旦孰營。 遊鵾獨運,凌摩絳霄。 ",
1303
- " 鳴鳳在竹,白駒食場。 容止若思,言辭安定。 桓公匡合,濟弱扶傾。 化被草木,",
1304
- "賴及萬方。 篤初誠美,慎終宜令。 綺回漢惠,說感武丁。榮業所基,藉甚無竟。 俊乂",
1305
- "密勿,多士寔寧。 學優登仕,攝職從政。 晉楚更霸,趙魏困橫。 存",
1306
- "以甘棠,去而益詠。 假途滅虢,踐土會盟。 樂殊貴賤,禮別尊卑。 ",
1307
- "何遵約法,韓弊煩刑。 上和下睦,夫唱婦隨。 起翦頗牧,用軍最精。 ",
1308
- " 外受傅訓,入奉母儀。 宣威沙漠,馳譽丹青。 諸姑伯叔,猶",
1309
- "子比兒。 九州禹跡,百郡秦并。 孔懷兄弟,同氣連枝。 岳宗泰岱,禪主云亭",
1310
- "。 交友投分,切磨箴規。 雁門紫塞,雞田赤城。 仁慈隱惻,造次弗",
1311
- "離。 昆池碣石,鉅野洞庭。 節義廉退,顛沛匪虧。 曠遠綿邈,岩岫杳冥。 ",
1312
- "性靜情逸,心動神疲。 守真志滿,逐物意移。 堅持雅操,好爵自縻。"
1313
- ]
1314
-
1315
- testVector.forEach((v, i) => {
1316
- const split = i % 2 == 0
1317
-
1318
- it(`ok for "${v}" with split=${split}`, () => {
1319
- expect(runSync(Cbor.decodeString(Cbor.encodeString(v, split)))).toBe(v)
1320
- })
1321
- })
1322
- })
1323
-
1324
- describe("Cbor.isString()", () => {
1325
- it("returns true for [0x60]", () => {
1326
- expect(runSync(Cbor.isString([0x60]))).toBe(true)
1327
- })
1328
-
1329
- it("returns false for [0]", () => {
1330
- expect(runSync(Cbor.isString([0]))).toBe(false)
1331
- })
1332
-
1333
- it("fails for []", () => {
1334
- expect(() => runSync(Cbor.isString([]))).toThrow()
1335
- })
1336
-
1337
- it("doesn't change stream pos", () => {
1338
- const stream = makeStream([0x60])
1339
-
1340
- expect(runSync(Cbor.isString(stream))).toBe(true)
1341
- expect(stream.pos).toBe(0)
1342
- })
1343
-
1344
- it("doesn't change stream pos if not a string", () => {
1345
- const stream = makeStream(Cbor.encodeInt(0))
1346
-
1347
- expect(runSync(Cbor.isString(stream))).toBe(false)
1348
- expect(stream.pos).toBe(0)
1349
- })
1350
- })
1351
-
1352
- describe("Cbor.decodeTag()", () => {
1353
- it("returns 1 for #c11a514b67b0", () => {
1354
- expect(runSync(Cbor.decodeTag("c11a514b67b0"))).toBe(1n)
1355
- })
1356
-
1357
- it(`returns 1363896240 after decoding tag of #c11a514b67b0`, () => {
1358
- const stream = makeStream("c11a514b67b0")
1359
- runSync(Cbor.decodeTag(stream))
1360
- expect(runSync(Cbor.decodeInt(stream))).toBe(1363896240n)
1361
- })
1362
-
1363
- it("fails for []", () => {
1364
- expect(() => runSync(Cbor.decodeTag([]))).toThrow()
1365
- })
1366
-
1367
- it("fails for [0]", () => {
1368
- expect(() => runSync(Cbor.decodeTag([0]))).toThrow()
1369
- })
1370
-
1371
- it("decodes tag in d90102 as 258", () => {
1372
- expect(runSync(Cbor.decodeTag("d90102"))).toBe(258n)
1373
- })
1374
- })
1375
-
1376
- describe("Cbor.encodeTag()", () => {
1377
- it("returns #c11a514b67b0 for 1(1363896240n)", () => {
1378
- expect(Cbor.encodeTag(1).concat(Cbor.encodeInt(1363896240n))).toEqual(
1379
- toArray("c11a514b67b0")
1380
- )
1381
- })
1382
-
1383
- it("fails for a negative tag", () => {
1384
- expect(() => Cbor.encodeTag(-1)).toThrow()
1385
- })
1386
- })
1387
-
1388
- describe("Cbor.isTag()", () => {
1389
- it("detects tag in d90102", () => {
1390
- expect(runSync(Cbor.isTag("d90102"))).toBe(true)
1391
- })
1392
-
1393
- it("detects tag in set of signatures", () => {
1394
- expect(
1395
- runSync(
1396
- Cbor.isTag(
1397
- "d901028182582044f3523cc794ecd0e4cc6aa5d459d4c0b30064d7f7f68dac0eb0653819861b985840ad8a1887d409ca2c5205a9002b104ff77ddee415d730fd85925399e622c6840c2a0c68b72d4bd57979f1d9fec70c6ee7b15a01607da98119dddf05420e274e0a"
1398
- )
1399
- )
1400
- ).toBe(true)
1401
- })
1402
- })
1403
-
1404
- describe("Cbor.decodeTagged()", () => {
1405
- it("returns 1 when decoding first item of tuple [0, 1]", () => {
1406
- const tupleBytes = Cbor.encodeTuple([Cbor.encodeInt(0), Cbor.encodeInt(1)])
1407
-
1408
- const [tag, decodeItem] = runSync(Cbor.decodeTagged(tupleBytes))
1409
-
1410
- expect([tag, runSync(decodeItem(Cbor.decodeInt))]).toEqual([0, 1n])
1411
- })
1412
-
1413
- it("fails when decoding too many items", () => {
1414
- const tupleBytes = Cbor.encodeTuple([Cbor.encodeInt(0), Cbor.encodeInt(1)])
1415
-
1416
- const decodeItem = runSync(Cbor.decodeTagged(tupleBytes))[1]
1417
-
1418
- runSync(decodeItem(Cbor.decodeInt))
1419
-
1420
- expect(() => runSync(decodeItem(Cbor.decodeInt))).toThrow()
1421
- })
1422
-
1423
- it("returns tag 0 for d87982581cbd99a373075d42fe4ac9109515e46303d0940cb9620bf058b87986a9d87980 (plain const)", () => {
1424
- const [tag] = runSync(
1425
- Cbor.decodeTagged(
1426
- "d87982581cbd99a373075d42fe4ac9109515e46303d0940cb9620bf058b87986a9d87980"
1427
- )
1428
- )
1429
-
1430
- expect(tag).toBe(0)
1431
- })
1432
- })
1433
-
1434
- describe("Cbor.decodeTuple()", () => {
1435
- describe("returns [1n, [2n, 3n], [4n, 5n]]", () => {
1436
- const expected: [bigint, bigint[], bigint[]] = [1n, [2n, 3n], [4n, 5n]]
1437
-
1438
- const variants = [
1439
- "8301820203820405",
1440
- "9f018202039f0405ffff",
1441
- "9f01820203820405ff",
1442
- "83018202039f0405ff",
1443
- "83019f0203ff820405"
1444
- ]
1445
-
1446
- for (const v of variants) {
1447
- it(`decodes #${v}`, () => {
1448
- const actual = runSync(
1449
- Cbor.decodeTuple([
1450
- Cbor.decodeInt,
1451
- Cbor.decodeList(Cbor.decodeInt),
1452
- Cbor.decodeList(Cbor.decodeInt)
1453
- ])(v)
1454
- )
1455
-
1456
- expect(actual).toEqual(expected)
1457
- })
1458
- }
1459
- })
1460
-
1461
- it('returns ["a", {b: "c"}] for #826161a161626163', () => {
1462
- const actual = runSync(
1463
- Cbor.decodeTuple([
1464
- Cbor.decodeString,
1465
- Cbor.decodeObjectSKey({ b: Cbor.decodeString })
1466
- ])("826161a161626163")
1467
- )
1468
-
1469
- expect(actual).toEqual(["a", { b: "c" }])
1470
- })
1471
-
1472
- it('returns ["a", {b: "c"}] for #826161bf61626163ff', () => {
1473
- const actual = runSync(
1474
- Cbor.decodeTuple([
1475
- Cbor.decodeString,
1476
- Cbor.decodeObjectSKey({ b: Cbor.decodeString })
1477
- ])("826161bf61626163ff")
1478
- )
1479
-
1480
- expect(actual).toEqual(["a", { b: "c" }])
1481
- })
1482
-
1483
- it('returns ["a", {b: "c"}] for #826161bf61626163ff, with the second decoder being optional', () => {
1484
- const actual = runSync(
1485
- Cbor.decodeTuple(
1486
- [Cbor.decodeString],
1487
- [Cbor.decodeObjectSKey({ b: Cbor.decodeString })]
1488
- )("826161bf61626163ff")
1489
- )
1490
-
1491
- expect(actual).toEqual(["a", { b: "c" }])
1492
- })
1493
-
1494
- it("fails if an optional decoder is missing for third entry", () => {
1495
- expect(() => {
1496
- runSync(
1497
- Cbor.decodeTuple(
1498
- [Cbor.decodeString],
1499
- [Cbor.decodeObjectSKey({ b: Cbor.decodeString })]
1500
- )(
1501
- Cbor.encodeTuple([
1502
- Cbor.encodeString("a"),
1503
- Cbor.encodeObjectSKey({ b: Cbor.encodeString("c") }),
1504
- Cbor.encodeInt(0)
1505
- ])
1506
- )
1507
- )
1508
- }).toThrow()
1509
- })
1510
-
1511
- it("fails for #826161bf61626163ff when decoding 3 items", () => {
1512
- expect(() =>
1513
- runSync(
1514
- Cbor.decodeTuple([
1515
- Cbor.decodeString,
1516
- Cbor.decodeObjectSKey({ b: Cbor.decodeString }),
1517
- Cbor.decodeString
1518
- ])("826161bf61626163ff")
1519
- )
1520
- ).toThrow()
1521
- })
1522
-
1523
- it("fails for #826161bf61626163ff when decoding only 1 item", () => {
1524
- expect(() =>
1525
- runSync(Cbor.decodeTuple([Cbor.decodeString])("826161bf61626163ff"))
1526
- ).toThrow()
1527
- })
1528
-
1529
- it("fails for #826161bf61626163ff when decoding 0 items", () => {
1530
- expect(() => {
1531
- runSync(Cbor.decodeTuple([])("826161bf61626163ff"))
1532
- })
1533
- })
1534
- })
1535
-
1536
- describe("Cbor.decodeTupleLazy()", () => {
1537
- it("fails for []", () => {
1538
- expect(() => runSync(Cbor.decodeTupleLazy([]))).toThrow()
1539
- })
1540
-
1541
- it("fails for [0]", () => {
1542
- expect(() => runSync(Cbor.decodeTupleLazy([0]))).toThrow()
1543
- })
1544
-
1545
- it("succeeds when not calling the callback for [0x80] (i.e. empty list)", () => {
1546
- runSync(Cbor.decodeTupleLazy([0x80]))
1547
- })
1548
-
1549
- it("fails when calling the callback for [0x80] (i.e. empty list)", () => {
1550
- const callback = runSync(Cbor.decodeTupleLazy([0x80]))
1551
-
1552
- expect(() => {
1553
- runSync(callback(Cbor.decodeInt))
1554
- }).toThrow(/end-of-list/)
1555
- })
1556
-
1557
- it('returns [1n,"hello world"]', () => {
1558
- const callback = runSync(
1559
- Cbor.decodeTupleLazy(
1560
- Cbor.encodeTuple([Cbor.encodeInt(1), Cbor.encodeString("hello world")])
1561
- )
1562
- )
1563
-
1564
- expect(runSync(callback(Cbor.decodeInt))).toBe(1n)
1565
- expect(runSync(callback(Cbor.decodeString))).toBe("hello world")
1566
-
1567
- expect(() => {
1568
- runSync(callback(Cbor.decodeInt))
1569
- }).toThrow(/end-of-list/)
1570
- })
1571
- })
1572
-
1573
- describe("Cbor.encodeTuple()", () => {
1574
- it('returns #826161a161626163 for ["a", {b: "c"}]', () => {
1575
- expect(
1576
- Cbor.encodeTuple([
1577
- Cbor.encodeString("a"),
1578
- Cbor.encodeObjectSKey({ b: Cbor.encodeString("c") })
1579
- ])
1580
- ).toEqual(toArray("826161a161626163"))
1581
- })
1582
- })
1583
-
1584
- describe("Cbor.isTuple()", () => {
1585
- it("fails for []", () => {
1586
- expect(() => runSync(Cbor.isTuple([]))).toThrow()
1587
- })
1588
-
1589
- it("returns false for [0]", () => {
1590
- expect(runSync(Cbor.isTuple([0]))).toBe(false)
1591
- })
1592
-
1593
- it("returns true for #8301820203820405", () => {
1594
- expect(runSync(Cbor.isTuple("8301820203820405"))).toBe(true)
1595
- })
1596
-
1597
- it("doesn't change stream pos", () => {
1598
- const stream = makeStream("8301820203820405")
1599
-
1600
- expect(runSync(Cbor.isTuple(stream))).toBe(true)
1601
- expect(stream.pos).toBe(0)
1602
- })
1603
-
1604
- it("doesn't change stream pos if not a tuple", () => {
1605
- const stream = makeStream(Cbor.encodeInt(0))
1606
-
1607
- expect(runSync(Cbor.isTuple(stream))).toBe(false)
1608
- expect(stream.pos).toBe(0)
1609
- })
1610
- })