forked from json-quiz/json-quiz
-
Notifications
You must be signed in to change notification settings - Fork 0
/
assert.js
169 lines (151 loc) · 5 KB
/
assert.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
/**
* Helpers for testing schema validation.
*/
var assert = require('assert');
var Ajv = require('ajv');
var ajv = Ajv({ allErrors: true });
var resolved = {
'keyword': 'misc/keyword',
'metadata': 'metadata',
'content': 'content',
'hint': 'hint',
'category': 'category',
'base-score': 'score/base',
'fixed-score': 'score/fixed',
'manual-score': 'score/manual',
'none-score': 'score/none',
'rules-score': 'score/rules',
'sum-score': 'score/sum',
'base-question': 'question/base',
'choice-answer': 'answer-data/choice',
'choice-question': 'question/choice',
'graphic-answer': 'answer-data/graphic',
'graphic-question': 'question/graphic',
'match-answer': 'answer-data/match',
'match-question': 'question/match',
'set-answer': 'answer-data/set',
'set-question': 'question/set',
'pair-answer': 'answer-data/pair',
'pair-question': 'question/pair',
'sort-question': 'question/sort',
'words-answer': 'answer-data/words',
'words-question': 'question/words',
'cloze-answer': 'answer-data/cloze',
'cloze-question': 'question/cloze',
'open-answer': 'answer-data/open',
'open-question': 'question/open',
'selection-answer': 'answer-data/selection',
'selection-question': 'question/selection',
'grid-question': 'question/grid',
'grid-answer': 'answer-data/grid',
'ordering-question': 'question/ordering',
'ordering-answer': 'answer-data/ordering',
'boolean-question': 'question/boolean',
'boolean-answer': 'answer-data/boolean',
'waveform-question': 'question/waveform',
'waveform-answer': 'answer-data/waveform',
'answer': 'answer',
'step': 'step',
'quiz': 'quiz',
'paper': 'paper'
};
Object.keys(resolved).forEach(function (id) {
var schema = require('./format/' + resolved[id] + '/schema.json');
var uri = getSchemaUri(id);
ajv.addSchema(schema, uri);
});
/**
* Returns the URI corresponding to a short schema identifier.
*/
function getSchemaUri(schemaId) {
return 'http://json-quiz.github.io/json-quiz/schemas/'
+ resolved[schemaId]
+ '/schema.json';
}
/**
* Returns the validator function for a given schema.
*/
function getSchema(schemaId) {
var validate = ajv.getSchema(getSchemaUri(schemaId));
if (!validate) {
throw new Error('Unknown schema "' + schemaId + '"');
}
return validate;
}
/**
* Returns the directory of a schema.
*/
function getSchemaDir(schemaId) {
if (!resolved[schemaId]) {
throw new Error('Cannot resolve directory of schema "' + schemaId + "'");
}
return 'format/' + resolved[schemaId];
}
/**
* Validates a data sample and returns errors as a hash, where
* property names are data paths and property values are error
* messages.
*/
function validateAndNormalizeErrors(schemaId, dataFilePath) {
var validate = getSchema(schemaId);
var data = require('./' + getSchemaDir(schemaId) + '/examples/' + dataFilePath);
var errors = {};
validate(data);
if (!validate.errors) {
validate.errors = [];
}
validate.errors.forEach(function (error) {
errors[error.dataPath] = errors[error.dataPath] || [];
errors[error.dataPath].push(error.message);
});
return errors;
}
/**
* Returns a set of assertion helpers for a given schema.
*
* The schema file must be included above in the *resolved* and
* *ajv.addSchema* statements.
*
* Assert functions will assume that the examples passed in are
* located the *format/[schemaDir]/examples/[valid|invalid]*
* directory.
*/
function makeAsserters(schemaId) {
var _schemaId = schemaId;
return {
isValid: function (exampleName) {
var filePath = 'valid/' + exampleName + '.json';
var errors = validateAndNormalizeErrors(_schemaId, filePath);
var errorsAsString = '(errors: ' + JSON.stringify(errors) + ')';
assert.deepStrictEqual(errors, {}, 'Validation was not supposed to return any errors for "' + exampleName + '" ' + errorsAsString);
},
areValid: function (exampleNames) {
var self = this;
exampleNames.forEach(function (example) {
it(getSchemaDir(_schemaId) + '/examples/valid/' + example + '.json', function () {
self.isValid(example);
});
});
},
hasError: function (exampleName, expectedError) {
var filePath = 'invalid/' + exampleName + '.json';
var errors = validateAndNormalizeErrors(_schemaId, filePath);
var expectedPath = Object.keys(expectedError)[0];
var expectedMessage = expectedError[expectedPath];
var errorsAsString = '(errors: ' + JSON.stringify(errors) + ')';
assert(expectedPath in errors, 'No error at expected path for "' + exampleName + '" ' + errorsAsString);
assert(
errors[expectedPath].indexOf(expectedMessage) > -1,
'The expected error ("' + expectedMessage+ '") is missing ' + errorsAsString
);
},
hasErrors: function (exampleName, expectedErrors) {
Object.keys(expectedErrors).forEach(function (path) {
var error = {};
error[path] = expectedErrors[path];
this.hasError(exampleName, error);
}, this);
}
};
}
module.exports = makeAsserters;