/
tests.js
179 lines (177 loc) · 4.97 KB
/
tests.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
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
#!/usr/bin/env node
const assert = require('assert');
const jfTpl = require('./index');
function testTpl(config, expected)
{
assert.equal(jfTpl(config), expected.tpl);
if (expected.context)
{
assert.deepStrictEqual(config.context, expected.context);
}
if (expected.defaults)
{
assert.deepStrictEqual(config.defaults, expected.defaults);
}
}
const context = {
name : 'Guest',
site : 'home'
};
const defaults = {
name : 'User',
osite : 'university'
};
//------------------------------------------------------------------------------
// Verificamos que si no hay plantilla no falle.
//------------------------------------------------------------------------------
testTpl({ tpl : '' }, { tpl : '' });
//------------------------------------------------------------------------------
// Verificamos que si no hay contexto no falle.
//------------------------------------------------------------------------------
testTpl(
{
tpl : 'Hello {name}, welcome to {site}'
},
{
tpl : 'Hello , welcome to '
}
);
testTpl(
{
tpl : 'Hello {name}, welcome to {site}',
keep : true
},
{
tpl : 'Hello {name}, welcome to {site}'
}
);
//------------------------------------------------------------------------------
// Verificamos que no se modifiquen los valores por defecto.
//------------------------------------------------------------------------------
testTpl(
{
context,
tpl : 'Hello {name}, welcome to {site}'
},
{
context,
tpl : 'Hello Guest, welcome to home'
}
);
//------------------------------------------------------------------------------
// Verificamos el uso de left y right usando caracteres usados en RegExp.
// Verificamos que no se modifiquen ni el contexto ni los valores por defecto.
//------------------------------------------------------------------------------
[['<[', ']>'], ['{{', '}}'], ['{{{', '}}}'], ['[', ']'], ['[[', ']]'], ['[[[', ']]]'], ['/', '*']].forEach(
delims => testTpl(
{
tpl : `Hello ${delims[0]}name${delims[1]}, welcome to ${delims[0]}osite${delims[1]}`,
left : delims[0],
right : delims[1],
context,
defaults
},
{
tpl : 'Hello Guest, welcome to university',
context : Object.assign({}, context),
defaults : Object.assign({}, defaults)
}
)
);
//------------------------------------------------------------------------------
// Verificamos el uso de left y right.
// Verificamos el uso de delKeys, modificando tanto el contexto como los valores
// por defecto dependiendo de quién aporte el valor.
//------------------------------------------------------------------------------
testTpl(
{
tpl : 'Hello {name} {name}, welcome to {osite}',
delKeys : true,
context,
defaults
},
{
tpl : 'Hello Guest Guest, welcome to university',
context : { site : 'home' },
defaults : { name : 'User' }
}
);
//------------------------------------------------------------------------------
// Verificamos el uso de keep.
//------------------------------------------------------------------------------
testTpl(
{
tpl : 'Hello {name}, welcome to {osite}',
keep : true,
context : {
name : 'User'
}
},
{
tpl : 'Hello User, welcome to {osite}',
context : {
name : 'User'
}
}
);
//------------------------------------------------------------------------------
// Verificamos que si un valor es null o undefined no se reemplace.
//------------------------------------------------------------------------------
testTpl(
{
context : {
name : undefined,
site : null
},
tpl : 'Hello {name}, welcome to {site}',
keep : true
},
{
tpl : 'Hello {name}, welcome to {site}'
}
);
testTpl(
{
context : {
name : undefined,
site : null
},
tpl : 'Hello {name}, welcome to {site}'
},
{
tpl : 'Hello , welcome to '
}
);
//------------------------------------------------------------------------------
// Verificamos el uso de un array como contexto.
//------------------------------------------------------------------------------
testTpl(
{
context : ['Abcdef', 'Xyz'],
tpl : 'Hello {0}, welcome to {1}'
},
{
context : ['Abcdef', 'Xyz'],
tpl : 'Hello Abcdef, welcome to Xyz'
}
);
testTpl(
{
context : ['Abcdef', 'Xyz'],
tpl : 'Hello {0}, welcome to {1} {2}',
delKeys : true
},
{
tpl : 'Hello Abcdef, welcome to Xyz '
}
);
testTpl(
{
defaults : ['Abcdef', 'Xyz'],
tpl : 'Hello {0}, welcome to {1}',
delKeys : true
},
{
tpl : 'Hello Abcdef, welcome to Xyz'
}
);