|
| 1 | +/* eslint-disable sonarjs/no-duplicate-string */ |
| 2 | +import { describe, it, expect } from 'vitest'; |
| 3 | +import { FormulaFieldCore } from '../models/field/derivate/formula.field'; |
| 4 | +import { FormulaExpansionVisitor, type IFieldExpansionMap } from './expansion.visitor'; |
| 5 | + |
| 6 | +describe('FormulaExpansionVisitor', () => { |
| 7 | + const parseAndExpand = (expression: string, expansionMap: IFieldExpansionMap): string => { |
| 8 | + const tree = FormulaFieldCore.parse(expression); |
| 9 | + const visitor = new FormulaExpansionVisitor(expansionMap); |
| 10 | + visitor.visit(tree); |
| 11 | + return visitor.getResult(); |
| 12 | + }; |
| 13 | + |
| 14 | + describe('basic field reference expansion', () => { |
| 15 | + it('should expand a single field reference', () => { |
| 16 | + const expansionMap = { |
| 17 | + field1: 'expanded_field1', |
| 18 | + }; |
| 19 | + |
| 20 | + const result = parseAndExpand('{field1}', expansionMap); |
| 21 | + expect(result).toBe('expanded_field1'); |
| 22 | + }); |
| 23 | + |
| 24 | + it('should expand field references in expressions', () => { |
| 25 | + const expansionMap = { |
| 26 | + field1: '(base_field + 10)', |
| 27 | + }; |
| 28 | + |
| 29 | + const result = parseAndExpand('{field1} * 2', expansionMap); |
| 30 | + expect(result).toBe('(base_field + 10) * 2'); |
| 31 | + }); |
| 32 | + |
| 33 | + it('should expand multiple field references', () => { |
| 34 | + const expansionMap = { |
| 35 | + field1: 'expanded_field1', |
| 36 | + field2: 'expanded_field2', |
| 37 | + }; |
| 38 | + |
| 39 | + const result = parseAndExpand('{field1} + {field2}', expansionMap); |
| 40 | + expect(result).toBe('expanded_field1 + expanded_field2'); |
| 41 | + }); |
| 42 | + }); |
| 43 | + |
| 44 | + describe('complex expressions', () => { |
| 45 | + it('should handle nested parentheses in expansions', () => { |
| 46 | + const expansionMap = { |
| 47 | + field1: '((base + 5) * 2)', |
| 48 | + field2: '(other - 1)', |
| 49 | + }; |
| 50 | + |
| 51 | + const result = parseAndExpand('({field1} + {field2}) / 3', expansionMap); |
| 52 | + expect(result).toBe('(((base + 5) * 2) + (other - 1)) / 3'); |
| 53 | + }); |
| 54 | + |
| 55 | + it('should handle function calls with expanded fields', () => { |
| 56 | + const expansionMap = { |
| 57 | + field1: 'SUM(column1)', |
| 58 | + field2: 'AVG(column2)', |
| 59 | + }; |
| 60 | + |
| 61 | + const result = parseAndExpand('MAX({field1}, {field2})', expansionMap); |
| 62 | + expect(result).toBe('MAX(SUM(column1), AVG(column2))'); |
| 63 | + }); |
| 64 | + |
| 65 | + it('should handle string literals mixed with field references', () => { |
| 66 | + const expansionMap = { |
| 67 | + field1: 'user_name', |
| 68 | + }; |
| 69 | + |
| 70 | + const result = parseAndExpand('"Hello " + {field1} + "!"', expansionMap); |
| 71 | + expect(result).toBe('"Hello " + user_name + "!"'); |
| 72 | + }); |
| 73 | + }); |
| 74 | + |
| 75 | + describe('edge cases', () => { |
| 76 | + it('should preserve field references without expansions', () => { |
| 77 | + const expansionMap = { |
| 78 | + field1: 'expanded_field1', |
| 79 | + }; |
| 80 | + |
| 81 | + const result = parseAndExpand('{field1} + {field2}', expansionMap); |
| 82 | + expect(result).toBe('expanded_field1 + {field2}'); |
| 83 | + }); |
| 84 | + |
| 85 | + it('should handle empty expansion map', () => { |
| 86 | + const expansionMap = {}; |
| 87 | + |
| 88 | + const result = parseAndExpand('{field1} + {field2}', expansionMap); |
| 89 | + expect(result).toBe('{field1} + {field2}'); |
| 90 | + }); |
| 91 | + |
| 92 | + it('should handle expressions without field references', () => { |
| 93 | + const expansionMap = { |
| 94 | + field1: 'expanded_field1', |
| 95 | + }; |
| 96 | + |
| 97 | + const result = parseAndExpand('1 + 2 * 3', expansionMap); |
| 98 | + expect(result).toBe('1 + 2 * 3'); |
| 99 | + }); |
| 100 | + |
| 101 | + it('should handle field references in complex nested expressions', () => { |
| 102 | + const expansionMap = { |
| 103 | + a: '(x + y)', |
| 104 | + b: '(z * 2)', |
| 105 | + }; |
| 106 | + |
| 107 | + const result = parseAndExpand('IF({a} > 0, {b}, -{b})', expansionMap); |
| 108 | + expect(result).toBe('IF((x + y) > 0, (z * 2), -(z * 2))'); |
| 109 | + }); |
| 110 | + }); |
| 111 | + |
| 112 | + describe('visitor reuse', () => { |
| 113 | + it('should allow visitor reuse with reset', () => { |
| 114 | + const visitor = new FormulaExpansionVisitor({ field1: 'expanded' }); |
| 115 | + |
| 116 | + // First use |
| 117 | + const tree1 = FormulaFieldCore.parse('{field1} + 1'); |
| 118 | + visitor.visit(tree1); |
| 119 | + const result1 = visitor.getResult(); |
| 120 | + expect(result1).toBe('expanded + 1'); |
| 121 | + |
| 122 | + // Reset and reuse |
| 123 | + visitor.reset(); |
| 124 | + const tree2 = FormulaFieldCore.parse('{field1} * 2'); |
| 125 | + visitor.visit(tree2); |
| 126 | + const result2 = visitor.getResult(); |
| 127 | + expect(result2).toBe('expanded * 2'); |
| 128 | + }); |
| 129 | + }); |
| 130 | + |
| 131 | + describe('real-world formula expansion scenarios', () => { |
| 132 | + it('should handle the JSDoc example scenario', () => { |
| 133 | + // Simulates the scenario described in FormulaExpansionService JSDoc |
| 134 | + const expansionMap = { |
| 135 | + field2: '({field1} + 10)', |
| 136 | + }; |
| 137 | + |
| 138 | + const result = parseAndExpand('{field2} * 2', expansionMap); |
| 139 | + expect(result).toBe('({field1} + 10) * 2'); |
| 140 | + }); |
| 141 | + |
| 142 | + it('should handle nested formula expansion', () => { |
| 143 | + // field1 -> field2 -> field3 expansion chain |
| 144 | + const expansionMap = { |
| 145 | + field2: '({field1} + 10)', |
| 146 | + field3: '(({field1} + 10) * 2)', |
| 147 | + }; |
| 148 | + |
| 149 | + const result = parseAndExpand('{field3} + 5', expansionMap); |
| 150 | + expect(result).toBe('(({field1} + 10) * 2) + 5'); |
| 151 | + }); |
| 152 | + }); |
| 153 | +}); |
0 commit comments