-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
_diff_text: use repr with escape characters #6702
_diff_text: use repr with escape characters #6702
Conversation
r"- 'spam\n'", | ||
r"+ 'eggs\n'", | ||
r" 'bar'", | ||
] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm a bit confused by this change (and others) - do you consider a newline a non-printable/zero-width character? Why? It seems quite confusing to me to see a multi-line output but also \n
, i.e. with newlines represented twice.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why?
Otherwise it will not be visible. Check https://github.com/pytest-dev/pytest/pull/6702/files/9318b236ff35fc6a0734ee91bff2011afb93971a#diff-14ad89cac128f1dd3aaa927c656035b3R1342.
Note: this is for a line (text/str) diff.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It could be made smarter though maybe to not show them when not at the end of a line?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For illustration, on master
:
def test_eq_similar_text():
x ="foo\n1 bar"
> assert x == "foo\n2 bar"
E AssertionError: assert 'foo\n1 bar' == 'foo\n2 bar'
E foo
E - 1 bar
E ? ^
E + 2 bar
E ? ^
On this branch:
def test_eq_similar_text():
x ="foo\n1 bar"
> assert x == "foo\n2 bar"
E AssertionError: assert 'foo\n1 bar' == 'foo\n2 bar'
E NOTE: Strings contain non-printable/zero-width characters. Escaping them using repr().
E 'foo\n'
E - '1 bar'
E ? ^
E + '2 bar'
E ? ^
Is this what you would prefer @The-Compiler ?
def test_eq_similar_text():
x ="foo\n1 bar"
> assert x == "foo\n2 bar"
E AssertionError: assert 'foo\n1 bar' == 'foo\n2 bar'
E NOTE: Strings contain non-printable/zero-width characters. Escaping them using repr().
E - 'foo\n1 bar'
E ? ^
E + 'foo\n2 bar'
E ? ^
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For illustration, on
master
:def test_eq_similar_text(): x ="foo\n1 bar" > assert x == "foo\n2 bar" E AssertionError: assert 'foo\n1 bar' == 'foo\n2 bar' E foo E - 1 bar E ? ^ E + 2 bar E ? ^
On master it has a different order (which I find still wrong - it should be left-to-right, top-to-bottom, with only +/- swapped, but off topic here - maybe therefore edited manually?):
def test_eq_similar_text(self):
x = "foo\n1 bar"
> assert x == "foo\n2 bar"
E AssertionError: assert 'foo\n1 bar' == 'foo\n2 bar'
E foo
E - 2 bar
E ? ^
E + 1 bar
E ? ^
How about?
def test_eq_similar_text(self):
x = "foo\n1 bar\n"
> assert x == "foo\n2 bar"
E AssertionError: assert 'foo\n1 bar\n' == 'foo\n2 bar'
E NOTE: Strings contain different line-endings. Escaping them using repr().
E - 'foo\n1 bar\n'
E ? ^ --
E + 'foo\n2 bar'
E ? ^
However, with longer strings it is useful to split them on newlines, of course.
> assert x == "foo\n1 bar"
E AssertionError: assert 'foo\n1 bar\n' == 'foo\n1 bar'
E NOTE: Strings contain different line-endings. Escaping them using repr().
E foo
E - 1 bar\n
E ? --
E + 1 bar
E -
FWIW it always looked a bit strange to me seeing:
- foo
+ foo
^
(it could also be a space etc)
E - 'foo\n1 bar' E ? ^ E + 'foo\n2 bar' E ? ^
This could also be triggered via some minimal length (related: blueyed#218, where I split it onto separate lines with a certain length).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, I guess explicit is indeed better than implicit in this case. I agree having the ^
marker pointing to "nothing" is odd, and I remember people being confused about that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
WIP to handle different line endings only
43b11af
to
2ba59a1
Compare
This helps especially with comparing strings containing terminal escape
sequences, but also newlines already.
Ref: #3443
TODO: