|
| 1 | +import assert from 'assert'; |
| 2 | +import { AlgorithmEngine } from '../src/AlgorithmEngine.js'; |
| 3 | + |
| 4 | +// 对齐 C# FunctionCacheTest |
| 5 | +// JS 中对应的能力为 AlgorithmEngine.UseParseCache + Parse/ParseWithoutError |
| 6 | +class EngineWithCache extends AlgorithmEngine { |
| 7 | + constructor() { |
| 8 | + super(); |
| 9 | + this.UseParseCache = true; |
| 10 | + } |
| 11 | +} |
| 12 | + |
| 13 | +function testParseWithCacheSameFormulaReturnsSameInstance() { |
| 14 | + console.log('开始测试 ParseWithCache_SameFormula_ReturnsSameInstance...'); |
| 15 | + |
| 16 | + const cache = new EngineWithCache(); |
| 17 | + const r1 = cache.Parse("abs(1)"); |
| 18 | + const r2 = cache.Parse("abs(1)"); |
| 19 | + assert.ok(r1 != null, "Parse('abs(1)') 不应为空"); |
| 20 | + assert.strictEqual(r1, r2, "同一公式解析结果应为同一实例"); |
| 21 | + |
| 22 | + console.log('ParseWithCache_SameFormula_ReturnsSameInstance 测试通过!'); |
| 23 | +} |
| 24 | + |
| 25 | +function testParseWithCacheDifferentFormulasReturnsDifferentInstances() { |
| 26 | + console.log('开始测试 ParseWithCache_DifferentFormulas_ReturnsDifferentInstances...'); |
| 27 | + |
| 28 | + const cache = new EngineWithCache(); |
| 29 | + const r1 = cache.Parse("abs(1)"); |
| 30 | + const r2 = cache.Parse("abs(2)"); |
| 31 | + assert.ok(r1 != null && r2 != null, "解析结果不应为空"); |
| 32 | + assert.notStrictEqual(r1, r2, "不同公式解析结果应为不同实例"); |
| 33 | + |
| 34 | + console.log('ParseWithCache_DifferentFormulas_ReturnsDifferentInstances 测试通过!'); |
| 35 | +} |
| 36 | + |
| 37 | +function testParseWithCacheStringLeafReturnsFunctionBase() { |
| 38 | + console.log('开始测试 ParseWithCache_StringLeaf_ReturnsFunctionBase...'); |
| 39 | + |
| 40 | + const cache = new EngineWithCache(); |
| 41 | + // 修复: String 叶子节点也写入缓存(修复前直接返回不缓存) |
| 42 | + const r1 = cache.Parse("'abc'"); |
| 43 | + const r2 = cache.Parse("'abc'"); |
| 44 | + assert.ok(r1 != null, "Parse(\"'abc'\") 不应为空"); |
| 45 | + assert.strictEqual(r1, r2, "字符串叶子节点同一公式解析结果应为同一实例"); |
| 46 | + |
| 47 | + console.log('ParseWithCache_StringLeaf_ReturnsFunctionBase 测试通过!'); |
| 48 | +} |
| 49 | + |
| 50 | +function testParseWithCacheNestedFunctionReturnsFunctionBase() { |
| 51 | + console.log('开始测试 ParseWithCache_NestedFunction_ReturnsFunctionBase...'); |
| 52 | + |
| 53 | + const cache = new EngineWithCache(); |
| 54 | + const r = cache.Parse("max(1,2,3)"); |
| 55 | + assert.ok(r != null, "Parse('max(1,2,3)') 不应为空"); |
| 56 | + |
| 57 | + console.log('ParseWithCache_NestedFunction_ReturnsFunctionBase 测试通过!'); |
| 58 | +} |
| 59 | + |
| 60 | +function testParseWithCacheDiyExpressionReturnsFunctionBase() { |
| 61 | + console.log('开始测试 ParseWithCache_DiyExpression_ReturnsFunctionBase...'); |
| 62 | + |
| 63 | + const cache = new EngineWithCache(); |
| 64 | + const r = cache.Parse("myVar"); |
| 65 | + assert.ok(r != null, "Parse('myVar') 不应为空"); |
| 66 | + |
| 67 | + console.log('ParseWithCache_DiyExpression_ReturnsFunctionBase 测试通过!'); |
| 68 | +} |
| 69 | + |
| 70 | +function testParseWithoutErrorWithCacheCachesResult() { |
| 71 | + console.log('开始测试 ParseWithoutError_WithCache...'); |
| 72 | + |
| 73 | + const cache = new EngineWithCache(); |
| 74 | + const r1 = cache.ParseWithoutError("1=1"); |
| 75 | + const r2 = cache.ParseWithoutError("1=1"); |
| 76 | + assert.ok(r1 != null, "ParseWithoutError('1=1') 不应为空"); |
| 77 | + assert.strictEqual(r1, r2, "同一公式 ParseWithoutError 结果应为同一实例"); |
| 78 | + |
| 79 | + console.log('ParseWithoutError_WithCache 测试通过!'); |
| 80 | +} |
| 81 | + |
| 82 | +function testParseCacheInvalidFormulaReturnsNull() { |
| 83 | + console.log('开始测试 ParseCache_InvalidFormula_ReturnsNull...'); |
| 84 | + |
| 85 | + const cache = new EngineWithCache(); |
| 86 | + const r = cache.ParseWithoutError("1+"); |
| 87 | + assert.ok(r == null, "ParseWithoutError('1+') 应为 null"); |
| 88 | + assert.ok(cache.LastError != null, "无效公式应设置 LastError"); |
| 89 | + |
| 90 | + console.log('ParseCache_InvalidFormula_ReturnsNull 测试通过!'); |
| 91 | +} |
| 92 | + |
| 93 | +function testParseCacheCanEvaluate() { |
| 94 | + console.log('开始测试 ParseCache_CanEvaluate...'); |
| 95 | + |
| 96 | + const cache = new EngineWithCache(); |
| 97 | + const r = cache.Parse("abs(1)"); |
| 98 | + assert.ok(r != null, "Parse('abs(1)') 不应为空"); |
| 99 | + |
| 100 | + // 缓存实例仍可正常求值 |
| 101 | + const e = new EngineWithCache(); |
| 102 | + const t = e.TryEvaluate_Int("abs(-5)", 0); |
| 103 | + assert.strictEqual(t, 5, "abs(-5) 应该等于 5"); |
| 104 | + |
| 105 | + console.log('ParseCache_CanEvaluate 测试通过!'); |
| 106 | +} |
| 107 | + |
| 108 | +// 运行所有测试 |
| 109 | +function runAllTests() { |
| 110 | + try { |
| 111 | + testParseWithCacheSameFormulaReturnsSameInstance(); |
| 112 | + testParseWithCacheDifferentFormulasReturnsDifferentInstances(); |
| 113 | + testParseWithCacheStringLeafReturnsFunctionBase(); |
| 114 | + testParseWithCacheNestedFunctionReturnsFunctionBase(); |
| 115 | + testParseWithCacheDiyExpressionReturnsFunctionBase(); |
| 116 | + testParseWithoutErrorWithCacheCachesResult(); |
| 117 | + testParseCacheInvalidFormulaReturnsNull(); |
| 118 | + testParseCacheCanEvaluate(); |
| 119 | + console.log('所有测试通过!'); |
| 120 | + } catch (error) { |
| 121 | + console.error('测试失败:', error.message); |
| 122 | + process.exit(1); |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +// 执行测试 |
| 127 | +runAllTests(); |
| 128 | + |
| 129 | +export { |
| 130 | + testParseWithCacheSameFormulaReturnsSameInstance, |
| 131 | + testParseWithCacheDifferentFormulasReturnsDifferentInstances, |
| 132 | + testParseWithCacheStringLeafReturnsFunctionBase, |
| 133 | + testParseWithCacheNestedFunctionReturnsFunctionBase, |
| 134 | + testParseWithCacheDiyExpressionReturnsFunctionBase, |
| 135 | + testParseWithoutErrorWithCacheCachesResult, |
| 136 | + testParseCacheInvalidFormulaReturnsNull, |
| 137 | + testParseCacheCanEvaluate, |
| 138 | + runAllTests |
| 139 | +}; |
0 commit comments