Skip to content

Commit

Permalink
Finalize First Pass Default and Clean Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sinclairzx81 committed Oct 27, 2023
1 parent c9be830 commit 6dd19d3
Show file tree
Hide file tree
Showing 8 changed files with 192 additions and 2 deletions.
8 changes: 6 additions & 2 deletions examples/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ import { Run } from './benchmark'

// Todo: Investigate Union Default (initialize interior types)
// Todo: Implement Value.Clean() Tests
const T = Type.Tuple([Type.Number(), Type.Number()])
const R = Value.Clean(T, [1, 2, 3])

const X = Type.Object({ x: Type.Number() })
const Y = Type.Object({ y: Type.Number() })
const T = Type.Union([X, Y])
const R = Value.Clean(T, { u: null, x: 1 })

console.log(R)
5 changes: 5 additions & 0 deletions test/runtime/value/clean/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,8 @@ import './string'
import './symbol'
import './template-literal'
import './tuple'
import './uint8array'
import './undefined'
import './union'
import './unknown'
import './void'
3 changes: 3 additions & 0 deletions test/runtime/value/clean/object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ import { Type } from '@sinclair/typebox'
import { Assert } from '../../assert/index'

describe('value/clean/Object', () => {
// ----------------------------------------------------------------
// Clean
// ----------------------------------------------------------------
it('Should clean 1', () => {
const T = Type.Object({ x: Type.Number() })
const R = Value.Clean(T, null)
Expand Down
11 changes: 11 additions & 0 deletions test/runtime/value/clean/uint8array.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Value } from '@sinclair/typebox/value'
import { Type } from '@sinclair/typebox'
import { Assert } from '../../assert/index'

describe('value/clean/Uint8Array', () => {
it('Should clean 1', () => {
const T = Type.Uint8Array()
const R = Value.Clean(T, null)
Assert.IsEqual(R, null)
})
})
11 changes: 11 additions & 0 deletions test/runtime/value/clean/undefined.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Value } from '@sinclair/typebox/value'
import { Type } from '@sinclair/typebox'
import { Assert } from '../../assert/index'

describe('value/clean/Undefined', () => {
it('Should clean 1', () => {
const T = Type.Undefined()
const R = Value.Clean(T, null)
Assert.IsEqual(R, null)
})
})
134 changes: 134 additions & 0 deletions test/runtime/value/clean/union.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
import { Value } from '@sinclair/typebox/value'
import { Type } from '@sinclair/typebox'
import { Assert } from '../../assert/index'

describe('value/clean/Union', () => {
// ----------------------------------------------------------------
// Clean
// ----------------------------------------------------------------
it('Should clean 1', () => {
const T = Type.Union([Type.Number(), Type.Boolean()])
const R = Value.Clean(T, null)
Assert.IsEqual(R, null)
})
it('Should clean 2', () => {
const T = Type.Union([Type.Number(), Type.Boolean()])
const R = Value.Clean(T, 1)
Assert.IsEqual(R, 1)
})
it('Should clean 2', () => {
const T = Type.Union([Type.Number(), Type.Boolean()])
const R = Value.Clean(T, true)
Assert.IsEqual(R, true)
})
// ----------------------------------------------------------------
// Clean Select
// ----------------------------------------------------------------
it('Should clean select 1', () => {
const X = Type.Object({ x: Type.Number() })
const Y = Type.Object({ y: Type.Number() })
const T = Type.Union([X, Y])
const R = Value.Clean(T, null)
Assert.IsEqual(R, null)
})
it('Should clean select 2', () => {
const X = Type.Object({ x: Type.Number() })
const Y = Type.Object({ y: Type.Number() })
const T = Type.Union([X, Y])
const R = Value.Clean(T, {})
Assert.IsEqual(R, {})
})
it('Should clean select 3', () => {
const X = Type.Object({ x: Type.Number() })
const Y = Type.Object({ y: Type.Number() })
const T = Type.Union([X, Y])
const R = Value.Clean(T, { x: null })
Assert.IsEqual(R, { x: null })
})
it('Should clean select 4', () => {
const X = Type.Object({ x: Type.Number() })
const Y = Type.Object({ y: Type.Number() })
const T = Type.Union([X, Y])
const R = Value.Clean(T, { y: null })
Assert.IsEqual(R, { y: null })
})
// ----------------------------------------------------------------
// Clean Select Discard
// ----------------------------------------------------------------
it('Should clean select discard 1', () => {
const X = Type.Object({ x: Type.Number() })
const Y = Type.Object({ y: Type.Number() })
const T = Type.Union([X, Y])
const R = Value.Clean(T, null)
Assert.IsEqual(R, null)
})
it('Should clean select discard 2', () => {
const X = Type.Object({ x: Type.Number() })
const Y = Type.Object({ y: Type.Number() })
const T = Type.Union([X, Y])
const R = Value.Clean(T, { u: null })
Assert.IsEqual(R, { u: null }) // no match
})
it('Should clean select discard 3', () => {
const X = Type.Object({ x: Type.Number() })
const Y = Type.Object({ y: Type.Number() })
const T = Type.Union([X, Y])
const R = Value.Clean(T, { u: null, x: 1 })
Assert.IsEqual(R, { x: 1 })
})
it('Should clean select discard 4', () => {
const X = Type.Object({ x: Type.Number() })
const Y = Type.Object({ y: Type.Number() })
const T = Type.Union([X, Y])
const R = Value.Clean(T, { u: null, y: 1 })
Assert.IsEqual(R, { y: 1 })
})
// ----------------------------------------------------------------
// Clean Select Retain
// ----------------------------------------------------------------
it('Should clean select retain 1', () => {
const X = Type.Object({ x: Type.Number() }, { additionalProperties: Type.Null() })
const Y = Type.Object({ y: Type.Number() }, { additionalProperties: Type.Null() })
const T = Type.Union([X, Y])
const R = Value.Clean(T, null)
Assert.IsEqual(R, null)
})
it('Should clean select retain 2', () => {
const X = Type.Object({ x: Type.Number() }, { additionalProperties: Type.Null() })
const Y = Type.Object({ y: Type.Number() }, { additionalProperties: Type.Null() })
const T = Type.Union([X, Y])
const R = Value.Clean(T, { u: null })
Assert.IsEqual(R, { u: null })
})
it('Should clean select retain 3', () => {
const X = Type.Object({ x: Type.Number() }, { additionalProperties: Type.Null() })
const Y = Type.Object({ y: Type.Number() }, { additionalProperties: Type.Null() })
const T = Type.Union([X, Y])
const R = Value.Clean(T, { u: null, x: 1 })
Assert.IsEqual(R, { u: null, x: 1 })
})
it('Should clean select retain 4', () => {
const X = Type.Object({ x: Type.Number() }, { additionalProperties: Type.Null() })
const Y = Type.Object({ y: Type.Number() }, { additionalProperties: Type.Null() })
const T = Type.Union([X, Y])
const R = Value.Clean(T, { u: null, y: 1 })
Assert.IsEqual(R, { u: null, y: 1 })
})
// ----------------------------------------------------------------
// Clean Select First and Discard
// ----------------------------------------------------------------
it('Should clean select first and discard 1', () => {
const X = Type.Object({ x: Type.Number() })
const Y = Type.Object({ y: Type.Number() }, { additionalProperties: Type.Null() })
const T = Type.Union([X, Y])
const R = Value.Clean(T, { u: null, x: 1 })
Assert.IsEqual(R, { x: 1 })
})
it('Should clean select first and discard 2', () => {
const X = Type.Object({ x: Type.Number() })
const Y = Type.Object({ y: Type.Number() }, { additionalProperties: Type.Null() })
const T = Type.Union([X, Y])
const R = Value.Clean(T, { u: null, y: 1 })
Assert.IsEqual(R, { u: null, y: 1 })
})
})
11 changes: 11 additions & 0 deletions test/runtime/value/clean/unknown.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Value } from '@sinclair/typebox/value'
import { Type } from '@sinclair/typebox'
import { Assert } from '../../assert/index'

describe('value/clean/Unknown', () => {
it('Should clean 1', () => {
const T = Type.Unknown()
const R = Value.Clean(T, null)
Assert.IsEqual(R, null)
})
})
11 changes: 11 additions & 0 deletions test/runtime/value/clean/void.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Value } from '@sinclair/typebox/value'
import { Type } from '@sinclair/typebox'
import { Assert } from '../../assert/index'

describe('value/clean/Void', () => {
it('Should clean 1', () => {
const T = Type.Void()
const R = Value.Clean(T, null)
Assert.IsEqual(R, null)
})
})

0 comments on commit 6dd19d3

Please sign in to comment.