Skip to content

Commit

Permalink
Internal: Format tests
Browse files Browse the repository at this point in the history
  • Loading branch information
DZakh committed Apr 30, 2024
1 parent 9d65a0c commit f818c30
Show file tree
Hide file tree
Showing 51 changed files with 1,135 additions and 715 deletions.
26 changes: 16 additions & 10 deletions packages/tests/src/core/S_Array_max_test.res
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,14 @@ test("Successfully parses valid data", t => {
test("Fails to parse invalid data", t => {
let schema = S.array(S.int)->S.Array.max(1)

t->U.assertErrorResult([1, 2, 3, 4]->S.parseAnyWith(schema), {
code: OperationFailed("Array must be 1 or fewer items long"),
operation: Parsing,
path: S.Path.empty,
})
t->U.assertErrorResult(
[1, 2, 3, 4]->S.parseAnyWith(schema),
{
code: OperationFailed("Array must be 1 or fewer items long"),
operation: Parsing,
path: S.Path.empty,
},
)
})

test("Successfully serializes valid value", t => {
Expand All @@ -27,11 +30,14 @@ test("Successfully serializes valid value", t => {
test("Fails to serialize invalid value", t => {
let schema = S.array(S.int)->S.Array.max(1)

t->U.assertErrorResult([1, 2, 3, 4]->S.serializeToUnknownWith(schema), {
code: OperationFailed("Array must be 1 or fewer items long"),
operation: Serializing,
path: S.Path.empty,
})
t->U.assertErrorResult(
[1, 2, 3, 4]->S.serializeToUnknownWith(schema),
{
code: OperationFailed("Array must be 1 or fewer items long"),
operation: Serializing,
path: S.Path.empty,
},
)
})

test("Returns custom error message", t => {
Expand Down
26 changes: 16 additions & 10 deletions packages/tests/src/core/S_Array_min_test.res
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,14 @@ test("Successfully parses valid data", t => {
test("Fails to parse invalid data", t => {
let schema = S.array(S.int)->S.Array.min(1)

t->U.assertErrorResult([]->S.parseAnyWith(schema), {
code: OperationFailed("Array must be 1 or more items long"),
operation: Parsing,
path: S.Path.empty,
})
t->U.assertErrorResult(
[]->S.parseAnyWith(schema),
{
code: OperationFailed("Array must be 1 or more items long"),
operation: Parsing,
path: S.Path.empty,
},
)
})

test("Successfully serializes valid value", t => {
Expand All @@ -27,11 +30,14 @@ test("Successfully serializes valid value", t => {
test("Fails to serialize invalid value", t => {
let schema = S.array(S.int)->S.Array.min(1)

t->U.assertErrorResult([]->S.serializeToUnknownWith(schema), {
code: OperationFailed("Array must be 1 or more items long"),
operation: Serializing,
path: S.Path.empty,
})
t->U.assertErrorResult(
[]->S.serializeToUnknownWith(schema),
{
code: OperationFailed("Array must be 1 or more items long"),
operation: Serializing,
path: S.Path.empty,
},
)
})

test("Returns custom error message", t => {
Expand Down
26 changes: 16 additions & 10 deletions packages/tests/src/core/S_Float_max_test.res
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,14 @@ test("Successfully parses valid data", t => {
test("Fails to parse invalid data", t => {
let schema = S.float->S.Float.max(1.)

t->U.assertErrorResult(1234->S.parseAnyWith(schema), {
code: OperationFailed("Number must be lower than or equal to 1"),
operation: Parsing,
path: S.Path.empty,
})
t->U.assertErrorResult(
1234->S.parseAnyWith(schema),
{
code: OperationFailed("Number must be lower than or equal to 1"),
operation: Parsing,
path: S.Path.empty,
},
)
})

test("Successfully serializes valid value", t => {
Expand All @@ -27,11 +30,14 @@ test("Successfully serializes valid value", t => {
test("Fails to serialize invalid value", t => {
let schema = S.float->S.Float.max(1.)

t->U.assertErrorResult(1234.->S.serializeToUnknownWith(schema), {
code: OperationFailed("Number must be lower than or equal to 1"),
operation: Serializing,
path: S.Path.empty,
})
t->U.assertErrorResult(
1234.->S.serializeToUnknownWith(schema),
{
code: OperationFailed("Number must be lower than or equal to 1"),
operation: Serializing,
path: S.Path.empty,
},
)
})

test("Returns custom error message", t => {
Expand Down
26 changes: 16 additions & 10 deletions packages/tests/src/core/S_Float_min_test.res
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,14 @@ test("Successfully parses valid data", t => {
test("Fails to parse invalid data", t => {
let schema = S.float->S.Float.min(1.)

t->U.assertErrorResult(0->S.parseAnyWith(schema), {
code: OperationFailed("Number must be greater than or equal to 1"),
operation: Parsing,
path: S.Path.empty,
})
t->U.assertErrorResult(
0->S.parseAnyWith(schema),
{
code: OperationFailed("Number must be greater than or equal to 1"),
operation: Parsing,
path: S.Path.empty,
},
)
})

test("Successfully serializes valid value", t => {
Expand All @@ -27,11 +30,14 @@ test("Successfully serializes valid value", t => {
test("Fails to serialize invalid value", t => {
let schema = S.float->S.Float.min(1.)

t->U.assertErrorResult(0.->S.serializeToUnknownWith(schema), {
code: OperationFailed("Number must be greater than or equal to 1"),
operation: Serializing,
path: S.Path.empty,
})
t->U.assertErrorResult(
0.->S.serializeToUnknownWith(schema),
{
code: OperationFailed("Number must be greater than or equal to 1"),
operation: Serializing,
path: S.Path.empty,
},
)
})

test("Returns custom error message", t => {
Expand Down
26 changes: 16 additions & 10 deletions packages/tests/src/core/S_Int_max_test.res
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,14 @@ test("Successfully parses valid data", t => {
test("Fails to parse invalid data", t => {
let schema = S.int->S.Int.max(1)

t->U.assertErrorResult(1234->S.parseAnyWith(schema), {
code: OperationFailed("Number must be lower than or equal to 1"),
operation: Parsing,
path: S.Path.empty,
})
t->U.assertErrorResult(
1234->S.parseAnyWith(schema),
{
code: OperationFailed("Number must be lower than or equal to 1"),
operation: Parsing,
path: S.Path.empty,
},
)
})

test("Successfully serializes valid value", t => {
Expand All @@ -27,11 +30,14 @@ test("Successfully serializes valid value", t => {
test("Fails to serialize invalid value", t => {
let schema = S.int->S.Int.max(1)

t->U.assertErrorResult(1234->S.serializeToUnknownWith(schema), {
code: OperationFailed("Number must be lower than or equal to 1"),
operation: Serializing,
path: S.Path.empty,
})
t->U.assertErrorResult(
1234->S.serializeToUnknownWith(schema),
{
code: OperationFailed("Number must be lower than or equal to 1"),
operation: Serializing,
path: S.Path.empty,
},
)
})

test("Returns custom error message", t => {
Expand Down
26 changes: 16 additions & 10 deletions packages/tests/src/core/S_Int_min_test.res
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,14 @@ test("Successfully parses valid data", t => {
test("Fails to parse invalid data", t => {
let schema = S.int->S.Int.min(1)

t->U.assertErrorResult(0->S.parseAnyWith(schema), {
code: OperationFailed("Number must be greater than or equal to 1"),
operation: Parsing,
path: S.Path.empty,
})
t->U.assertErrorResult(
0->S.parseAnyWith(schema),
{
code: OperationFailed("Number must be greater than or equal to 1"),
operation: Parsing,
path: S.Path.empty,
},
)
})

test("Successfully serializes valid value", t => {
Expand All @@ -27,11 +30,14 @@ test("Successfully serializes valid value", t => {
test("Fails to serialize invalid value", t => {
let schema = S.int->S.Int.min(1)

t->U.assertErrorResult(0->S.serializeToUnknownWith(schema), {
code: OperationFailed("Number must be greater than or equal to 1"),
operation: Serializing,
path: S.Path.empty,
})
t->U.assertErrorResult(
0->S.serializeToUnknownWith(schema),
{
code: OperationFailed("Number must be greater than or equal to 1"),
operation: Serializing,
path: S.Path.empty,
},
)
})

test("Returns custom error message", t => {
Expand Down
5 changes: 4 additions & 1 deletion packages/tests/src/core/S_Int_port_test.res
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ test("Successfully serializes valid value", t => {
test("Fails to serialize invalid value", t => {
let schema = S.int->S.Int.port

t->U.assertErrorResult(-80->S.serializeToUnknownWith(schema), {code: OperationFailed("Invalid port"), operation: Serializing, path: S.Path.empty})
t->U.assertErrorResult(
-80->S.serializeToUnknownWith(schema),
{code: OperationFailed("Invalid port"), operation: Serializing, path: S.Path.empty},
)
})

test("Returns custom error message", t => {
Expand Down
13 changes: 8 additions & 5 deletions packages/tests/src/core/S_Option_getOrWith_test.res
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,14 @@ test("Successfully parses nested option with default value", t => {
test("Fails to parse data with default", t => {
let schema = S.bool->S.option->S.Option.getOrWith(() => false)

t->U.assertErrorResult(%raw(`"string"`)->S.parseAnyWith(schema), {
code: InvalidType({expected: schema->S.toUnknown, received: %raw(`"string"`)}),
operation: Parsing,
path: S.Path.empty,
})
t->U.assertErrorResult(
%raw(`"string"`)->S.parseAnyWith(schema),
{
code: InvalidType({expected: schema->S.toUnknown, received: %raw(`"string"`)}),
operation: Parsing,
path: S.Path.empty,
},
)
})

test("Successfully serializes schema with transformation", t => {
Expand Down
13 changes: 8 additions & 5 deletions packages/tests/src/core/S_Option_getOr_test.res
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,14 @@ test("Successfully serializes nested option with default value", t => {
test("Fails to parse data with default", t => {
let schema = S.bool->S.option->S.Option.getOr(false)

t->U.assertErrorResult(%raw(`"string"`)->S.parseAnyWith(schema), {
code: InvalidType({expected: schema->S.toUnknown, received: %raw(`"string"`)}),
operation: Parsing,
path: S.Path.empty,
})
t->U.assertErrorResult(
%raw(`"string"`)->S.parseAnyWith(schema),
{
code: InvalidType({expected: schema->S.toUnknown, received: %raw(`"string"`)}),
operation: Parsing,
path: S.Path.empty,
},
)
})

test("Successfully parses schema with transformation", t => {
Expand Down
5 changes: 4 additions & 1 deletion packages/tests/src/core/S_String_cuid_test.res
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ test("Successfully serializes valid value", t => {
test("Fails to serialize invalid value", t => {
let schema = S.string->S.String.cuid

t->U.assertErrorResult("cifjhdsfhsd-invalid-cuid"->S.serializeToUnknownWith(schema), {code: OperationFailed("Invalid CUID"), operation: Serializing, path: S.Path.empty})
t->U.assertErrorResult(
"cifjhdsfhsd-invalid-cuid"->S.serializeToUnknownWith(schema),
{code: OperationFailed("Invalid CUID"), operation: Serializing, path: S.Path.empty},
)
})

test("Returns custom error message", t => {
Expand Down
26 changes: 16 additions & 10 deletions packages/tests/src/core/S_String_datetime_test.res
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,27 @@ test("Successfully parses valid data", t => {
test("Fails to parse non UTC date string", t => {
let schema = S.string->S.String.datetime

t->U.assertErrorResult("Thu Apr 20 2023 10:45:48 GMT+0400"->S.parseAnyWith(schema), {
code: OperationFailed("Invalid datetime string! Must be UTC"),
operation: Parsing,
path: S.Path.empty,
})
t->U.assertErrorResult(
"Thu Apr 20 2023 10:45:48 GMT+0400"->S.parseAnyWith(schema),
{
code: OperationFailed("Invalid datetime string! Must be UTC"),
operation: Parsing,
path: S.Path.empty,
},
)
})

test("Fails to parse UTC date with timezone offset", t => {
let schema = S.string->S.String.datetime

t->U.assertErrorResult("2020-01-01T00:00:00+02:00"->S.parseAnyWith(schema), {
code: OperationFailed("Invalid datetime string! Must be UTC"),
operation: Parsing,
path: S.Path.empty,
})
t->U.assertErrorResult(
"2020-01-01T00:00:00+02:00"->S.parseAnyWith(schema),
{
code: OperationFailed("Invalid datetime string! Must be UTC"),
operation: Parsing,
path: S.Path.empty,
},
)
})

test("Uses custom message on failure", t => {
Expand Down
26 changes: 16 additions & 10 deletions packages/tests/src/core/S_String_email_test.res
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,14 @@ test("Successfully parses valid data", t => {
test("Fails to parse invalid data", t => {
let schema = S.string->S.String.email

t->U.assertErrorResult("dzakh.dev"->S.parseAnyWith(schema), {
code: OperationFailed("Invalid email address"),
operation: Parsing,
path: S.Path.empty,
})
t->U.assertErrorResult(
"dzakh.dev"->S.parseAnyWith(schema),
{
code: OperationFailed("Invalid email address"),
operation: Parsing,
path: S.Path.empty,
},
)
})

test("Successfully serializes valid value", t => {
Expand All @@ -29,11 +32,14 @@ test("Successfully serializes valid value", t => {
test("Fails to serialize invalid value", t => {
let schema = S.string->S.String.email

t->U.assertErrorResult("dzakh.dev"->S.serializeToUnknownWith(schema), {
code: OperationFailed("Invalid email address"),
operation: Serializing,
path: S.Path.empty,
})
t->U.assertErrorResult(
"dzakh.dev"->S.serializeToUnknownWith(schema),
{
code: OperationFailed("Invalid email address"),
operation: Serializing,
path: S.Path.empty,
},
)
})

test("Returns custom error message", t => {
Expand Down
Loading

1 comment on commit f818c30

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Benchmark

Benchmark suite Current: f818c30 Previous: 9fb2e69 Ratio
Parse string 818029550 ops/sec (±0.08%) 819064824 ops/sec (±0.11%) 1.00
Serialize string 816589929 ops/sec (±0.08%) 819779932 ops/sec (±0.08%) 1.00
Advanced object schema factory 432760 ops/sec (±0.43%) 431262 ops/sec (±0.44%) 1.00
Parse advanced object 46745805 ops/sec (±0.26%) 46671133 ops/sec (±0.42%) 1.00
Create and parse advanced object 33929 ops/sec (±1.18%) 34129 ops/sec (±0.58%) 1.01
Parse advanced strict object 22234000 ops/sec (±0.16%) 22262984 ops/sec (±0.24%) 1.00
Serialize advanced object 805751089 ops/sec (±0.22%) 807737293 ops/sec (±0.20%) 1.00

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.