-
Notifications
You must be signed in to change notification settings - Fork 108
/
Copy pathlifetime.test.ts
156 lines (134 loc) · 4.43 KB
/
lifetime.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import assert from "assert"
import { describe, it } from "vitest"
import { DisposableResult, Lifetime, Scope, createDisposableArray } from "./lifetime"
describe("Lifetime", () => {
describe(".consume", () => {
it("yeilds the value", () => {
const lifetime = new Lifetime(1)
const result = lifetime.consume((l) => l.value + 1)
assert.strictEqual(result, 2)
})
it("disposes the lifetime", () => {
let disposed = false
const lifetime = new Lifetime(2, undefined, () => {
disposed = true
})
lifetime.consume((l) => l.value * 2)
assert.strictEqual(lifetime.alive, false)
assert.strictEqual(disposed, true)
})
})
describe("using statement", () => {
it("disposes the lifetime", () => {
let outerLifetime
{
using lifetime = new Lifetime(1)
outerLifetime = lifetime
assert.strictEqual(outerLifetime.alive, true)
}
assert.strictEqual(outerLifetime.alive, false)
})
})
})
describe("Scope", () => {
describe(".withScope", () => {
function counter() {
let n = 0
return {
increment: () => n++,
count: () => n,
}
}
it("disposes all the lifetimes", () => {
const { increment, count } = counter()
const secondLifetime = Scope.withScope((scope) => {
scope.manage(new Lifetime("first", undefined, () => increment()))
return scope.manage(new Lifetime("second", undefined, () => increment()))
})
assert.strictEqual(secondLifetime.alive, false)
assert.strictEqual(count(), 2)
})
})
})
describe("createDisposableArray", () => {
it("is alive if any element is alive", () => {
const lifetimes = [new Lifetime(1), new Lifetime(2)]
const result = createDisposableArray(lifetimes)
assert.strictEqual(result.alive, true)
lifetimes[0].dispose()
assert.strictEqual(result.alive, true)
lifetimes[1].dispose()
assert.strictEqual(result.alive, false)
})
it(".dispose() disposes all alive elements and ignores dead elements", () => {
const lifetimes = [new Lifetime(1), new Lifetime(2)]
lifetimes[0].dispose()
const result = createDisposableArray(lifetimes)
result.dispose()
assert.strictEqual(lifetimes[0].alive, false)
assert.strictEqual(lifetimes[1].alive, false)
})
})
describe("DisposableResult", () => {
function itBehavesAsDisposable(factory: <T>(v: T) => DisposableResult<T, T>) {
it("is alive when the value is not disposable", () => {
const result = factory(false)
assert.strictEqual(result.alive, true)
})
it("is alive when the value is disposable and alive", () => {
const lifetime = new Lifetime(1)
const result = factory(lifetime)
assert.strictEqual(result.alive, true)
lifetime.dispose()
assert.strictEqual(result.alive, false)
})
it("disposes the value when dispose is called", () => {
const lifetime = new Lifetime(1)
const result = factory(lifetime)
result.dispose()
assert.strictEqual(lifetime.alive, false)
})
it("can call unwrapOr", () => {
const result = factory("hi")
result.unwrapOr(0)
})
}
describe("success", () => {
itBehavesAsDisposable((v) => DisposableResult.success(v))
it("unwrap returns the value", () => {
const val = {}
const result = DisposableResult.success(val)
assert.strictEqual(result.unwrap(), val)
})
it("unwrapOr returns the value", () => {
const val = {}
const result = DisposableResult.success(val)
assert.strictEqual(result.unwrapOr(0), val)
})
})
describe("fail", () => {
itBehavesAsDisposable((v) => DisposableResult.fail(v, () => {}))
it("unwrap calls the onUnwrap", () => {
let calledWith: unknown = undefined
const expectedError = new Error("will throw")
const result = DisposableResult.fail("error", (v) => {
calledWith = v
throw expectedError
})
assert.throws(
() => result.unwrap(),
(e) => e === expectedError,
)
assert.strictEqual(calledWith, result)
})
it("throws even if unwrap doesnt", () => {
const result = DisposableResult.fail("error", () => {})
assert.throws(() => result.unwrap())
})
it("unwrapOr returns the fallback", () => {
const fallback = {}
const result = DisposableResult.fail("error", () => {})
assert.strictEqual(result.unwrapOr(fallback), fallback)
})
})
})