From e5846a273662297a8ea05d75e4cf22254b4835b6 Mon Sep 17 00:00:00 2001 From: Jacek Kobus Date: Tue, 23 Jan 2018 15:33:17 +0100 Subject: [PATCH] matcher fails when trying to use empty string as a pattern --- src/Parser.php | 4 ++++ tests/MatcherTest.php | 28 ++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/src/Parser.php b/src/Parser.php index c22c6be7..bab3a1aa 100644 --- a/src/Parser.php +++ b/src/Parser.php @@ -46,6 +46,10 @@ public function parse(string $pattern) : Pattern\TypePattern public function getAST(string $pattern) : AST\Pattern { + if($pattern === '') { + return new AST\Pattern(new AST\Type('')); + } + $this->lexer->setInput($pattern); return $this->getPattern(); } diff --git a/tests/MatcherTest.php b/tests/MatcherTest.php index 5c3cf669..e4ee6399 100644 --- a/tests/MatcherTest.php +++ b/tests/MatcherTest.php @@ -421,4 +421,32 @@ public static function nullExamples() ], ]; } + + public static function emptyPatternString() + { + return [ + [ + '', '', true, + '123', '', false, + ' ', '', false, + null, '', false, + 1, '', false, + 0, '', false, + '{"name": "123"}', '{"name": ""}', false, + '{"name": ""}', '{"name": ""}', true, + ], + ]; + } + + /** + * @dataProvider emptyPatternString + */ + public function test_empty_pattern_in_the_json($value, $pattern, $expectedResult) + { + $factory = new SimpleFactory(); + $matcher = $factory->createMatcher(); + + $match = $matcher->match($value, $pattern); + $this->assertSame($expectedResult, $match); + } }