diff --git a/i18n/en.xliff.dist b/i18n/en.xliff.dist index ad092dfe..efc49c32 100644 --- a/i18n/en.xliff.dist +++ b/i18n/en.xliff.dist @@ -199,6 +199,10 @@ the JSON node :node should be equal to :text + + the JSON node :node should match :pattern + + the JSON nodes should be equal to: diff --git a/src/Context/JsonContext.php b/src/Context/JsonContext.php index a8dc8dc1..bdb7185b 100644 --- a/src/Context/JsonContext.php +++ b/src/Context/JsonContext.php @@ -76,6 +76,24 @@ public function theJsonNodesShouldBeEqualTo(TableNode $nodes) } } + /** + * Checks, that given JSON node matches given pattern + * + * @Then the JSON node :node should match :pattern + */ + public function theJsonNodeShouldMatch($node, $pattern) + { + $json = $this->getJson(); + + $actual = $this->inspector->evaluate($json, $node); + + if (preg_match($pattern, $actual) === 0) { + throw new \Exception( + sprintf("The node value is '%s'", json_encode($actual)) + ); + } + } + /** * Checks, that given JSON node is null * diff --git a/tests/features/json.feature b/tests/features/json.feature index 824dbaf1..cbc980a1 100644 --- a/tests/features/json.feature +++ b/tests/features/json.feature @@ -28,6 +28,9 @@ Feature: Testing JSONContext And the JSON node "numbers[3].complexeshizzle" should be equal to "true" And the JSON node "numbers[3].so[0]" should be equal to "very" And the JSON node "numbers[3].so[1].complicated" should be equal to "indeed" + And the JSON node "numbers[0]" should match "/o.{1}e/" + And the JSON node "numbers[1]" should match "/.{2}o/" + And the JSON node "numbers[2]" should match "/[a-z]{3}e.+/" And the JSON nodes should be equal to: | foo | bar |