-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest.js
199 lines (173 loc) · 5.69 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
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
'use strict';
require('mocha');
var File = require('vinyl');
var assert = require('assert');
var parser = require('./');
describe('parsers', function() {
describe('.parseSync()', function() {
it('should support passing a string', function() {
var file = parser.parseSync('abc');
assert(file.data);
assert.equal(file.content, 'abc');
});
it('should parse front matter from a string', function() {
var file = parser.parseSync('---\ntitle: foo\n---\nbar');
assert(file.data);
assert(file.data.title);
assert.equal(file.data.title, 'foo');
assert.equal(file.content, 'bar');
});
it('should support parsing the content property on an object.', function() {
var file = parser.parseSync({
content: 'abc'
});
assert(file.data);
assert.equal(file.content, 'abc');
});
it('should support the contents property on an object', function() {
var file = parser.parseSync({
contents: 'abc'
});
assert(file.data);
assert.equal(file.content, 'abc');
});
it('should not choke on empty strings', function() {
var file = parser.parseSync({
contents: ''
});
assert(file.data);
assert.equal(file.content, '');
});
it('should support contents as a buffer', function() {
var file = parser.parseSync({
contents: new Buffer('abc')
});
assert(file.data);
assert(file.content);
assert.equal(file.content, 'abc');
});
it('should add front matter data to a "data" object.', function() {
var file = parser.parseSync({
content: '---\ntitle: abc\n---\nfoo'
});
assert(file.data);
assert(file.data.title);
assert.equal(file.data.title, 'abc');
assert.equal(file.content, 'foo');
});
it('should not choke on front-matter only', function() {
var file = parser.parseSync({
content: '---\ntitle: abc\n---'
});
assert(file.data);
assert(file.data.title);
assert.equal(file.data.title, 'abc');
assert.equal(file.content, '');
});
it('should strip newlines after front matter before content', function() {
var file = parser.parseSync({
content: '---\ntitle: abc\n---\n\n\n\n\nfoo'
});
assert(file.data);
assert(file.data.title);
assert.equal(file.data.title, 'abc');
assert.equal(file.content, 'foo');
});
it('should strip whitespace lines after front matter before content', function() {
var file = parser.parseSync({
content: '---\ntitle: abc\n---\n \n\n \n \nfoo'
});
assert(file.data);
assert(file.data.title);
assert.equal(file.data.title, 'abc');
assert.equal(file.content, 'foo');
});
});
describe('.stringify()', function() {
it('should throw when a callback is not passed', function(cb) {
try {
parser.stringify({});
cb(new Error('expected an error'));
} catch (err) {
assert(err);
assert(err.message);
assert.equal(err.message, 'expected a callback function');
cb();
}
});
it('should stringify the yfm object', function() {
var file = parser.parseSync({content: '---\nyfm:\n title: abc\n---\nfoo'});
assert.equal(file.content, 'foo');
assert.deepEqual(file.data, {yfm: { title: 'abc' }});
file = parser.stringifySync(file);
assert.equal(file.content, '---\ntitle: abc\n---\nfoo\n');
assert.deepEqual(file.data, {});
});
});
describe('.parse()', function() {
it('should throw when a callback is not passed', function(cb) {
try {
parser.parse({});
cb(new Error('expected an error'));
} catch (err) {
assert(err);
assert(err.message);
assert.equal(err.message, 'expected a callback function');
cb();
}
});
it('should handle parser errors', function(cb) {
parser.parse({content: '---\na : b : c :bar\n---\nfoo'}, function(err, file) {
assert(err);
cb();
});
});
it('should support passing a string', function(cb) {
parser.parse('---\ntitle: foo\n---\nbar', function(err, file) {
if (err) return cb(err);
assert(file.data);
assert(file.data.title);
assert.equal(file.data.title, 'foo');
assert.equal(file.content, 'bar');
cb();
});
});
it('should use the content property on the given file', function(cb) {
parser.parse({content: 'abc'}, function(err, file) {
if (err) return cb(err);
assert(file.data);
assert.equal(file.content, 'abc');
cb();
});
});
it('should support the contents property', function(cb) {
parser.parse({contents: new Buffer('abc')}, function(err, file) {
if (err) return cb(err);
assert(file.data);
assert.equal(typeof file.content, 'string');
assert.equal(file.content, 'abc');
assert.equal(file.contents.toString(), 'abc');
assert(Buffer.isBuffer(file.contents));
cb();
});
});
});
describe('.file', function() {
it('should decorate a `.parse` function onto the file object', function() {
var file = new File({path: 'abc', contents: new Buffer('foo')});
parser.file(file);
assert.equal(typeof file.parseMatter, 'function');
});
it('should parse front matter when `file.parse()` is called', function() {
var file = new File({
path: 'abc',
contents: new Buffer('---\ntitle: foo\n---\nbar')
});
parser.file(file);
assert(!file.data);
file.parseMatter();
assert.equal(file.data.title, 'foo');
assert.equal(file.content, 'bar');
});
});
});