Skip to content

Commit

Permalink
Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sinclairzx81 committed Feb 20, 2025
1 parent f8114a0 commit b086e16
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 7 deletions.
12 changes: 7 additions & 5 deletions example/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@ import { Type, TypeGuard, Kind, Static, TSchema, StaticDecode, StaticEncode } fr
import { Syntax } from '@sinclair/typebox/syntax'

const T = Type.Module({
U: Syntax('1 | 2'),
A: Type.Transform(Syntax('{ x: 1, y: 2, z: 3, u: U }'))
.Decode((value) => value)
.Encode((value) => value),
T: Type.String(),
A: Type.Transform(Type.Ref('T'))
.Decode((value) => parseInt(value as string))
.Encode((value) => value.toString()),
B: Type.Ref('A'),
C: Type.Ref('B'),
D: Type.Ref('C'),
E: Type.Ref('D'),
}).Import('E')

const result = Value.Decode(T, { x: 1, y: 2, z: 3, u: 3 })
const result = Value.Decode(T, '12345')

console.log(result, typeof result)
68 changes: 66 additions & 2 deletions test/runtime/value/transform/import.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,13 +135,13 @@ describe('value/transform/Import', () => {
const T5 = Type.Transform(Type.Module({ A: Type.Object({ x: Type.String(), y: Type.String() })}).Import('A'))
.Decode((value) => new Map(Object.entries(value)))
.Encode((value) => Object.fromEntries(value.entries()) as any)
it('should decode map', () => {
it('Should decode map', () => {
const R = Encoder.Decode(T5, { x: 'hello', y: 'world' })
Assert.IsInstanceOf(R, Map)
Assert.IsEqual(R.get('x'), 'hello')
Assert.IsEqual(R.get('y'), 'world')
})
it('should encode map', () => {
it('Should encode map', () => {
const R = Encoder.Encode(
T5,
new Map([
Expand All @@ -154,4 +154,68 @@ describe('value/transform/Import', () => {
it('Should throw on map decode', () => {
Assert.Throws(() => Encoder.Decode(T5, {}))
})

// -------------------------------------------------------------
// https://github.com/sinclairzx81/typebox/issues/1178
// -------------------------------------------------------------
// immediate
it('Should transform embedded module codec 1', () => {
const T = Type.Module({
A: Type.Transform(Type.String())
.Decode((value) => parseInt(value))
.Encode((value) => value.toString()),
}).Import('A')

const D = Value.Decode(T, '123')
const E = Value.Encode(T, 123)
Assert.IsEqual(D, 123)
Assert.IsEqual(E, '123')
})
// referential
it('Should transform embedded module codec 2', () => {
const T = Type.Module({
A: Type.Transform(Type.String())
.Decode((value) => parseInt(value))
.Encode((value) => value.toString()),
B: Type.Ref('A'),
}).Import('B')
const D = Value.Decode(T, '123')
const E = Value.Encode(T, 123)
Assert.IsEqual(D, 123)
Assert.IsEqual(E, '123')
})
// deep-referential
it('Should transform embedded module codec 3', () => {
const T = Type.Module({
A: Type.Transform(Type.String())
.Decode((value) => parseInt(value))
.Encode((value) => value.toString()),
B: Type.Ref('A'),
C: Type.Ref('B'),
D: Type.Ref('C'),
E: Type.Ref('D'),
}).Import('E')
const D = Value.Decode(T, '123')
const E = Value.Encode(T, 123)
Assert.IsEqual(D, 123)
Assert.IsEqual(E, '123')
})
// interior-transform referential (limitation: cannot infer interior transform reference)
it('Should transform embedded module codec 4', () => {
// const T = Type.Module({
// T: Type.String(),
// A: Type.Transform(Type.Ref('T'))
// .Decode((value) => parseInt(value as string))
// .Encode((value) => value.toString()),
// B: Type.Ref('A'),
// C: Type.Ref('B'),
// D: Type.Ref('C'),
// E: Type.Ref('D'),
// }).Import('E')

// const D = Value.Decode(T, '123')
// const E = Value.Encode(T, 123)
// Assert.IsEqual(D, 123)
// Assert.IsEqual(E, '123')
})
})
53 changes: 53 additions & 0 deletions test/static/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -320,3 +320,56 @@ import { Expect } from './assert'
const x1 = c1.Decode({})
const x2 = Value.Decode({} as any, {})
}
// -------------------------------------------------------------
// https://github.com/sinclairzx81/typebox/issues/1178
// -------------------------------------------------------------
// immediate
{
const T = Type.Module({
A: Type.Transform(Type.String())
.Decode((value) => parseInt(value))
.Encode((value) => value.toString()),
}).Import('A')
Expect(T).ToStaticDecode<number>()
Expect(T).ToStaticEncode<string>()
}
// referential
{
const T = Type.Module({
A: Type.Transform(Type.String())
.Decode((value) => parseInt(value))
.Encode((value) => value.toString()),
B: Type.Ref('A'),
}).Import('B')
Expect(T).ToStaticDecode<number>()
Expect(T).ToStaticEncode<string>()
}
// deep-referential
{
const T = Type.Module({
A: Type.Transform(Type.String())
.Decode((value) => parseInt(value))
.Encode((value) => value.toString()),
B: Type.Ref('A'),
C: Type.Ref('B'),
D: Type.Ref('C'),
E: Type.Ref('D'),
}).Import('E')
Expect(T).ToStaticDecode<number>()
Expect(T).ToStaticEncode<string>()
}
// interior-transform referential (limitation: cannot infer interior transform reference)
{
const T = Type.Module({
T: Type.String(),
A: Type.Transform(Type.Ref('T'))
.Decode((value) => parseInt(value as string))
.Encode((value) => value.toString()),
B: Type.Ref('A'),
C: Type.Ref('B'),
D: Type.Ref('C'),
E: Type.Ref('D'),
}).Import('E')
Expect(T).ToStaticDecode<number>()
Expect(T).ToStaticEncode<unknown>()
}

0 comments on commit b086e16

Please sign in to comment.