forked from edclements/uri-templates
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
87 lines (70 loc) · 2.56 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
"use strict";
var uriTemplates = require('./uri-templates');
var assert = require('proclaim');
describe("Basic tests", function () {
it("Basic substitution", function () {
var template = uriTemplates("/prefix/{var}/suffix");
var uri = template.fillFromObject({var: "test"});
assert.strictEqual(uri, "/prefix/test/suffix");
});
});
function createTests(title, examplesDoc) {
describe(title + "(substitution)", function () {
for (var sectionTitle in examplesDoc) {
var exampleSet = examplesDoc[sectionTitle];
describe(sectionTitle, function () {
var variables = exampleSet.variables;
var variableFunction = function (varName) {
return variables[varName];
};
for (var i = 0; i < exampleSet.testcases.length; i++) {
var pair = exampleSet.testcases[i];
(function (templateString, expected) {
it(templateString, function () {
var template = uriTemplates(templateString);
var actualUri = template.fillFromObject(variables);
if (typeof expected == "string") {
assert.strictEqual(actualUri, expected);
} else {
assert.includes(expected, actualUri);
}
});
})(pair[0], pair[1]);
}
});
}
});
var unguessable = {};
describe(title + " (de-substitution)", function () {
for (var sectionTitle in examplesDoc) {
var exampleSet = examplesDoc[sectionTitle];
describe(sectionTitle, function () {
for (var i = 0; i < exampleSet.testcases.length; i++) {
var pair = exampleSet.testcases[i];
(function (templateString, expected, exampleSet) {
if (unguessable[templateString]) {
return;
}
it(templateString, function () {
var original = (typeof expected == 'string') ? expected : expected[0];
var template = uriTemplates(templateString);
var guessedVariables = template.fromUri(original);
assert.isObject(guessedVariables);
var reconstructed = template.fillFromObject(guessedVariables);
if (typeof expected == "string") {
assert.strictEqual(reconstructed, expected);
} else {
assert.includes(expected, reconstructed);
}
});
})(pair[0], pair[1], exampleSet);
}
});
}
});
}
createTests("Spec examples by section", require('./uritemplate-test/spec-examples-by-section.json'));
createTests("Extended examples", require('./uritemplate-test/extended-tests.json'));
createTests("Custom examples 1", require('./test/custom-tests.json'));
createTests("Custom examples 2", require('./test/custom-tests-2.json'));
require('./test/custom-tests.js');