Skip to content

Commit 8f5e47f

Browse files
committed
Update testing framework.
- Refactor and align closer with rdf-canonize testing. - Update EARL reporting.
1 parent ca09db4 commit 8f5e47f

File tree

3 files changed

+340
-317
lines changed

3 files changed

+340
-317
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
"karma-sourcemap-loader": "^0.3.7",
7070
"karma-tap-reporter": "0.0.6",
7171
"karma-webpack": "^4.0.2",
72+
"klona": "^2.0.5",
7273
"mocha": "^8.3.2",
7374
"mocha-lcov-reporter": "^1.3.0",
7475
"nyc": "^15.1.0",

tests/earl-report.js

Lines changed: 136 additions & 133 deletions
Original file line numberDiff line numberDiff line change
@@ -7,114 +7,152 @@
77
*/
88

99
/**
10-
* Create an EARL Reporter.
11-
*
12-
* @param options {Object} reporter options
13-
* id: {String} report id
14-
* env: {Object} environment description
10+
* EARL Reporter
1511
*/
16-
function EarlReport(options) {
17-
let today = new Date();
18-
today = today.getFullYear() + '-' +
19-
(today.getMonth() < 9 ?
20-
'0' + (today.getMonth() + 1) : today.getMonth() + 1) + '-' +
21-
(today.getDate() < 10 ? '0' + today.getDate() : today.getDate());
22-
// one date for tests with no subsecond resolution
23-
this.now = new Date();
24-
this.now.setMilliseconds(0);
25-
this.id = options.id;
26-
this.env = options.env;
27-
// test environment
28-
this._environment = null;
29-
/* eslint-disable quote-props */
30-
this._report = {
31-
'@context': {
32-
'doap': 'http://usefulinc.com/ns/doap#',
33-
'foaf': 'http://xmlns.com/foaf/0.1/',
34-
'dc': 'http://purl.org/dc/terms/',
35-
'earl': 'http://www.w3.org/ns/earl#',
36-
'xsd': 'http://www.w3.org/2001/XMLSchema#',
37-
'jsonld': 'http://www.w3.org/ns/json-ld#',
38-
'doap:homepage': {'@type': '@id'},
39-
'doap:license': {'@type': '@id'},
40-
'dc:creator': {'@type': '@id'},
41-
'foaf:homepage': {'@type': '@id'},
42-
'subjectOf': {'@reverse': 'earl:subject'},
43-
'earl:assertedBy': {'@type': '@id'},
44-
'earl:mode': {'@type': '@id'},
45-
'earl:test': {'@type': '@id'},
46-
'earl:outcome': {'@type': '@id'},
47-
'dc:date': {'@type': 'xsd:date'},
48-
'doap:created': {'@type': 'xsd:date'}
49-
},
50-
'@id': 'https://github.com/digitalbazaar/jsonld.js',
51-
'@type': [
52-
'doap:Project',
53-
'earl:TestSubject',
54-
'earl:Software'
55-
],
56-
'doap:name': 'jsonld.js',
57-
'dc:title': 'jsonld.js',
58-
'doap:homepage': 'https://github.com/digitalbazaar/jsonld.js',
59-
'doap:license':
60-
'https://github.com/digitalbazaar/jsonld.js/blob/master/LICENSE',
61-
'doap:description': 'A JSON-LD processor for JavaScript',
62-
'doap:programming-language': 'JavaScript',
63-
'dc:creator': 'https://digitalbazaar.com/',
64-
'doap:developer': {
65-
'@id': 'https://digitalbazaar.com/',
12+
class EarlReport {
13+
/**
14+
* Create an EARL Reporter.
15+
*
16+
* @param options {Object} reporter options
17+
* env: {Object} environment description
18+
*/
19+
constructor(options) {
20+
let today = new Date();
21+
today = today.getFullYear() + '-' +
22+
(today.getMonth() < 9 ?
23+
'0' + (today.getMonth() + 1) : today.getMonth() + 1) + '-' +
24+
(today.getDate() < 10 ? '0' + today.getDate() : today.getDate());
25+
// one date for tests with no subsecond resolution
26+
this.now = new Date();
27+
this.now.setMilliseconds(0);
28+
this.env = options.env;
29+
// test environment
30+
this._environment = null;
31+
/* eslint-disable quote-props */
32+
this._report = {
33+
'@context': {
34+
'doap': 'http://usefulinc.com/ns/doap#',
35+
'foaf': 'http://xmlns.com/foaf/0.1/',
36+
'dc': 'http://purl.org/dc/terms/',
37+
'earl': 'http://www.w3.org/ns/earl#',
38+
'xsd': 'http://www.w3.org/2001/XMLSchema#',
39+
'jsonld': 'http://www.w3.org/ns/json-ld#',
40+
'doap:homepage': {'@type': '@id'},
41+
'doap:license': {'@type': '@id'},
42+
'dc:creator': {'@type': '@id'},
43+
'foaf:homepage': {'@type': '@id'},
44+
'subjectOf': {'@reverse': 'earl:subject'},
45+
'earl:assertedBy': {'@type': '@id'},
46+
'earl:mode': {'@type': '@id'},
47+
'earl:test': {'@type': '@id'},
48+
'earl:outcome': {'@type': '@id'},
49+
'dc:date': {'@type': 'xsd:date'},
50+
'doap:created': {'@type': 'xsd:date'}
51+
},
52+
'@id': 'https://github.com/digitalbazaar/jsonld.js',
6653
'@type': [
67-
'foaf:Organization',
68-
'earl:Assertor'
54+
'doap:Project',
55+
'earl:TestSubject',
56+
'earl:Software'
6957
],
70-
'foaf:name': 'Digital Bazaar, Inc.',
71-
'foaf:homepage': 'https://digitalbazaar.com/'
72-
},
73-
'doap:release': {
74-
'doap:revision': '',
75-
'doap:created': today
76-
},
77-
'subjectOf': []
78-
};
79-
/* eslint-enable quote-props */
80-
if(this.env && this.env.version) {
81-
this._report['doap:release']['doap:revision'] = this.env.version;
58+
'doap:name': 'jsonld.js',
59+
'dc:title': 'jsonld.js',
60+
'doap:homepage': 'https://github.com/digitalbazaar/jsonld.js',
61+
'doap:license':
62+
'https://github.com/digitalbazaar/jsonld.js/blob/master/LICENSE',
63+
'doap:description': 'A JSON-LD processor for JavaScript',
64+
'doap:programming-language': 'JavaScript',
65+
'dc:creator': 'https://digitalbazaar.com/',
66+
'doap:developer': {
67+
'@id': 'https://digitalbazaar.com/',
68+
'@type': [
69+
'foaf:Organization',
70+
'earl:Assertor'
71+
],
72+
'foaf:name': 'Digital Bazaar, Inc.',
73+
'foaf:homepage': 'https://digitalbazaar.com/'
74+
},
75+
'doap:release': {
76+
'doap:revision': '',
77+
'doap:created': today
78+
},
79+
'subjectOf': []
80+
};
81+
/* eslint-enable quote-props */
82+
if(this.env && this.env.version) {
83+
this._report['doap:release']['doap:revision'] = this.env.version;
84+
}
8285
}
83-
}
8486

85-
EarlReport.prototype.addAssertion = function(test, pass, options) {
86-
options = options || {};
87-
const assertion = {
88-
'@type': 'earl:Assertion',
89-
'earl:assertedBy': this._report['doap:developer']['@id'],
90-
'earl:mode': 'earl:automatic',
91-
'earl:test': test['@id'],
92-
'earl:result': {
93-
'@type': 'earl:TestResult',
94-
'dc:date': this.now.toISOString(),
95-
'earl:outcome': pass ? 'earl:passed' : 'earl:failed'
96-
}
97-
};
98-
if(options.benchmarkResult) {
99-
const result = {
100-
...options.benchmarkResult
87+
addAssertion(test, pass, options) {
88+
options = options || {};
89+
const assertion = {
90+
'@type': 'earl:Assertion',
91+
'earl:assertedBy': this._report['doap:developer']['@id'],
92+
'earl:mode': 'earl:automatic',
93+
'earl:test': test['@id'],
94+
'earl:result': {
95+
'@type': 'earl:TestResult',
96+
'dc:date': this.now.toISOString(),
97+
'earl:outcome': pass ? 'earl:passed' : 'earl:failed'
98+
}
10199
};
102-
if(this._environment) {
103-
result['jldb:environment'] = this._environment['@id'];
100+
if(options.benchmarkResult) {
101+
const result = {
102+
...options.benchmarkResult
103+
};
104+
if(this._environment) {
105+
result['jldb:environment'] = this._environment['@id'];
106+
}
107+
assertion['jldb:result'] = result;
104108
}
105-
assertion['jldb:result'] = result;
109+
this._report.subjectOf.push(assertion);
110+
return this;
106111
}
107-
this._report.subjectOf.push(assertion);
108-
return this;
109-
};
110112

111-
EarlReport.prototype.report = function() {
112-
return this._report;
113-
};
113+
report() {
114+
return this._report;
115+
}
114116

115-
EarlReport.prototype.reportJson = function() {
116-
return JSON.stringify(this._report, null, 2);
117-
};
117+
reportJson() {
118+
return JSON.stringify(this._report, null, 2);
119+
}
120+
121+
// setup @context and environment to handle benchmark data
122+
setupForBenchmarks(options) {
123+
// add context if needed
124+
if(!Array.isArray(this._report['@context'])) {
125+
this._report['@context'] = [this._report['@context']];
126+
}
127+
if(!this._report['@context'].some(c => c === _benchmarkContext)) {
128+
this._report['@context'].push(_benchmarkContext);
129+
}
130+
if(options.testEnv) {
131+
// add report environment
132+
const fields = [
133+
['label', 'jldb:label'],
134+
['arch', 'jldb:arch'],
135+
['cpu', 'jldb:cpu'],
136+
['cpuCount', 'jldb:cpuCount'],
137+
['platform', 'jldb:platform'],
138+
['runtime', 'jldb:runtime'],
139+
['runtimeVersion', 'jldb:runtimeVersion'],
140+
['comment', 'jldb:comment']
141+
];
142+
const _env = {
143+
'@id': '_:environment:0'
144+
};
145+
for(const [field, property] of fields) {
146+
if(options.testEnv[field]) {
147+
_env[property] = options.testEnv[field];
148+
}
149+
}
150+
this._environment = _env;
151+
this._report['@included'] = this._report['@included'] || [];
152+
this._report['@included'].push(_env);
153+
}
154+
}
155+
}
118156

119157
/* eslint-disable quote-props */
120158
const _benchmarkContext = {
@@ -162,39 +200,4 @@ const _benchmarkContext = {
162200
};
163201
/* eslint-enable quote-props */
164202

165-
// setup @context and environment to handle benchmark data
166-
EarlReport.prototype.setupForBenchmarks = function(options) {
167-
// add context if needed
168-
if(!Array.isArray(this._report['@context'])) {
169-
this._report['@context'] = [this._report['@context']];
170-
}
171-
if(!this._report['@context'].some(c => c === _benchmarkContext)) {
172-
this._report['@context'].push(_benchmarkContext);
173-
}
174-
if(options.testEnv) {
175-
// add report environment
176-
const fields = [
177-
['label', 'jldb:label'],
178-
['arch', 'jldb:arch'],
179-
['cpu', 'jldb:cpu'],
180-
['cpuCount', 'jldb:cpuCount'],
181-
['platform', 'jldb:platform'],
182-
['runtime', 'jldb:runtime'],
183-
['runtimeVersion', 'jldb:runtimeVersion'],
184-
['comment', 'jldb:comment']
185-
];
186-
const _env = {
187-
'@id': '_:environment:0'
188-
};
189-
for(const [field, property] of fields) {
190-
if(options.testEnv[field]) {
191-
_env[property] = options.testEnv[field];
192-
}
193-
}
194-
this._environment = _env;
195-
this._report['@included'] = this._report['@included'] || [];
196-
this._report['@included'].push(_env);
197-
}
198-
};
199-
200203
module.exports = EarlReport;

0 commit comments

Comments
 (0)