UNPKG

5.33 kBJavaScriptView Raw
1
2const _ = require('underscore');
3const path = require('path');
4const assert = require('assert');
5const utils = require('../lib/utils');
6const Firedoc = require('../lib/firedoc').Firedoc;
7
8describe('firedoc.utils', function () {
9
10 describe('.escapeHTML', function () {
11 it('should escape some htmls', function () {
12 assert.equal('&lt;a&gt;&lt;&#x2F;a&gt;', utils.escapeHTML('<a></a>'));
13 assert.equal('&amp;', utils.escapeHTML('&'));
14 assert.equal('&#x60;', utils.escapeHTML('`'));
15 assert.equal('&quot;', utils.escapeHTML('"'));
16 });
17 });
18
19 describe('.safetrim', function () {
20 it('should trim strings', function () {
21 assert.equal('abc', utils.safetrim(' abc '));
22 });
23 it('should trim unknown object', function () {
24 assert.equal('true', utils.safetrim(true));
25 assert.equal('', utils.safetrim(null));
26 assert.equal('', utils.safetrim(undefined));
27 assert.equal('1', utils.safetrim(1));
28 });
29 });
30
31 describe('.unindent', function () {
32 it('should return unidented string', function () {
33 assert.equal('* foobar', utils.unindent(' * foobar'));
34 });
35 });
36
37 describe('.filterFileName', function () {
38 it('should return filename', function () {
39 var actual = utils.filterFileName('path/to/file');
40 assert.equal('path_to_file', actual);
41 });
42 });
43
44 describe('.getLayouts', function () {
45 it('should get layouts', function () {
46 var layouts = utils.getLayouts(path.join(__dirname, '../themes/default'));
47 assert.ok(layouts.main);
48 assert.ok(layouts.xhr);
49 });
50 });
51
52 describe('.getPages', function () {
53 var themedir = path.join(__dirname, '../themes/default');
54 it('should get layouts', function () {
55 var layouts = utils.getLayouts(themedir);
56 assert.ok(layouts.main);
57 assert.ok(layouts.xhr);
58 });
59 it('should get partials', function () {
60 var partials = utils.getPartials(themedir);
61 assert.ok(partials['index']);
62 assert.ok(partials['enum']);
63 assert.ok(partials['class']);
64 assert.ok(partials['module']);
65 });
66 });
67
68 describe('.prepare', function () {
69 var themedir = path.join(__dirname, '../themes/default');
70 it('should call prepare function', function () {
71 var actual = utils.prepare([themedir], {});
72 assert.ok(actual.meta);
73 assert.ok(actual.layouts);
74 assert.ok(actual.partials);
75 });
76 it('should use same dirs', function () {
77 var actual = utils.prepare([themedir, themedir], {});
78 assert.ok(actual.meta);
79 assert.ok(actual.layouts);
80 assert.ok(actual.partials);
81 });
82 it('should use skipLoad', function () {
83 var actual = utils.prepare([themedir], {skipLoad: true});
84 assert.ok(actual.meta);
85 assert.deepEqual(actual.layouts, {});
86 assert.deepEqual(actual.partials, {});
87 });
88 });
89
90 describe('.fixType', function () {
91 it('should fix types', function () {
92 assert.equal('String', utils.fixType('string'));
93 assert.equal('Object', utils.fixType('object'));
94 });
95 });
96
97 describe('.webpath', function () {
98 it('should return web(http) path from local', function () {
99 assert.equal('path/to/file', utils.webpath('path\\to\\file'));
100 });
101 });
102
103 describe('.localize', function () {
104 it('should localize a string', function () {
105 var raw = ''+
106 '!#en english'+
107 '!#zh 中文';
108 var en = utils.localize(raw);
109 var zh = utils.localize(raw, 'zh');
110 assert.equal('english', utils.localize(raw));
111 assert.equal('中文', utils.localize(raw, 'zh'));
112 });
113 it('should localize a complex string', function () {
114 var raw = ''+
115 '!#zh: 这个类用来封装编辑器针对节点的操作。\n'+
116 'Note: 接口中以 "N" 结尾的使用的都是 Runtime 的原生 Node 类型。\n'+
117 '!#en: This is a wrapper class for operating...';
118 var en = utils.localize(raw);
119 var zh = utils.localize(raw, 'zh');
120 console.log(en);
121 console.log(zh);
122 });
123 });
124
125 describe('.markdownLink', function () {
126 it('should call markdownLink', function () {
127 assert.equal('foobarx-10-20', utils.markdownLink('foobar(x,10,20)'));
128 });
129 });
130
131 describe('.buildFileTree', function () {
132 it('should call buildFileTree', function (next) {
133 var src = path.join(__dirname, './targets/basic');
134 var doc = new Firedoc({'path': src, 'parseOnly': true});
135 doc.build(function (err, ast, options) {
136 var filetree = utils.buildFileTree(_.values(ast.files));
137 assert.equal('test/targets/basic/index.js', filetree.test.targets.basic['index.js'].path);
138 assert.equal('test_targets_basic_index.js', filetree.test.targets.basic['index.js'].name);
139 next();
140 });
141 });
142 });
143
144 describe('.stringlog', function () {
145 it('should return the sinle', function () {
146 var data = {
147 file: 'file',
148 line: 20
149 };
150 var actual = utils.stringlog(data);
151 assert.equal(actual, ' file:20');
152 });
153 });
154
155 describe('.defineReadonly', function () {
156 var obj = {};
157 it('define property', function () {
158 utils.defineReadonly(obj, 'x', 10);
159 assert.equal(10, obj.x);
160 });
161 it('change the obj.x', function () {
162 obj.x = 100;
163 assert.equal(10, obj.x);
164 });
165 });
166
167});
\No newline at end of file