idlize

Форк
0
181 строка · 5.7 Кб
1
/*
2
 * Copyright (c) 2022-2023 Huawei Device Co., Ltd.
3
 * Licensed under the Apache License, Version 2.0 (the "License");
4
 * you may not use this file except in compliance with the License.
5
 * You may obtain a copy of the License at
6
 *
7
 * http://www.apache.org/licenses/LICENSE-2.0
8
 *
9
 * Unless required by applicable law or agreed to in writing, software
10
 * distributed under the License is distributed on an "AS IS" BASIS,
11
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
 * See the License for the specific language governing permissions and
13
 * limitations under the License.
14
 */
15

16
import { assert } from "chai"
17
import { UniqueId, KoalaCallsiteKey } from "@koalaui/common"
18
import {
19
    GlobalStateManager,
20
    Repeat,
21
    RepeatByArray,
22
    RepeatRange,
23
    RepeatWithKey,
24
    State,
25
    TestNode,
26
    mutableState,
27
    rememberDisposable,
28
    testTick,
29
} from "../../src"
30

31
const collector = new Array<string>()
32

33
interface Page {
34
    /** @memo */
35
    readonly page: () => void
36
    readonly id: KoalaCallsiteKey
37
}
38

39
function createPage(name: string): Page {
40
    return {
41
        id: parseInt(new UniqueId().addString(name).compute().slice(0, 10), 16),
42
        page:
43
            /** @memo */
44
            (): number => rememberDisposable(() => collector.push("+" + name), () => collector.push("-" + name))
45
    }
46
}
47

48
function createPages(...names: string[]): ReadonlyArray<Page> {
49
    return names.map(createPage)
50
}
51

52
function testExpected(root: State<TestNode>, ...expected: string[]) {
53
    collector.length = 0
54
    testTick(root)
55
    assert.deepEqual(collector, expected)
56
    if (expected.length > 0) testExpected(root)
57
}
58

59
function testInsert(
60
    /** @memo */
61
    content: (array: ReadonlyArray<Page>) => void
62
) {
63
    GlobalStateManager.reset()
64
    const state = mutableState(createPages())
65
    const root = TestNode.create(() => content(state.value))
66
    testExpected(root)
67
    state.value = createPages("three")
68
    testExpected(root, "+three")
69
    state.value = createPages("one", "three")
70
    testExpected(root, "+one")
71
    state.value = createPages("one", "three", "five")
72
    testExpected(root, "+five")
73
    state.value = createPages("one", "two", "three", "four", "five")
74
    testExpected(root, "+two", "+four")
75
}
76

77
function testRemove(
78
    /** @memo */
79
    content: (array: ReadonlyArray<Page>) => void
80
) {
81
    GlobalStateManager.reset()
82
    const state = mutableState(createPages("one", "two", "three", "four", "five"))
83
    const root = TestNode.create(() => content(state.value))
84
    testExpected(root, "+one", "+two", "+three", "+four", "+five")
85
    state.value = createPages("one", "three", "five")
86
    testExpected(root, "-two", "-four")
87
    state.value = createPages("three", "five")
88
    testExpected(root, "-one")
89
    state.value = createPages("three")
90
    testExpected(root, "-five")
91
    state.value = createPages()
92
    testExpected(root, "-three")
93
}
94

95
function testSwap(
96
    /** @memo */
97
    content: (array: ReadonlyArray<Page>) => void
98
) {
99
    GlobalStateManager.reset()
100
    const state = mutableState(createPages("one", "two", "three", "four", "five"))
101
    const root = TestNode.create(() => content(state.value))
102
    testExpected(root, "+one", "+two", "+three", "+four", "+five")
103
    state.value = createPages("two", "one", "three", "four", "five")
104
    testExpected(root, "-one", "+one")
105
    state.value = createPages("two", "four", "one", "three", "five")
106
    testExpected(root, "-one", "-three", "+one", "+three")
107
}
108

109
suite("repeat tests", () => {
110

111
    test("Repeat", () => {
112
        GlobalStateManager.reset()
113
        const state = mutableState(createPages())
114
        const root = TestNode.create(() => Repeat(state.value.length, index => state.value[index].page())) // index-based key
115
        testExpected(root)
116
        state.value = createPages("one")
117
        testExpected(root, "+one")
118
        state.value = createPages("one", "two", "three")
119
        testExpected(root, "+two", "+three")
120
        state.value = createPages("one", "two")
121
        testExpected(root, "-three")
122
        state.value = createPages("two")
123
        testExpected(root, "-two") // because of index-based key; should be "-one"
124
        state.value = createPages("one", "two")
125
        testExpected(root, "+two") // because of index-based key; should be "+one"
126
    })
127

128

129
    test("RepeatWithKey.insert", () => testInsert(array => RepeatWithKey(
130
        array.length,
131
        index => array[index].id,
132
        index => array[index].page())))
133

134
    test("RepeatByArray.insert", () => testInsert(array => RepeatByArray(
135
        array,
136
        element => element.id,
137
        element => element.page())))
138

139
    test("RepeatRange.insert", () => testInsert(array => RepeatRange(
140
        0,
141
        array.length,
142
        index => array[index],
143
        element => element.id,
144
        element => element.page())))
145

146

147
    test("RepeatWithKey.remove", () => testRemove(array => RepeatWithKey(
148
        array.length,
149
        index => array[index].id,
150
        index => array[index].page())))
151

152
    test("RepeatByArray.remove", () => testRemove(array => RepeatByArray(
153
        array,
154
        element => element.id,
155
        element => element.page())))
156

157
    test("RepeatRange.remove", () => testRemove(array => RepeatRange(
158
        0,
159
        array.length,
160
        index => array[index],
161
        element => element.id,
162
        element => element.page())))
163

164

165
    test("RepeatWithKey.swap", () => testSwap(array => RepeatWithKey(
166
        array.length,
167
        index => array[index].id,
168
        index => array[index].page())))
169

170
    test("RepeatByArray.swap", () => testSwap(array => RepeatByArray(
171
        array,
172
        element => element.id,
173
        element => element.page())))
174

175
    test("RepeatRange.swap", () => testSwap(array => RepeatRange(
176
        0,
177
        array.length,
178
        index => array[index],
179
        element => element.id,
180
        element => element.page())))
181
})
182

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.