-
-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathtest.js
85 lines (71 loc) · 2.37 KB
/
test.js
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
import test from 'ava';
import pupa, {MissingValueError} from './index.js';
test('main', t => {
// Normal placeholder
t.is(pupa('{foo}', {foo: '!'}), '!');
t.is(pupa('{foo}', {foo: 10}), '10');
t.is(pupa('{foo}', {foo: 0}), '0');
t.is(pupa('{fo-o}', {'fo-o': 0}), '0');
t.is(pupa('{foo}{foo}', {foo: '!'}), '!!');
t.is(pupa('{foo}{bar}{foo}', {foo: '!', bar: '#'}), '!#!');
t.is(pupa('yo {foo} lol {bar} sup', {foo: '🦄', bar: '🌈'}), 'yo 🦄 lol 🌈 sup');
t.is(pupa('{foo}{deeply.nested.valueFoo}', {
foo: '!',
deeply: {
nested: {
valueFoo: '#',
},
},
}), '!#');
t.is(pupa('{0}{1}', ['!', '#']), '!#');
// Encoding HTML Entities to avoid code injection
t.is(pupa('{{foo}}', {foo: '!'}), '!');
t.is(pupa('{{foo}}', {foo: 10}), '10');
t.is(pupa('{{foo}}', {foo: 0}), '0');
t.is(pupa('{{foo}}{{foo}}', {foo: '!'}), '!!');
t.is(pupa('{foo}{{bar}}{foo}', {foo: '!', bar: '#'}), '!#!');
t.is(pupa('yo {{foo}} lol {{bar}} sup', {foo: '🦄', bar: '🌈'}), 'yo 🦄 lol 🌈 sup');
t.is(pupa('{foo}{{deeply.nested.valueFoo}}', {
foo: '!',
deeply: {
nested: {
valueFoo: '<br>#</br>',
},
},
}), '!<br>#</br>');
t.is(pupa('{{0}}{{1}}', ['!', '#']), '!#');
t.is(pupa('{{0}}{{1}}', ['<br>yo</br>', '<i>lol</i>']), '<br>yo</br><i>lol</i>');
});
test('do not match non-identifiers', t => {
const fixture = '"*.{json,md,css,graphql,html}"';
t.is(pupa(fixture, []), fixture);
});
test('ignore missing', t => {
const template = 'foo{{bar}}{undefined}';
const options = {ignoreMissing: true};
t.is(pupa(template, {}, options), template);
});
test('throw on undefined by default', t => {
t.throws(() => {
pupa('{foo}', {});
}, {instanceOf: MissingValueError});
});
test('transform and ignore missing', t => {
const options = {
ignoreMissing: true,
transform: ({value}) => Number.isNaN(Number.parseInt(value, 10)) ? undefined : value,
};
t.is(pupa('{0} {1} {2}', ['0', 42, 3.14], options), '0 42 3.14');
t.is(pupa('{0} {1} {2}', ['0', null, 3.14], options), '0 {1} 3.14');
});
test('transform and throw on undefined', t => {
const options = {
transform: ({value}) => Number.isNaN(Number.parseInt(value, 10)) ? undefined : value,
};
t.notThrows(() => {
pupa('{0} {1} {2}', ['0', 42, 3.14], options);
});
t.throws(() => {
pupa('{0} {1} {2}', ['0', null, 3.14], options);
}, {instanceOf: MissingValueError});
});