|
1 | 1 | import {Writer} from '@jsonjoy.com/buffers/lib/Writer'; |
2 | 2 | import {CborDecoder} from '@jsonjoy.com/json-pack/lib/cbor/CborDecoder'; |
3 | 3 | import {CborEncoder} from '@jsonjoy.com/json-pack/lib/cbor/CborEncoder'; |
4 | | -import type {Type} from '../../../../type'; |
5 | | -import type {ModuleType} from '../../../../type/classes/ModuleType'; |
| 4 | +import {ModuleType} from '../../../../type/classes/ModuleType'; |
6 | 5 | import {testBinaryCodegen} from '../../__tests__/testBinaryCodegen'; |
7 | 6 | import {CborCodegen} from '../CborCodegen'; |
| 7 | +import {unknown, Value} from '../../../../value'; |
| 8 | +import type {Type} from '../../../../type'; |
8 | 9 |
|
9 | 10 | const encoder = new CborEncoder(new Writer(16)); |
10 | 11 | const decoder = new CborDecoder(); |
11 | 12 |
|
| 13 | +describe('inline Value', () => { |
| 14 | + test('can encode "any" field', () => { |
| 15 | + const {t} = new ModuleType(); |
| 16 | + const type = t.object({foo: t.any}); |
| 17 | + const fn = CborCodegen.get(type); |
| 18 | + encoder.writer.reset(); |
| 19 | + fn({foo: true}, encoder); |
| 20 | + const encoded = encoder.writer.flush(); |
| 21 | + const decoded = decoder.decode(encoded); |
| 22 | + expect(decoded).toEqual({foo: true}); |
| 23 | + }); |
| 24 | + |
| 25 | + test('can encode anon Value<unknown>', () => { |
| 26 | + const {t} = new ModuleType(); |
| 27 | + const type = t.object({foo: t.any}); |
| 28 | + const fn = CborCodegen.get(type); |
| 29 | + encoder.writer.reset(); |
| 30 | + const value = unknown('test'); |
| 31 | + fn({foo: value}, encoder); |
| 32 | + const encoded = encoder.writer.flush(); |
| 33 | + const decoded = decoder.decode(encoded); |
| 34 | + expect(decoded).toEqual({foo: 'test'}); |
| 35 | + }); |
| 36 | + |
| 37 | + test('can encode typed Value<T>', () => { |
| 38 | + const {t} = new ModuleType(); |
| 39 | + const type = t.object({foo: t.any}); |
| 40 | + const fn = CborCodegen.get(type); |
| 41 | + encoder.writer.reset(); |
| 42 | + const value = new Value(123, t.con(123)); |
| 43 | + fn({foo: value}, encoder); |
| 44 | + const encoded = encoder.writer.flush(); |
| 45 | + const decoded = decoder.decode(encoded); |
| 46 | + expect(decoded).toEqual({foo: 123}); |
| 47 | + }); |
| 48 | +}); |
| 49 | + |
12 | 50 | const transcode = (system: ModuleType, type: Type, value: unknown) => { |
13 | 51 | const fn = CborCodegen.get(type); |
14 | 52 | encoder.writer.reset(); |
|
0 commit comments