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 pathparseArray-test.js
109 lines (88 loc) · 4.03 KB
/
parseArray-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
const { Fury } = require('fury');
const { expect } = require('../chai');
const parseArray = require('../../../lib/parser/parseArray');
const { minim: namespace } = new Fury();
const Context = require('../../../lib/context');
describe('#parseArray', () => {
let name;
let context;
let array;
beforeEach(() => {
name = 'Example Array';
context = new Context(namespace);
array = new namespace.elements.Array([
'Doe',
'Hello',
]);
});
it('provides warning when given element is non-array', () => {
const element = new namespace.elements.String();
const parseValue = value => value;
const parseResult = parseArray(context, name, parseValue)(element);
expect(parseResult).to.contain.warning("'Example Array' is not an array");
});
it('can parse an array when the transform returns an element', () => {
const parseValue = value => value;
const parseResult = parseArray(context, name, parseValue)(array);
expect(parseResult.length).to.equal(1);
expect(parseResult.get(0)).to.be.instanceof(namespace.elements.Array);
expect(parseResult.get(0).length).to.equal(2);
expect(parseResult.get(0).get(0).toValue()).to.equal('Doe');
expect(parseResult.get(0).get(1).toValue()).to.equal('Hello');
});
it('can parse an array when the transform returns a parse result', () => {
const parseValue = value => new namespace.elements.ParseResult([value]);
const parseResult = parseArray(context, name, parseValue)(array);
expect(parseResult.length).to.equal(1);
expect(parseResult.get(0)).to.be.instanceof(namespace.elements.Array);
expect(parseResult.get(0).length).to.equal(2);
expect(parseResult.get(0).get(0).toValue()).to.equal('Doe');
expect(parseResult.get(0).get(1).toValue()).to.equal('Hello');
});
it('can parse an array when the transform returns a parse result including a warning annotation', () => {
const parseValue = (value) => {
const warning = new namespace.elements.Annotation(
`${value.toValue()} warning`,
{ classes: ['warning'] }
);
return new namespace.elements.ParseResult([value, warning]);
};
const parseResult = parseArray(context, name, parseValue)(array);
expect(parseResult.length).to.equal(3);
expect(parseResult.get(0)).to.be.instanceof(namespace.elements.Array);
expect(parseResult.get(0).length).to.equal(2);
expect(parseResult.get(0).get(0).toValue()).to.equal('Doe');
expect(parseResult.get(0).get(1).toValue()).to.equal('Hello');
expect(parseResult.warnings.length).to.equal(2);
expect(parseResult.warnings.get(0).toValue()).to.equal('Doe warning');
expect(parseResult.warnings.get(1).toValue()).to.equal('Hello warning');
});
it('discards any values when the transform returns a parse result which includes an error', () => {
const parseValue = (value) => {
const warning = new namespace.elements.Annotation(
`${value.toValue()} error`,
{ classes: ['error'] }
);
return new namespace.elements.ParseResult([value, warning]);
};
const parseResult = parseArray(context, name, parseValue)(array);
expect(parseResult.length).to.equal(2);
expect(parseResult.errors.get(0).toValue()).to.equal('Doe error');
expect(parseResult.errors.get(1).toValue()).to.equal('Hello error');
});
it('discards any values when the transform returns a parse result which includes a warning and no value', () => {
const parseValue = (value) => {
const warning = new namespace.elements.Annotation(
`${value.toValue()} warning`,
{ classes: ['warning'] }
);
return new namespace.elements.ParseResult([warning]);
};
const parseResult = parseArray(context, name, parseValue)(array);
expect(parseResult.length).to.equal(3);
expect(parseResult.get(0)).to.be.instanceof(namespace.elements.Array);
expect(parseResult.get(0).isEmpty).to.be.true;
expect(parseResult.warnings.get(0).toValue()).to.equal('Doe warning');
expect(parseResult.warnings.get(1).toValue()).to.equal('Hello warning');
});
});