Skip to content
This repository was archived by the owner on Nov 30, 2024. It is now read-only.

Check for nil instead of truthiness when making a diff #377

Merged
merged 1 commit into from
Jun 8, 2019

Conversation

lnestor
Copy link

@lnestor lnestor commented May 31, 2019

Previous Behavior

The following lines create different output:

expect(true).to match a_falsy_value

# =>
       expected true to match (a falsey value)
       Diff:
       @@ -1,2 +1,2 @@
       -(a falsey value)
       +true
expect(false).to match a_truthy_value

# =>
       expected false to match (a truthy value)

The first generates a diff, whereas the second does not. This is because the actual and expected values are checked for presence with if actual && expected when creating the diff. In the first example, the actual is true, so it passes the check. In the second, the actual is false and the diff returned is an empty string.

New Behavior

These should be consistent with each other. The two options would be to make them both create a diff or modify the matcher so neither do. However, since all other match(something) matchers produce a diff output, it makes sense to do the same here.

If it is necessary for that check to also check for truthiness, then another solution could be found. But, I cannot find a situation where false would be passed in where we wouldn't want to create a diff.

expected_diff = "\n@@ -1,2 +1,2 @@\n-false\n+true\n"

diff = differ.diff(expected,actual)
expect(diff).to be_diffed_as(expected_diff)
Copy link
Member

Choose a reason for hiding this comment

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

This doesn't actually cover both scenarios, rather than setting actual / expected false, do you just want to cover that diffing true, false and false, true both produce diffs?

@lnestor lnestor force-pushed the diff-generation-nil-check branch from 79e7470 to 4f01c77 Compare June 5, 2019 18:29
@lnestor
Copy link
Author

lnestor commented Jun 5, 2019

I went ahead and checked that a diff is produced for both. I can separate them into their own it instances if you want. Also, CI failed because it timed out, is there a way to retry the job?

@pirj
Copy link
Member

pirj commented Jun 5, 2019

@lnestor I suggest you to:

git commit --amend
git push --force-with-lease lnestor diff-generation-nil-check

to re-run the build.

When trying to make a diff where either the expected
or actual value was false, the diff would be an empty
string. This change makes it to diff against the false
value.
@lnestor lnestor force-pushed the diff-generation-nil-check branch from 4f01c77 to 2756621 Compare June 6, 2019 13:32
@JonRowe JonRowe merged commit 2c80604 into rspec:master Jun 8, 2019
JonRowe added a commit that referenced this pull request Jun 8, 2019
JonRowe added a commit that referenced this pull request Jun 10, 2019
JonRowe added a commit that referenced this pull request Jun 10, 2019
Check for nil instead of truthiness when making a diff
yujinakayama pushed a commit to yujinakayama/rspec-monorepo that referenced this pull request Oct 6, 2021
…f-generation-nil-check

Check for nil instead of truthiness when making a diff

---
This commit was imported from rspec/rspec-support@a2642ed.
yujinakayama pushed a commit to yujinakayama/rspec-monorepo that referenced this pull request Oct 6, 2021
…f-generation-nil-check

Check for nil instead of truthiness when making a diff

---
This commit was imported from rspec/rspec-support@a2642ed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants