Skip to content

Commit c63a596

Browse files
committed
Add support for normalization test suite.
- Handle rdfn:Urgna2012EvalTest and rdfn:Urdna2015EvalTest. - Use nquads input format for normalization tests. - Support normalization test suite format with various tweaks. Compacting the data would eliminate most of these changes but can't rely on compact to work in the code that tests compact! So hard coded fixes are used. - Support 'entries' and 'sequence' for tests. - Support 'include' as filename without a .jsonld extension. - Support 'type' and '@type' as aliases. - Support 'id' and '@id' as aliases. - Support 'action' and 'input' as aliases. - Support 'result' and 'expect' as aliases. - No longer strip '#t' prefix from '#tNNN' ids. - Default to positive test if nothing specified. - Add normalization test to travis-ci config. - Fix container infrastructure flag from 'root' to 'sudo'. - Update README with new testing info.
1 parent 57b3279 commit c63a596

File tree

3 files changed

+133
-29
lines changed

3 files changed

+133
-29
lines changed

.travis.yml

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
language: php
22
php:
3-
- 5.6
4-
- 5.5
5-
- 5.4
63
- 5.3
7-
root: false
4+
- 5.4
5+
- 5.5
6+
- 5.6
7+
sudo: false
88
# download test suite and run tests... submodule? meta testing project with
99
# all of the reference implementations?
1010
script:
11-
- git clone https://github.com/json-ld/json-ld.org.git spec
12-
- phpunit test.php -d spec/test-suite
11+
- git clone https://github.com/json-ld/json-ld.org.git _json-ld.org
12+
- phpunit test.php -d ./_json-ld.org/test-suite
13+
- git clone https://github.com/json-ld/normalization.git _normalization
14+
- phpunit test.php -d ./_normalization/tests
1315
notifications:
1416
email:
1517
on_success: change

README.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -171,14 +171,16 @@ This library includes a sample testing utility which may be used to verify
171171
that changes to the processor maintain the correct output.
172172

173173
To run the sample tests you will need to get the test suite files by cloning
174-
the [json-ld.org repository][json-ld.org] hosted on GitHub:
174+
the `json-ld.org` and `normalization` repositories hosted on GitHub:
175175

176-
https://github.com/json-ld/json-ld.org
176+
- https://github.com/json-ld/json-ld.org
177+
- https://github.com/json-ld/normalization
177178

178-
Then run the PHPUnit test.php application and point it at the directory
179-
containing the tests.
179+
Then run the PHPUnit test.php application and point it at the directories
180+
containing the tests:
180181

181182
phpunit test.php -d {PATH_TO_JSON_LD_ORG/test-suite}
183+
phpunit test.php -d {PATH_TO_NORMALIZATION/tests}
182184

183185
[Digital Bazaar]: http://digitalbazaar.com/
184186
[JSON-LD]: http://json-ld.org/
@@ -187,4 +189,3 @@ containing the tests.
187189
[PHP]: http://php.net
188190
[RDFa]: http://www.w3.org/TR/rdfa-core/
189191
[RFC7159]: http://tools.ietf.org/html/rfc7159
190-
[json-ld.org]: https://github.com/json-ld/json-ld.org

test.php

Lines changed: 119 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,42 @@ public function testNormalize($test) {
130130
$test->run('jsonld_normalize', array($input, $options));
131131
}
132132

133+
/**
134+
* Tests URGNA2012 normalization.
135+
*
136+
* @param JsonLdTest $test the test to run.
137+
*
138+
* @group normalize
139+
* @dataProvider urgna2012Provider
140+
*/
141+
public function testUrgna2012($test) {
142+
$this->test = $test;
143+
$input = $test->readProperty('action');
144+
$options = $test->createOptions(array(
145+
'algorithm' => 'URGNA2012',
146+
'inputFormat' => 'application/nquads',
147+
'format' => 'application/nquads'));
148+
$test->run('jsonld_normalize', array($input, $options));
149+
}
150+
151+
/**
152+
* Tests URDNA2015 normalization.
153+
*
154+
* @param JsonLdTest $test the test to run.
155+
*
156+
* @group normalize
157+
* @dataProvider urdna2015Provider
158+
*/
159+
public function testUrdna2015($test) {
160+
$this->test = $test;
161+
$input = $test->readProperty('action');
162+
$options = $test->createOptions(array(
163+
'algorithm' => 'URDNA2015',
164+
'inputFormat' => 'application/nquads',
165+
'format' => 'application/nquads'));
166+
$test->run('jsonld_normalize', array($input, $options));
167+
}
168+
133169
public function expandProvider() {
134170
return new JsonLdTestIterator('jld:ExpandTest');
135171
}
@@ -157,6 +193,14 @@ public function normalizeProvider() {
157193
public function frameProvider() {
158194
return new JsonLdTestIterator('jld:FrameTest');
159195
}
196+
197+
public function urgna2012Provider() {
198+
return new JsonLdTestIterator('rdfn:Urgna2012EvalTest');
199+
}
200+
201+
public function urdna2015Provider() {
202+
return new JsonLdTestIterator('rdfn:Urdna2015EvalTest');
203+
}
160204
}
161205

162206
class JsonLdManifest {
@@ -167,8 +211,14 @@ public function __construct($data, $filename) {
167211
}
168212

169213
public function load(&$tests) {
170-
$sequence = JsonLdProcessor::getValues($this->data, 'sequence');
171-
foreach($sequence as $entry) {
214+
$entries = array_merge(
215+
JsonLdProcessor::getValues($this->data, 'sequence'),
216+
JsonLdProcessor::getValues($this->data, 'entries'));
217+
$includes = JsonLdProcessor::getValues($this->data, 'include');
218+
foreach($includes as $include) {
219+
array_push($entries, $include . '.jsonld');
220+
}
221+
foreach($entries as $entry) {
172222
if(is_string($entry)) {
173223
$filename = join(
174224
DIRECTORY_SEPARATOR, array($this->dirname, $entry));
@@ -177,14 +227,17 @@ public function load(&$tests) {
177227
$filename = $this->filename;
178228
}
179229

180-
if(JsonLdProcessor::hasValue($entry, '@type', 'mf:Manifest')) {
230+
if(JsonLdProcessor::hasValue($entry, '@type', 'mf:Manifest') ||
231+
JsonLdProcessor::hasValue($entry, 'type', 'mf:Manifest')) {
181232
// entry is another manifest
182233
$manifest = new JsonLdManifest($entry, $filename);
183234
$manifest->load($tests);
184235
} else {
185236
// assume entry is a test
186237
$test = new JsonLdTest($this, $entry, $filename);
187-
$types = JsonLdProcessor::getValues($test->data, '@type');
238+
$types = array_merge(
239+
JsonLdProcessor::getValues($test->data, '@type'),
240+
JsonLdProcessor::getValues($test->data, 'type'));
188241
foreach($types as $type) {
189242
if(!isset($tests[$type])) {
190243
$tests[$type] = array();
@@ -202,27 +255,64 @@ public function __construct($manifest, $data, $filename) {
202255
$this->data = $data;
203256
$this->filename = $filename;
204257
$this->dirname = dirname($filename);
205-
$this->isPositive = JsonLdProcessor::hasValue(
206-
$data, '@type', 'jld:PositiveEvaluationTest');
207-
$this->isNegative = JsonLdProcessor::hasValue(
208-
$data, '@type', 'jld:NegativeEvaluationTest');
258+
$this->isPositive =
259+
JsonLdProcessor::hasValue(
260+
$data, '@type', 'jld:PositiveEvaluationTest') ||
261+
JsonLdProcessor::hasValue(
262+
$data, 'type', 'jld:PositiveEvaluationTest');
263+
$this->isNegative =
264+
JsonLdProcessor::hasValue(
265+
$data, '@type', 'jld:NegativeEvaluationTest') ||
266+
JsonLdProcessor::hasValue(
267+
$data, 'type', 'jld:NegativeEvaluationTest');
209268

210269
// generate test name
211-
$this->name = $manifest->data->name . ' ' . substr($data->{'@id'}, 2) .
212-
' - ' . $this->data->name;
270+
if(isset($manifest->data->name)) {
271+
$manifestLabel = $manifest->data->name;
272+
} else if(isset($manifest->data->label)) {
273+
$manifestLabel = $manifest->data->label;
274+
} else {
275+
$manifestLabel = 'UNNAMED';
276+
}
277+
if(isset($this->data->id)) {
278+
$testId = $this->data->id;
279+
} else {
280+
$testId = $this->data->{'@id'};
281+
}
282+
if(isset($this->data->name)) {
283+
$testLabel = $this->data->name;
284+
} else if(isset($this->data->label)) {
285+
$testLabel = $this->data->label;
286+
} else {
287+
$testLabel = 'UNNAMED';
288+
}
289+
290+
$this->name = $manifestLabel . ' ' . $testId . ' - ' . $testLabel;
213291

214292
// expand @id and input base
215-
$data->{'@id'} = ($manifest->data->baseIri .
216-
basename($manifest->filename) . $data->{'@id'});
217-
$this->base = $manifest->data->baseIri . $data->input;
293+
if(isset($manifest->data->baseIri)) {
294+
$data->{'@id'} = ($manifest->data->baseIri .
295+
basename($manifest->filename) . $data->{'@id'});
296+
$this->base = $manifest->data->baseIri . $data->input;
297+
}
298+
}
299+
300+
private function _getResultProperty() {
301+
if(isset($this->data->expect)) {
302+
return 'expect';
303+
} else if(isset($this->data->result)) {
304+
return 'result';
305+
} else {
306+
throw new Exception('No test result property found.');
307+
}
218308
}
219309

220310
public function run($fn, $params) {
221311
// read expected data
222312
if($this->isNegative) {
223313
$this->expected = $this->data->expect;
224314
} else {
225-
$this->expected = $this->readProperty('expect');
315+
$this->expected = $this->readProperty($this->_getResultProperty());
226316
}
227317

228318
try {
@@ -232,11 +322,14 @@ public function run($fn, $params) {
232322
}
233323
PHPUnit_Framework_TestCase::assertEquals($this->expected, $this->actual);
234324
} catch(Exception $e) {
235-
if($this->isPositive) {
325+
// assume positive test
326+
if($this->isNegative) {
327+
$this->actual = $this->getJsonLdErrorCode($e);
328+
PHPUnit_Framework_TestCase::assertEquals(
329+
$this->expected, $this->actual);
330+
} else {
236331
throw $e;
237332
}
238-
$this->actual = $this->getJsonLdErrorCode($e);
239-
PHPUnit_Framework_TestCase::assertEquals($this->expected, $this->actual);
240333
}
241334
}
242335

@@ -541,8 +634,16 @@ public function addFailure(
541634
PHPUnit_Framework_AssertionFailedError $e, $time) {
542635
$this->addAssertion($test->test, false);
543636
if($test->result->shouldStop()) {
637+
if(isset($test->test->name)) {
638+
$name = $test->test->name;
639+
} else if(isset($test->test->label)) {
640+
$name = $test->test->label;
641+
} else {
642+
$name = 'UNNAMED';
643+
}
644+
// FIXME
544645
printf("\n\nFAILED\n");
545-
printf("Test: %s\n", $test->test->name);
646+
printf("Test: %s\n", $name);
546647
printf("Purpose: %s\n", $test->test->data->purpose);
547648
printf("EXPECTED: %s\n", Util::jsonldEncode($test->test->expected));
548649
printf("ACTUAL: %s\n", Util::jsonldEncode($test->test->actual));

0 commit comments

Comments
 (0)