Skip to content

Commit 6f1d5cc

Browse files
author
Hex
committed
增加测试代码
1 parent 1a54e8d commit 6f1d5cc

File tree

6 files changed

+49
-19
lines changed

6 files changed

+49
-19
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,5 @@ typings/
5959

6060
# next.js build output
6161
.next
62+
63+
package-lock.json

lib/helper.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ const helpers = {
5959

6060
isEmptyObject(obj) {
6161
for (const prop in obj) {
62-
if (obj.hasOwnProperty(prop)) {
62+
if (Object.prototype.hasOwnProperty.call(obj, prop)) {
6363
return false;
6464
}
6565
}

lib/layout.js

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -73,39 +73,39 @@ class Layout {
7373

7474
utils.getFileTime(filename, (time) => {
7575
this.templateTimes[filename] = time;
76-
76+
7777
const extendsName = this.getExtends(content);
78-
78+
7979
// 判断是否第一行是否有 extends 指令
8080
if (extendsName) {
8181
// 有 extends 指令,表示需要解析父模板
8282
this.parseParent(extendsName, content, filename);
8383
// 处理模板
8484
content = this.processParent(content);
8585
}
86-
86+
8787
content = this.removeCommand(content);
88-
88+
8989
// 准备缓存文件的数据
90-
90+
9191
const cacheInfo = {
9292
version: this.core.version,
9393
files: this.templateTimes
9494
};
95-
95+
9696
let result = `'/* changba template engine\n${JSON.stringify(cacheInfo)}\n*/+'`;
97-
97+
9898
if (block !== '') {
9999
// 支持直接获取 block 内容
100100
result += this.blocks[block] ? this.blocks[block] : `Block ${block} not found!`;
101101
}
102102
else {
103103
result += this.core._parse(content);
104104
}
105-
105+
106106
// 写入缓存(异步)
107107
this.writeCache(cacheFilename, result);
108-
108+
109109
callback(null, result);
110110
});
111111
});
@@ -144,23 +144,23 @@ class Layout {
144144

145145
utils.getFileTime(filename, (time) => {
146146
this.templateTimes[filename] = time;
147-
147+
148148
// 模板内容入栈,等待后续处理
149149
this.templates.push(content);
150-
150+
151151
// 获取父模板名称
152152
const extendsName = this.getExtends(content);
153-
153+
154154
if (extendsName) {
155155
// 有 extends 指令,表示需要加载父模板
156156
this.parseParent(extendsName, content, filename);
157157
}
158-
158+
159159
// 合并每一级的 block 信息,备用
160160
this.rawBlocks.push(this.getBlocks(content));
161-
161+
162162
this.depth--;
163-
163+
164164
if (this.depth === 0) {
165165
// 如果解析到最末一级,则合并最末一级模板的 block 内容
166166
// 此处需要特殊处理,否则会丢掉最末一级模板的 block 内容

lib/utils.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ const mkdirp = (p, callback, opts, made) => {
7575
if (err1) {
7676
throw err0;
7777
}
78-
78+
7979
if (!stat.isDirectory()) {
8080
throw err0;
8181
}

package.json

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44
"description": "唱吧模板引擎",
55
"main": "index.js",
66
"scripts": {
7-
"test": "echo \"Error: no test specified\" && exit 1"
7+
"eslint": "eslint *.js",
8+
"test-spec": "mocha test/*.test.js",
9+
"test-cov": "nyc npm run test-spec",
10+
"test": "npm run eslint && npm run test-cov"
811
},
912
"repository": {
1013
"type": "git",
@@ -20,6 +23,8 @@
2023
},
2124
"homepage": "https://github.com/ChangbaFE/cbT#readme",
2225
"devDependencies": {
23-
"eslint": "^6.8.0"
26+
"eslint": "^6.8.0",
27+
"mocha": "^7.1.2",
28+
"nyc": "^15.0.1"
2429
}
2530
}

test/cbt.test.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
'use strict';
2+
3+
const assert = require('assert')
4+
const cbT = require('..');
5+
6+
describe('cbT', function() {
7+
const basicTemplate = `<title><%=title%></title>`;
8+
const render = cbT.compile(basicTemplate);
9+
10+
describe('#compile()', function() {
11+
it('should return a function', function() {
12+
assert.equal(typeof render, 'function');
13+
});
14+
});
15+
16+
describe('#()', function() {
17+
it('should render the template', function() {
18+
assert.equal(render({title: 'http'}), '<title>http</title>');
19+
assert.equal(render({title: '<b>test</b>'}), '<title>&lt;b&gt;test&lt;/b&gt;</title>');
20+
assert.equal(render({}), '<title></title>');
21+
});
22+
});
23+
});

0 commit comments

Comments
 (0)