Skip to content

Commit 888107e

Browse files
Lots of new tests
1 parent 31e54f6 commit 888107e

48 files changed

Lines changed: 3382 additions & 286 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

karma.conf.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ var baseConfig = {
1010
'tests/bower_components/chai/chai.js',
1111
'tests/bower_components/sinon-js/sinon.js',
1212

13-
// Json Schema Ref Parser
13+
// Json Schema $Ref Parser
1414
'dist/ref-parser.min.js',
1515
{pattern: 'dist/*.map', included: false, served: true},
1616

tests/fixtures/helper.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,54 @@
3232
};
3333
};
3434

35+
/**
36+
* Tests the {@link $RefParser.resolve} method,
37+
* and asserts that the given file paths resolve to the given values.
38+
*
39+
* @param {string} filePath - The file path that should be resolved
40+
* @param {*} resolvedValue - The resolved value of the file
41+
* @param {...*} [params] - Additional file paths and resolved values
42+
* @returns {Function}
43+
*/
44+
helper.testResolve = function testResolve(filePath, resolvedValue, params) {
45+
var schemaFile = path.rel(arguments[0]);
46+
var parsedSchema = arguments[1];
47+
var expectedFiles = [], expectedValues = [];
48+
for (var i = 0; i < arguments.length; i++) {
49+
expectedFiles.push(path.abs(arguments[i]));
50+
expectedValues.push(arguments[++i]);
51+
}
52+
53+
return function(done) {
54+
var parser = new $RefParser();
55+
parser
56+
.resolve(schemaFile)
57+
.then(function($refs) {
58+
expect(parser.schema).to.deep.equal(parsedSchema);
59+
expect(parser.$refs).to.equal($refs);
60+
61+
// Resolved file paths
62+
expect($refs.paths()).to.have.same.members(expectedFiles);
63+
if (userAgent.isNode) {
64+
expect($refs.paths(['fs'])).to.have.same.members(expectedFiles);
65+
expect($refs.paths('http', 'https')).to.be.an('array').with.lengthOf(0);
66+
}
67+
else {
68+
expect($refs.paths(['http', 'https'])).to.have.same.members(expectedFiles);
69+
expect($refs.paths('fs')).to.be.an('array').with.lengthOf(0);
70+
}
71+
72+
// Resolved values
73+
var values = $refs.values();
74+
expect(values).to.have.keys(expectedFiles);
75+
expectedFiles.forEach(function(file, i) {
76+
expect(values[file]).to.deep.equal(expectedValues[i], file);
77+
});
78+
79+
done();
80+
})
81+
.catch(helper.shouldNotGetCalled(done));
82+
}
83+
};
84+
3585
})();

tests/index.html

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,23 +27,43 @@
2727
<script src="fixtures/path.js"></script>
2828

2929
<!-- Tests -->
30-
<script src="specs/blank/blank.spec.js"></script>
31-
<script src="specs/invalid/invalid.spec.js"></script>
32-
<script src="specs/empty/empty.spec.js"></script>
33-
3430
<script src="specs/no-refs/no-refs.parsed.js"></script>
3531
<script src="specs/no-refs/no-refs.spec.js"></script>
3632

37-
<script src="specs/internal-refs/internal-refs.parsed.js"></script>
38-
<script src="specs/internal-refs/internal-refs.dereferenced.js"></script>
39-
<script src="specs/internal-refs/internal-refs.bundled.js"></script>
40-
<script src="specs/internal-refs/internal-refs.spec.js"></script>
33+
<script src="specs/internal/internal.parsed.js"></script>
34+
<script src="specs/internal/internal.dereferenced.js"></script>
35+
<script src="specs/internal/internal.bundled.js"></script>
36+
<script src="specs/internal/internal.spec.js"></script>
37+
38+
<script src="specs/external/external.parsed.js"></script>
39+
<script src="specs/external/external.dereferenced.js"></script>
40+
<script src="specs/external/external.bundled.js"></script>
41+
<script src="specs/external/external.spec.js"></script>
42+
43+
<script src="specs/circular/circular.parsed.js"></script>
44+
<script src="specs/circular/circular.dereferenced.js"></script>
45+
<script src="specs/circular/circular.spec.js"></script>
4146

42-
<script src="specs/external-refs/external-refs.parsed.js"></script>
43-
<script src="specs/external-refs/external-refs.dereferenced.js"></script>
44-
<script src="specs/external-refs/external-refs.bundled.js"></script>
45-
<script src="specs/external-refs/external-refs.spec.js"></script>
47+
<script src="specs/circular-external/circular-external.parsed.js"></script>
48+
<script src="specs/circular-external/circular-external.dereferenced.js"></script>
49+
<script src="specs/circular-external/circular-external.bundled.js"></script>
50+
<script src="specs/circular-external/circular-external.spec.js"></script>
51+
52+
<script src="specs/deep/deep.parsed.js"></script>
53+
<script src="specs/deep/deep.dereferenced.js"></script>
54+
<script src="specs/deep/deep.bundled.js"></script>
55+
<script src="specs/deep/deep.spec.js"></script>
56+
57+
<script src="specs/deep-circular/deep-circular.parsed.js"></script>
58+
<script src="specs/deep-circular/deep-circular.dereferenced.js"></script>
59+
<script src="specs/deep-circular/deep-circular.bundled.js"></script>
60+
<script src="specs/deep-circular/deep-circular.spec.js"></script>
61+
62+
<script src="specs/blank/blank.spec.js"></script>
63+
<script src="specs/invalid/invalid.spec.js"></script>
64+
<script src="specs/empty/empty.spec.js"></script>
4665

66+
<script src="specs/callbacks.spec.js"></script>
4767
<script src="specs/yaml.spec.js"></script>
4868

4969
<script>

tests/specs/callbacks.spec.js

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
'use strict';
2+
3+
describe('Callback & Promise syntax', function() {
4+
beforeEach(function() {
5+
// These tests all have a 200ms delay,
6+
// to ensure that all callbacks and promises are called.
7+
this.currentTest.slow(500);
8+
});
9+
10+
['parse', 'resolve', 'dereference', 'bundle'].forEach(function(method) {
11+
describe(method + ' method', function() {
12+
it('should call the callback function and Promise.then()', testCallbackAndPromise_Success(method));
13+
it('should call the callback function and Promise.catch()', testCallbackAndPromise_Error(method));
14+
});
15+
});
16+
17+
/**
18+
* Calls the specified $RefParser method, and asserts that the callback function
19+
* is called without an error, and that Promise.then() is fired.
20+
*/
21+
function testCallbackAndPromise_Success(method) {
22+
return function(done) {
23+
var callbackFn = sinon.spy();
24+
var thenFn = sinon.spy();
25+
var catchFn = sinon.spy();
26+
27+
var parser = new $RefParser();
28+
parser[method](path.rel('specs/internal/internal.yaml'), callbackFn)
29+
.then(thenFn)
30+
.catch(catchFn);
31+
32+
setTimeout(function() {
33+
try {
34+
var result = method === 'resolve' ? parser.$refs : parser.schema;
35+
var schema = method === 'resolve' ? helper.parsed.internal : helper[method + 'd'].internal;
36+
37+
expect(parser.schema).to.deep.equal(schema);
38+
expect(parser.$refs.paths()).to.deep.equal([path.abs('specs/internal/internal.yaml')]);
39+
40+
sinon.assert.calledOnce(callbackFn);
41+
sinon.assert.calledWithExactly(callbackFn, null, result);
42+
43+
sinon.assert.calledOnce(thenFn);
44+
sinon.assert.calledWithExactly(thenFn, result);
45+
46+
sinon.assert.notCalled(catchFn);
47+
48+
done();
49+
}
50+
catch (e) {
51+
done(e)
52+
}
53+
}, 200);
54+
}
55+
}
56+
57+
/**
58+
* Calls the specified $RefParser method, and asserts that the callback function
59+
* is called with an error, and that Promise.catch() is fired.
60+
*/
61+
function testCallbackAndPromise_Error(method) {
62+
return function(done) {
63+
var callbackFn = sinon.spy();
64+
var thenFn = sinon.spy();
65+
var catchFn = sinon.spy();
66+
67+
var parser = new $RefParser();
68+
parser[method](path.rel('specs/invalid/invalid.yaml'), callbackFn)
69+
.then(thenFn)
70+
.catch(catchFn);
71+
72+
setTimeout(function() {
73+
try {
74+
var result = method === 'resolve' ? parser.$refs : parser.schema;
75+
76+
expect(parser.schema).to.be.null;
77+
expect(parser.$refs.paths()).to.deep.equal([path.abs('specs/invalid/invalid.yaml')]);
78+
79+
sinon.assert.calledOnce(callbackFn);
80+
sinon.assert.calledWithExactly(callbackFn, sinon.match.instanceOf(SyntaxError), result);
81+
82+
sinon.assert.calledOnce(catchFn);
83+
sinon.assert.calledWithExactly(catchFn, sinon.match.instanceOf(SyntaxError));
84+
85+
sinon.assert.notCalled(thenFn);
86+
87+
done();
88+
}
89+
catch (e) {
90+
done(e)
91+
}
92+
}, 200);
93+
}
94+
}
95+
});
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
helper.bundled.circularExternal =
2+
{
3+
"definitions": {
4+
"thing": {
5+
"$ref": "#/definitions/thing"
6+
},
7+
"person": {
8+
"title": "person",
9+
"type": "object",
10+
"properties": {
11+
"spouse": {
12+
"type": {
13+
"$ref": "#/definitions/person"
14+
}
15+
},
16+
"name": {
17+
"type": "string"
18+
}
19+
}
20+
},
21+
"parent": {
22+
"title": "parent",
23+
"type": "object",
24+
"properties": {
25+
"name": {
26+
"type": "string"
27+
},
28+
"children": {
29+
"items": {
30+
"$ref": "#/definitions/child"
31+
},
32+
"type": "array"
33+
}
34+
}
35+
},
36+
"child": {
37+
"title": "child",
38+
"type": "object",
39+
"properties": {
40+
"parents": {
41+
"items": {
42+
"$ref": "#/definitions/parent"
43+
},
44+
"type": "array"
45+
},
46+
"name": {
47+
"type": "string"
48+
}
49+
}
50+
}
51+
}
52+
};
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
helper.dereferenced.circularExternal =
2+
{
3+
"definitions": {
4+
"thing": {
5+
"$ref": "circular-external.yaml#/definitions/thing"
6+
},
7+
"person": {
8+
"title": "person",
9+
"type": "object",
10+
"properties": {
11+
"spouse": {
12+
"type": null
13+
},
14+
"name": {
15+
"type": "string"
16+
}
17+
}
18+
},
19+
"parent": {
20+
"title": "parent",
21+
"type": "object",
22+
"properties": {
23+
"name": {
24+
"type": "string"
25+
},
26+
"children": {
27+
"items": null,
28+
"type": "array"
29+
}
30+
}
31+
},
32+
"child": {
33+
"title": "child",
34+
"type": "object",
35+
"properties": {
36+
"parents": {
37+
"items": null,
38+
"type": "array"
39+
},
40+
"name": {
41+
"type": "string"
42+
}
43+
}
44+
}
45+
}
46+
};
47+
48+
helper.dereferenced.circularExternal.definitions.person.properties.spouse.type = helper.dereferenced.circularExternal.definitions.person;
49+
helper.dereferenced.circularExternal.definitions.parent.properties.children.items = helper.dereferenced.circularExternal.definitions.child;
50+
helper.dereferenced.circularExternal.definitions.child.properties.parents.items = helper.dereferenced.circularExternal.definitions.parent;
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
helper.parsed.circularExternal =
2+
{
3+
schema: {
4+
"definitions": {
5+
"thing": {
6+
"$ref": "circular-external.yaml#/definitions/thing"
7+
},
8+
"person": {
9+
"$ref": "definitions/person.yaml"
10+
},
11+
"parent": {
12+
"$ref": "definitions/parent.yaml"
13+
},
14+
"child": {
15+
"$ref": "definitions/child.yaml"
16+
}
17+
}
18+
},
19+
20+
child: {
21+
"type": "object",
22+
"properties": {
23+
"parents": {
24+
"items": {
25+
"$ref": "parent.yaml"
26+
},
27+
"type": "array"
28+
},
29+
"name": {
30+
"type": "string"
31+
}
32+
},
33+
"title": "child"
34+
},
35+
36+
parent: {
37+
"type": "object",
38+
"properties": {
39+
"name": {
40+
"type": "string"
41+
},
42+
"children": {
43+
"items": {
44+
"$ref": "child.yaml"
45+
},
46+
"type": "array"
47+
}
48+
},
49+
"title": "parent"
50+
},
51+
52+
person: {
53+
"type": "object",
54+
"properties": {
55+
"spouse": {
56+
"type": {
57+
"$ref": "person.yaml"
58+
}
59+
},
60+
"name": {
61+
"type": "string"
62+
}
63+
},
64+
"title": "person"
65+
}
66+
};

0 commit comments

Comments
 (0)