-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest.js
121 lines (92 loc) · 3.89 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
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
describe('replaces', function () {
'use strict';
var assume = require('assume')
, replaces = require('./')
, re = /{test(\W+)([^}]+?)}/g;
it('is exported as function', function () {
assume(replaces).is.a('function');
});
it('replaces multipe tags in a string', function () {
var tpl = 'woop {test:tag} {test:tag}'
, data = { tag: 'woop' };
assume(replaces(tpl, re, data)).equals('woop woop woop');
});
it('looks up multiple values in a string', function () {
var tpl = 'foo {test:bar} {test:baz}'
, data = { baz: 'woop', bar: 'oi' };
assume(replaces(tpl, re, data)).equals('foo oi woop');
});
it('can return deeply nested data', function () {
var no = 'no {test:deep.nesting.no}'
, yes = 'yes {test:deep.nesting.yes}'
, data = { deep: { nesting: { yes: true, no: false }} };
assume(replaces(yes, re, data)).equals('yes true');
assume(replaces(no, re, data)).equals('no false');
});
it('can find data on prop, even if it uses dot notation', function () {
var yes = 'yes {test:deep.nesting.yes}'
, data = { 'deep.nesting.yes': true };
assume(replaces(yes, re, data)).equals('yes true');
});
it('can return data from an nested array', function () {
var no = 'no {test:deep.nesting.1.what}'
, yes = 'yes {test:deep.nesting.0.what}'
, data = { deep: { nesting: [ { what: 'yes' }, { what: 'nope' } ]} };
assume(replaces(yes, re, data)).equals('yes yes');
assume(replaces(no, re, data)).equals('no nope');
});
it('the found data is `toString()`d', function () {
var tpl = 'hello {test:data}'
, data = { data: { toString: function () { return 'world'; }}};
assume(replaces(tpl, re, data)).equals('hello world');
});
describe('modifiers', function () {
describe('<>', function () {
it('escapes the output as HTML', function () {
var tpl = '{test<>html}, {test:html}'
, data = { html: '<div>' };
assume(replaces(tpl, re, data)).equals('<div>, <div>');
});
it('does not escape or break slashes', function () {
var tpl = '{test<>html}, {test:html}'
, data = { html: '/3846c91f105fc08b0b5993c7045c314553f28052.js' };
assume(replaces(tpl, re, data)).equals(data.html +', '+ data.html);
});
});
describe('~', function () {
it('escapes the output as JSON', function () {
var tpl = '{test~data}, {test:data}'
, data = { data: { structure: 'hi' } };
assume(replaces(tpl, re, data)).equals('{"structure":"hi"}, [object Object]');
});
});
describe('%', function () {
it('escapes the output', function () {
var tpl = '{test%data}, {test:data}'
, data = { data: 'hello world' };
assume(replaces(tpl, re, data)).equals('hello%20world, hello world');
});
});
describe('@', function () {
it('escapes circular references', function () {
var data = { foo: 'bar', data: { foo: 'foo' } }
, tpl = '{test@data}, {test:data}';
data.data.data = data.data;
assume(replaces(tpl, re, data)).equals('{"foo":"foo","data":"[Circular ~]"}, [object Object]');
});
});
describe('$', function () {
it('escapes circular references', function () {
var data = { foo: 'bar', data: { foo: '<div class="woop">hi</div>' } }
, tpl = '{test$data}, {test:data}';
data.data.data = data.data;
assume(replaces(tpl, re, data)).equals('{"foo":"<div class="woop">hi</div>","data":"[Circular ~]"}, [object Object]');
});
it('escapes HTML inside the JSON', function () {
var tpl = '{test$data}, {test:data}'
, data = { data: { structure: '<div class="woop">hi</div>' } };
assume(replaces(tpl, re, data)).equals('{"structure":"<div class="woop">hi</div>"}, [object Object]');
});
});
});
});