Skip to content

Commit

Permalink
抽出 Support 以及 assertSupported
Browse files Browse the repository at this point in the history
  • Loading branch information
xieyuheng committed Jun 24, 2024
1 parent 466266c commit de02a36
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 20 deletions.
2 changes: 1 addition & 1 deletion TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

> - https://github.com/cicada-lang/propagator/issues/2
抽出来一个叫 `Support` 的 type
抽出 `SupportLike` 以及 `toSupport`

improve interface for `put` -- second argument should be list of optional promises

Expand Down
41 changes: 39 additions & 2 deletions src/dependency/Supported.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
import assert from "node:assert"
import { isNonNullObject } from "../utils/isNonNullObject.js"
import { log } from "../utils/log.js"

export type Support = Set<string>

export type Supported<T> = {
"@type": "Supported"
value: T
support: Set<string>
support: Support
}

export function Supported<T>(value: T, support: Set<string>): Supported<T> {
export function Supported<T>(
value: T,
support: Support | Array<string>,
): Supported<T> {
if (support instanceof Array) {
support = new Set(support)
}

return {
"@type": "Supported",
value,
Expand All @@ -22,3 +33,29 @@ export function toSupported(x: any): Supported<any> {
if (isSupported(x)) return x
return Supported(x, new Set())
}

export function assertSupported(
target: any,
support?: Support | Array<string>,
): asserts target is Supported<any> {
if (!isSupported(target)) {
const message = `Assertion fails.`
log({
kind: "Error",
who: "assertSupported",
message,
target,
support,
})

throw new Error(`[assertSupported] ${message}`)
}

if (support !== undefined) {
if (support instanceof Array) {
support = new Set(support)
}

assert.deepStrictEqual(target.support, support)
}
}
28 changes: 11 additions & 17 deletions src/examples/barometer.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import assert from "node:assert"
import { test } from "node:test"
import { Cell, put } from "../cell/index.js"
import { Supported } from "../dependency/index.js"
import { Supported, assertSupported } from "../dependency/index.js"
import { Interval, intervalAlmostEqual } from "../interval/index.js"
import { run } from "../scheduler/index.js"
import { log } from "../utils/log.js"
Expand Down Expand Up @@ -78,13 +78,13 @@ test("examples / barometer / similarTriangles & fallDuration", async () => {
test("examples / barometer / with supported value", async () => {
const [barometerShadow, barometerHeight, buildingShadow, buildingHeight] =
similarTriangles()
put(buildingShadow, Supported(Interval(54.9, 55.1), new Set(["shadows"])))
put(barometerHeight, Supported(Interval(0.3, 0.32), new Set(["shadows"])))
put(barometerShadow, Supported(Interval(0.36, 0.37), new Set(["shadows"])))
put(buildingShadow, Supported(Interval(54.9, 55.1), ["shadows"]))
put(barometerHeight, Supported(Interval(0.3, 0.32), ["shadows"]))
put(barometerShadow, Supported(Interval(0.36, 0.37), ["shadows"]))

await run()

assert.deepStrictEqual(buildingHeight.content.support, new Set(["shadows"]))
assertSupported(buildingHeight.content, ["shadows"])
assert(
intervalAlmostEqual(
buildingHeight.content.value,
Expand All @@ -95,11 +95,11 @@ test("examples / barometer / with supported value", async () => {

const fallTime = Cell()
fallDuration(fallTime, buildingHeight)
put(fallTime, Supported(Interval(2.9, 3.3), new Set(["lousy-fall-time"])))
put(fallTime, Supported(Interval(2.9, 3.3), ["lousy-fall-time"]))

await run()

assert.deepStrictEqual(buildingHeight.content.support, new Set(["shadows"]))
assertSupported(buildingHeight.content, ["shadows"])
assert(
intervalAlmostEqual(
buildingHeight.content.value,
Expand All @@ -108,14 +108,11 @@ test("examples / barometer / with supported value", async () => {
),
)

put(fallTime, Supported(Interval(2.9, 3.1), new Set(["better-fall-time"])))
put(fallTime, Supported(Interval(2.9, 3.1), ["better-fall-time"]))

await run()

assert.deepStrictEqual(
buildingHeight.content.support,
new Set(["shadows", "better-fall-time"]),
)
assertSupported(buildingHeight.content, ["shadows", "better-fall-time"])
assert(
intervalAlmostEqual(
buildingHeight.content.value,
Expand All @@ -124,14 +121,11 @@ test("examples / barometer / with supported value", async () => {
),
)

put(buildingHeight, Supported(45, new Set(["superintendent"])))
put(buildingHeight, Supported(45, ["superintendent"]))

await run()

assert.deepStrictEqual(
buildingHeight.content.support,
new Set(["superintendent"]),
)
assertSupported(buildingHeight.content, ["superintendent"])
assert.deepStrictEqual(buildingHeight.content.value, 45)

// (content barometer-height)
Expand Down

0 comments on commit de02a36

Please sign in to comment.