Fix bugs to pass unit tests, do JSON comparison in test runner.

This commit is contained in:
Dave Longley 2013-02-08 00:15:45 -05:00
parent 3831d5336a
commit 61e1140b3b
2 changed files with 22 additions and 19 deletions

View file

@ -173,10 +173,14 @@ class TestRunner {
echo $line; echo $line;
} }
public function check($test, $expect, $result) { public function check($test, $expect, $result, $compare_json) {
global $eol; global $eol;
if(deep_compare($expect, $result)) { $json_equal = true;
if($compare_json) {
$json_equal = (json_encode($expect) === json_encode($result));
}
if($json_equal || deep_compare($expect, $result)) {
$this->passed += 1; $this->passed += 1;
echo "PASS$eol"; echo "PASS$eol";
} }
@ -257,6 +261,7 @@ class TestRunner {
foreach($manifest->sequence as $test) { foreach($manifest->sequence as $test) {
// read test input files // read test input files
$type = $test->{'@type'}; $type = $test->{'@type'};
$compare_json = true;
$options = array( $options = array(
'base' => 'http://json-ld.org/test-suite/tests/' . $test->input); 'base' => 'http://json-ld.org/test-suite/tests/' . $test->input);
@ -311,6 +316,7 @@ class TestRunner {
$test->expect = read_test_nquads($test->expect, $filepath); $test->expect = read_test_nquads($test->expect, $filepath);
$options['format'] = 'application/nquads'; $options['format'] = 'application/nquads';
$result = jsonld_to_rdf($input, $options); $result = jsonld_to_rdf($input, $options);
$compare_json = false;
} }
else { else {
echo "Skipping test \"{$test->name}\" of type: " . echo "Skipping test \"{$test->name}\" of type: " .
@ -319,7 +325,7 @@ class TestRunner {
} }
// check results // check results
$this->check($test, $test->expect, $result); $this->check($test, $test->expect, $result, $compare_json);
} }
catch(JsonLdException $e) { catch(JsonLdException $e) {
echo $eol . $e; echo $eol . $e;

View file

@ -167,8 +167,7 @@ global $jsonld_cache;
$jsonld_cache = new stdClass(); $jsonld_cache = new stdClass();
/** The default active context cache. */ /** The default active context cache. */
// FIXME: turn on $jsonld_cache->activeCtx = new ActiveContextCache();
//$jsonld_cache->activeCtx = new ActiveContextCache();
/** The default JSON-LD URL resolver. */ /** The default JSON-LD URL resolver. */
global $jsonld_default_url_resolver; global $jsonld_default_url_resolver;
@ -1963,7 +1962,6 @@ class JsonLdProcessor {
if(!property_exists($subject, '@graph')) { if(!property_exists($subject, '@graph')) {
$subject->{'@graph'} = array(); $subject->{'@graph'} = array();
} }
$nodeMap = $graphs->{$graph_name};
$ids = array_keys((array)$node_map); $ids = array_keys((array)$node_map);
sort($ids); sort($ids);
foreach($ids as $id) { foreach($ids as $id) {
@ -1999,17 +1997,16 @@ class JsonLdProcessor {
'@merged' => new stdClass())); '@merged' => new stdClass()));
// produce a map of all graphs and name each bnode // produce a map of all graphs and name each bnode
$namer = new UniqueNamer('_:t');
$this->_flatten($input, $state->graphs, '@default', $namer, null, null);
$namer = new UniqueNamer('_:t');
$this->_flatten($input, $state->graphs, '@merged', $namer, null, null);
// FIXME: currently uses subjects from @merged graph only // FIXME: currently uses subjects from @merged graph only
$namer = new UniqueNamer('_:t');
$this->_createNodeMap($input, $state->graphs, '@merged', $namer);
$state->subjects = $state->graphs->{'@merged'}; $state->subjects = $state->graphs->{'@merged'};
// frame the subjects // frame the subjects
$framed = new ArrayObject(); $framed = new ArrayObject();
$this->_matchFrame( $keys = array_keys((array)$state->subjects);
$state, array_keys((array)$state->subjects), $frame, $framed, null); sort($keys);
$this->_matchFrame($state, $keys, $frame, $framed, null);
return (array)$framed; return (array)$framed;
} }
@ -2827,7 +2824,7 @@ class JsonLdProcessor {
} }
// add non-object to list // add non-object to list
if(!is_object($input) || self::_isValue($input)) { if(!is_object($input)) {
if($list !== null) { if($list !== null) {
$list[] = $input; $list[] = $input;
} }
@ -2907,7 +2904,7 @@ class JsonLdProcessor {
continue; continue;
} }
// iterate over objects // iterate over objects (ensure property is added for empty arrays)
$objects = $input->{$property}; $objects = $input->{$property};
if(count($objects) === 0) { if(count($objects) === 0) {
self::addValue( self::addValue(
@ -3101,7 +3098,8 @@ class JsonLdProcessor {
if(property_exists($next, '@default')) { if(property_exists($next, '@default')) {
$preserve = self::copy($next->{'@default'}); $preserve = self::copy($next->{'@default'});
} }
$output->{$prop} = (object)array('@preserve' => $preserve); $preserve = self::arrayify($preserve);
$output->{$prop} = array((object)array('@preserve' => $preserve));
} }
} }
@ -4943,7 +4941,6 @@ class JsonLdProcessor {
* *
* @return bool true if the target has the given key and its value matches. * @return bool true if the target has the given key and its value matches.
*/ */
// FIXME: use this function throughout processor, check for "property_exists"
protected static function _hasKeyValue($target, $key, $value) { protected static function _hasKeyValue($target, $key, $value) {
return (property_exists($target, $key) && $target->{$key} === $value); return (property_exists($target, $key) && $target->{$key} === $value);
} }
@ -5198,7 +5195,7 @@ class ActiveContextCache {
$key2 = serialize($local_ctx); $key2 = serialize($local_ctx);
$this->order[] = (object)array( $this->order[] = (object)array(
'activeCtx' => $key1, 'localCtx' => $key2); 'activeCtx' => $key1, 'localCtx' => $key2);
if(!property_exists($this->cache)) { if(!property_exists($this->cache, $key1)) {
$this->cache->{$key1} = new stdClass(); $this->cache->{$key1} = new stdClass();
} }
$this->cache->{$key1}->{$key2} = $result; $this->cache->{$key1}->{$key2} = $result;