Test multiple pointers

This commit is contained in:
Andrea Marco Sartori 2022-12-21 19:30:18 +10:00
parent e1450733d1
commit c760a5981c
3 changed files with 134 additions and 10 deletions

View File

@ -42,6 +42,16 @@ class Dataset
}
}
/**
* Retrieve the dataset to test invalid pointers
*
* @return Generator
*/
public static function forInvalidPointers(): Generator
{
yield from ['abc', '/foo~2', '/~', ' '];
}
/**
* Retrieve the dataset to test single pointers
*
@ -61,12 +71,20 @@ class Dataset
}
/**
* Retrieve the dataset to test invalid pointers
* Retrieve the dataset to test multiple pointers
*
* @return Generator
*/
public static function forInvalidPointers(): Generator
public static function forMultiplePointers(): Generator
{
yield from ['abc', '/foo~2', '/~', ' '];
$multiplePointers = require __DIR__ . '/fixtures/pointers/multiple_pointers.php';
foreach ($multiplePointers as $fixture => $valueByPointers) {
$json = file_get_contents(__DIR__ . "/fixtures/json/{$fixture}.json");
foreach ($valueByPointers as $pointers => $value) {
yield [$json, explode(',', $pointers), $value];
}
}
}
}

View File

@ -5,15 +5,15 @@ use Cerbero\JsonParser\Exceptions\PointerException;
use Cerbero\JsonParser\JsonParser;
it('supports single JSON pointers', function (string $json, string $pointer, array $parsed) {
expect(JsonParser::parse($json)->pointer($pointer))->toPointTo($parsed);
})->with(Dataset::forSinglePointers());
it('throws an exception when providing an invalid JSON pointer', function (string $pointer) {
expect(fn () => iterator_to_array(JsonParser::parse('{}')->pointer($pointer)))
->toThrow(PointerException::class, "The string [$pointer] is not a valid JSON pointer");
})->with(Dataset::forInvalidPointers());
// it('supports multiple JSON pointers', function (string $json, array $pointers, array $parsed) {
// expect(JsonParser::parse($json)->pointer(...$pointers))->toParseTo($parsed);
// })->with(Dataset::forMultiplePointers());
it('supports single JSON pointers', function (string $json, string $pointer, array $parsed) {
expect(JsonParser::parse($json)->pointer($pointer))->toPointTo($parsed);
})->with(Dataset::forSinglePointers());
it('supports multiple JSON pointers', function (string $json, array $pointers, array $parsed) {
expect(JsonParser::parse($json)->pointer(...$pointers))->toPointTo($parsed);
})->with(Dataset::forMultiplePointers());

View File

@ -0,0 +1,106 @@
<?php
return [
'complex_array' => [
'/-1,/-2' => [],
'/-/id,/-/batters/batter/-/type' => [
'id' => ['0001', '0002', '0003'],
'type' => ['Regular', 'Chocolate', 'Blueberry', "Devil's Food", 'Regular', 'Regular', 'Chocolate'],
],
'/-/name,/-/topping/-/type,/-/id' => [
'id' => ['0001', '0002', '0003'],
'name' => ['Cake', 'Raised', 'Old Fashioned'],
'type' => ['None', 'Glazed', 'Sugar', 'Powdered Sugar', 'Chocolate with Sprinkles', 'Chocolate', 'Maple', 'None', 'Glazed', 'Sugar', 'Chocolate', 'Maple', 'None', 'Glazed', 'Chocolate', 'Maple'],
],
'/-/batters/batter/-,/-/name' => [
'name' => ['Cake', 'Raised', 'Old Fashioned'],
[
"id" => "1001",
"type" => "Regular",
],
[
"id" => "1002",
"type" => "Chocolate",
],
[
"id" => "1003",
"type" => "Blueberry",
],
[
"id" => "1004",
"type" => "Devil's Food",
],
[
"id" => "1001",
"type" => "Regular",
],
[
"id" => "1001",
"type" => "Regular",
],
[
"id" => "1002",
"type" => "Chocolate",
],
],
],
'complex_object' => [
'/-1,/-2' => [],
'/id,/batters/batter/-/type' => [
'id' => '0001',
'type' => ['Regular', 'Chocolate', 'Blueberry', "Devil's Food"],
],
'/name,/topping/-/type,/id' => [
'id' => '0001',
'name' => 'Cake',
'type' => ['None', 'Glazed', 'Sugar', 'Powdered Sugar', 'Chocolate with Sprinkles', 'Chocolate', 'Maple'],
],
'/batters/batter/-,/type' => [
'type' => 'donut',
[
"id" => "1001",
"type" => "Regular",
],
[
"id" => "1002",
"type" => "Chocolate",
],
[
"id" => "1003",
"type" => "Blueberry",
],
[
"id" => "1004",
"type" => "Devil's Food",
],
],
],
'empty_array' => [
'/-1,/-2' => [],
'/foo,/bar' => [],
],
'empty_object' => [
'/-1,/-2' => [],
'/foo,/bar' => [],
],
'simple_array' => [
'/-1,/-2' => [],
'/0,/1' => [1, ''],
'/1,/0' => [1, ''],
'/0,/2' => [1, 'foo'],
'/2,/3' => ['foo', '"bar"'],
'/3,/4,/5' => ['"bar"', 'hej då', 3.14],
'/4,/5,/3' => ['"bar"', 'hej då', 3.14],
'/6,/7,/8,/9' => [false, null, [], []],
'/9,/8,/7,/6' => [false, null, [], []],
],
'simple_object' => [
'/-1,/-2' => [],
'/int,/empty_string' => ['int' => 1, 'empty_string' => ''],
'/empty_string,/int' => ['int' => 1, 'empty_string' => ''],
'/string,/escaped_string,/\"escaped_key\"' => ['string' => 'foo', 'escaped_string' => '"bar"', '"escaped_key"' => 'baz'],
'/unicode,/bool,/empty_array' => ['unicode' => "hej då", 'bool' => false, 'empty_array' => []],
'/,/a~1b,/c%d,/e^f,/g|h,/i\\\\j' => ['' => 0, 'a/b' => 1, 'c%d' => 2, 'e^f' => 3, 'g|h' => 4, 'i\\j' => 5],
'/k\"l,/ ,/m~0n' => ['k"l' => 6, ' ' => 7, 'm~n' => 8],
],
];