-
Notifications
You must be signed in to change notification settings - Fork 106
/
white-spaces.spec.ts
141 lines (122 loc) · 5.9 KB
/
white-spaces.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
import {Config} from '../../src/Config'
import {SheetMapping} from '../../src/DependencyGraph'
import {buildTranslationPackage, enGB} from '../../src/i18n'
import {buildLexerConfig, FormulaLexer} from '../../src/parser'
import {CellReference, EqualsOp, ProcedureName, RangeSeparator, RParen, WhiteSpace} from '../../src/parser/LexerConfig'
import {bindWhitespacesToTokens} from '../../src/parser/ParserWithCaching'
import {expectArrayWithSameContent} from '../testUtils'
describe('tokenizeFormula', () => {
const config = new Config()
const sheetMapping = new SheetMapping(buildTranslationPackage(enGB))
sheetMapping.addSheet('Sheet1')
const lexer = new FormulaLexer(buildLexerConfig(config))
it('should not skip whitespaces', () => {
const tokens = lexer.tokenizeFormula('= A1 + 2').tokens
expect(tokens.length).toBe(7)
})
it('should forget about trailing whitespaces', () => {
const tokens = lexer.tokenizeFormula('=SUM(A1:A2) ').tokens
expect(tokens.length).toBe(6)
expect(tokens.map(token => token.tokenType).find(tokenType => tokenType === WhiteSpace)).toBe(undefined)
})
it('should skip whitespace inside range', () => {
const tokens = lexer.tokenizeFormula('=A1: A1').tokens
const tokenTypes = tokens.map(token => token.tokenType.name)
expectArrayWithSameContent(tokenTypes, ['EqualsOp', 'CellReference', 'RangeSeparator', 'CellReference'])
})
it('should skip whitespace inside range 2', () => {
const tokens = lexer.tokenizeFormula('=A1 :A1').tokens
const tokenTypes = tokens.map(token => token.tokenType.name)
expectArrayWithSameContent(tokenTypes, ['EqualsOp', 'CellReference', 'RangeSeparator', 'CellReference'])
})
it('should skip whitespace inside range 3', () => {
const tokens = lexer.tokenizeFormula('=A1 : A1').tokens
const tokenTypes = tokens.map(token => token.tokenType.name)
expectArrayWithSameContent(tokenTypes, ['EqualsOp', 'CellReference', 'RangeSeparator', 'CellReference'])
})
it('should skip whitespace before function args separator', () => {
const tokens = lexer.tokenizeFormula('=SUM(A1 , A2)').tokens
const tokenTypes = tokens.map(token => token.tokenType.name)
expectArrayWithSameContent(tokenTypes, ['EqualsOp', 'ProcedureName', 'CellReference', 'ArgSeparator', 'WhiteSpace', 'CellReference', 'RParen'])
})
it('should treat space as whitespace', () => {
const tokens = lexer.tokenizeFormula('= 1').tokens
expect(tokens[1].tokenType).toEqual(WhiteSpace)
})
it('should treat tabulator (U+0009) as whitespace', () => {
const tokens = lexer.tokenizeFormula('=\t1').tokens
expect(tokens[1].tokenType).toEqual(WhiteSpace)
})
it('should treat carriage return (U+000D) as whitespace', () => {
const tokens = lexer.tokenizeFormula('=\r1').tokens
expect(tokens[1].tokenType).toEqual(WhiteSpace)
})
it('should treat line feed (U+000A) as whitespace ', () => {
const tokens = lexer.tokenizeFormula('=\n1').tokens
expect(tokens[1].tokenType).toEqual(WhiteSpace)
})
it('should treat multiple whitespaces as one token', () => {
const tokens = lexer.tokenizeFormula('=\n\t\r 1').tokens
expect(tokens.length).toEqual(3)
expect(tokens[1].tokenType).toEqual(WhiteSpace)
expect(tokens[1].image).toEqual('\n\t\r ')
})
})
describe('processWhitespaces', () => {
const config = new Config()
const sheetMapping = new SheetMapping(buildTranslationPackage(enGB))
sheetMapping.addSheet('Sheet1')
const lexer = new FormulaLexer(buildLexerConfig(config))
it('should do nothing when no whitespaces', () => {
const tokens = lexer.tokenizeFormula('=SUM(A1:A2)').tokens
const processed = bindWhitespacesToTokens(tokens)
expect(processed.length).toBe(6)
expect(processed.map(processed => processed.leadingWhitespace).every(processed => processed === undefined)).toBe(true)
expectArrayWithSameContent(
[EqualsOp, ProcedureName, CellReference, RangeSeparator, CellReference, RParen],
processed.map(token => token.tokenType)
)
})
it('should add leading whitespace to token', () => {
const tokens = lexer.tokenizeFormula('= SUM(A1:A2)').tokens
const processed = bindWhitespacesToTokens(tokens)
expect(processed.length).toBe(6)
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
expect(processed[1].leadingWhitespace!.image).toBe(' ')
expectArrayWithSameContent(
[EqualsOp, ProcedureName, CellReference, RangeSeparator, CellReference, RParen],
processed.map(token => token.tokenType)
)
})
it('should work for multiple whitespaces', () => {
const tokens = lexer.tokenizeFormula('= SUM(A1:A2)').tokens
const processed = bindWhitespacesToTokens(tokens)
expect(processed.length).toBe(6)
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
expect(processed[1].leadingWhitespace!.image).toBe(' ')
expectArrayWithSameContent(
[EqualsOp, ProcedureName, CellReference, RangeSeparator, CellReference, RParen],
processed.map(token => token.tokenType)
)
})
it('should work for whitespace at the beginning', () => {
const tokens = lexer.tokenizeFormula(' =SUM(A1:A2)').tokens
const processed = bindWhitespacesToTokens(tokens)
expect(processed.length).toBe(6)
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
expect(processed[0].leadingWhitespace!.image).toBe(' ')
expectArrayWithSameContent(
[EqualsOp, ProcedureName, CellReference, RangeSeparator, CellReference, RParen],
processed.map(token => token.tokenType)
)
})
it('should not include whitespaces directly on the list', () => {
const tokens = lexer.tokenizeFormula('= SUM( A1:A2) ').tokens
const processed = bindWhitespacesToTokens(tokens)
expect(processed.length).toBe(6)
expectArrayWithSameContent(
[EqualsOp, ProcedureName, CellReference, RangeSeparator, CellReference, RParen],
processed.map(token => token.tokenType)
)
})
})