-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcorner-cases.tests.js
More file actions
189 lines (168 loc) · 5.26 KB
/
corner-cases.tests.js
File metadata and controls
189 lines (168 loc) · 5.26 KB
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
const fs = require('fs')
const path = require('path')
const { Pair, parse, parseLines, stringify } = require('../lib/index')
describe('lines', () => {
const srcPath = path.resolve(
__dirname,
'node-properties-parser',
'test.properties'
)
const src = fs.readFileSync(srcPath, 'utf8')
const exp = [
'# You are reading the ".properties" entry.',
'! The exclamation mark can also mark text as comments.',
['lala', '\u210A the foo foo lalala;'],
['website', 'http://en.wikipedia.org/'],
['language', 'English'],
'# The backslash below tells the application to continue reading',
'# the value onto the next line.',
['message', 'Welcome to Wikipedia!'],
'# Add spaces to the key',
[
'key with spaces',
'This is the value that could be looked up with the key "key with spaces".'
],
'# Unicode',
['tab', '\u0009'],
['long-unicode', '\u00000009'],
['space separator', 'key val \n three'],
['another-test', ':: hihi'],
['null-prop', '']
]
test('read lines', () => {
const lines = parseLines(src + '\n\n')
expect(lines).toMatchObject([...exp, '', ''])
})
test('read lines with CRLF endings', () => {
const src = `language = English\r\nmessage = Welcome to \\\r\n Wikipedia!\r\n\r\n`
const lines = parseLines(src)
expect(lines).toMatchObject([
['language', 'English'],
['message', 'Welcome to Wikipedia!'],
''
])
})
test('write lines', () => {
const str = stringify(exp)
expect(parseLines(str)).not.toMatchObject(exp)
const fix = str.replace('# The exclamation mark', '! The exclamation mark')
expect(parseLines(fix)).toMatchObject(exp)
})
})
describe('stringify', () => {
test('empty input', () => {
const res = stringify(null)
expect(res).toBe('')
})
test('ascii', () => {
const src = 'ipsum áé ĐѺ lore\0'
const exp = 'ipsum áé \\u0110\\u047a lore\\u0000'
const res0 = stringify([['', src]], { keySep: '' })
const res1 = stringify([['', src]], { latin1: false, keySep: '' })
expect(res0).toBe(exp)
expect(res1).toBe(src.slice(0, -1) + '\\u0000')
})
test('\\\\ overdose', () => {
const slash = '\\'.repeat(200)
expect(() => stringify([[slash + slash]])).not.toThrow()
})
test('manual line breaks', () => {
const lorem = `\r
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed\r
do eiusmod tempor incididunt ut labore et dolore magna\r
aliqua. Ut enim ad minim veniam, quis nostrud exercitation\r
ullamco laboris nisi ut aliquip ex ea commodo consequat.\r
Duis aute irure dolor in reprehenderit in voluptate velit\r
esse cillum dolore eu fugiat nulla pariatur. Excepteur sint\r
occaecat cupidatat non proident, sunt in culpa qui officia\r
deserunt mollit anim id est laborum.`
expect(stringify([lorem])).toBe(lorem.replace(/\r\n/gm, '\n# ').trim())
expect(stringify([['key', lorem]])).toBe(
'key = \\\n ' + lorem.replace(/\r\n/g, '\\r\\n\\\n ')
)
})
test('lines with empty strings result in blank lines', () => {
const emptyLine = ''
expect(stringify([emptyLine])).toBe(emptyLine)
const lines = [['key1', 'value1'], '', ['key2', 'value2']]
expect(stringify(lines)).toBe('key1 = value1\n\nkey2 = value2')
})
test('empty comments', () => {
const lines = [['key1', 'value1'], '#', '! ', ['key2', 'value2']]
expect(stringify(lines)).toBe('key1 = value1\n# \n# \nkey2 = value2')
})
test('Negative linewidth', () => {
const foo = 'foo '.repeat(200)
expect(stringify([foo], { lineWidth: -1 })).toBe(`# ${foo}`)
})
test('Whitespace in comment at folding point', () => {
const lines = ['comment\rcomment\ncomment\fcomment\t']
expect(stringify(lines, { lineWidth: 10 })).toBe(
'# comment\n# comment\n# comment\f\n# comment\t'
)
})
})
describe('options', () => {
const obj = {
'': 'root',
a: { '': 'A.', a: 'A.A' },
b: { '': 'B', b: 'B.B' }
}
test('read default values', () => {
const src = `:root
a:A
a.:A.
a.a:A.A
b.b:B.B
b:B`
expect(parse(src, true)).toMatchObject(obj)
})
test('write default values', () => {
const src = `:root
a:A.
a.a:A.A
b:B
b.b:B.B`
expect(stringify(obj, { keySep: ':' })).toBe(src)
})
test('custom path separator', () => {
const src = `:root
a:A
a/:A.
a/a:A.A
b/b:B.B
b:B`
expect(parse(src, '/')).toMatchObject(obj)
})
})
describe('bad input', () => {
test('malformed unicode escape', () => {
const src = `foo: \\uabcx`
expect(parse(src, true)).toEqual({ foo: 'uabcx' })
})
test('prototype pollution', () => {
const src = '__proto__.pollutedKey = pollutedValue'
expect(parse(src, true)).toEqual({})
expect({}.pollutedKey).not.toBeDefined()
})
})
describe('AST', () => {
test('pair separator', () => {
const src = 'key:\nkey2: value2'
const ast = parseLines(src, true)
expect(ast[0].separator(src)).toBe(':')
expect(ast[1].separator(src)).toBe(': ')
})
test('add node', () => {
const src = 'key:\nkey2: value2'
const ast = parseLines(src, true)
ast.push(new Pair('key3', 'value3'))
expect(ast[2].separator(src)).toBeNull()
expect(parse(ast)).toMatchObject({
key: '',
key2: 'value2',
key3: 'value3'
})
expect(stringify(ast)).toBe('key = \nkey2 = value2\nkey3 = value3')
})
})