UNPKG

8.95 kBJavaScriptView Raw
1"use strict";
2const aries = require("../index.js");
3const assert = require('assert');
4const async = require('async');
5const fs = require('fs');
6const path = require('path');
7//设置白羊座
8aries.set({
9 path:__dirname,
10 includeId:function(templateId, cb){
11 return fs.readFile(path.join(__dirname,templateId+'.html'), cb)
12 },
13 cacheTime:10000,
14});
15
16let taskList = [];
17let taskList2 = [];
18
19
20// 测试普通字符串
21taskList.push(function(callback){
22 let syncStr = "<html><body>helloworld</body></html>";
23 aries.compile(syncStr, {}, (err, renderStr, a,b,c,isUseCache) => {
24 if(err){
25 console.log(err.stack)
26 }
27 assert(!err);
28 assert(!isUseCache);
29 assert(renderStr === "<html><body>helloworld</body></html>");
30
31 aries.compile(syncStr, {}, (err, renderStr, a,b,c,isUseCache) => {
32 assert(!err);
33 assert(isUseCache);
34 assert(renderStr === "<html><body>helloworld</body></html>");
35 console.log("normal str test ok");
36 callback();
37 })
38 })
39});
40
41
42// //测试转移字符串
43taskList.push(function(callback){
44 let syncStr = "<%= ctx.data %>";
45 aries.compile(syncStr, {data:"<p>\"\'</p>"}, (err, renderStr, a, b, c, isUseCache) => {
46 if(err){
47 console.log(err.stack)
48 }
49 console.log(renderStr)
50 assert(!err);
51 assert(!isUseCache);
52 assert(renderStr === "&lt;p&gt;&quot;&apos;&lt;/p&gt;");
53
54 aries.compile(syncStr, {data:"<p>\"\'</p>"}, (err, renderStr, a, b, c, isUseCache) => {
55 assert(!err);
56 assert(isUseCache);
57 assert(renderStr === "&lt;p&gt;&quot;&apos;&lt;/p&gt;");
58 console.log("escape sync str test ok");
59 callback();
60 })
61 })
62});
63
64
65// //测试渲染字符串
66taskList.push(function(callback){
67 let syncStr = "<%- ctx.data %>";
68 aries.compile(syncStr, {data:"123"}, (err, renderStr, a, b, c, isUseCache) => {
69 if(err){
70 console.log(err.stack)
71 }
72 assert(!err);
73 assert(!isUseCache);
74 assert(renderStr === "123");
75
76 aries.compile(syncStr, {data:"1234"}, (err, renderStr, a, b, c, isUseCache) => {
77 assert(!err);
78 assert(isUseCache);
79 assert(renderStr === "1234");
80 console.log("sync str test ok");
81 callback();
82 })
83 })
84});
85
86
87// //测试渲染异步模板字符串
88taskList.push(function(callback){
89 let syncStr = "<%? setTimeout(function(){ctx.data='123';aries();},ctx.timeout) %><%- ctx.data %>";
90 aries.compile(syncStr, {timeout:1000}, function(err, renderStr, a, b, c, isUseCache){
91 if(err) {
92 console.log(err.stack)
93 }
94 assert(!err);
95 assert(!isUseCache);
96 assert(renderStr === "123");
97
98 aries.compile(syncStr, {timeout:1000}, (err, renderStr, a, b, c, isUseCache) => {
99 assert(!err);
100 assert(isUseCache);
101 assert(renderStr === "123");
102 console.log("async str test ok");
103 callback();
104 })
105 })
106});
107
108
109
110// //测试同步include文件功能
111taskList.push(function(callback){
112 let syncStr = `<% if(true){ %>
113 <% include sync.html %>
114 <%}%>`;
115 aries.compile(syncStr, {data:"123"}, (err, renderStr, a, includeIds, includeFs, isUseCache) => {
116
117 assert(!err);
118 assert(!isUseCache);
119 assert(renderStr.trim() === "123");
120 assert(includeFs.length === 1)
121 assert(includeFs[0] === "sync.html")
122
123
124 aries.compile(syncStr, {data:"1234"}, (err, renderStr, a, includeIds, includeFs, isUseCache) => {
125 assert(!err);
126 assert(isUseCache);
127 assert(renderStr.trim() === "1234");
128 console.log("include test ok");
129 callback();
130 })
131 })
132});
133
134
135// //测试异步includeId文件功能
136taskList.push(function(callback){
137 let syncStr = "<% includeId async %>";
138 aries.compile(syncStr, {data:"123"}, function(err, renderStr, a, includeIds, includeFs, isUseCache){
139
140 assert(!err);
141 assert(!isUseCache);
142 assert(renderStr === "123");
143
144 assert(includeIds.length === 1)
145 assert(includeIds[0] === "async")
146
147 aries.compile(syncStr, {data:"123"}, (err, renderStr, a, b, c, isUseCache) => {
148 assert(!err);
149 assert(isUseCache);
150 assert(renderStr === "123");
151 console.log("includeId test ok");
152 callback();
153 })
154 })
155});
156
157
158
159
160// // //测试include嵌套
161taskList.push(function(callback){
162 let syncStr = "<% include incLv1.html %>";
163 aries.compile(syncStr, {data:"123"}, (err, renderStr, a, includeIds, includeFs, isUseCache) => {
164 if(err){
165 console.log(err.stack)
166 }
167 assert(!err);
168 assert(!isUseCache);
169 assert(renderStr === "123");
170
171 assert(includeFs.length === 4)
172 assert(includeFs[0] === "incLv1.html")
173 assert(includeFs[1] === "incLv2.html")
174 assert(includeFs[2] === "incLv3.html")
175 assert(includeFs[3] === "incLv4.html")
176
177
178 aries.compile(syncStr, {data:"123"}, (err, renderStr, a, b, c, isUseCache) => {
179 assert(!err);
180 assert(isUseCache);
181 assert(renderStr === "123");
182 console.log("include embed test ok");
183 callback();
184 })
185 })
186});
187
188
189
190// // //测试include嵌套
191taskList.push(function(callback){
192 let syncStr = "<% include incLv1.html %>";
193
194 aries.includes(syncStr, (err, includeIds, includeFs) => {
195 if(err){
196 console.log(err)
197 }
198 assert(!err);
199
200 assert(includeFs.length === 4)
201 assert(includeFs[0] === "incLv1.html")
202 assert(includeFs[1] === "incLv2.html")
203 assert(includeFs[2] === "incLv3.html")
204 assert(includeFs[3] === "incLv4.html")
205
206 console.log("include embed only include test ok");
207 callback();
208
209 })
210});
211
212
213// //测试 if else 语句
214taskList.push(function(callback){
215 let syncStr = "<% if(false){ %><%- ctx.data %><%} else {%><%- ctx.data %><%}%>";
216 aries.compile(syncStr, {data:"123"}, (err, renderStr, a, b, c, isUseCache) => {
217 assert(!err);
218 assert(!isUseCache);
219 assert(renderStr === "123");
220
221 aries.compile(syncStr, {data:"1234"}, (err, renderStr, a, b, c, isUseCache) => {
222 assert(!err);
223 assert(isUseCache);
224 assert(renderStr === "1234");
225 console.log("if else test ok");
226 callback();
227 })
228 })
229});
230
231// //测试 for 语句
232taskList.push(function(callback){
233 let syncStr = "<% for(var i=1;i<ctx.data;i++){%><%- i %><%}%>";
234 aries.compile(syncStr, {data:10}, (err, renderStr, a, b, c, isUseCache) => {
235 assert(!err);
236 assert(!isUseCache);
237 assert(renderStr === "123456789");
238
239 aries.compile(syncStr, {data:5}, (err, renderStr, a, b, c, isUseCache) => {
240 assert(!err);
241 assert(isUseCache);
242 assert(renderStr === "1234");
243 console.log("for loop test ok");
244 callback();
245 })
246 })
247});
248
249
250
251
252//测试获取模版文件,相对路径
253taskList.push(function(callback){
254 let tplPath = "async/async.html";
255 aries.compileFile(tplPath, {data:10}, (err, renderStr, a, b, c, isUseCache) => {
256 if(err){
257 console.log(err.stack)
258 }
259 assert(!err);
260 assert(!isUseCache);
261 assert(renderStr === "20");
262
263 aries.compileFile(tplPath, {data:20}, (err, renderStr, a, b, c, isUseCache) => {
264 assert(!err);
265 assert(isUseCache);
266 assert(renderStr === "40");
267 console.log("tpl async tpl test ok");
268 callback();
269 })
270 })
271});
272
273
274//测试获取模版文件,绝对路径
275taskList.push(function(callback){
276 let tplPath = path.join(__dirname,"async", "async.html");
277 console.log("tplPath abs path "+tplPath)
278 aries.compileFile(tplPath, {data:10}, (err, renderStr, a, b, c, isUseCache) => {
279 assert(!err);
280 assert(isUseCache);//已经缓存
281 assert(renderStr === "20");
282
283 aries.compileFile(tplPath, {data:20}, (err, renderStr, a, b, c, isUseCache) => {
284 assert(!err);
285 assert(isUseCache);
286 assert(renderStr === "40");
287 console.log("tpl abs path tpl test ok");
288 callback();
289 })
290 })
291});
292
293//测试include模板传参
294taskList.push(function(callback){
295 let syncStr = "<% ctx.data = 1%><% includeId paramLv1?data=a %>|<%= ctx.data %>";
296 aries.compile(syncStr, {}, function(err, renderStr, a, includeIds, includeFs, isUseCache){
297
298 assert(!err);
299 assert(!isUseCache);
300 assert(renderStr.trim() === "a|a");
301 console.log("tpl includeId param test ok");
302 callback();
303 })
304});
305
306
307//测试includefile模板传参
308taskList.push(function(callback){
309 let syncStr = "<% ctx.data = 1%><% include paramLv1.html?data=a %>|<%= ctx.data %>";
310 aries.compile(syncStr, {}, function(err, renderStr, a, includeIds, includeFs, isUseCache){
311
312 assert(!err);
313 assert(!isUseCache);
314 assert(renderStr.trim() === "a|a");
315 console.log("tpl include file param test ok");
316 callback();
317 })
318});
319
320
321
322async.series(taskList, function(err){
323 if(err){
324 console.log(err);
325 return
326 }
327 console.log("all test ok");
328 process.exit(0)
329})
\No newline at end of file