|
1 | 1 | import { describe, it, expect } from 'vitest' |
2 | 2 | import { transform } from '../src/index.js' |
3 | | -import type { Variable } from 'cddl' |
| 3 | +import type { Group, Property, Variable } from 'cddl' |
| 4 | + |
| 5 | +function variable(name: string, propertyType: Variable['PropertyType']): Variable { |
| 6 | + return { |
| 7 | + Type: 'variable', |
| 8 | + Name: name, |
| 9 | + PropertyType: propertyType, |
| 10 | + Comments: [], |
| 11 | + IsChoiceAddition: false |
| 12 | + } |
| 13 | +} |
| 14 | + |
| 15 | +function property(name: string, type: Property['Type']): Property { |
| 16 | + return { |
| 17 | + HasCut: false, |
| 18 | + Occurrence: { n: 1, m: 1 }, |
| 19 | + Name: name, |
| 20 | + Type: type, |
| 21 | + Comments: [] |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +function group(name: string, properties: Group['Properties']): Group { |
| 26 | + return { |
| 27 | + Type: 'group', |
| 28 | + Name: name, |
| 29 | + IsChoiceAddition: false, |
| 30 | + Properties: properties, |
| 31 | + Comments: [] |
| 32 | + } |
| 33 | +} |
4 | 34 |
|
5 | 35 | describe('literal transformation direct', () => { |
6 | 36 | it('should transform bigint literals correctly', () => { |
7 | | - const assignment: Variable = { |
8 | | - Type: 'variable', |
9 | | - Name: 'MyBigInt', |
10 | | - PropertyType: { |
11 | | - Type: 'literal', |
12 | | - Value: 9007199254740995n |
13 | | - } as any, |
14 | | - Comments: [], |
15 | | - IsChoiceAddition: false |
16 | | - } |
| 37 | + const assignment = variable('MyBigInt', { |
| 38 | + Type: 'literal', |
| 39 | + Value: 9007199254740995n |
| 40 | + } as any) |
17 | 41 |
|
18 | 42 | const output = transform([assignment]) |
19 | 43 | expect(output).toContain('export type MyBigInt = 9007199254740995n;') |
20 | 44 | }) |
| 45 | + |
| 46 | + it('should transform float32 aliases correctly', () => { |
| 47 | + const assignment = variable('score', 'float32') |
| 48 | + |
| 49 | + const output = transform([assignment]) |
| 50 | + expect(output).toContain('export type Score = number;') |
| 51 | + }) |
| 52 | + |
| 53 | + it.each([ |
| 54 | + ['integer-value', 'integer', 'number'], |
| 55 | + ['negative-value', 'nint', 'number'], |
| 56 | + ['unsigned-value', 'unsigned', 'number'], |
| 57 | + ['half-float', 'float16', 'number'], |
| 58 | + ['double-float', 'float64', 'number'], |
| 59 | + ['float-window', 'float16-32', 'number'], |
| 60 | + ['float-range', 'float32-64', 'number'], |
| 61 | + ['binary-payload', 'bytes', 'Uint8Array'], |
| 62 | + ['binary-blob', 'bstr', 'Uint8Array'], |
| 63 | + ['missing-value', 'undefined', 'undefined'] |
| 64 | + ] as const)('should map %s (%s) to %s', (name, propertyType, expectedType) => { |
| 65 | + const output = transform([variable(name, propertyType)]) |
| 66 | + expect(output).toContain(`export type ${name.split('-').map((part) => `${part[0]!.toUpperCase()}${part.slice(1)}`).join('')} = ${expectedType};`) |
| 67 | + }) |
| 68 | + |
| 69 | + it('should keep bytes fields as object properties instead of record aliases', () => { |
| 70 | + const output = transform([ |
| 71 | + group('network-get-data-result', [ |
| 72 | + property('bytes', { |
| 73 | + Type: 'group', |
| 74 | + Value: 'network.BytesValue', |
| 75 | + Unwrapped: false |
| 76 | + } as any) |
| 77 | + ]) |
| 78 | + ]) |
| 79 | + |
| 80 | + expect(output).toContain('export interface NetworkGetDataResult {') |
| 81 | + expect(output).toContain('bytes: NetworkBytesValue;') |
| 82 | + expect(output).not.toContain('export type NetworkGetDataResult = Record') |
| 83 | + }) |
21 | 84 | }) |
0 commit comments