This repository was archived by the owner on Nov 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathparse-result-test.js
271 lines (234 loc) · 6.66 KB
/
parse-result-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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
/*
* Tests for parse result namespace elements, including all their
* convenience properties and methods.
*/
const chai = require('chai');
const minim = require('minim');
const minimParseResult = require('../lib/parse-result');
const { Assertion, expect } = chai;
const namespace = minim.namespace()
.use(minimParseResult);
const Annotation = namespace.getElementClass('annotation');
const Category = namespace.getElementClass('category');
chai.use((_chai, utils) => {
/*
* Asserts that an element has a certain class.
*/
utils.addMethod(Assertion.prototype, 'class', function hasClass(name) {
// eslint-disable-next-line no-underscore-dangle
const obj = this._obj;
this.assert(
obj.classes.contains(name),
'Expected class list #{act} to contain #{exp}',
'Expected class list #{act} to not contain #{exp}',
name,
obj.classes.toValue()
);
});
});
/*
* Shortcut to get an attribute value from an element.
*/
function attrValue(element, name) {
return element.attributes.getValue(name);
}
describe('Parse result namespace', () => {
context('parse result element', () => {
let parseResult;
let refracted;
beforeEach(() => {
refracted = {
element: 'parseResult',
content: [
{
element: 'annotation',
meta: {
classes: {
element: 'array',
content: [
{
element: 'string',
content: 'warning',
},
],
},
},
},
{
element: 'annotation',
meta: {
classes: {
element: 'array',
content: [
{
element: 'string',
content: 'error',
},
],
},
},
},
{
element: 'category',
meta: {
classes: {
element: 'array',
content: [
{
element: 'string',
content: 'api',
},
],
},
},
},
],
};
parseResult = namespace.fromRefract(refracted);
});
it('should round-trip correctly', () => {
expect(namespace.toRefract(parseResult)).to.deep.equal(refracted);
});
it('should have element name parseResult', () => {
expect(parseResult.element).to.equal('parseResult');
});
it('should contain an API', () => {
const { api } = parseResult;
expect(api).to.be.an.instanceof(Category);
expect(api).to.have.class('api');
});
it('should contain two annotations', () => {
const items = parseResult.annotations;
expect(items).to.have.length(2);
items.forEach((item) => {
expect(item).to.be.an.instanceof(Annotation);
});
});
it('should contain a warning', () => {
const items = parseResult.warnings;
expect(items).to.have.length(1);
items.forEach((item) => {
expect(item).to.be.an.instanceof(Annotation);
expect(item).to.have.class('warning');
});
});
it('should contain an error', () => {
const items = parseResult.errors;
expect(items).to.have.length(1);
items.forEach((item) => {
expect(item).to.be.an.instanceof(Annotation);
expect(item).to.have.class('error');
});
});
});
context('annotation element', () => {
let annotation;
let refracted;
beforeEach(() => {
refracted = {
element: 'annotation',
attributes: {
code: {
element: 'number',
content: 123,
},
},
content: 'Missing argument description',
};
annotation = namespace.fromRefract(refracted);
});
it('should round-trip correctly', () => {
expect(namespace.toRefract(annotation)).to.deep.equal(refracted);
});
it('should have element name annotation', () => {
expect(annotation.element).to.equal('annotation');
});
it('should get a code', () => {
expect(annotation.code.toValue()).to.equal(123);
});
it('should set a code', () => {
annotation.code = 456;
expect(attrValue(annotation, 'code')).to.equal(456);
});
it('should have text content', () => {
expect(annotation.content).to.equal('Missing argument description');
});
});
context('source map element', () => {
let sourceMap;
beforeEach(() => {
sourceMap = namespace.fromRefract({
element: 'sourceMap',
content: [],
});
});
it('should have element name sourceMap', () => {
expect(sourceMap.element).to.equal('sourceMap');
});
});
context('source maps', () => {
let element;
let sourceMaps;
let refracted;
beforeEach(() => {
refracted = {
element: 'string',
attributes: {
sourceMap: {
element: 'array',
content: [
{
element: 'sourceMap',
content: [
{
element: 'array',
content: [
{
element: 'number',
content: 1,
},
{
element: 'number',
content: 2,
},
],
},
],
},
],
},
},
content: '',
};
element = namespace.fromRefract(refracted);
sourceMaps = element.attributes.get('sourceMap');
});
it('should round-trip correctly', () => {
expect(namespace.toRefract(element)).to.deep.equal(refracted);
});
it('should contain a sourceMap attribute with one item', () => {
expect(sourceMaps).to.exist;
expect(sourceMaps).to.have.length(1);
});
it('should have the source map location', () => {
expect(sourceMaps.first.toValue()).to.deep.equal([[1, 2]]);
});
it('should have a convenience method for retrieving source map', () => {
expect(element.sourceMapValue).to.deep.equal([[1, 2]]);
});
});
context('source map convenience function', () => {
let element;
let refracted;
beforeEach(() => {
refracted = {
element: 'string',
content: '',
};
element = namespace.fromRefract(refracted);
});
it('should have a convenience method for retrieving source map', () => {
expect(element.sourceMapValue).to.be.undefined;
});
});
});