-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest.js
121 lines (109 loc) · 3.71 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
'use strict';
const { writeFileSync, unlinkSync } = require('fs');
const { resolve } = require('path');
const { expect } = require('chai');
const read = require('node-read-yaml');
const filename = resolve(__dirname, '.test.yml');
const yamlText1 = `
_id: 0g3043l9tm
name: Example
url: https://example.com/
desc: About example.
langs: [en,zh]
`;
const yamlText2 = `
_id: 0g3043l9tm
name: Example
url: https://example.com/
desc: About example.
langs: [en,zh]
---
_id: 0g3043l9tm
name: Example
url: https://example.com/
desc: About example.
langs: [en,zh]
`;
describe('node-read-yaml', () => {
afterEach(() => {
try {
unlinkSync(filename);
} catch (error) {
// ignore error
console.debug(error.message);
}
});
describe('asynchronously', () => {
it('should return a JSON object.', () => {
writeFileSync(filename, yamlText1);
return read(filename).then((json) => {
console.log(json);
expect(json).to.be.a('object');
});
});
it('with option multi: true - should return an array when read a multi-document.', () => {
writeFileSync(filename, yamlText2);
return read(filename, { multi: true }).then((json) => {
console.log(json);
expect(json).to.be.an('array').with.lengthOf(2);
expect(json[0]).to.be.an('object');
});
});
it('with option multi: true - should return an array when read a single-document.', () => {
writeFileSync(filename, yamlText1);
return read(filename, { multi: true }).then((json) => {
console.log(json);
expect(json).to.be.an('array').with.lengthOf(1);
expect(json[0]).to.be.an('object');
});
});
it('should throw an error when it cannot parse the file as YAML.', () => {
return read('README.md')
.then(() => expect.fail('should not come to here'))
.catch((error) => {
expect(error.name).not.to.be.eql('AssertionError');
expect(error.name).to.be.eql('YAMLException');
});
});
it('should throw an error when it cannot read the file.', () => {
return read('__nofile__')
.then(() => expect.fail('should not come to here'))
.catch((error) => {
expect(error.name).not.to.be.eql('AssertionError');
expect(error.name).not.to.be.eql('YAMLException');
expect(error.name).to.be.eql('Error');
expect(error.message).to.have.string('no such file or directory');
});
});
});
describe('synchronously', () => {
it('should return a JSON object.', () => {
writeFileSync(filename, yamlText1);
const json = read.sync(filename);
console.log(json);
expect(json).to.be.a('object');
});
it('with option multi: true - should return an array when read a multi-document.', () => {
writeFileSync(filename, yamlText2);
const json = read.sync(filename, { multi: true });
console.log(json);
expect(json).to.be.an('array').with.lengthOf(2);
expect(json[0]).to.be.an('object');
});
it('with option multi: true - should return an array when read a single-document.', () => {
writeFileSync(filename, yamlText1);
const json = read.sync(filename, { multi: true });
console.log(json);
expect(json).to.be.an('array').with.lengthOf(1);
expect(json[0]).to.be.an('object');
});
it('should throw an error when it cannot parse the file as YAML.', () => {
expect(() => read.sync('README.md')).to.throw(read.YAMLException);
});
it('should throw an error when it cannot read the file.', () => {
const func = () => read.sync('__nofile__');
expect(func).not.to.throw(read.YAMLException);
expect(func).to.throw(Error, 'no such file or director');
});
});
});