Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve custom matchers #3424

Draft
wants to merge 1 commit into
base: next-minor
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/khaki-apples-retire.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"effect": minor
"@effect/vitest": minor
---

Improve custom matchers
32 changes: 2 additions & 30 deletions packages/effect/src/Equal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import type { Equivalence } from "./Equivalence.js"
import * as Hash from "./Hash.js"
import { hasProperty } from "./Predicate.js"
import { structuralRegionState } from "./Utils.js"

/**
* @since 2.0.0
Expand Down Expand Up @@ -44,41 +43,14 @@ function compareBoth(self: unknown, that: unknown): boolean {
if (selfType === "object" || selfType === "function") {
if (self !== null && that !== null) {
if (isEqual(self) && isEqual(that)) {
if (Hash.hash(self) === Hash.hash(that) && self[symbol](that)) {
return true
} else {
return structuralRegionState.enabled && structuralRegionState.tester
? structuralRegionState.tester(self, that)
: false
}
return Hash.hash(self) === Hash.hash(that) && self[symbol](that)
} else if (self instanceof Date && that instanceof Date) {
return self.toISOString() === that.toISOString()
}
}
if (structuralRegionState.enabled) {
if (Array.isArray(self) && Array.isArray(that)) {
return self.length === that.length && self.every((v, i) => compareBoth(v, that[i]))
}
if (Object.getPrototypeOf(self) === Object.prototype && Object.getPrototypeOf(self) === Object.prototype) {
const keysSelf = Object.keys(self as any)
const keysThat = Object.keys(that as any)
if (keysSelf.length === keysThat.length) {
for (const key of keysSelf) {
// @ts-expect-error
if (!(key in that && compareBoth(self[key], that[key]))) {
return structuralRegionState.tester ? structuralRegionState.tester(self, that) : false
}
}
return true
}
}
return structuralRegionState.tester ? structuralRegionState.tester(self, that) : false
}
}

return structuralRegionState.enabled && structuralRegionState.tester
? structuralRegionState.tester(self, that)
: false
return false
}

/**
Expand Down
5 changes: 0 additions & 5 deletions packages/effect/src/Hash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import { pipe } from "./Function.js"
import { globalValue } from "./GlobalValue.js"
import { hasProperty } from "./Predicate.js"
import { structuralRegionState } from "./Utils.js"

/** @internal */
const randomHashCache = globalValue(
Expand All @@ -31,10 +30,6 @@ export interface Hash {
* @category hashing
*/
export const hash: <A>(self: A) => number = <A>(self: A) => {
if (structuralRegionState.enabled === true) {
return 0
}

switch (typeof self) {
case "number":
return number(self)
Expand Down
38 changes: 0 additions & 38 deletions packages/effect/src/Utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
* @since 2.0.0
*/
import { identity } from "./Function.js"
import { globalValue } from "./GlobalValue.js"
import type { Kind, TypeLambda } from "./HKT.js"
import { getBugErrorMessage } from "./internal/errors.js"
import { isNullable, isObject } from "./Predicate.js"
Expand Down Expand Up @@ -751,43 +750,6 @@ export function yieldWrapGet<T>(self: YieldWrap<T>): T {
throw new Error(getBugErrorMessage("yieldWrapGet"))
}

/**
* Note: this is an experimental feature made available to allow custom matchers in tests, not to be directly used yet in user code
*
* @since 3.1.1
* @status experimental
* @category modifiers
*/
export const structuralRegionState = globalValue(
"effect/Utils/isStructuralRegion",
(): { enabled: boolean; tester: ((a: unknown, b: unknown) => boolean) | undefined } => ({
enabled: false,
tester: undefined
})
)

/**
* Note: this is an experimental feature made available to allow custom matchers in tests, not to be directly used yet in user code
*
* @since 3.1.1
* @status experimental
* @category modifiers
*/
export const structuralRegion = <A>(body: () => A, tester?: (a: unknown, b: unknown) => boolean): A => {
const current = structuralRegionState.enabled
const currentTester = structuralRegionState.tester
structuralRegionState.enabled = true
if (tester) {
structuralRegionState.tester = tester
}
try {
return body()
} finally {
structuralRegionState.enabled = current
structuralRegionState.tester = currentTester
}
}

const tracingFunction = (name: string) => {
const wrap = {
[name]<A>(body: () => A) {
Expand Down
27 changes: 0 additions & 27 deletions packages/effect/test/Hash.test.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,9 @@
import * as Equal from "effect/Equal"
import { absurd, identity } from "effect/Function"
import * as Hash from "effect/Hash"
import * as HashSet from "effect/HashSet"
import * as Option from "effect/Option"
import * as Utils from "effect/Utils"
import { describe, expect, it } from "vitest"

describe("Hash", () => {
it("structural", () => {
const a = { foo: { bar: "ok", baz: { arr: [0, 1, 2] } } }
const b = { foo: { bar: "ok", baz: { arr: [0, 1, 2] } } }
expect(Hash.hash(a)).not.toBe(Hash.hash(b))
expect(Equal.equals(a, b)).toBe(false)
Utils.structuralRegion(() => {
expect(Hash.hash(a)).toBe(Hash.hash(b))
expect(Equal.equals(a, b)).toBe(true)
})
expect(Hash.hash(a)).not.toBe(Hash.hash(b))
expect(Equal.equals(a, b)).toBe(false)
})
it("structural cached", () => {
const a = Option.some({ foo: { bar: "ok", baz: { arr: [0, 1, 2] } } })
const b = Option.some({ foo: { bar: "ok", baz: { arr: [0, 1, 2] } } })
expect(Hash.hash(a)).not.toBe(Hash.hash(b))
expect(Equal.equals(a, b)).toBe(false)
Utils.structuralRegion(() => {
expect(Hash.hash(a)).toBe(Hash.hash(b))
expect(Equal.equals(a, b)).toBe(true)
})
expect(Hash.hash(a)).not.toBe(Hash.hash(b))
expect(Equal.equals(a, b)).toBe(false)
})
it("exports", () => {
expect(Hash.string).exist
expect(Hash.structureKeys).exist
Expand Down
32 changes: 23 additions & 9 deletions packages/vitest/src/internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,20 @@
*/
import type { Tester, TesterContext } from "@vitest/expect"
import * as Cause from "effect/Cause"
import * as Chunk from "effect/Chunk"
import * as Duration from "effect/Duration"
import * as Effect from "effect/Effect"
import * as Equal from "effect/Equal"
import * as Either from "effect/Either"
import * as Exit from "effect/Exit"
import { flow, identity, pipe } from "effect/Function"
import * as HashSet from "effect/HashSet"
import * as Layer from "effect/Layer"
import * as Logger from "effect/Logger"
import * as Option from "effect/Option"
import * as Schedule from "effect/Schedule"
import type * as Scope from "effect/Scope"
import * as TestEnvironment from "effect/TestContext"
import type * as TestServices from "effect/TestServices"
import * as Utils from "effect/Utils"
import * as V from "vitest"
import type * as Vitest from "./index.js"

Expand All @@ -41,14 +43,26 @@ const TestEnv = TestEnvironment.TestContext.pipe(
)

/** @internal */
function customTester(this: TesterContext, a: unknown, b: unknown, customTesters: Array<Tester>) {
if (!Equal.isEqual(a) || !Equal.isEqual(b)) {
return undefined
function customTester(this: TesterContext, _a: unknown, _b: unknown, _customTesters: Array<Tester>) {
if (Chunk.isChunk(_a) && Chunk.isChunk(_b)) {
return this.equals(Array.from(_a), Array.from(_b), _customTesters)
}
return Utils.structuralRegion(
() => Equal.equals(a, b),
(x, y) => this.equals(x, y, customTesters.filter((t) => t !== customTester))
)
if (Option.isOption(_a) && Option.isOption(_b)) {
return _a._tag === _b._tag && (_a._tag === "None" || this.equals(_a.value, (_b as any).value, _customTesters))
}
if (Either.isEither(_a) && Either.isEither(_b)) {
return (Either.isLeft(_a) && Either.isLeft(_b) && this.equals(_a.left, _b.left, _customTesters)) ||
(Either.isRight(_a) && Either.isRight(_b) && this.equals(_a.right, _b.right, _customTesters))
}
if (Cause.isCause(_a) && Cause.isCause(_b)) {
return this.equals(Cause.failures(_a), Cause.failures(_b), _customTesters) &&
this.equals(Cause.defects(_a), Cause.defects(_b), _customTesters) &&
this.equals(Cause.interruptors(_a), Cause.interruptors(_b), _customTesters)
}
if (HashSet.isHashSet(_a) && HashSet.isHashSet(_b)) {
return this.equals(new Set(_a), new Set(_b), _customTesters)
}
return undefined
}

/** @internal */
Expand Down
Loading