From e93aacac7837509c5675be27887ac2275fa3c21b Mon Sep 17 00:00:00 2001 From: Andrea Marco Sartori Date: Wed, 22 Mar 2023 23:36:00 +1000 Subject: [PATCH] Test eager loading --- tests/Dataset.php | 46 ++++- tests/Feature/ParsingTest.php | 4 + tests/Feature/PointersTest.php | 16 ++ .../pointers/multiple_pointers_to_array.php | 94 +++++++++ .../pointers/single_pointer_to_array.php | 178 ++++++++++++++++++ 5 files changed, 336 insertions(+), 2 deletions(-) create mode 100644 tests/fixtures/pointers/multiple_pointers_to_array.php create mode 100644 tests/fixtures/pointers/single_pointer_to_array.php diff --git a/tests/Dataset.php b/tests/Dataset.php index 511abee..351c89a 100644 --- a/tests/Dataset.php +++ b/tests/Dataset.php @@ -64,7 +64,18 @@ final class Dataset */ public static function forSinglePointers(): Generator { - $singlePointers = require fixture('pointers/single_pointer.php'); + yield from self::forSinglePointersWithFixture('pointers/single_pointer.php'); + } + + /** + * Retrieve the dataset to test single pointers with the given fixture + * + * @param string $path + * @return Generator + */ + private static function forSinglePointersWithFixture(string $path): Generator + { + $singlePointers = require fixture($path); foreach ($singlePointers as $fixture => $pointers) { $json = file_get_contents(fixture("json/{$fixture}.json")); @@ -75,6 +86,16 @@ final class Dataset } } + /** + * Retrieve the dataset to test single pointers eager loading + * + * @return Generator + */ + public static function forSinglePointersToArray(): Generator + { + yield from self::forSinglePointersWithFixture('pointers/single_pointer_to_array.php'); + } + /** * Retrieve the dataset to test multiple pointers * @@ -82,7 +103,18 @@ final class Dataset */ public static function forMultiplePointers(): Generator { - $multiplePointers = require fixture('pointers/multiple_pointers.php'); + yield from self::forMultiplePointersWithFixture('pointers/multiple_pointers.php'); + } + + /** + * Retrieve the dataset to test multiple pointers with the given fixture + * + * @param string $path + * @return Generator + */ + private static function forMultiplePointersWithFixture(string $path): Generator + { + $multiplePointers = require fixture($path); foreach ($multiplePointers as $fixture => $valueByPointers) { $json = file_get_contents(fixture("json/{$fixture}.json")); @@ -93,6 +125,16 @@ final class Dataset } } + /** + * Retrieve the dataset to test multiple pointers eager loading + * + * @return Generator + */ + public static function forMultiplePointersToArray(): Generator + { + yield from self::forMultiplePointersWithFixture('pointers/multiple_pointers_to_array.php'); + } + /** * Retrieve the dataset to test intersecting pointers with wildcards * diff --git a/tests/Feature/ParsingTest.php b/tests/Feature/ParsingTest.php index 7e2e07f..b93b1a7 100644 --- a/tests/Feature/ParsingTest.php +++ b/tests/Feature/ParsingTest.php @@ -17,3 +17,7 @@ it('parses JSON when instantiated statically', function (string $json, array $pa it('parses JSON when calling the helper', function (string $json, array $parsed) { expect(parseJson($json))->toParseTo($parsed); })->with(Dataset::forParsing()); + +it('eager loads JSON into an array', function (string $json, array $parsed) { + expect(JsonParser::parse($json)->toArray())->toBe($parsed); +})->with(Dataset::forParsing()); diff --git a/tests/Feature/PointersTest.php b/tests/Feature/PointersTest.php index 2a44cd3..d7d7298 100644 --- a/tests/Feature/PointersTest.php +++ b/tests/Feature/PointersTest.php @@ -15,10 +15,26 @@ it('loads JSON from a single JSON pointer', function (string $json, string $poin expect(JsonParser::parse($json)->pointer($pointer))->toPointTo($parsed); })->with(Dataset::forSinglePointers()); +it('eager loads pointers into an array', function (string $json, string $pointer, array $expected) { + expect(JsonParser::parse($json)->pointer($pointer)->toArray())->toBe($expected); +})->with(Dataset::forSinglePointersToArray()); + +it('eager loads lazy pointers into an array', function (string $json, string $pointer, array $expected) { + expect(JsonParser::parse($json)->lazyPointer($pointer)->toArray())->toBe($expected); +})->with(Dataset::forSinglePointersToArray()); + it('loads JSON from multiple JSON pointers', function (string $json, array $pointers, array $parsed) { expect(JsonParser::parse($json)->pointers($pointers))->toPointTo($parsed); })->with(Dataset::forMultiplePointers()); +it('eager loads multiple pointers into an array', function (string $json, array $pointers, array $expected) { + expect(JsonParser::parse($json)->pointers($pointers)->toArray())->toBe($expected); +})->with(Dataset::forMultiplePointersToArray()); + +it('eager loads multiple lazy pointers into an array', function (string $json, array $pointers, array $expected) { + expect(JsonParser::parse($json)->lazyPointers($pointers)->toArray())->toBe($expected); +})->with(Dataset::forMultiplePointersToArray()); + it('can intersect pointers with wildcards', function (string $json, array $pointers, array $parsed) { expect(JsonParser::parse($json)->pointers($pointers))->toPointTo($parsed); })->with(Dataset::forIntersectingPointersWithWildcards()); diff --git a/tests/fixtures/pointers/multiple_pointers_to_array.php b/tests/fixtures/pointers/multiple_pointers_to_array.php new file mode 100644 index 0000000..a57adf3 --- /dev/null +++ b/tests/fixtures/pointers/multiple_pointers_to_array.php @@ -0,0 +1,94 @@ + [ + '/-1,/-2' => [], + '/-/id,/-/batters/batter/-/type' => [ + 'id' => '0003', + 'type' => 'Chocolate', + ], + '/-/name,/-/topping/-/type,/-/id' => [ + 'id' => '0003', + 'name' => 'Old Fashioned', + 'type' => 'Maple', + ], + '/-/batters/batter/-,/-/name' => [ + 'name' => 'Old Fashioned', + [ + "id" => "1001", + "type" => "Regular", + ], + [ + "id" => "1002", + "type" => "Chocolate", + ], + [ + "id" => "1003", + "type" => "Blueberry", + ], + [ + "id" => "1004", + "type" => "Devil's Food", + ], + ], + ], + 'complex_object' => [ + '/-1,/-2' => [], + '/id,/batters/batter/-/type' => [ + 'id' => '0001', + 'type' => "Devil's Food", + ], + '/name,/topping/-/type,/id' => [ + 'id' => '0001', + 'name' => 'Cake', + 'type' => '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' => [0 => 1, 1 => ''], + '/1,/0' => [0 => 1, 1 => ''], + '/0,/2' => [0 => 1, 2 => 'foo'], + '/2,/3' => [2 => 'foo', 3 => '"bar"'], + '/3,/4,/5' => [3 => '"bar"', 4 => 'hej då', 5 => 3.14], + '/4,/5,/3' => [3 => '"bar"', 4 => 'hej då', 5 => 3.14], + '/6,/7,/8,/9' => [6 => false, 7 => null, 8 => [], 9 => []], + '/9,/8,/7,/6' => [6 => false, 7 => null, 8 => [], 9 => []], + ], + '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], + ], +]; diff --git a/tests/fixtures/pointers/single_pointer_to_array.php b/tests/fixtures/pointers/single_pointer_to_array.php new file mode 100644 index 0000000..fd116f3 --- /dev/null +++ b/tests/fixtures/pointers/single_pointer_to_array.php @@ -0,0 +1,178 @@ + [ + '' => $complexArray = require __DIR__ . '/../parsing/complex_array.php', + '/-' => $complexArray, + '/-/id' => ['id' => '0003'], + '/-/batters' => [ + 'batters' => [ + 'batter' => [ + [ + "id" => "1001", + "type" => "Regular", + ], + [ + "id" => "1002", + "type" => "Chocolate", + ], + ], + ], + ], + '/-/batters/batter' => [ + 'batter' => [ + [ + "id" => "1001", + "type" => "Regular", + ], + [ + "id" => "1002", + "type" => "Chocolate", + ], + ], + ], + '/-/batters/batter/-' => [ + [ + "id" => "1001", + "type" => "Regular", + ], + [ + "id" => "1002", + "type" => "Chocolate", + ], + [ + "id" => "1003", + "type" => "Blueberry", + ], + [ + "id" => "1004", + "type" => "Devil's Food", + ], + ], + '/-/batters/batter/-/id' => ['id' => "1002"], + ], + 'complex_object' => [ + '' => require __DIR__ . '/../parsing/complex_object.php', + '/-' => [], + '/id' => ['id' => '0001'], + '/batters' => [ + 'batters' => [ + 'batter' => [ + [ + "id" => "1001", + "type" => "Regular", + ], + [ + "id" => "1002", + "type" => "Chocolate", + ], + [ + "id" => "1003", + "type" => "Blueberry", + ], + [ + "id" => "1004", + "type" => "Devil's Food", + ], + ], + ], + ], + '/batters/batter' => [ + 'batter' => [ + [ + "id" => "1001", + "type" => "Regular", + ], + [ + "id" => "1002", + "type" => "Chocolate", + ], + [ + "id" => "1003", + "type" => "Blueberry", + ], + [ + "id" => "1004", + "type" => "Devil's Food", + ], + ], + ], + '/batters/batter/-' => [ + [ + "id" => "1001", + "type" => "Regular", + ], + [ + "id" => "1002", + "type" => "Chocolate", + ], + [ + "id" => "1003", + "type" => "Blueberry", + ], + [ + "id" => "1004", + "type" => "Devil's Food", + ], + ], + '/batters/batter/-/id' => ['id' => "1004"], + ], + 'empty_array' => [ + '' => [], + '/-' => [], + '/-1' => [], + '/0' => [], + '/foo' => [], + ], + 'empty_object' => [ + '' => [], + '/-' => [], + '/-1' => [], + '/0' => [], + '/foo' => [], + ], + 'simple_array' => [ + '' => $simpleArray = require __DIR__ . '/../parsing/simple_array.php', + '/-' => $simpleArray, + '/-1' => [], + '/0' => [0 => 1], + '/1' => [1 => ''], + '/2' => [2 => 'foo'], + '/3' => [3 => '"bar"'], + '/4' => [4 => 'hej då'], + '/5' => [5 => 3.14], + '/6' => [6 => false], + '/7' => [7 => null], + '/8' => [8 => []], + '/9' => [9 => []], + '/10' => [], + '/foo' => [], + ], + 'simple_object' => [ + '' => require __DIR__ . '/../parsing/simple_object.php', + '/-' => [], + '/-1' => [], + '/int' => ['int' => 1], + '/empty_string' => ['empty_string' => ''], + '/string' => ['string' => 'foo'], + '/escaped_string' => ['escaped_string' => '"bar"'], + '/\"escaped_key\"' => ['"escaped_key"' => 'baz'], + '/unicode' => ['unicode' => "hej då"], + '/float' => ['float' => 3.14], + '/bool' => ['bool' => false], + '/null' => ['null' => null], + '/empty_array' => ['empty_array' => []], + '/empty_object' => ['empty_object' => []], + '/10' => [], + '/foo' => [], + '/' => ['' => 0], + '/a~1b' => ['a/b' => 1], + '/c%d' => ['c%d' => 2], + '/e^f' => ['e^f' => 3], + '/g|h' => ['g|h' => 4], + '/i\\\\j' => ['i\\j' => 5], + '/k\"l' => ['k"l' => 6], + '/ ' => [' ' => 7], + '/m~0n' => ['m~n' => 8], + ], +];