|
1 | 1 | import assert from 'assert'; |
2 | 2 | import { CalculationLogicEngine } from '../src/CalculationLogic/CalculationLogicEngine.js'; |
| 3 | +import { OperandType } from '../src/Enums/OperandType.js'; |
3 | 4 |
|
4 | 5 | function createEngine(useCalculationLogicInfo = true) { |
5 | 6 | return new CalculationLogicEngine(useCalculationLogicInfo); |
@@ -220,6 +221,95 @@ function testSetValue_LayerRemark() { |
220 | 221 |
|
221 | 222 | // #endregion |
222 | 223 |
|
| 224 | +// #region GetValue 测试 |
| 225 | + |
| 226 | +function testGetValue_InitValue_Int() { |
| 227 | + const logic = createEngine(); |
| 228 | + logic.InitValue('a', 1); |
| 229 | + const operand = logic.GetValue('a'); |
| 230 | + assert.strictEqual(operand.IsNumber, true); |
| 231 | + assert.strictEqual(operand.Type, OperandType.NUMBER); |
| 232 | + assert.strictEqual(operand.NumberValue, 1); |
| 233 | +} |
| 234 | + |
| 235 | +function testGetValue_InitValue_String() { |
| 236 | + const logic = createEngine(); |
| 237 | + logic.InitValue('s', 'hello'); |
| 238 | + const operand = logic.GetValue('s'); |
| 239 | + assert.strictEqual(operand.IsText, true); |
| 240 | + assert.strictEqual(operand.Type, OperandType.TEXT); |
| 241 | + assert.strictEqual(operand.TextValue, 'hello'); |
| 242 | +} |
| 243 | + |
| 244 | +function testGetValue_InitValue_Decimal() { |
| 245 | + // JS 无 decimal 类型,使用 number 表示 |
| 246 | + const logic = createEngine(); |
| 247 | + logic.InitValue('d', 1.5); |
| 248 | + const operand = logic.GetValue('d'); |
| 249 | + assert.strictEqual(operand.IsNumber, true); |
| 250 | + assert.strictEqual(operand.NumberValue, 1.5); |
| 251 | +} |
| 252 | + |
| 253 | +function testGetValue_InitValue_Double() { |
| 254 | + const logic = createEngine(); |
| 255 | + logic.InitValue('dd', 1.5); |
| 256 | + const operand = logic.GetValue('dd'); |
| 257 | + assert.strictEqual(operand.IsNumber, true); |
| 258 | + assert.strictEqual(operand.NumberValue, 1.5); |
| 259 | +} |
| 260 | + |
| 261 | +function testGetValue_InitValue_Bool() { |
| 262 | + const logic = createEngine(); |
| 263 | + logic.InitValue('flag', true); |
| 264 | + const operand = logic.GetValue('flag'); |
| 265 | + assert.strictEqual(operand.IsBoolean, true); |
| 266 | + assert.strictEqual(operand.Type, OperandType.BOOLEAN); |
| 267 | + assert.strictEqual(operand.BooleanValue, true); |
| 268 | +} |
| 269 | + |
| 270 | +function testGetValue_SetValue() { |
| 271 | + const logic = createEngine(); |
| 272 | + logic.SetValue('v', 'text'); |
| 273 | + const operand = logic.GetValue('v'); |
| 274 | + assert.strictEqual(operand.IsText, true); |
| 275 | + assert.strictEqual(operand.TextValue, 'text'); |
| 276 | +} |
| 277 | + |
| 278 | +function testGetValue_SetFormula() { |
| 279 | + const logic = createEngine(); |
| 280 | + logic.InitValue('a', 1); |
| 281 | + logic.InitValue('b', 2); |
| 282 | + logic.SetFormula('x', 'a*3+b'); |
| 283 | + const operand = logic.GetValue('x'); |
| 284 | + assert.strictEqual(operand.IsNumber, true); |
| 285 | + assert.strictEqual(operand.NumberValue, 5); |
| 286 | +} |
| 287 | + |
| 288 | +function testGetValue_AfterSetFormulaOverwrite() { |
| 289 | + const logic = createEngine(); |
| 290 | + logic.InitValue('a', 1); |
| 291 | + logic.SetFormula('a', 'a+10'); |
| 292 | + const operand = logic.GetValue('a'); |
| 293 | + assert.strictEqual(operand.IsNumber, true); |
| 294 | + assert.strictEqual(operand.NumberValue, 11); |
| 295 | +} |
| 296 | + |
| 297 | +function testGetValue_NotFound_ReturnsError() { |
| 298 | + const logic = createEngine(); |
| 299 | + const operand = logic.GetValue('missing'); |
| 300 | + assert.strictEqual(operand.IsError, true); |
| 301 | + assert.strictEqual(operand.ErrorMsg, 'Parameter [missing] is missing.'); |
| 302 | +} |
| 303 | + |
| 304 | +function testGetValue_NoInfo() { |
| 305 | + // 关闭信息记录不影响取值 |
| 306 | + const logic = createEngine(false); |
| 307 | + logic.InitValue('a', 1); |
| 308 | + assert.strictEqual(logic.GetValue('a').NumberValue, 1); |
| 309 | +} |
| 310 | + |
| 311 | +// #endregion |
| 312 | + |
223 | 313 | // #region BlankLine 测试 |
224 | 314 |
|
225 | 315 | function testBlankLine() { |
@@ -309,6 +399,16 @@ function runAllTests() { |
309 | 399 | testSetValue_Double(); |
310 | 400 | testSetValue_Bool(); |
311 | 401 | testSetValue_LayerRemark(); |
| 402 | + testGetValue_InitValue_Int(); |
| 403 | + testGetValue_InitValue_String(); |
| 404 | + testGetValue_InitValue_Decimal(); |
| 405 | + testGetValue_InitValue_Double(); |
| 406 | + testGetValue_InitValue_Bool(); |
| 407 | + testGetValue_SetValue(); |
| 408 | + testGetValue_SetFormula(); |
| 409 | + testGetValue_AfterSetFormulaOverwrite(); |
| 410 | + testGetValue_NotFound_ReturnsError(); |
| 411 | + testGetValue_NoInfo(); |
312 | 412 | testBlankLine(); |
313 | 413 | testToInfoString_Empty(); |
314 | 414 | testToInfoString_Disabled(); |
@@ -349,6 +449,16 @@ export { |
349 | 449 | testSetValue_Double, |
350 | 450 | testSetValue_Bool, |
351 | 451 | testSetValue_LayerRemark, |
| 452 | + testGetValue_InitValue_Int, |
| 453 | + testGetValue_InitValue_String, |
| 454 | + testGetValue_InitValue_Decimal, |
| 455 | + testGetValue_InitValue_Double, |
| 456 | + testGetValue_InitValue_Bool, |
| 457 | + testGetValue_SetValue, |
| 458 | + testGetValue_SetFormula, |
| 459 | + testGetValue_AfterSetFormulaOverwrite, |
| 460 | + testGetValue_NotFound_ReturnsError, |
| 461 | + testGetValue_NoInfo, |
352 | 462 | testBlankLine, |
353 | 463 | testToInfoString_Empty, |
354 | 464 | testToInfoString_Disabled, |
|