From 61e1140b3bcc27797970f79711be9c14c1abefef Mon Sep 17 00:00:00 2001 From: Dave Longley Date: Fri, 8 Feb 2013 00:15:45 -0500 Subject: [PATCH] Fix bugs to pass unit tests, do JSON comparison in test runner. --- jsonld-tests.php | 12 +++++++++--- jsonld.php | 29 +++++++++++++---------------- 2 files changed, 22 insertions(+), 19 deletions(-) diff --git a/jsonld-tests.php b/jsonld-tests.php index 687e659..ad53ce1 100644 --- a/jsonld-tests.php +++ b/jsonld-tests.php @@ -173,10 +173,14 @@ class TestRunner { echo $line; } - public function check($test, $expect, $result) { + public function check($test, $expect, $result, $compare_json) { 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; echo "PASS$eol"; } @@ -257,6 +261,7 @@ class TestRunner { foreach($manifest->sequence as $test) { // read test input files $type = $test->{'@type'}; + $compare_json = true; $options = array( 'base' => 'http://json-ld.org/test-suite/tests/' . $test->input); @@ -311,6 +316,7 @@ class TestRunner { $test->expect = read_test_nquads($test->expect, $filepath); $options['format'] = 'application/nquads'; $result = jsonld_to_rdf($input, $options); + $compare_json = false; } else { echo "Skipping test \"{$test->name}\" of type: " . @@ -319,7 +325,7 @@ class TestRunner { } // check results - $this->check($test, $test->expect, $result); + $this->check($test, $test->expect, $result, $compare_json); } catch(JsonLdException $e) { echo $eol . $e; diff --git a/jsonld.php b/jsonld.php index 7cbd1b7..de3100d 100644 --- a/jsonld.php +++ b/jsonld.php @@ -167,8 +167,7 @@ global $jsonld_cache; $jsonld_cache = new stdClass(); /** The default active context cache. */ -// FIXME: turn on -//$jsonld_cache->activeCtx = new ActiveContextCache(); +$jsonld_cache->activeCtx = new ActiveContextCache(); /** The default JSON-LD URL resolver. */ global $jsonld_default_url_resolver; @@ -1963,7 +1962,6 @@ class JsonLdProcessor { if(!property_exists($subject, '@graph')) { $subject->{'@graph'} = array(); } - $nodeMap = $graphs->{$graph_name}; $ids = array_keys((array)$node_map); sort($ids); foreach($ids as $id) { @@ -1999,17 +1997,16 @@ class JsonLdProcessor { '@merged' => new stdClass())); // 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 - $state->subjects = $state->graphs->{'@merged'}; + $namer = new UniqueNamer('_:t'); + $this->_createNodeMap($input, $state->graphs, '@merged', $namer); + $state->subjects = $state->graphs->{'@merged'}; // frame the subjects $framed = new ArrayObject(); - $this->_matchFrame( - $state, array_keys((array)$state->subjects), $frame, $framed, null); + $keys = array_keys((array)$state->subjects); + sort($keys); + $this->_matchFrame($state, $keys, $frame, $framed, null); return (array)$framed; } @@ -2827,7 +2824,7 @@ class JsonLdProcessor { } // add non-object to list - if(!is_object($input) || self::_isValue($input)) { + if(!is_object($input)) { if($list !== null) { $list[] = $input; } @@ -2907,7 +2904,7 @@ class JsonLdProcessor { continue; } - // iterate over objects + // iterate over objects (ensure property is added for empty arrays) $objects = $input->{$property}; if(count($objects) === 0) { self::addValue( @@ -3101,7 +3098,8 @@ class JsonLdProcessor { if(property_exists($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. */ - // FIXME: use this function throughout processor, check for "property_exists" protected static function _hasKeyValue($target, $key, $value) { return (property_exists($target, $key) && $target->{$key} === $value); } @@ -5173,7 +5170,7 @@ class ActiveContextCache { $key2 = serialize($local_ctx); if(property_exists($this->cache, $key1)) { $level1 = $this->cache->{$key1}; - if(property_exists($level1, $key2)) { + if(property_exists($level1, $key2)) { // get shareable copy of cached active context return JsonLdProcessor::_shareActiveContext($level1->{$key2}); } @@ -5198,7 +5195,7 @@ class ActiveContextCache { $key2 = serialize($local_ctx); $this->order[] = (object)array( 'activeCtx' => $key1, 'localCtx' => $key2); - if(!property_exists($this->cache)) { + if(!property_exists($this->cache, $key1)) { $this->cache->{$key1} = new stdClass(); } $this->cache->{$key1}->{$key2} = $result;