-
-
Notifications
You must be signed in to change notification settings - Fork 230
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
moved prop testing to dedicated module
- Loading branch information
1 parent
8a69754
commit b8cca68
Showing
9 changed files
with
188 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
--- | ||
"@effect/vitest": patch | ||
--- | ||
|
||
Adds property testing to @effect/vitest | ||
|
||
```ts | ||
import { Schema } from "@effect/schema" | ||
import { prop } from "@effect/vitest/Schema" | ||
|
||
const realNumber = Schema.Finite.pipe(Schema.nonNaN()) | ||
|
||
prop("symmetry", [realNumber, realNumber], ([a, b]) => a + b === b + a) | ||
|
||
prop.effect("symmetry", [realNumber, realNumber], ([a, b]) => | ||
Effect.gen(function* () { | ||
yield* Effect.void | ||
return a + b === b + a | ||
}) | ||
) | ||
|
||
prop.scoped( | ||
"should detect the substring", | ||
{ a: Schema.String, b: Schema.String, c: Schema.String }, | ||
({ a, b, c }) => | ||
Effect.gen(function* () { | ||
yield* Effect.scope | ||
return (a + b + c).includes(b) | ||
}) | ||
) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
import * as Arbitrary from "@effect/schema/Arbitrary" | ||
import type * as Schema from "@effect/schema/Schema" | ||
import * as Effect from "effect/Effect" | ||
import type * as Scope from "effect/Scope" | ||
import type { TestServices } from "effect/TestServices" | ||
import fc from "fast-check" | ||
import * as V from "vitest" | ||
import * as internal from "./internal.js" | ||
|
||
/** | ||
* @since 1.0.0 | ||
*/ | ||
type SchemaObj<A, E, R> = Array<Schema.Schema<A, E, R>> | { [K in string]: Schema.Schema<A, E, R> } | ||
|
||
/** | ||
* @since 1.0.0 | ||
*/ | ||
export type TestFn<S extends SchemaObj<any, any, any>, R> = ( | ||
schemas: { [K in keyof S]: Schema.Schema.Type<S[K]> }, | ||
ctx: V.TaskContext<V.RunnerTestCase<{}>> & V.TestContext | ||
) => R | ||
|
||
/** | ||
* @internal | ||
*/ | ||
const makePropTester = () => { | ||
const f = <S extends SchemaObj<any, any, any>>( | ||
name: string, | ||
schemaObj: S, | ||
fn: TestFn<S, boolean | void | Promise<boolean | void>>, | ||
timeout?: number | V.TestOptions | ||
) => { | ||
if (Array.isArray(schemaObj)) { | ||
const arbs = schemaObj.map((schema) => Arbitrary.make(schema)) | ||
return V.it( | ||
name, | ||
// @ts-ignore | ||
(ctx) => fc.assert(fc.asyncProperty(...arbs, (...as) => fn(as as any, ctx))), | ||
timeout | ||
) | ||
} | ||
|
||
const arbs = fc.record( | ||
Object.keys(schemaObj).reduce(function(result, key) { | ||
result[key] = Arbitrary.make(schemaObj[key]) | ||
return result | ||
}, {} as Record<string, fc.Arbitrary<any>>) | ||
) | ||
|
||
return V.it( | ||
name, | ||
// @ts-ignore | ||
(ctx) => fc.assert(fc.asyncProperty(arbs, (...as) => fn(as[0] as any, ctx))), | ||
timeout | ||
) | ||
} | ||
|
||
const effect = <S extends SchemaObj<any, any, any>>( | ||
name: string, | ||
schemaObj: S, | ||
fn: TestFn<S, Effect.Effect<boolean | void, never, TestServices>>, | ||
timeout?: number | V.TestOptions | ||
) => | ||
f( | ||
name, | ||
schemaObj, | ||
(obj, ctx) => Effect.runPromise(fn(obj, ctx).pipe(Effect.provide(internal.TestEnv))), | ||
timeout | ||
) | ||
|
||
const live = <S extends SchemaObj<any, any, any>>( | ||
name: string, | ||
schemaObj: S, | ||
fn: TestFn<S, Effect.Effect<boolean | void>>, | ||
timeout?: number | V.TestOptions | ||
) => | ||
f( | ||
name, | ||
schemaObj, | ||
(obj, ctx) => Effect.runPromise(fn(obj, ctx)), | ||
timeout | ||
) | ||
|
||
const scoped = <S extends SchemaObj<any, any, any>>( | ||
name: string, | ||
schemaObj: S, | ||
fn: TestFn<S, Effect.Effect<boolean | void, never, Scope.Scope | TestServices>>, | ||
timeout?: number | V.TestOptions | ||
) => | ||
f( | ||
name, | ||
schemaObj, | ||
(obj, ctx) => Effect.runPromise(fn(obj, ctx).pipe(Effect.scoped, Effect.provide(internal.TestEnv))), | ||
timeout | ||
) | ||
|
||
const scopedLive = <S extends SchemaObj<any, any, any>>( | ||
name: string, | ||
schemaObj: S, | ||
fn: TestFn<S, Effect.Effect<boolean | void, never, Scope.Scope>>, | ||
timeout?: number | V.TestOptions | ||
) => | ||
f( | ||
name, | ||
schemaObj, | ||
(obj, ctx) => Effect.runPromise(fn(obj, ctx).pipe(Effect.scoped)), | ||
timeout | ||
) | ||
|
||
return Object.assign(f, { | ||
effect, | ||
scoped, | ||
live, | ||
scopedLive | ||
}) | ||
} | ||
|
||
export const prop = makePropTester() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { Schema } from "@effect/schema" | ||
import { Effect } from "effect" | ||
import { prop } from "@effect/vitest/Schema" | ||
|
||
// property testing | ||
|
||
const realNumber = Schema.Finite.pipe(Schema.nonNaN()) | ||
|
||
prop("symmetry", [realNumber, realNumber], ([a, b]) => a + b === b + a) | ||
|
||
prop.effect("symmetry", [realNumber, realNumber], ([a, b]) => | ||
Effect.gen(function*() { | ||
yield* Effect.void | ||
return a + b === b + a | ||
})) | ||
|
||
prop.scoped( | ||
"should detect the substring", | ||
{ a: Schema.String, b: Schema.String, c: Schema.String }, | ||
({ a, b, c }) => | ||
Effect.gen(function*() { | ||
yield* Effect.scope | ||
return (a + b + c).includes(b) | ||
}) | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters