forked from yanhaijing/template.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
119 lines (98 loc) · 3.77 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
/**
* @file 测试文件
* @author 颜海镜
* @date 2016年1月20日 21:55:57
*/
var expect = require('expect.js');
var template = require('../template.js');
var t = template;
describe('template.js', function() {
this.timeout(1000);
describe('template', function() {
it('编译', function() {
expect(typeof t('')).to.equal('function');
});
it('简单html', function() {
var tpl1 = '<div>123</div>';
expect(t(tpl1, {})).to.equal(tpl1);
});
it('输出简单变量', function() {
var tpl2 = '<div><%=name%></div>';
expect(t(tpl2, {name: 123})).to.equal('<div>123</div>');
});
it('输出html', function() {
var tpl2 = '<div><%=html%></div>';
expect(t(tpl2, {html: '<div id="test">'})).to.equal('<div><div id="test"></div>');
var tpl2 = '<div><%:h=html%></div>';
expect(t(tpl2, {html: '<div id="test">'})).to.equal('<div><div id="test"></div>');
});
it('输出变量', function() {
var tpl3 = '<div><%:=text%></div>';
expect(t(tpl3, {text: '<div>'})).to.equal('<div><div></div>');
});
it('输出url', function() {
var tpl3 = '<%:u=url%>';
expect(t(tpl3, {url: 'http://yanhaijing.com?page=颜海镜'})).to.equal('http://yanhaijing.com?page=%E9%A2%9C%E6%B5%B7%E9%95%9C');
});
it('自定义变量', function() {
var tpl = '<%var name = 123;%><%=name%>';
expect(t(tpl, {})).to.equal('123');
});
it('省略结尾分号不会报错', function() {
var tpl = '<%var a = 1%><%=a%>';
expect(t(tpl, {})).to.equal('1');
});
it('if语句', function() {
var tpl = '<%if (1) {%>a<%}%>';
expect(t(tpl, {})).to.equal('a');
});
it('for语句', function() {
var tpl = '<%for(var i = 0; i < 2; i++) {%>a<%}%>';
expect(t(tpl, {})).to.equal('aa');
});
it('while语句', function() {
var tpl = '<%var a = 3;while(a--) {%>a<%}%>';
expect(t(tpl, {})).to.equal('aaa');
});
it('引用空变量返回空字符串', function() {
var tpl = '<%=a%>';
expect(t(tpl, {})).to.equal('');
});
});
describe('template.config', function() {
it('sTag & eTag', function() {
var tpl = '<#=name%>';
t.config({sTag: '<#'});
expect(t(tpl, {name: 123})).to.equal('123');
var tpl = '<%=name#>';
t.config({sTag: '<%', eTag: '#>'});
expect(t(tpl, {name: 123})).to.equal('123');
});
it('compress', function() {
var tpl = '<div> </div>';
t.config({sTag: '<%', eTag: '%>', compress: true});
expect(t(tpl, {})).to.equal('<div> </div>');
});
it('测试escape', function() {
var tpl = '<%=html%>';
t.config({sTag: '<%', eTag: '%>', compress: false, escape: false});
expect(t(tpl, {html: '<div>'})).to.equal('<div>');
});
});
describe('template modifier&function', function() {
it('registerModifier', function() {
t.registerModifier('uc', function (data) {
return data.toUpperCase();
});
var tpl = '<%:uc="yan"%>';
expect(t(tpl, {})).to.equal('YAN');
});
it('registerFunction', function() {
t.registerFunction('upperCase', function (data) {
return data.toUpperCase();
});
var tpl = '<%=upperCase("yan")%>';
expect(t(tpl, {})).to.equal('YAN');
});
});
});