Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better messages for "JSON node should (not) contain" #33

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

jdeniau
Copy link

@jdeniau jdeniau commented Jun 4, 2024

Q A
Branch? main
Bug fix? yno
New feature? yno
Deprecations? yno
Tickets See #31
License MIT
Doc PR

See #31

Added better message for theJsonNodeShouldContain :

  And the JSON node "hydra:description" should contain "coucou"                                      # Behatch\Context\JsonContext::theJsonNodeShouldContain()
-       The string 'coucou' was not found. (Behat\Mink\Exception\ExpectationException)
+       The node "hydra:description" does not contain the expected text "coucou" (found "This value should be equal to 1.") (Behat\Mink\Exception\ExpectationException)
# if actual value is more that 50 char
+       The node "hydra:description" does not contain the expected text "coucou" (found "Lorem ipsum dolor sit amet, consectetur adipiscing[…]") (Behat\Mink\Exception\ExpectationException) 

and for theJsonNodeShouldNotContain

  And the JSON node "hydra:description" should not contain "ipsum"                                   # Behatch\Context\JsonContext::theJsonNodeShouldNotContain()
- The string 'ipsum' was found. (Behat\Mink\Exception\ExpectationException) 
+ The node "hydra:description" does contain "ipsum" while it should not (found "Lorem ipsum") (Behat\Mink\Exception\ExpectationException)   
# if actual value is more that 50 char                                         
+ The node "hydra:description" does contain "ipsum" while it should not (found "Lorem ipsum dolor sit amet, consectetur adipiscing[…]") (Behat\Mink\Exception\ExpectationException) 

@jdeniau jdeniau force-pushed the contain-better-message branch from c41fecc to 818c276 Compare June 4, 2024 10:07
$this->assertContains(
$text,
(string) $actual,
'The node "'.$node.'" does not contain the expected text "'.$text.'" (found "'.$printedActual.'")'
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The node '%s' should contain '%s', but it is '%s'. is more concise. WDYT ?

@@ -226,7 +226,13 @@ public function theJsonNodeShouldContain($node, $text): void

$actual = $this->inspector->evaluate($json, $node);

$this->assertContains($text, (string) $actual);
$printedActual = \strlen($actual) > 50 ? substr($actual, 0, 50).'[…]' : $actual;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Truncating the value might make debugging more difficult.

Comment on lines +261 to +267
$printedActual = \strlen($actual) > 50 ? substr($actual, 0, 50).'[…]' : $actual;

$this->assertNotContains(
$text,
(string) $actual,
'The node "'.$node.'" does contain "'.$text.'" while it should not (found "'.$printedActual.'")'
);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(same comments as previous)

$text,
(string) $actual,
'The node "'.$node.'" does contain "'.$text.'" while it should not (found "'.$printedActual.'")'
);
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a unit test too ?

@jdeniau
Copy link
Author

jdeniau commented Jun 4, 2024 via email

@Jean-Beru
Copy link
Collaborator

It does, but the value when you use "contain" might be a huge bloc of text. Maybe we can set the limit to 200 or 500 though. Julien Deniau

Sure ! We can keep this value uncut for now (like in other contexts) and create an other PR to truncate it. Maybe using the verbosity ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants