-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathCalculationLogicTest.js
More file actions
543 lines (474 loc) · 17.2 KB
/
Copy pathCalculationLogicTest.js
File metadata and controls
543 lines (474 loc) · 17.2 KB
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
import assert from 'assert';
import { CalculationLogicEngine } from '../src/CalculationLogic/CalculationLogicEngine.js';
import { FunctionCache } from '../src/FunctionCache.js';
import { OperandType } from '../src/Enums/OperandType.js';
function createEngine(useCalculationLogicInfo = true) {
return new CalculationLogicEngine(new FunctionCache(), useCalculationLogicInfo);
}
// #region SetSceneName 测试
function testSetSceneName() {
const logic = createEngine();
logic.SetSceneName('场景1');
assert.strictEqual(logic.ToInfoString(), '===== 场景1 =====\n');
}
function testSetSceneName_NoInfo() {
const logic = createEngine(false);
logic.SetSceneName('场景1');
assert.strictEqual(logic.ToInfoString(), '');
}
// #endregion
// #region InitValue 测试
function testInitValue_Int() {
const logic = createEngine();
logic.InitValue('a', 1);
logic.InitValue('b', 2);
assert.strictEqual(logic.ToInfoString(), '[初始] a=1;b=2;\n');
}
function testInitValue_String() {
const logic = createEngine();
logic.InitValue('s', 'hello');
assert.strictEqual(logic.ToInfoString(), '[初始] s=hello;\n');
}
function testInitValue_Decimal() {
// JS 无 decimal 类型,使用 number 表示
const logic = createEngine();
logic.InitValue('d', 1.5);
assert.strictEqual(logic.ToInfoString(), '[初始] d=1.5;\n');
}
function testInitValue_Double() {
const logic = createEngine();
logic.InitValue('dd', 1.5);
assert.strictEqual(logic.ToInfoString(), '[初始] dd=1.5;\n');
}
function testInitValue_Bool() {
const logic = createEngine();
logic.InitValue('flag', true);
assert.strictEqual(logic.ToInfoString(), '[初始] flag=True;\n');
}
function testInitValue_LayerRemark() {
// InitValue 的层级与备注不参与信息输出
const logic = createEngine();
logic.InitValue('i', 100, 2, '备注');
assert.strictEqual(logic.ToInfoString(), '[初始] i=100;\n');
}
function testInitValue_CanBeUsedInCondition() {
const logic = createEngine();
logic.InitValue('a', 1);
logic.InitValue('b', 2);
assert.strictEqual(logic.CheckCondition('a<b'), true);
assert.strictEqual(logic.CheckCondition('a>b'), false);
}
function testInitValue_DateTime() {
const logic = createEngine();
logic.InitValue('dt', new Date(2026, 7, 31, 12, 0, 0));
assert.strictEqual(logic.ToInfoString(), '[初始] dt="2026-08-31 12:00:00";\n');
}
function testInitValue_DateTime_LayerRemark() {
// InitValue 的层级与备注不参与信息输出
const logic = createEngine();
logic.InitValue('dt', new Date(2026, 7, 31, 12, 0, 0), 2, '备注');
assert.strictEqual(logic.ToInfoString(), '[初始] dt="2026-08-31 12:00:00";\n');
}
function testInitValue_DateTime_CanBeUsedInCondition() {
const logic = createEngine();
logic.InitValue('dt', new Date(2026, 7, 31, 12, 0, 0));
assert.strictEqual(logic.CheckCondition('YEAR(dt)=2026'), true);
assert.strictEqual(logic.CheckCondition('MONTH(dt)=8'), true);
assert.strictEqual(logic.CheckCondition("dt>DATEVALUE('2026-08-30')"), true);
}
// #endregion
// #region CheckCondition 测试
function testCheckCondition_True() {
const logic = createEngine();
logic.InitValue('a', 1);
logic.InitValue('b', 2);
assert.strictEqual(logic.CheckCondition('a<b'), true);
assert.strictEqual(logic.ToInfoString(), '[初始] a=1;b=2;\n[成功] if a<b: // 1<2\n');
}
function testCheckCondition_False() {
const logic = createEngine();
logic.InitValue('a', 1);
logic.InitValue('b', 2);
assert.strictEqual(logic.CheckCondition('a>b'), false);
assert.strictEqual(logic.ToInfoString(), '[初始] a=1;b=2;\n[失败] if a>b: // 1>2\n');
}
function testCheckCondition_LayerRemark() {
const logic = createEngine();
logic.InitValue('a', 1);
logic.InitValue('b', 2);
assert.strictEqual(logic.CheckCondition('a<b', 1, '备注'), true);
assert.strictEqual(logic.ToInfoString(), '[初始] a=1;b=2;\n[成功] if a<b: // 1<2 // 备注\n');
}
function testCheckCondition_StringCompare() {
const logic = createEngine();
logic.InitValue('s', 'abc');
assert.strictEqual(logic.CheckCondition(`s='abc'`), true);
assert.strictEqual(logic.CheckCondition(`s='ab'`), false);
assert.strictEqual(logic.ToInfoString(), `[初始] s=abc;\n[成功] if s='abc': // "abc"='abc'\n[失败] if s='ab': // "abc"='ab'\n`);
}
function testCheckCondition_NonBoolean_Throws() {
const logic = createEngine();
logic.InitValue('a', 1);
logic.InitValue('b', 2);
let ex = null;
try {
logic.CheckCondition('a+b');
} catch (e) {
ex = e;
}
assert.ok(ex, '应该抛出异常');
assert.strictEqual(ex.message, 'The condition must be a boolean value!');
assert.strictEqual(logic.ToInfoString(), '[初始] a=1;b=2;\n[条件][错误] if a+b: // 1+2 = The condition must be a boolean value!\n');
}
function testCheckCondition_Error_Throws() {
const logic = createEngine();
let ex = null;
try {
logic.CheckCondition('1/0=0');
} catch (e) {
ex = e;
}
assert.ok(ex, '应该抛出异常');
assert.strictEqual(ex.message, `Function '/' Div 0 error!`);
assert.strictEqual(logic.ToInfoString(), `[条件][错误] if 1/0=0: // 1/0=0 = Function '/' Div 0 error!\n`);
}
// #endregion
// #region SetFormula 测试
function testSetFormula() {
const logic = createEngine();
logic.InitValue('a', 1);
logic.InitValue('b', 2);
logic.SetFormula('x', 'a*3+b', 1, '备注');
assert.strictEqual(logic.ToInfoString(), '[初始] a=1;b=2;\n[赋值] x = a*3+b = 1*3+2 = 5 // 备注\n');
// 公式结果应已写入参数
assert.strictEqual(logic.CheckCondition('x=5'), true);
}
function testSetFormula_StringConcat() {
const logic = createEngine();
logic.InitValue('s', 'hello');
logic.SetFormula('y', `s&'!'`);
assert.strictEqual(logic.ToInfoString(), `[初始] s=hello;\n[赋值] y = s&'!' = "hello"&'!' = hello!\n`);
assert.strictEqual(logic.CheckCondition(`y='hello!'`), true);
}
function testSetFormula_TextAndNumber() {
const logic = createEngine();
logic.InitValue('a', 1);
logic.SetFormula('z', `'text'&a`);
assert.strictEqual(logic.ToInfoString(), `[初始] a=1;\n[赋值] z = 'text'&a = 'text'&1 = text1\n`);
}
function testSetFormula_Error_Throws() {
const logic = createEngine();
let ex = null;
try {
logic.SetFormula('x', '1/0');
} catch (e) {
ex = e;
}
assert.ok(ex, '应该抛出异常');
assert.strictEqual(ex.message, `Function '/' Div 0 error!`);
assert.strictEqual(logic.ToInfoString(), `[赋值][错误] x = 1/0 = 1/0 // Function '/' Div 0 error!\n`);
}
// #endregion
// #region SetValue 测试
function testSetValue_String() {
const logic = createEngine();
logic.SetValue('s', '文本');
assert.strictEqual(logic.ToInfoString(), '[赋值] s = "文本"\n');
assert.strictEqual(logic.CheckCondition(`s='文本'`), true);
}
function testSetValue_StringEscape() {
const logic = createEngine();
logic.SetValue('sq', 'a"b');
assert.strictEqual(logic.ToInfoString(), '[赋值] sq = "a\\"b"\n');
}
function testSetValue_Decimal() {
// JS 无 decimal 类型,使用 number 表示
const logic = createEngine();
logic.SetValue('d', 1.5);
assert.strictEqual(logic.ToInfoString(), '[赋值] d = 1.5\n');
}
function testSetValue_Double() {
const logic = createEngine();
logic.SetValue('dd', 1.5);
assert.strictEqual(logic.ToInfoString(), '[赋值] dd = 1.5\n');
}
function testSetValue_Bool() {
const logic = createEngine();
logic.SetValue('flag', true);
assert.strictEqual(logic.ToInfoString(), '[赋值] flag = True\n');
}
function testSetValue_LayerRemark() {
const logic = createEngine();
logic.SetValue('i', 100, 1, '备注');
assert.strictEqual(logic.ToInfoString(), '[赋值] i = 100 // 备注\n');
}
function testSetValue_DateTime() {
const logic = createEngine();
logic.SetValue('dt', new Date(2026, 7, 31, 12, 0, 0));
assert.strictEqual(logic.ToInfoString(), '[赋值] dt = "2026-08-31 12:00:00"\n');
}
function testSetValue_DateTime_LayerRemark() {
const logic = createEngine();
logic.SetValue('dt', new Date(2026, 7, 31, 12, 0, 0), 1, '备注');
assert.strictEqual(logic.ToInfoString(), '[赋值] dt = "2026-08-31 12:00:00" // 备注\n');
}
function testSetValue_DateTime_CanBeUsedInCondition() {
const logic = createEngine();
logic.SetValue('dt', new Date(2026, 7, 31, 12, 0, 0));
assert.strictEqual(logic.CheckCondition('YEAR(dt)=2026'), true);
assert.strictEqual(logic.CheckCondition('MONTH(dt)=8'), true);
assert.strictEqual(logic.CheckCondition("dt>DATEVALUE('2026-08-30')"), true);
}
// #endregion
// #region GetValue 测试
function testGetValue_InitValue_Int() {
const logic = createEngine();
logic.InitValue('a', 1);
const operand = logic.GetValue('a');
assert.strictEqual(operand.IsNumber, true);
assert.strictEqual(operand.Type, OperandType.NUMBER);
assert.strictEqual(operand.NumberValue, 1);
}
function testGetValue_InitValue_String() {
const logic = createEngine();
logic.InitValue('s', 'hello');
const operand = logic.GetValue('s');
assert.strictEqual(operand.IsText, true);
assert.strictEqual(operand.Type, OperandType.TEXT);
assert.strictEqual(operand.TextValue, 'hello');
}
function testGetValue_InitValue_Decimal() {
// JS 无 decimal 类型,使用 number 表示
const logic = createEngine();
logic.InitValue('d', 1.5);
const operand = logic.GetValue('d');
assert.strictEqual(operand.IsNumber, true);
assert.strictEqual(operand.NumberValue, 1.5);
}
function testGetValue_InitValue_Double() {
const logic = createEngine();
logic.InitValue('dd', 1.5);
const operand = logic.GetValue('dd');
assert.strictEqual(operand.IsNumber, true);
assert.strictEqual(operand.NumberValue, 1.5);
}
function testGetValue_InitValue_Bool() {
const logic = createEngine();
logic.InitValue('flag', true);
const operand = logic.GetValue('flag');
assert.strictEqual(operand.IsBoolean, true);
assert.strictEqual(operand.Type, OperandType.BOOLEAN);
assert.strictEqual(operand.BooleanValue, true);
}
function testGetValue_InitValue_DateTime() {
const logic = createEngine();
logic.InitValue('dt', new Date(2026, 7, 31, 12, 0, 0));
const operand = logic.GetValue('dt');
assert.strictEqual(operand.IsDate, true);
assert.strictEqual(operand.Type, OperandType.DATE);
assert.strictEqual(operand.DateValue.toString(), '2026-08-31 12:00:00');
}
function testGetValue_SetValue_DateTime() {
const logic = createEngine();
logic.SetValue('dt', new Date(2026, 7, 31, 12, 0, 0));
const operand = logic.GetValue('dt');
assert.strictEqual(operand.IsDate, true);
assert.strictEqual(operand.Type, OperandType.DATE);
assert.strictEqual(operand.DateValue.toString(), '2026-08-31 12:00:00');
}
function testGetValue_SetValue() {
const logic = createEngine();
logic.SetValue('v', 'text');
const operand = logic.GetValue('v');
assert.strictEqual(operand.IsText, true);
assert.strictEqual(operand.TextValue, 'text');
}
function testGetValue_SetFormula() {
const logic = createEngine();
logic.InitValue('a', 1);
logic.InitValue('b', 2);
logic.SetFormula('x', 'a*3+b');
const operand = logic.GetValue('x');
assert.strictEqual(operand.IsNumber, true);
assert.strictEqual(operand.NumberValue, 5);
}
function testGetValue_AfterSetFormulaOverwrite() {
const logic = createEngine();
logic.InitValue('a', 1);
logic.SetFormula('a', 'a+10');
const operand = logic.GetValue('a');
assert.strictEqual(operand.IsNumber, true);
assert.strictEqual(operand.NumberValue, 11);
}
function testGetValue_NotFound_ReturnsError() {
const logic = createEngine();
const operand = logic.GetValue('missing');
assert.strictEqual(operand.IsError, true);
assert.strictEqual(operand.ErrorMsg, 'Parameter [missing] is missing.');
}
function testGetValue_NoInfo() {
// 关闭信息记录不影响取值
const logic = createEngine(false);
logic.InitValue('a', 1);
assert.strictEqual(logic.GetValue('a').NumberValue, 1);
}
// #endregion
// #region BlankLine 测试
function testBlankLine() {
const logic = createEngine();
logic.BlankLine();
assert.strictEqual(logic.ToInfoString(), '\n');
logic.BlankLine();
logic.BlankLine();
assert.strictEqual(logic.ToInfoString(), '\n\n\n');
}
// #endregion
// #region ToInfoString 测试
function testToInfoString_Empty() {
const logic = createEngine();
assert.strictEqual(logic.ToInfoString(), '');
}
function testToInfoString_Disabled() {
// 关闭信息记录后,所有操作都不产生信息
const logic = createEngine(false);
logic.SetSceneName('场景1');
logic.InitValue('a', 1);
logic.CheckCondition('a>0');
logic.SetValue('b', 2);
logic.SetFormula('c', 'a+1');
logic.BlankLine();
assert.strictEqual(logic.ToInfoString(), '');
}
function testToInfoString_FullFlow() {
const logic = createEngine();
logic.SetSceneName('场景1');
logic.InitValue('a', 1);
logic.InitValue('b', 2);
if (logic.CheckCondition('a>b')) {
logic.SetValue('c', 3, 1);
logic.SetFormula('c', 'a*5+b', 1);
} else if (logic.CheckCondition('a<b')) {
logic.SetValue('c', 4, 1);
logic.SetFormula('c', 'a*3+b', 1);
}
logic.BlankLine();
logic.SetValue('e', 5);
const expected = '[初始] a=1;b=2;\n'
+ '===== 场景1 =====\n'
+ '[失败] if a>b: // 1>2\n'
+ '[成功] if a<b: // 1<2\n'
+ '[赋值] c = 4\n'
+ '[赋值] c = a*3+b = 1*3+2 = 5\n'
+ '\n'
+ '[赋值] e = 5\n';
assert.strictEqual(logic.ToInfoString(), expected);
}
// #endregion
// 运行所有测试
function runAllTests() {
try {
testSetSceneName();
testSetSceneName_NoInfo();
testInitValue_Int();
testInitValue_String();
testInitValue_Decimal();
testInitValue_Double();
testInitValue_Bool();
testInitValue_LayerRemark();
testInitValue_CanBeUsedInCondition();
testInitValue_DateTime();
testInitValue_DateTime_LayerRemark();
testInitValue_DateTime_CanBeUsedInCondition();
testCheckCondition_True();
testCheckCondition_False();
testCheckCondition_LayerRemark();
testCheckCondition_StringCompare();
testCheckCondition_NonBoolean_Throws();
testCheckCondition_Error_Throws();
testSetFormula();
testSetFormula_StringConcat();
testSetFormula_TextAndNumber();
testSetFormula_Error_Throws();
testSetValue_String();
testSetValue_StringEscape();
testSetValue_Decimal();
testSetValue_Double();
testSetValue_Bool();
testSetValue_LayerRemark();
testSetValue_DateTime();
testSetValue_DateTime_LayerRemark();
testSetValue_DateTime_CanBeUsedInCondition();
testGetValue_InitValue_Int();
testGetValue_InitValue_String();
testGetValue_InitValue_Decimal();
testGetValue_InitValue_Double();
testGetValue_InitValue_Bool();
testGetValue_InitValue_DateTime();
testGetValue_SetValue_DateTime();
testGetValue_SetValue();
testGetValue_SetFormula();
testGetValue_AfterSetFormulaOverwrite();
testGetValue_NotFound_ReturnsError();
testGetValue_NoInfo();
testBlankLine();
testToInfoString_Empty();
testToInfoString_Disabled();
testToInfoString_FullFlow();
console.log('所有 CalculationLogic 测试通过!');
} catch (error) {
console.error('测试失败:', error.message);
process.exit(1);
}
}
// 执行测试
runAllTests();
export {
testSetSceneName,
testSetSceneName_NoInfo,
testInitValue_Int,
testInitValue_String,
testInitValue_Decimal,
testInitValue_Double,
testInitValue_Bool,
testInitValue_LayerRemark,
testInitValue_CanBeUsedInCondition,
testInitValue_DateTime,
testInitValue_DateTime_LayerRemark,
testInitValue_DateTime_CanBeUsedInCondition,
testCheckCondition_True,
testCheckCondition_False,
testCheckCondition_LayerRemark,
testCheckCondition_StringCompare,
testCheckCondition_NonBoolean_Throws,
testCheckCondition_Error_Throws,
testSetFormula,
testSetFormula_StringConcat,
testSetFormula_TextAndNumber,
testSetFormula_Error_Throws,
testSetValue_String,
testSetValue_StringEscape,
testSetValue_Decimal,
testSetValue_Double,
testSetValue_Bool,
testSetValue_LayerRemark,
testSetValue_DateTime,
testSetValue_DateTime_LayerRemark,
testSetValue_DateTime_CanBeUsedInCondition,
testGetValue_InitValue_Int,
testGetValue_InitValue_String,
testGetValue_InitValue_Decimal,
testGetValue_InitValue_Double,
testGetValue_InitValue_Bool,
testGetValue_InitValue_DateTime,
testGetValue_SetValue_DateTime,
testGetValue_SetValue,
testGetValue_SetFormula,
testGetValue_AfterSetFormulaOverwrite,
testGetValue_NotFound_ReturnsError,
testGetValue_NoInfo,
testBlankLine,
testToInfoString_Empty,
testToInfoString_Disabled,
testToInfoString_FullFlow,
runAllTests
};