1
+ import * as fs from 'node:fs/promises'
1
2
import properties from '.'
2
3
3
4
describe ( 'parse' , ( ) => {
4
5
it ( 'should parse all lines' , ( ) => {
5
6
// Data
6
- const sample = 'registry=https://abcd\n#foo bar\n@scope:test=avx\n'
7
+ const sample = 'registry=https://abcd\n#foo bar\r\ n@scope:test=avx\rextra\r \n'
7
8
8
9
// Test
9
10
const result = properties . parse ( sample )
10
11
expect ( result . lines ) . toEqual ( [
11
12
'registry=https://abcd' ,
12
13
'#foo bar' ,
13
- '@scope:test=avx'
14
+ '@scope:test=avx' ,
15
+ 'extra'
14
16
] )
15
17
} )
16
18
} )
@@ -128,6 +130,19 @@ describe('data access', () => {
128
130
expect ( result ) . toBe ( expected )
129
131
} )
130
132
133
+ it ( 'should return last value of duplicate key' , ( ) => {
134
+ const config : properties . Properties = {
135
+ lines : [
136
+ 'key1=foo1' ,
137
+ 'key2=foo2' ,
138
+ 'key1=foo3'
139
+ ]
140
+ }
141
+
142
+ const result = properties . get ( config , 'key1' )
143
+ expect ( result ) . toBe ( 'foo3' )
144
+ } )
145
+
131
146
it . each ( [
132
147
[ 'foo' , 'bar' , 'foo=bar' ] ,
133
148
[ 'foo8:' , 'bar8' , 'foo8\\:=bar8' ] ,
@@ -218,6 +233,22 @@ describe('data access', () => {
218
233
] )
219
234
} )
220
235
236
+ it ( 'should remove duplicate keys on set' , ( ) => {
237
+ const config : properties . Properties = {
238
+ lines : [
239
+ 'key1=foo1' ,
240
+ 'key2=foo2' ,
241
+ 'key1=foo3'
242
+ ]
243
+ }
244
+
245
+ properties . set ( config , 'key1' , 'test' )
246
+ expect ( config . lines ) . toEqual ( [
247
+ 'key1=test' ,
248
+ 'key2=foo2'
249
+ ] )
250
+ } )
251
+
221
252
it ( 'should remove existing key with set undefined' , ( ) => {
222
253
const config : properties . Properties = {
223
254
lines : [ 'foo=bar' ]
@@ -234,6 +265,19 @@ describe('data access', () => {
234
265
expect ( config . lines ) . toEqual ( [ ] )
235
266
} )
236
267
268
+ it ( 'should remove all duplicate keys with remove' , ( ) => {
269
+ const config : properties . Properties = {
270
+ lines : [
271
+ 'key1=foo1' ,
272
+ 'key2=foo2' ,
273
+ 'key1=foo3'
274
+ ]
275
+ }
276
+
277
+ properties . remove ( config , 'key1' )
278
+ expect ( config . lines ) . toEqual ( [ 'key2=foo2' ] )
279
+ } )
280
+
237
281
describe ( 'list' , ( ) => {
238
282
it ( 'should list all key-value pairs' , ( ) => {
239
283
const result = [ ...properties . list ( sample ) ]
@@ -293,6 +337,40 @@ describe('data access', () => {
293
337
[ 'c' , 'd' ]
294
338
] )
295
339
} )
340
+
341
+ it ( 'should parse test file' , async ( ) => {
342
+ const contents = await fs . readFile ( '../fixtures/test-all.properties' , 'utf-8' )
343
+
344
+ // Parse
345
+ const result = properties . toMap ( properties . parse ( contents ) )
346
+
347
+ // Verify
348
+ expect ( result ) . toEqual ( {
349
+ '' : 'So does this line.' ,
350
+ category : 'file format' ,
351
+ duplicateKey : 'second' ,
352
+ empty : '' ,
353
+ encodedHelloInJapanese : 'こんにちは' ,
354
+ evenKey : 'This is on one line\\' ,
355
+ 'evenLikeThis\\' : '' ,
356
+ hello : 'hello' ,
357
+ helloInJapanese : 'こんにちは' ,
358
+ keyWithBackslashes : 'This has random backslashes' ,
359
+ 'keyWithDelimiters:= ' : 'This is the value for the key "keyWithDelimiters:= "' ,
360
+ 'keyWitheven\\' : 'this colon is not escaped' ,
361
+ language : 'English' ,
362
+ multiline : 'This line continues on 3 lines' ,
363
+ multilineKey : 'this is a multiline key' ,
364
+ noWhiteSpace : 'The key will be "noWhiteSpace" without any whitespace. ' ,
365
+ oddKey : 'This is line one and\\# This is line two' ,
366
+ orLikeThis : '' ,
367
+ path : 'c:\\wiki\\templates' ,
368
+ topic : '.properties file' ,
369
+ valueWithEscapes : 'This is a newline\n, a carriage return\r, a tab\t and a formfeed\f.' ,
370
+ website : 'https://en.wikipedia.org/' ,
371
+ welcome : 'Welcome to Wikipedia! '
372
+ } )
373
+ } )
296
374
} )
297
375
} )
298
376
0 commit comments