idlize
121 строка · 4.1 Кб
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
16import { assert } from "chai"
17import * as child from "child_process"
18import * as path from "path"
19
20function runTsc(file: string) {
21let npx = process.platform === "win32" ? "npx.cmd" : "npx"
22return child.spawnSync(npx, ["tsc", path.join('test', 'diagnostics', 'input', file)])
23}
24
25function diagnostics(name: string, file: string, message: string) {
26test(name, () => {
27const result = runTsc(file)
28console.log(result.stdout.toString())
29assert.include(result.stdout.toString(), message)
30}).timeout(30000);
31}
32
33function noDiagnostics(name: string, file: string, message: string) {
34test(name, () => {
35const result = runTsc(file)
36console.log(result.stdout.toString())
37assert.notInclude(result.stderr.toString(), message)
38assert.notInclude(result.stdout.toString(), message)
39}).timeout(30000);
40}
41
42suite("Memo in non-memo context", () => {
43diagnostics("Global memo",
44"global_memo.input.ts",
45'Calling a @memo function "foo" from a non-@memo context'
46)
47diagnostics("memo in handler",
48"memo_in_handler.input.ts",
49'Calling a @memo function "foo" from a non-@memo context'
50)
51diagnostics("memo in inner function",
52"memo_in_inner_function.input.ts",
53'Calling a @memo function "foo" from a non-@memo context'
54)
55noDiagnostics("memo with @memo:entry",
56"memo_entry.input.ts",
57'Calling a @memo function "foo" from a non-@memo context'
58)
59diagnostics("memo in non-memo stack",
60"no_memo_entry.input.ts",
61'Calling a @memo function "foo" from a non-@memo context'
62)
63})
64
65suite("Changing state in @memo", () => {
66noDiagnostics("BinaryCorrect",
67"BinaryCorrect.ts",
68'Changing state "y" in @memo context is not allowed'
69)
70diagnostics("BinaryIncorrect",
71"BinaryIncorrect.ts",
72'Changing state "y" in @memo context is not allowed'
73)
74noDiagnostics("BinaryNoChange",
75"BinaryNoChange.ts",
76'Changing state "y" in @memo context is not allowed'
77)
78noDiagnostics("UnaryCorrect",
79"UnaryCorrect.ts",
80'Changing state "y" in @memo context is not allowed'
81)
82diagnostics("UnaryIncorrect",
83"UnaryIncorrect.ts",
84'Changing state "y" in @memo context is not allowed'
85)
86noDiagnostics("UnaryNoChange",
87"UnaryNoChange.ts",
88'Changing state "y" in @memo context is not allowed'
89)
90diagnostics("Assignment to parameters",
91"ParameterAssignment.ts",
92"Can not assign to memo function parameter: x"
93)
94noDiagnostics("Assignment to non-memo parameters",
95"NonmemoParameterAssignment.ts",
96"Can not assign to memo function parameter: x"
97)
98})
99
100suite("Function declaration with @memo initializer", () => {
101noDiagnostics("NonMemoArgument",
102"NonMemoArgument.ts",
103'Can not call @memo "bar" in parameter "x" default value expression'
104)
105diagnostics("MemoArgument",
106"MemoArgument.ts",
107'Can not call @memo "bar" in parameter "x" default value expression'
108)
109diagnostics("MemoComplexArgument",
110"MemoComplexArgument.ts",
111'Can not call @memo "bar" in parameter "x" default value expression'
112)
113diagnostics("MemoArgumentInArrow",
114"MemoArgumentInArrow.ts",
115'Can not call @memo "bar" in parameter "x" default value expression'
116)
117noDiagnostics("NonMemoArgumentInArrow",
118"NonMemoArgumentInArrow.ts",
119'Can not call @memo "bar" in parameter "x" default value expression'
120)
121})
122
123