Skip to content

Commit

Permalink
inline instantiations
Browse files Browse the repository at this point in the history
  • Loading branch information
ShawnMorreau committed May 1, 2024
1 parent c2a0140 commit 4f640c7
Show file tree
Hide file tree
Showing 20 changed files with 232 additions and 227 deletions.
2 changes: 1 addition & 1 deletion ark/attest/__tests__/assertions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as assert from "node:assert/strict"

const o = { ark: "type" }

specify(() => {
specify("type assertions", () => {
it("type parameter", () => {
attest<{ ark: string }>(o)
assert.throws(
Expand Down
1 change: 0 additions & 1 deletion ark/attest/__tests__/benchExpectedOutput.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { bench } from "@arktype/attest"
import { type } from "arktype"
import type { makeComplexType as externalmakeComplexType } from "./utils.js"

const fakeCallOptions = {
Expand Down
1 change: 1 addition & 0 deletions ark/attest/__tests__/benchTemplate.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { bench } from "@arktype/attest"
import { type } from "arktype"

Check warning on line 2 in ark/attest/__tests__/benchTemplate.ts

View workflow job for this annotation

GitHub Actions / core

'type' is defined but never used
import type { makeComplexType as externalmakeComplexType } from "./utils.js"

const fakeCallOptions = {
Expand Down
14 changes: 5 additions & 9 deletions ark/attest/__tests__/instantiations.test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
import { attest } from "@arktype/attest"
import { attest, contextualize } from "@arktype/attest"
import { type } from "arktype"
import { describe, it } from "mocha"
import { it } from "mocha"

type makeComplexType<S extends string> = S extends `${infer head}${infer tail}`
? head | tail | makeComplexType<tail>
: S

describe("instantiations", () => {
it("Can give me instantiations", () => {
contextualize(() => {
it("Inline instantiations", () => {
type("string")
attest.instantiations([4766, "instantiations"])
attest.instantiations([1968, "instantiations"])
})
})
14 changes: 7 additions & 7 deletions ark/attest/__tests__/snapExpectedOutput.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,25 @@ import { attest, cleanup, setup } from "@arktype/attest"

setup()

attest({ re: "do" }).equals({ re: "do" }).type.toString.snap(`{ re: string; }`)
attest({ re: "do" }).equals({ re: "do" }).type.toString.snap("{ re: string; }")

attest(5).snap(5)

attest({ re: "do" }).snap({ re: `do` })
attest({ re: "do" }).snap({ re: "do" })

// @ts-expect-error (using internal updateSnapshots hook)
attest({ re: "dew" }, { updateSnapshots: true }).snap({ re: `dew` })
attest({ re: "dew" }, { cfg: { updateSnapshots: true } }).snap({ re: "dew" })

// @ts-expect-error (using internal updateSnapshots hook)
attest(5, { updateSnapshots: true }).snap(5)
attest(5, { cfg: { updateSnapshots: true } }).snap(5)

attest(undefined).snap(`(undefined)`)
attest(undefined).snap("(undefined)")

attest({ a: undefined }).snap({ a: `(undefined)` })
attest({ a: undefined }).snap({ a: "(undefined)" })

attest("multiline\nmultiline").snap(`multiline
multiline`)

attest("with `quotes`").snap(`with \`quotes\``)
attest("with `quotes`").snap("with `quotes`")

cleanup()
4 changes: 2 additions & 2 deletions ark/attest/__tests__/snapPopulation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ contextualize(() => {
fromHere("benchExpectedOutput.ts")
).replaceAll("\r\n", "\n")
equal(actual, expectedOutput)
})
}).timeout(10000)

it("snap populates file", () => {
const actual = runThenGetContents(fromHere("snapTemplate.ts"))
const expectedOutput = readFile(
fromHere("snapExpectedOutput.ts")
).replaceAll("\r\n", "\n")
equal(actual, expectedOutput)
})
}).timeout(10000)
})
4 changes: 2 additions & 2 deletions ark/attest/__tests__/snapTemplate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ attest(5).snap()
attest({ re: "do" }).snap()

// @ts-expect-error (using internal updateSnapshots hook)
attest({ re: "dew" }, { updateSnapshots: true }).snap()
attest({ re: "dew" }, { cfg: { updateSnapshots: true } }).snap({ re: "do" })

// @ts-expect-error (using internal updateSnapshots hook)
attest(5, { updateSnapshots: true }).snap(6)
attest(5, { cfg: { updateSnapshots: true } }).snap(6)

attest(undefined).snap()

Expand Down
13 changes: 5 additions & 8 deletions ark/attest/assert/assertions.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { printable, throwInternalError } from "@arktype/util"
import { hasKey, printable, throwInternalError } from "@arktype/util"

Check warning on line 1 in ark/attest/assert/assertions.ts

View workflow job for this annotation

GitHub Actions / core

'hasKey' is defined but never used

Check warning on line 1 in ark/attest/assert/assertions.ts

View workflow job for this annotation

GitHub Actions / core

'hasKey' is defined but never used
import { AssertionError } from "node:assert"
import * as assert from "node:assert/strict"
import type { TypeAssertionData } from "../cache/writeAssertionCache.js"
import type { TypeRelationshipAssertionData } from "../cache/writeAssertionCache.js"
import type { AssertionContext } from "./attest.js"

export type ThrowAssertionErrorContext = {
Expand Down Expand Up @@ -34,7 +34,7 @@ export type MappedTypeAssertionResult = {
export class TypeAssertionMapping {
constructor(
public fn: (
data: TypeAssertionData,
data: TypeRelationshipAssertionData,
ctx: AssertionContext
) => MappedTypeAssertionResult
) {}
Expand All @@ -44,7 +44,7 @@ export const versionableAssertion =
(fn: AssertFn): AssertFn =>
(expected, actual, ctx) => {
if (actual instanceof TypeAssertionMapping) {
if (!ctx.typeAssertionEntries) {
if (!ctx.typeRelationshipAssertionEntries) {
throwInternalError(
`Unexpected missing typeAssertionEntries when passed a TypeAssertionMapper`
)
Expand Down Expand Up @@ -108,10 +108,7 @@ export const typeEqualityMapping = new TypeAssertionMapping(data => {
}
return null
})
/**
* todoshawn
* extract entires -> should just be an array should be type assertion data
*/

export const assertEqualOrMatching = versionableAssertion(
(expected, actual, ctx) => {
const assertionArgs = { actual, expected, ctx }
Expand Down
18 changes: 11 additions & 7 deletions ark/attest/assert/attest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@ import { getBenchCtx } from "../bench/bench.js"
import type { Measure } from "../bench/measure.js"
import { instantiationDataHandler } from "../bench/type.js"
import {
getTypeAssertionsAtPosition,
getTypeRelationshipAssertionsAtPosition,
type VersionedTypeAssertion
} from "../cache/getCachedAssertions.js"
import type { TypeAssertionData } from "../cache/writeAssertionCache.js"
import type {
TypeBenchmarkingAssertionData,
TypeRelationshipAssertionData
} from "../cache/writeAssertionCache.js"
import { getConfig, type AttestConfig } from "../config.js"
import { assertEquals, typeEqualityMapping } from "./assertions.js"
import {
Expand Down Expand Up @@ -38,7 +41,8 @@ export type AssertionContext = {
position: SourcePosition
defaultExpected?: unknown
assertionStack: string
typeAssertionEntries?: VersionedTypeAssertion[]
typeRelationshipAssertionEntries?: VersionedTypeAssertion<TypeRelationshipAssertionData>[]
typeBenchmarkingAssertionEntries?: VersionedTypeAssertion<TypeBenchmarkingAssertionData>[]
lastSnapName?: string
}

Expand All @@ -63,9 +67,9 @@ export const attestInternal = (
...ctxHooks
}
if (!cfg.skipTypes) {
ctx.typeAssertionEntries = getTypeAssertionsAtPosition(position)
//todoshawn is this one ok to cast
if ((ctx.typeAssertionEntries[0][1] as TypeAssertionData).typeArgs[0]) {
ctx.typeRelationshipAssertionEntries =
getTypeRelationshipAssertionsAtPosition(position)
if (ctx.typeRelationshipAssertionEntries[0][1].typeArgs[0]) {
// if there is an expected type arg, check it immediately
assertEquals(undefined, typeEqualityMapping, ctx)
}
Expand All @@ -87,4 +91,4 @@ attestInternal.instantiations = (
instantiationDataHandler({ ...ctx, kind: "instantiations" }, args, false)
}

export const attest = attestInternal as AttestFn
export const attest: AttestFn = attestInternal as AttestFn
38 changes: 12 additions & 26 deletions ark/attest/assert/chainableAssertions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@ import {
updateExternalSnapshot,
type SnapshotArgs
} from "../cache/snapshots.js"
import type {
Completions,
TypeAssertionData
} from "../cache/writeAssertionCache.js"
import type { Completions } from "../cache/writeAssertionCache.js"
import { chainableNoOpProxy } from "../utils.js"
import {
TypeAssertionMapping,
Expand All @@ -42,12 +39,12 @@ export class ChainableAssertions implements AssertionRecord {
return snapshot(value)
}

//todoshawn unsafe casting maybe
private get actual() {
return this.ctx.actual instanceof TypeAssertionMapping ?
this.ctx.actual.fn(this.ctx.typeAssertionEntries![0][1], this.ctx)!
.actual
: this.ctx.actual
if (this.ctx.actual instanceof TypeAssertionMapping) {
const assertionEntry = this.ctx.typeRelationshipAssertionEntries![0][1]
return this.ctx.actual.fn(assertionEntry, this.ctx)!.actual
}
return this.ctx.actual
}

private get serializedActual() {
Expand All @@ -69,7 +66,9 @@ export class ChainableAssertions implements AssertionRecord {
ctx: this.ctx,
message:
messageOnError ??
`${this.serializedActual} failed to satisfy predicate${predicate.name ? ` ${predicate.name}` : ""}`
`${this.serializedActual} failed to satisfy predicate${
predicate.name ? ` ${predicate.name}` : ""
}`
})
}
return this.actual as never
Expand All @@ -83,6 +82,7 @@ export class ChainableAssertions implements AssertionRecord {
assert.equal(this.actual, expected)
return this
}

equals(expected: unknown): this {
assertEquals(expected, this.actual, this.ctx)
return this
Expand Down Expand Up @@ -171,7 +171,7 @@ export class ChainableAssertions implements AssertionRecord {
})
}

get throws() {
get throws(): unknown {
const result = callAssertedFunction(this.actual as Function)
this.ctx.actual = getThrownMessage(result, this.ctx)
this.ctx.allowRegex = true
Expand All @@ -196,21 +196,7 @@ export class ChainableAssertions implements AssertionRecord {
}
}

instanceOf(expected: Constructor): this {
if (!(this.actual instanceof expected)) {
throwAssertionError({
ctx: this.ctx,
message: `Expected an instance of ${expected.name} (was ${
typeof this.actual === "object" && this.actual !== null ?
this.actual.constructor.name
: this.serializedActual
})`
})
}
return this
}

get completions() {
get completions(): any {
if (this.ctx.cfg.skipTypes) return chainableNoOpProxy

this.ctx.actual = new TypeAssertionMapping(data => {
Expand Down
2 changes: 1 addition & 1 deletion ark/attest/bench/baseline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,4 @@ const handleNegativeDelta = (formattedDelta: string, ctx: BenchContext) => {
1
)}! Consider setting a new baseline.`
)
}
}
Loading

0 comments on commit 4f640c7

Please sign in to comment.