Skip to content

Commit

Permalink
add Sample (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
IMax153 committed Jan 5, 2023
1 parent 3d501b7 commit 69f68af
Show file tree
Hide file tree
Showing 15 changed files with 725 additions and 80 deletions.
5 changes: 5 additions & 0 deletions .changeset/funny-countries-refuse.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@effect/test": patch
---

add Sample
2 changes: 1 addition & 1 deletion docs-ts.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"exclude": ["src/internal/**/*.ts", "src/**/*.d.ts"],
"exclude": ["src/internal/**/*.ts"],
"theme": "mikearnaldi/just-the-docs"
}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@
}
},
"dependencies": {
"@effect/io": "^0.0.52",
"@effect/io": "~0.0.52",
"@effect/stream": "~0.0.9",
"@fp-ts/core": "~0.0.11",
"@fp-ts/data": "~0.0.36"
},
Expand Down
12 changes: 11 additions & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 7 additions & 6 deletions src/Annotations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,15 +76,15 @@ export declare namespace Annotations {
* @since 1.0.0
* @category environment
*/
export const Tag: Context.Tag<Annotations> = annotations.Tag
export const Tag: Context.Tag<Annotations> = annotations.Tag as any

/**
* A `Layer` containing an instance of `Annotations`.
*
* @since 1.0.0
* @category environment
*/
export const live: Layer.Layer<never, never, Annotations> = annotations.live
export const live: Layer.Layer<never, never, Annotations> = annotations.live as any

/**
* Returns `true` if the specified value is an `Annotations`, `false`
Expand All @@ -93,7 +93,7 @@ export const live: Layer.Layer<never, never, Annotations> = annotations.live
* @since 1.0.0
* @category refinements
*/
export const isAnnotations: (u: unknown) => u is Annotations = annotations.isAnnotations
export const isAnnotations: (u: unknown) => u is Annotations = annotations.isAnnotations as any

/**
* Accesses an `Annotations` instance in the environment and retrieves the
Expand All @@ -103,7 +103,8 @@ export const isAnnotations: (u: unknown) => u is Annotations = annotations.isAnn
* @since 1.0.0
* @category getters
*/
export const get: <A>(key: TestAnnotation.TestAnnotation<A>) => Effect.Effect<Annotations, never, A> = annotations.get
export const get: <A>(key: TestAnnotation.TestAnnotation<A>) => Effect.Effect<Annotations, never, A> = annotations
.get as any

/**
* Accesses an `Annotations` instance in the environment and appends the
Expand All @@ -116,7 +117,7 @@ export const get: <A>(key: TestAnnotation.TestAnnotation<A>) => Effect.Effect<An
export const annotate: <A>(
key: TestAnnotation.TestAnnotation<A>,
value: A
) => Effect.Effect<Annotations, never, void> = annotations.annotate
) => Effect.Effect<Annotations, never, void> = annotations.annotate as any

/**
* Returns the set of all fibers in this test.
Expand All @@ -129,4 +130,4 @@ export const supervisedFibers: () => Effect.Effect<
Annotations,
never,
SortedSet.SortedSet<Fiber.RuntimeFiber<unknown, unknown>>
> = annotations.supervisedFibers
> = annotations.supervisedFibers as any
164 changes: 164 additions & 0 deletions src/Sample.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
/**
* @since 1.0.0
*/
import type * as Effect from "@effect/io/Effect"
import type * as Stream from "@effect/stream/Stream"
import * as internal from "@effect/test/internal/sample"
import type * as Option from "@fp-ts/data/Option"
import type { Predicate } from "@fp-ts/data/Predicate"

/**
* @since 1.0.0
* @category symbols
*/
export const SampleTypeId: unique symbol = internal.SampleTypeId

/**
* @since 1.0.0
* @category symbols
*/
export type SampleTypeId = typeof SampleTypeId

/**
* A sample is a single observation from a random variable, together with a tree
* of "shrinkings" used for minimization of "large" failures.
*
* @since 1.0.0
* @category models
*/
export interface Sample<R, A> extends Sample.Variance<R, A> {
readonly value: A
readonly shrink: Stream.Stream<R, never, Option.Option<Sample<R, A>>>
}

/**
* @since 1.0.0
*/
export declare namespace Sample {
/**
* @since 1.0.0
* @category models
*/
export interface Variance<R, A> {
readonly [SampleTypeId]: {
readonly _R: (_: never) => R
readonly _A: (_: never) => A
}
}
}

/**
* Filters this sample by replacing it with its shrink tree if the value does
* not meet the specified predicate and recursively filtering the shrink tree.
*
* @since 1.0.0
* @category filtering
*/
export const filter: <A>(
predicate: Predicate<A>
) => <R>(self: Sample<R, A>) => Stream.Stream<R, never, Option.Option<Sample<R, A>>> = internal.filter

/**
* @since 1.0.0
* @category sequencing
*/
export const flatMap: <A, R2, A2>(f: (a: A) => Sample<R2, A2>) => <R>(self: Sample<R, A>) => Sample<R2 | R, A2> =
internal.flatMap

/**
* Executes the specified effectual function for each element of the sample.
*
* @macro traced
* @since 1.0.0
* @category traversing
*/
export const forEach: <A, R2, A2>(
f: (a: A) => Effect.Effect<R2, never, A2>
) => <R>(self: Sample<R, A>) => Effect.Effect<R2 | R, never, Sample<R2 | R, A2>> = internal.forEach

/**
* @since 1.0.0
* @category mapping
*/
export const map: <A, B>(f: (a: A) => B) => <R>(self: Sample<R, A>) => Sample<R, B> = internal.map

/**
* Constructs a `Sample` without shrinking.
*
* @since 1.0.0
* @category constructors
*/
export const noShrink: <A>(value: A) => Sample<never, A> = internal.noShrink

/**
* @since 1.0.0
* @category constructors
*/
export const shrinkBigInt: (smallest: bigint) => (value: bigint) => Sample<never, bigint> = internal.shrinkBigInt

/**
* @since 1.0.0
* @category constructors
*/
export const shrinkFractional: (smallest: number) => (value: number) => Sample<never, number> =
internal.shrinkFractional

/**
* @since 1.0.0
* @category constructors
*/
export const shrinkIntegral: (smallest: number) => (value: number) => Sample<never, number> = internal.shrinkIntegral

/**
* Converts the shrink tree into a stream of shrinkings by recursively
* searching the shrink tree, using the specified function to determine
* whether a value is a failure. The resulting stream will contain all values
* explored, regardless of whether they are successes or failures.
*
* @since 1.0.0
* @category utils
*/
export const shrinkSearch: <A>(predicate: Predicate<A>) => <R>(self: Sample<R, A>) => Stream.Stream<R, never, A> =
internal.shrinkSearch

/**
* Unfolds a `Sample` from an initial value and a continuation function.
*
* @since 1.0.0
* @category constructors
*/
export const unfold: <S, A, R>(start: S, f: (s: S) => readonly [A, Stream.Stream<R, never, S>]) => Sample<R, A> =
internal.unfold

/**
* Composes this sample with the specified sample to create a cartesian
* product of values and shrinkings.
*
* @since 1.0.0
* @category zipping
*/
export const zip: <R2, A2>(that: Sample<R2, A2>) => <R, A>(self: Sample<R, A>) => Sample<R2 | R, readonly [A, A2]> =
internal.zip

/**
* Composes this sample with the specified sample to create a cartesian
* product of values and shrinkings.
*
* @since 1.0.0
* @category zipping
*/
export const zipFlatten: <R2, A2>(
that: Sample<R2, A2>
) => <R, A extends ReadonlyArray<any>>(self: Sample<R, A>) => Sample<R2 | R, readonly [...A, A2]> = internal.zipFlatten

/**
* Composes this sample with the specified sample to create a cartesian
* product of values and shrinkings with the specified function.
*
* @since 1.0.0
* @category zipping
*/
export const zipWith: <R2, A2, A, A3>(
that: Sample<R2, A2>,
f: (a: A, a2: A2) => A3
) => <R>(self: Sample<R, A>) => Sample<R2 | R, A3> = internal.zipWith
18 changes: 8 additions & 10 deletions src/TestAnnotation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* @since 1.0.0
*/
import type * as Fiber from "@effect/io/Fiber"
import * as testAnnotation from "@effect/io/internal/testing/testAnnotation"
import type * as Chunk from "@fp-ts/data/Chunk"
import type * as Context from "@fp-ts/data/Context"
import type * as Either from "@fp-ts/data/Either"
Expand All @@ -10,9 +11,6 @@ import type * as HashSet from "@fp-ts/data/HashSet"
import type * as MutableRef from "@fp-ts/data/MutableRef"
import type * as SortedSet from "@fp-ts/data/SortedSet"

/** @internal */
import * as testAnnotation from "@effect/io/internal/testing/testAnnotation"

/**
* @since 1.0.0
* @category symbols
Expand Down Expand Up @@ -71,7 +69,7 @@ export declare namespace TestAnnotation {
* @since 1.0.0
* @category refinements
*/
export const isTestAnnotation: (u: unknown) => u is TestAnnotation<unknown> = testAnnotation.isTestAnnotation
export const isTestAnnotation: (u: unknown) => u is TestAnnotation<unknown> = testAnnotation.isTestAnnotation as any

/**
* Constructs a new `TestAnnotation`.
Expand All @@ -84,7 +82,7 @@ export const make: <A>(
tag: Context.Tag<A>,
initial: A,
combine: (a: A, b: A) => A
) => TestAnnotation<A> = testAnnotation.make
) => TestAnnotation<A> = testAnnotation.make as any

/**
* @since 1.0.0
Expand All @@ -95,36 +93,36 @@ export const fibers: TestAnnotation<
number,
Chunk.Chunk<MutableRef.MutableRef<SortedSet.SortedSet<Fiber.RuntimeFiber<unknown, unknown>>>>
>
> = testAnnotation.fibers
> = testAnnotation.fibers as any

/**
* An annotation which counts ignored tests.
*
* @since 1.0.0
* @category annotations
*/
export const ignored: TestAnnotation<number> = testAnnotation.ignored
export const ignored: TestAnnotation<number> = testAnnotation.ignored as any

/**
* An annotation which counts repeated tests.
*
* @since 1.0.0
* @category annotations
*/
export const repeated: TestAnnotation<number> = testAnnotation.repeated
export const repeated: TestAnnotation<number> = testAnnotation.repeated as any

/**
* An annotation which counts retried tests.
*
* @since 1.0.0
* @category annotations
*/
export const retried: TestAnnotation<number> = testAnnotation.retried
export const retried: TestAnnotation<number> = testAnnotation.retried as any

/**
* An annotation which tags tests with strings.
*
* @since 1.0.0
* @category annotations
*/
export const tagged: TestAnnotation<HashSet.HashSet<string>> = testAnnotation.tagged
export const tagged: TestAnnotation<HashSet.HashSet<string>> = testAnnotation.tagged as any
Loading

0 comments on commit 69f68af

Please sign in to comment.