-
Notifications
You must be signed in to change notification settings - Fork 107
/
unparse.spec.ts
299 lines (219 loc) · 9.68 KB
/
unparse.spec.ts
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
import {Config} from '../../src'
import {SheetMapping} from '../../src/DependencyGraph'
import {ParserWithCaching, buildLexerConfig} from '../../src/parser'
import {CellAddress} from '../../src/parser'
import {Unparser} from '../../src/parser'
import {adr} from '../testUtils'
import {enGB, plPL} from "../../src/i18n";
describe('Unparse', () => {
const config = new Config()
const lexerConfig = buildLexerConfig(config)
const sheetMapping = new SheetMapping(enGB)
sheetMapping.addSheet('Sheet1')
sheetMapping.addSheet('Sheet2')
sheetMapping.addSheet('Sheet with spaces')
const parser = new ParserWithCaching(config, sheetMapping.get)
const unparser = new Unparser(config, lexerConfig, sheetMapping.name)
it('#unparse', () => {
const formula = '=1+SUM(1,2,3)*3'
const ast = parser.parse(formula, CellAddress.absolute(0, 0, 0)).ast
const unparsed = unparser.unparse(ast, adr('A1'))
expect(unparsed).toEqual(formula)
})
it('#unparse simple addreess', () => {
const formula = '=A1'
const ast = parser.parse(formula, CellAddress.absolute(0, 0, 0)).ast
const unparsed = unparser.unparse(ast, adr('A1'))
expect(unparsed).toEqual(formula)
})
it('#unparse simple addreess from other sheet', () => {
const formula = '=Sheet1!A1'
const ast = parser.parse(formula, CellAddress.absolute(1, 0, 0)).ast
const unparsed = unparser.unparse(ast, adr('A1', 1))
expect(unparsed).toEqual(formula)
})
it('#unparse absolute col', () => {
const formula = '=$A1'
const ast = parser.parse(formula, CellAddress.absolute(0, 0, 0)).ast
const unparsed = unparser.unparse(ast, adr('A1'))
expect(unparsed).toEqual(formula)
})
it('#unparse absolute row addreess', () => {
const formula = '=A$1'
const ast = parser.parse(formula, CellAddress.absolute(0, 0, 0)).ast
const unparsed = unparser.unparse(ast, adr('A1'))
expect(unparsed).toEqual(formula)
})
it('#unparse absolute address', () => {
const formula = '=$A$1'
const ast = parser.parse(formula, CellAddress.absolute(0, 0, 0)).ast
const unparsed = unparser.unparse(ast, adr('A1'))
expect(unparsed).toEqual(formula)
})
it('#unparse cell ref between strings', () => {
const formula = '="A5"+A4+"A6"'
const ast = parser.parse(formula, CellAddress.absolute(0, 0, 0)).ast
const unparsed = unparser.unparse(ast, adr('A1'))
expect(unparsed).toEqual(formula)
})
it('#unparse cell ref in string with escape', () => {
const formula = '="fdsaf\\"A5"'
const ast = parser.parse(formula, CellAddress.absolute(0, 0, 0)).ast
const unparsed = unparser.unparse(ast, adr('A1'))
expect(unparsed).toEqual(formula)
})
it('#unparse cell range from same sheet', () => {
const formula = '=$A$1:B$2'
const ast = parser.parse(formula, CellAddress.absolute(0, 0, 0)).ast
const unparsed = unparser.unparse(ast, adr('A1'))
expect(unparsed).toEqual(formula)
})
it('#unparse cell range from other sheet', () => {
const formula = '=Sheet1!$A$1:B$2'
const ast = parser.parse(formula, CellAddress.absolute(1, 0, 0)).ast
const unparsed = unparser.unparse(ast, adr('A1', 1))
expect(unparsed).toEqual(formula)
})
it('#unparse ops', () => {
const formula = '=-1+1-1*1/1^1&1=1<>1<1<=1>1<1'
const ast = parser.parse(formula, CellAddress.absolute(0, 0, 0)).ast
const unparsed = unparser.unparse(ast, adr('A1'))
expect(unparsed).toEqual(formula)
})
it('#unparse with unspecified error', () => {
const formula = '=1+'
const ast = parser.parse(formula, CellAddress.absolute(0, 0, 0)).ast
const unparsed = unparser.unparse(ast, adr('A1'))
expect(unparsed).toEqual('=#ERR!')
})
it('#unparse with known error', () => {
const formula = '=#REF!'
const ast = parser.parse(formula, CellAddress.absolute(0, 0, 0)).ast
const unparsed = unparser.unparse(ast, adr('A1'))
expect(unparsed).toEqual('=#REF!')
})
it('#unparse with known error with translation', () => {
const config = new Config({ language: plPL })
const parser = new ParserWithCaching(config, sheetMapping.get)
const unparser = new Unparser(config, buildLexerConfig(config), sheetMapping.name)
const formula = '=#ADR!'
const ast = parser.parse(formula, CellAddress.absolute(0, 0, 0)).ast
const unparsed = unparser.unparse(ast, adr('A1'))
expect(unparsed).toEqual('=#ADR!')
})
it('#unparse forgets about downcase', () => {
const formula = '=sum(1,2,3)'
const ast = parser.parse(formula, CellAddress.absolute(0, 0, 0)).ast
const unparsed = unparser.unparse(ast, adr('A1'))
expect(unparsed).toEqual('=SUM(1,2,3)')
})
it('#unparse forgets about spaces', () => {
const formula = '= 1 + sum(1,2, 3)'
const ast = parser.parse(formula, CellAddress.absolute(0, 0, 0)).ast
const unparsed = unparser.unparse(ast, adr('A1'))
expect(unparsed).toEqual('=1+SUM(1,2,3)')
})
it('#unparse forgets about OFFSET', () => {
const formula = '=OFFSET(C3, 1, 1)'
const ast = parser.parse(formula, CellAddress.absolute(0, 0, 0)).ast
const unparsed = unparser.unparse(ast, adr('A1'))
expect(unparsed).toEqual('=D4')
})
it('#unparse doesnt forget about unnecessary parenthesis', () => {
const formula = '=(1+2)'
const ast = parser.parse(formula, CellAddress.absolute(0, 0, 0)).ast
const unparsed = unparser.unparse(ast, adr('A1'))
expect(unparsed).toEqual('=(1+2)')
})
it('#unparse forgets about unnecessary sheet reference', () => {
const formula = '=Sheet1!C3'
const ast = parser.parse(formula, CellAddress.absolute(0, 0, 0)).ast
const unparsed = unparser.unparse(ast, adr('A1'))
expect(unparsed).toEqual('=C3')
})
it('#unparse necessary parenthesis from left subtree', () => {
const formula = '=(1+2)*3'
const ast = parser.parse(formula, CellAddress.absolute(0, 0, 0)).ast
const unparsed = unparser.unparse(ast, adr('A1'))
expect(unparsed).toEqual('=(1+2)*3')
})
it('#unparse necessary parenthesis from right subtree', () => {
const formula = '=3*(1+2)'
const ast = parser.parse(formula, CellAddress.absolute(0, 0, 0)).ast
const unparsed = unparser.unparse(ast, adr('A1'))
expect(unparsed).toEqual('=3*(1+2)')
})
it('#unparse doesnt use parenthesis for the same operations', () => {
const formula = '=4*3*2'
const ast = parser.parse(formula, CellAddress.absolute(0, 0, 0)).ast
const unparsed = unparser.unparse(ast, adr('A1'))
expect(unparsed).toEqual('=4*3*2')
})
it('#unparse doesnt use parenthesis for different operations of same precedece', () => {
const formula1 = '=4/3*2'
const formula2 = '=4*3/2'
const ast1 = parser.parse(formula1, CellAddress.absolute(0, 0, 0)).ast
const ast2 = parser.parse(formula2, CellAddress.absolute(0, 0, 0)).ast
const unparsed1 = unparser.unparse(ast1, adr('A1'))
const unparsed2 = unparser.unparse(ast2, adr('A1'))
expect(unparsed1).toEqual(formula1)
expect(unparsed2).toEqual(formula2)
})
it('#unparse doesnt use parenthesis for functions or other non-operator node types', () => {
const formula1 = '=TRUE()*2'
const formula2 = '=2*TRUE()'
const ast1 = parser.parse(formula1, CellAddress.absolute(0, 0, 0)).ast
const ast2 = parser.parse(formula2, CellAddress.absolute(0, 0, 0)).ast
const unparsed1 = unparser.unparse(ast1, adr('A1'))
const unparsed2 = unparser.unparse(ast2, adr('A1'))
expect(unparsed1).toEqual(formula1)
expect(unparsed2).toEqual(formula2)
})
it('#unparse necessary parenthesis from subtree', () => {
const formula = '=-(3+4)'
const ast = parser.parse(formula, CellAddress.absolute(0, 0, 0)).ast
const unparsed = unparser.unparse(ast, adr('A1'))
expect(unparsed).toEqual(formula)
})
it('#unparse double unary minus', () => {
const formula = '=-(-(3+4))'
const ast = parser.parse(formula, CellAddress.absolute(0, 0, 0)).ast
const unparsed = unparser.unparse(ast, adr('A1'))
expect(unparsed).toEqual(formula)
})
it('#unparse doesnt use parenthesis for non-operator node types', () => {
const formula = '=-42'
const ast = parser.parse(formula, CellAddress.absolute(0, 0, 0)).ast
const unparsed = unparser.unparse(ast, adr('A1'))
expect(unparsed).toEqual(formula)
})
it('#unparse percent', () => {
const formula = '=42%'
const ast = parser.parse(formula, CellAddress.absolute(0, 0, 0)).ast
const unparsed = unparser.unparse(ast, adr('A1'))
expect(unparsed).toEqual(formula)
})
it('#unparse use language configuration', () => {
const configEN = new Config({ language: enGB })
const configPL = new Config({ language: plPL })
const parser = new ParserWithCaching(configPL, sheetMapping.get)
const unparserPL = new Unparser(configPL, buildLexerConfig(configPL), sheetMapping.name)
const unparserEN = new Unparser(configEN, buildLexerConfig(configEN), sheetMapping.name)
const formula = '=SUMA(1,2)'
const ast = parser.parse(formula, CellAddress.absolute(0, 0, 0)).ast
expect(unparserPL.unparse(ast, adr('A1'))).toEqual('=SUMA(1,2)')
expect(unparserEN.unparse(ast, adr('A1'))).toEqual('=SUM(1,2)')
})
it('unparsing sheet names in references sometimes have to wrap in quotes', () => {
const formula = "='Sheet with spaces'!A1"
const ast = parser.parse(formula, CellAddress.absolute(1, 0, 0)).ast
const unparsed = unparser.unparse(ast, adr('A1', 1))
expect(unparsed).toEqual(formula)
})
it('unparsing sheet names in ragnes sometimes have to wrap in quotes', () => {
const formula = "='Sheet with spaces'!A1:B1"
const ast = parser.parse(formula, CellAddress.absolute(0, 0, 0)).ast
const unparsed = unparser.unparse(ast, adr('A1', 0))
expect(unparsed).toEqual(formula)
})
})