forked from friendica/php-json-ld
Fix remaining bugs with framing and test harness.
This commit is contained in:
parent
084727c586
commit
30622b2aeb
|
@ -220,7 +220,9 @@ class TestRunner {
|
||||||
*/
|
*/
|
||||||
global $eol;
|
global $eol;
|
||||||
foreach($manifests as $manifest) {
|
foreach($manifests as $manifest) {
|
||||||
|
if(property_exists($manifest, 'name')) {
|
||||||
$this->group($manifest->name);
|
$this->group($manifest->name);
|
||||||
|
}
|
||||||
$filepath = $manifest->filepath;
|
$filepath = $manifest->filepath;
|
||||||
foreach($manifest->sequence as $test) {
|
foreach($manifest->sequence as $test) {
|
||||||
// read test input files
|
// read test input files
|
||||||
|
@ -240,7 +242,6 @@ class TestRunner {
|
||||||
$result = jsonld_expand($input, $options);
|
$result = jsonld_expand($input, $options);
|
||||||
}
|
}
|
||||||
else if(in_array('jld:CompactTest', $type)) {
|
else if(in_array('jld:CompactTest', $type)) {
|
||||||
continue;
|
|
||||||
$this->test($test->name);
|
$this->test($test->name);
|
||||||
$input = read_test_json($test->input, $filepath);
|
$input = read_test_json($test->input, $filepath);
|
||||||
$test->context = read_test_json($test->context, $filepath);
|
$test->context = read_test_json($test->context, $filepath);
|
||||||
|
@ -248,7 +249,6 @@ class TestRunner {
|
||||||
$result = jsonld_compact($input, $test->context, $options);
|
$result = jsonld_compact($input, $test->context, $options);
|
||||||
}
|
}
|
||||||
else if(in_array('jld:FrameTest', $type)) {
|
else if(in_array('jld:FrameTest', $type)) {
|
||||||
continue;
|
|
||||||
$this->test($test->name);
|
$this->test($test->name);
|
||||||
$input = read_test_json($test->input, $filepath);
|
$input = read_test_json($test->input, $filepath);
|
||||||
$test->frame = read_test_json($test->frame, $filepath);
|
$test->frame = read_test_json($test->frame, $filepath);
|
||||||
|
@ -264,6 +264,9 @@ class TestRunner {
|
||||||
// check results
|
// check results
|
||||||
$this->check($test, $test->expect, $result);
|
$this->check($test, $test->expect, $result);
|
||||||
}
|
}
|
||||||
|
if(property_exists($manifest, 'name')) {
|
||||||
|
$this->ungroup();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -281,6 +284,7 @@ $tr = new TestRunner();
|
||||||
$tr->group('JSON-LD');
|
$tr->group('JSON-LD');
|
||||||
$tr->run($tr->load($options['d']));
|
$tr->run($tr->load($options['d']));
|
||||||
$tr->ungroup();
|
$tr->ungroup();
|
||||||
|
echo "Done. Total:{$tr->total} Passed:{$tr->passed} Failed:{$tr->failed}$eol";
|
||||||
echo "All tests complete.$eol";
|
echo "All tests complete.$eol";
|
||||||
|
|
||||||
?>
|
/* end of file, omit ?> */
|
||||||
|
|
36
jsonld.php
36
jsonld.php
|
@ -329,7 +329,8 @@ class JsonLdProcessor {
|
||||||
isset($options['resolver']) or $options['resolver'] = 'jsonld_resolve_url';
|
isset($options['resolver']) or $options['resolver'] = 'jsonld_resolve_url';
|
||||||
|
|
||||||
// preserve frame context
|
// preserve frame context
|
||||||
$ctx = $frame->{'@context'} ?: new stdClass();
|
$ctx = (property_exists($frame, '@context') ?
|
||||||
|
$frame->{'@context'} : new stdClass());
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// expand input
|
// expand input
|
||||||
|
@ -1920,8 +1921,8 @@ class JsonLdProcessor {
|
||||||
|
|
||||||
// get flags for current frame
|
// get flags for current frame
|
||||||
$options = $state->options;
|
$options = $state->options;
|
||||||
$embedOn = $this->_getFrameFlag($frame, $options, 'embed');
|
$embed_on = $this->_getFrameFlag($frame, $options, 'embed');
|
||||||
$explicitOn = $this->_getFrameFlag($frame, $options, 'explicit');
|
$explicit_on = $this->_getFrameFlag($frame, $options, 'explicit');
|
||||||
|
|
||||||
// add matches to output
|
// add matches to output
|
||||||
foreach($matches as $id => $subject) {
|
foreach($matches as $id => $subject) {
|
||||||
|
@ -1940,18 +1941,18 @@ class JsonLdProcessor {
|
||||||
$embed = (object)array('parent' => $parent, 'property' => $property);
|
$embed = (object)array('parent' => $parent, 'property' => $property);
|
||||||
|
|
||||||
// if embed is on and there is an existing embed
|
// if embed is on and there is an existing embed
|
||||||
if($embedOn && property_exists($state->embeds, $id)) {
|
if($embed_on && property_exists($state->embeds, $id)) {
|
||||||
// only overwrite an existing embed if it has already been added to its
|
// only overwrite an existing embed if it has already been added to its
|
||||||
// parent -- otherwise its parent is somewhere up the tree from this
|
// parent -- otherwise its parent is somewhere up the tree from this
|
||||||
// embed and the embed would occur twice once the tree is added
|
// embed and the embed would occur twice once the tree is added
|
||||||
$embedOn = false;
|
$embed_on = false;
|
||||||
|
|
||||||
// existing embed's parent is an array
|
// existing embed's parent is an array
|
||||||
$existing = $state->embeds->{$id};
|
$existing = $state->embeds->{$id};
|
||||||
if(is_array($existing->parent)) {
|
if(is_array($existing->parent)) {
|
||||||
foreach($existing->parent as $p) {
|
foreach($existing->parent as $p) {
|
||||||
if(self::compareValues($output, $p)) {
|
if(self::compareValues($output, $p)) {
|
||||||
$embedOn = true;
|
$embed_on = true;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1959,17 +1960,17 @@ class JsonLdProcessor {
|
||||||
// existing embed's parent is an object
|
// existing embed's parent is an object
|
||||||
else if(self::hasValue(
|
else if(self::hasValue(
|
||||||
$existing->parent, $existing->property, $output)) {
|
$existing->parent, $existing->property, $output)) {
|
||||||
$embedOn = true;
|
$embed_on = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
// existing embed has already been added, so allow an overwrite
|
// existing embed has already been added, so allow an overwrite
|
||||||
if($embedOn) {
|
if($embed_on) {
|
||||||
$this->_removeEmbed($state, $id);
|
$this->_removeEmbed($state, $id);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// not embedding, add output without any other properties
|
// not embedding, add output without any other properties
|
||||||
if(!$embedOn) {
|
if(!$embed_on) {
|
||||||
$this->_addFrameOutput($state, $parent, $property, $output);
|
$this->_addFrameOutput($state, $parent, $property, $output);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
@ -1989,7 +1990,7 @@ class JsonLdProcessor {
|
||||||
// if property isn't in the frame
|
// if property isn't in the frame
|
||||||
if(!property_exists($frame, $prop)) {
|
if(!property_exists($frame, $prop)) {
|
||||||
// if explicit is off, embed values
|
// if explicit is off, embed values
|
||||||
if(!$explicitOn) {
|
if(!$explicit_on) {
|
||||||
$this->_embedValues($state, $subject, $prop, $output);
|
$this->_embedValues($state, $subject, $prop, $output);
|
||||||
}
|
}
|
||||||
continue;
|
continue;
|
||||||
|
@ -2046,8 +2047,9 @@ class JsonLdProcessor {
|
||||||
// if omit default is off, then include default values for properties
|
// if omit default is off, then include default values for properties
|
||||||
// that appear in the next frame but are not in the matching subject
|
// that appear in the next frame but are not in the matching subject
|
||||||
$next = $frame->{$prop}[0];
|
$next = $frame->{$prop}[0];
|
||||||
$omitDefaultOn = $this->_getFrameFlag($next, $options, 'omitDefault');
|
$omit_default_on = $this->_getFrameFlag(
|
||||||
if(!$omitDefaultOn && !property_exists($output, $prop)) {
|
$next, $options, 'omitDefault');
|
||||||
|
if(!$omit_default_on && !property_exists($output, $prop)) {
|
||||||
$preserve = '@null';
|
$preserve = '@null';
|
||||||
if(property_exists($next, '@default')) {
|
if(property_exists($next, '@default')) {
|
||||||
$preserve = self::copy($next->{'@default'});
|
$preserve = self::copy($next->{'@default'});
|
||||||
|
@ -2072,7 +2074,7 @@ class JsonLdProcessor {
|
||||||
* @return mixed $the flag value.
|
* @return mixed $the flag value.
|
||||||
*/
|
*/
|
||||||
protected function _getFrameFlag($frame, $options, $name) {
|
protected function _getFrameFlag($frame, $options, $name) {
|
||||||
$flag = "'@'$name";
|
$flag = "@$name";
|
||||||
return (property_exists($frame, $flag) ?
|
return (property_exists($frame, $flag) ?
|
||||||
$frame->{$flag}[0] : $options[$name]);
|
$frame->{$flag}[0] : $options[$name]);
|
||||||
}
|
}
|
||||||
|
@ -2135,10 +2137,10 @@ class JsonLdProcessor {
|
||||||
}
|
}
|
||||||
|
|
||||||
// check ducktype
|
// check ducktype
|
||||||
foreach($frame as $key) {
|
foreach($frame as $k => $v) {
|
||||||
// only not a duck if @id or non-keyword isn't in subject
|
// only not a duck if @id or non-keyword isn't in subject
|
||||||
if(($key === '@id' || !self::_isKeyword($key)) &&
|
if(($k === '@id' || !self::_isKeyword($k)) &&
|
||||||
!property_exists($subject, $key)) {
|
!property_exists($subject, $k)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2255,7 +2257,7 @@ class JsonLdProcessor {
|
||||||
* @param mixed $output the output to add.
|
* @param mixed $output the output to add.
|
||||||
*/
|
*/
|
||||||
protected function _addFrameOutput($state, $parent, $property, $output) {
|
protected function _addFrameOutput($state, $parent, $property, $output) {
|
||||||
if(is_object($parent)) {
|
if(is_object($parent) && !($parent instanceof ArrayObject)) {
|
||||||
self::addValue($parent, $property, $output, true);
|
self::addValue($parent, $property, $output, true);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
|
Loading…
Reference in a new issue