forked from neo13/blade.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBTC-test-suit.js
258 lines (229 loc) · 9.15 KB
/
BTC-test-suit.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
global.__TESTING = true;
var
expect = require("chai").expect,
blade = require("../blade.js"),
btc = blade.__PRIVATE.BTC,
dsc = describe,
btfm = new blade.__PRIVATE.BTFM("template_dir");
dsc("Blade.js Template Compiler", function () {
it("if template is vanilla html it should pass it", function() {
var src = btfm.readTemplate('footer'),
cmp = btc(src, btfm),
fn = new Function(cmp);
expect(fn()).to.be.equal('<div class="footer">\r\n <p>This is Footer</p>\r\n</div>');
});
it("should resolve multiple `include` statement", function () {
var src = btfm.readTemplate('base'),
cmp = btc(src, btfm),
fn = new Function(cmp);
expect(fn()).to.be.equal('<html>\r\n<head>\r\n <title>This is a template test</title>\r\n</head>\r\n<body>\r\n <div class="header">This is the header!</div>\r\n <div class="contents"></div>\r\n <div class="footer">\r\n <p>This is Footer</p>\r\n</div>\r\n</body>\r\n</html>');
});
dsc("should return exception if `include` statement points to invalid template", function () {
it("should return exception if template is empty", function () {
var src = "@include('empty_template_name')",
exception;
try {
btc(src, btfm);
}
catch (e) {
exception = e;
}
expect(exception).to.be.an.instanceof(blade.__PRIVATE.BEH)
.and.to.have.deep.property('err.message', "Empty Template: template_dir/empty_template_name.blade.js");
});
it("should return exception if template dose not exist", function () {
var src = "@include('fake_template_name')",
exception;
try {
btc(src, btfm);
}
catch (e) {
exception = e;
}
expect(exception).to.be.an.instanceof(blade.__PRIVATE.BEH)
.and.to.have.deep.property('err.message', "Template does not exist: template_dir/fake_template_name.blade.js");
});
it("should return exception if template is not accessible", function () {
var src = "@include('no_permission_template_name')",
exception;
try {
btc(src, btfm);
}
catch (e) {
exception = e;
}
expect(exception).to.be.an.instanceof(blade.__PRIVATE.BEH);
});
});
it("should resolve nested `extends` statement", function () {
var src = btfm.readTemplate('site.admin.admin_extends'),
cmp = btc(src, btfm),
fn = new Function(cmp);
expect(fn()).to.be.equal('<html>\r\n<head>\r\n <title>This is a template test</title>\r\n</head>\r\n<body>\r\n <div class="header">This is the header!</div>\r\n <div class="contents"></div>\r\n <div class="footer">\r\n <p>This is Footer</p>\r\n</div>\r\n</body>\r\n</html>');
});
dsc("should return exception if `extends` statement points to invalid template", function () {
it("should return exception if template is empty", function () {
var src = "@extends('empty_template_name')",
exception;
try {
btc(src, btfm);
}
catch (e) {
exception = e;
}
expect(exception).to.be.an.instanceof(blade.__PRIVATE.BEH)
.and.to.have.deep.property('err.message', "Empty Template: template_dir/empty_template_name.blade.js");
});
it("should return exception if template dose not exist", function () {
var src = "@extends('fake_template_name')",
exception;
try {
btc(src, btfm);
}
catch (e) {
exception = e;
}
expect(exception).to.be.an.instanceof(blade.__PRIVATE.BEH)
.and.to.have.deep.property('err.message', "Template does not exist: template_dir/fake_template_name.blade.js");
});
it("should return exception if template is not accessible", function () {
var src = "@extends('no_permission_template_name')",
exception;
try {
btc(src, btfm);
}
catch (e) {
exception = e;
}
expect(exception).to.be.an.instanceof(blade.__PRIVATE.BEH);
});
it("should return exception if template is extending itself", function () {
var src = btfm.readTemplate('extend_itself'),
exception;
try {
btc(src, btfm);
}
catch (e) {
exception = e;
}
expect(exception).to.be.an.instanceof(blade.__PRIVATE.BEH)
.and.to.have.deep.property('err.message', "Inheritance Logic Error: Can't extend a template inside itself!");
});
});
it("should return exception when multiple `extends` is used", function () {
var src = "@extends('base')\n @extends(footer)",
exception;
try {
btc(src, btfm);
}
catch (e) {
exception = e;
}
expect(exception).to.be.an.instanceof(blade.__PRIVATE.BEH)
.and.to.have.deep.property('err.message', "Template Error: Can't Inheritance from more that one template");
});
it("should resolve `yield` and `section` properly", function () {
var src = btfm.readTemplate('site.admin.index'),
cmp = btc(src, btfm),
fn = new Function(cmp);
expect(fn()).to.be.equal('<html>\r\n<head>\r\n <title>This is a template test</title>\r\n</head>\r\n<body>\r\n <div class="header">This is the header!</div>\r\n <div class="contents">\r\n this is site.base\r\n\t\r\n this is site.admin.index header\r\n\r\n\t\r\n this is site.admin.index footer\r\n\r\n\t\r\n this is the copyright\r\n\r\n</div>\r\n <div class="footer">\r\n <p>This is Footer</p>\r\n</div>\r\n</body>\r\n</html>');
});
it("should return exception when using two `section` pointing to one `yield`", function () {
var src = btfm.readTemplate('site.admin.two_section'),
exception;
try {
btc(src, btfm);
}
catch (e) {
exception = e;
}
expect(exception).to.be.an.instanceof(blade.__PRIVATE.BEH)
.and.to.have.deep.property('err.message', "Template Logical Error: More than one `@section` with the same id.\n*Notice* id shall be unique.");
});
dsc("`if` statement", function () {
it("should work if the `if` condition is true", function () {
var src = "@if(c) IF OK @elseif(d) ElseIf OK @else Else OK @endif",
cmp = btc(src, btfm),
injection = [
"var c=true, d=false;",
"var c=true, d=true;",
];
for (i in injection) {
fn = new Function(injection[i]+cmp);
expect(fn()).to.be.equal(' IF OK ');
}
});
it('should work if `if` condition is not true but the `elseif` condition is true', function () {
var src = "@if(c) IF OK @elseif(d) ElseIf OK @else Else OK @endif",
cmp = btc(src, btfm),
injection = "var c=false, d=true;",
fn = new Function(injection+cmp);
expect(fn()).to.be.equal(' ElseIf OK ');
});
it('should work if `if` condition is not true and the `elseif` condition is not true either', function () {
var src = "@if(c) IF OK @elseif(d) ElseIf OK @else Else OK @endif",
cmp = btc(src, btfm),
injection = "var c=false, d=false;",
fn = new Function(injection+cmp);
expect(fn()).to.be.equal(' Else OK ');
});
});
dsc("`unless` statement", function () {
it("should replace `unless` containments if the condition have been meet", function () {
var src = "@unless(c) Unless OK @endunless",
cmp = btc(src, btfm),
injection = "var c=true;";
fn = new Function(injection+cmp);
expect(fn()).to.be.equal(' Unless OK ');
});
it("should not replace `unless` containments if the condition is not true", function () {
var src = "@unless(c) Unless OK @endunless",
cmp = btc(src, btfm),
injection = "var c=false;",
fn = new Function(injection+cmp);
expect(fn()).to.be.equal('');
});
});
it("should compile `for` statement clearly", function () {
var src = "@for(i=0; i<5; i=i+1) For OK @endfor",
cmp = btc(src, btfm),
fn = new Function(cmp);
expect(fn()).to.be.equal(' For OK For OK For OK For OK For OK ');
});
dsc("`foreach` statement", function () {
it("should compile when using Lists", function () {
var src = "@foreach( users as user ) @if(user.status === 'admin') welcome admin, {{ user.name }} @elseif (user.status === 'reseller' ) welcome reseller, {{user.name}} @else welcome user, {{user.name }}@endif @endforeach",
cmp = btc(src, btfm),
injection = "var users = [ { 'name': 'ali' }, { 'name': 'hasan' }, { 'name': 'taghi' }];";
fn = new Function(injection+cmp);
expect(fn()).to.be.equal('\t\t\twelcome user, ali \t\t\twelcome user, hasan \t\t\twelcome user, taghi ');
});
it("should compile when using Objects", function () {
var src = "@foreach( users as user ) @if(user.status === 'admin') welcome admin, {{ user.name }} @elseif (user.status === 'reseller' ) welcome reseller, {{user.name}} @else welcome user, {{user.name }}@endif @endforeach",
cmp = btc(src, btfm),
injection = "var users = { 'ali':{ 'name': 'ali' }, 'hasan':{ 'name': 'hasan' }, 'taghi':{ 'name': 'taghi' }};";
fn = new Function(injection+cmp);
expect(fn()).to.be.equal('\t\t\twelcome user, ali \t\t\twelcome user, hasan \t\t\twelcome user, taghi ');
});
});
it("should compile `while` statement clearly", function () {
var src = "@while(c) @{{c-=1;}}While OK @endfor",
cmp = btc(src, btfm),
fn = new Function("var c=5;"+cmp);
expect(fn()).to.be.equal(' While OK While OK While OK While OK While OK ');
});
dsc("substitution", function () {
it("should echo", function () {
var src = "{{c}}",
cmp = btc(src, btfm),
fn = new Function("var c='this is a test';"+cmp);
expect(fn()).to.be.equal('this is a test');
});
it("should purify", function () {
var src = "{{{c}}}",
cmp = btc(src, btfm),
fn = new Function("var c='<html>';"+cmp);
expect(fn()).to.be.equal('<html>');
});
});
});