Skip to content

Commit

Permalink
Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sinclairzx81 committed Oct 27, 2023
1 parent b2c5420 commit c9be830
Show file tree
Hide file tree
Showing 11 changed files with 385 additions and 35 deletions.
32 changes: 2 additions & 30 deletions examples/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,6 @@ import { Run } from './benchmark'

// Todo: Investigate Union Default (initialize interior types)
// Todo: Implement Value.Clean() Tests

function ParseEnv<T extends TObject>(schema: T, value: unknown = process.env): Static<T> {
const converted = Value.Convert(schema, value)
const defaulted = Value.Default(schema, converted)
const checked = Value.Check(schema, defaulted)
return checked
? (Value.Clean(schema, defaulted) as Static<T>)
: (() => {
const first = Value.Errors(schema, defaulted).First()!
throw new Error(`${first.message} ${first.path}. Value is ${first.value}`)
})()
}
console.log(process.env)

const R = Run(
() => {
const E = ParseEnv(
Type.Object({
PROCESSOR_ARCHITECTURE: Type.String(),
PROCESSOR_IDENTIFIER: Type.String(),
PROCESSOR_LEVEL: Type.Number(),
PROCESSOR_REVISION: Type.Number(),
}),
)
},
{
iterations: 2000,
},
)

const T = Type.Tuple([Type.Number(), Type.Number()])
const R = Value.Clean(T, [1, 2, 3])
console.log(R)
9 changes: 6 additions & 3 deletions src/value/clean.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ function TIntersect(schema: Types.TIntersect, references: Types.TSchema[], value
return composite
}
function TObject(schema: Types.TObject, references: Types.TSchema[], value: unknown): any {
if (!IsObject(value) || IsArray(value)) return value // IsArray escape for AllowArrayObject
if (!IsObject(value) || IsArray(value)) return value // Check IsArray for AllowArrayObject configuration
const additionalProperties = schema.additionalProperties as Types.TSchema
for (const key of Object.getOwnPropertyNames(value)) {
if (key in schema.properties) {
Expand Down Expand Up @@ -103,11 +103,14 @@ function TThis(schema: Types.TThis, references: Types.TSchema[], value: unknown)
function TTuple(schema: Types.TTuple, references: Types.TSchema[], value: unknown): any {
if (!IsArray(value)) return value
if (IsUndefined(schema.items)) return []
const length = schema.items.length
const length = Math.min(value.length, schema.items.length)
for (let i = 0; i < length; i++) {
value[i] = Visit(schema.items[i], references, value[i])
}
return value.length > length ? value.splice(length) : value
// prettier-ignore
return value.length > length
? value.slice(0, length)
: value
}
function TUnion(schema: Types.TUnion, references: Types.TSchema[], value: unknown): any {
for (const inner of schema.anyOf) {
Expand Down
7 changes: 7 additions & 0 deletions test/runtime/value/clean/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,10 @@ import './null'
import './object'
import './promise'
import './record'
import './recursive'
import './ref'
import './regexp'
import './string'
import './symbol'
import './template-literal'
import './tuple'
28 changes: 26 additions & 2 deletions test/runtime/value/clean/record.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,38 @@ describe('value/clean/Record', () => {
const T = Type.Record(Type.Number(), Type.String(), {
additionalProperties: Type.Boolean(),
})
const R = Value.Clean(T, {})
const R = Value.Clean(T, { a: null })
Assert.IsEqual(R, {})
})
it('Should clean additional properties discard 3', () => {
const T = Type.Record(Type.Number(), Type.String(), {
additionalProperties: Type.Boolean(),
})
const R = Value.Clean(T, { 0: null })
const R = Value.Clean(T, { a: null, 0: null })
Assert.IsEqual(R, { 0: null })
})
// ----------------------------------------------------------------
// Additional Properties Keep
// ----------------------------------------------------------------
it('Should clean additional properties keep 1', () => {
const T = Type.Record(Type.Number(), Type.String(), {
additionalProperties: Type.Boolean(),
})
const R = Value.Clean(T, null)
Assert.IsEqual(R, null)
})
it('Should clean additional properties keep 2', () => {
const T = Type.Record(Type.Number(), Type.String(), {
additionalProperties: Type.Boolean(),
})
const R = Value.Clean(T, { a: true })
Assert.IsEqual(R, { a: true })
})
it('Should clean additional properties keep 3', () => {
const T = Type.Record(Type.Number(), Type.String(), {
additionalProperties: Type.Boolean(),
})
const R = Value.Clean(T, { a: true, 0: null })
Assert.IsEqual(R, { a: true, 0: null })
})
})
112 changes: 112 additions & 0 deletions test/runtime/value/clean/recursive.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
import { Value } from '@sinclair/typebox/value'
import { Type } from '@sinclair/typebox'
import { Assert } from '../../assert/index'

describe('value/clean/Recursive', () => {
// ----------------------------------------------------------------
// Clean
// ----------------------------------------------------------------
it('Should clean 1', () => {
const T = Type.Recursive((This) =>
Type.Object({
id: Type.String(),
nodes: Type.Array(This),
}),
)
const R = Value.Clean(T, null)
Assert.IsEqual(R, null)
})
it('Should clean 2', () => {
const T = Type.Recursive((This) =>
Type.Object({
id: Type.String(),
nodes: Type.Array(This),
}),
)
const R = Value.Clean(T, { id: null })
Assert.IsEqual(R, { id: null })
})
it('Should clean 3', () => {
const T = Type.Recursive((This) =>
Type.Object({
id: Type.String(),
nodes: Type.Array(This),
}),
)
const R = Value.Clean(T, { id: null, nodes: null })
Assert.IsEqual(R, { id: null, nodes: null })
})
it('Should clean 4', () => {
const T = Type.Recursive((This) =>
Type.Object({
id: Type.String(),
nodes: Type.Array(This),
}),
)
const R = Value.Clean(T, { id: null, nodes: [] })
Assert.IsEqual(R, { id: null, nodes: [] })
})
it('Should clean 5', () => {
const T = Type.Recursive((This) =>
Type.Object({
id: Type.String(),
nodes: Type.Array(This),
}),
)
const R = Value.Clean(T, { id: null, nodes: [{ id: null }] })
Assert.IsEqual(R, { id: null, nodes: [{ id: null }] })
})
// ----------------------------------------------------------------
// Clean Discard
// ----------------------------------------------------------------
it('Should clean discard 1', () => {
const T = Type.Recursive((This) =>
Type.Object({
id: Type.String(),
nodes: Type.Array(This),
}),
)
const R = Value.Clean(T, null)
Assert.IsEqual(R, null)
})
it('Should clean discard 2', () => {
const T = Type.Recursive((This) =>
Type.Object({
id: Type.String(),
nodes: Type.Array(This),
}),
)
const R = Value.Clean(T, { u: null, id: null })
Assert.IsEqual(R, { id: null })
})
it('Should clean discard 3', () => {
const T = Type.Recursive((This) =>
Type.Object({
id: Type.String(),
nodes: Type.Array(This),
}),
)
const R = Value.Clean(T, { u: null, id: null, nodes: null })
Assert.IsEqual(R, { id: null, nodes: null })
})
it('Should clean discard 4', () => {
const T = Type.Recursive((This) =>
Type.Object({
id: Type.String(),
nodes: Type.Array(This),
}),
)
const R = Value.Clean(T, { u: null, id: null, nodes: [] })
Assert.IsEqual(R, { id: null, nodes: [] })
})
it('Should clean discard 5', () => {
const T = Type.Recursive((This) =>
Type.Object({
id: Type.String(),
nodes: Type.Array(This),
}),
)
const R = Value.Clean(T, { u: null, id: null, nodes: [{ u: null, id: null }] })
Assert.IsEqual(R, { id: null, nodes: [{ id: null }] })
})
})
78 changes: 78 additions & 0 deletions test/runtime/value/clean/ref.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import { Value } from '@sinclair/typebox/value'
import { Type } from '@sinclair/typebox'
import { Assert } from '../../assert/index'

describe('value/clean/Ref', () => {
// ----------------------------------------------------------------
// Clean
// ----------------------------------------------------------------
it('Should clean 1', () => {
const A = Type.Object(
{
x: Type.Number(),
},
{ $id: 'A' },
)
const T = Type.Ref('A')
const R = Value.Clean(T, [A], null)
Assert.IsEqual(R, null)
})
it('Should clean 2', () => {
const A = Type.Object(
{
x: Type.Number(),
},
{ $id: 'A' },
)
const T = Type.Ref('A')
const R = Value.Clean(T, [A], {})
Assert.IsEqual(R, {})
})
it('Should clean 3', () => {
const A = Type.Object(
{
x: Type.Number(),
},
{ $id: 'A' },
)
const T = Type.Ref('A')
const R = Value.Clean(T, [A], { x: null })
Assert.IsEqual(R, { x: null })
})
// ----------------------------------------------------------------
// Clean Discard
// ----------------------------------------------------------------
it('Should clean discard 1', () => {
const A = Type.Object(
{
x: Type.Number(),
},
{ $id: 'A' },
)
const T = Type.Ref('A')
const R = Value.Clean(T, [A], null)
Assert.IsEqual(R, null)
})
it('Should clean discard 2', () => {
const A = Type.Object(
{
x: Type.Number(),
},
{ $id: 'A' },
)
const T = Type.Ref('A')
const R = Value.Clean(T, [A], { a: null })
Assert.IsEqual(R, {})
})
it('Should clean discard 3', () => {
const A = Type.Object(
{
x: Type.Number(),
},
{ $id: 'A' },
)
const T = Type.Ref('A')
const R = Value.Clean(T, [A], { a: null, x: null })
Assert.IsEqual(R, { x: null })
})
})
11 changes: 11 additions & 0 deletions test/runtime/value/clean/regexp.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/RegExp', () => {
it('Should clean 1', () => {
const T = Type.RegExp('')
const R = Value.Clean(T, null)
Assert.IsEqual(R, null)
})
})
11 changes: 11 additions & 0 deletions test/runtime/value/clean/string.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/String', () => {
it('Should clean 1', () => {
const T = Type.String()
const R = Value.Clean(T, null)
Assert.IsEqual(R, null)
})
})
11 changes: 11 additions & 0 deletions test/runtime/value/clean/symbol.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/Symbol', () => {
it('Should clean 1', () => {
const T = Type.Symbol()
const R = Value.Clean(T, null)
Assert.IsEqual(R, null)
})
})
11 changes: 11 additions & 0 deletions test/runtime/value/clean/template-literal.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/TemplateLiteral', () => {
it('Should clean 1', () => {
const T = Type.TemplateLiteral('')
const R = Value.Clean(T, null)
Assert.IsEqual(R, null)
})
})
Loading

0 comments on commit c9be830

Please sign in to comment.