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

assertEqual doesn't display newline differences quite well #56247

Open
pitrou opened this issue May 9, 2011 · 7 comments
Open

assertEqual doesn't display newline differences quite well #56247

pitrou opened this issue May 9, 2011 · 7 comments
Assignees
Labels
3.11 only security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@pitrou
Copy link
Member

pitrou commented May 9, 2011

BPO 12038
Nosy @terryjreedy, @gpshead, @pitrou, @ezio-melotti, @voidspace, @iritkatriel
Files
  • issue-12038.patch: Print a more human readable assert message when two strings don't match.
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = 'https://github.com/voidspace'
    closed_at = None
    created_at = <Date 2011-05-09.16:36:50.398>
    labels = ['type-bug', 'library', '3.11']
    title = "assertEqual doesn't display newline differences quite well"
    updated_at = <Date 2021-12-09.21:45:16.085>
    user = 'https://github.com/pitrou'

    bugs.python.org fields:

    activity = <Date 2021-12-09.21:45:16.085>
    actor = 'iritkatriel'
    assignee = 'michael.foord'
    closed = False
    closed_date = None
    closer = None
    components = ['Library (Lib)']
    creation = <Date 2011-05-09.16:36:50.398>
    creator = 'pitrou'
    dependencies = []
    files = ['22486']
    hgrepos = []
    issue_num = 12038
    keywords = ['patch']
    message_count = 7.0
    messages = ['135608', '135657', '135953', '136007', '136079', '139190', '408160']
    nosy_count = 7.0
    nosy_names = ['terry.reedy', 'gregory.p.smith', 'pitrou', 'ezio.melotti', 'michael.foord', 'mouad', 'iritkatriel']
    pr_nums = []
    priority = 'low'
    resolution = None
    stage = None
    status = 'open'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue12038'
    versions = ['Python 3.11']

    @pitrou
    Copy link
    Member Author

    pitrou commented May 9, 2011

    >>> tc.assertEqual("x\n" * 40 + "\n", "x\n" * 40 + "\r\n")
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "/home/antoine/cpython/default/Lib/unittest/case.py", line 662, in assertEqual
        assertion_func(first, second, msg=msg)
      File "/home/antoine/cpython/default/Lib/unittest/case.py", line 1041, in assertMultiLineEqual
        self.fail(self._formatMessage(msg, standardMsg))
      File "/home/antoine/cpython/default/Lib/unittest/case.py", line 531, in fail
        raise self.failureException(msg)
    AssertionError: 'x\nx\nx\nx\nx\nx\nx\nx\nx\nx\nx\nx\nx\nx\nx\nx\nx\nx\nx\nx\nx\nx\nx\nx\nx\nx\nx [truncated]... != 'x\nx\nx\nx\nx\nx\nx\nx\nx\nx\nx\nx\nx\nx\nx\nx\nx\nx\nx\nx\nx\nx\nx\nx\nx\nx\nx [truncated]...
      x
      x
      x
      x
      x
      x
      x
      x
      x
      x
      x
      x
      x
      x
      x
      x
      x
      x
      x
      x
      x
      x
      x
      x
      x
      x
      x
      x
      x
      x
      x
      x
      x
      x
      x
      x
      x
      x
      x
      x
    - 
    + 

    It isn't obvious when looking at this printout that the last newline differed.

    @pitrou pitrou added stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error labels May 9, 2011
    @voidspace
    Copy link
    Contributor

    Yup, I agree that's pretty sucky. Any suggestions for a fix?

    @terryjreedy
    Copy link
    Member

    A possible fix is to condense the output by omitting stuff in the center rather than as the end:
    "x\nx\nx\nx\nx\nx\nx\nx\nx\nx\nx\nx...x\nx\nx\nx\nx\nx\nx\nx\nx\nx\n"
    "x\nx\nx\nx\nx\nx\nx\nx\nx\nx\nx\nx...x\nx\nx\nx\nx\nx\nx\nx\nx\nx\r\n"
    would be clear.

    @gpshead
    Copy link
    Member

    gpshead commented May 15, 2011

    We should ensure that the first differing character in the string is always included in what is displayed as a diff. if we're going to shorten a string we should elide something that matches.

    @voidspace
    Copy link
    Contributor

    Yep, ensuring the truncated repr includes at least the *first* difference sounds like the right approach.

    @voidspace voidspace self-assigned this May 16, 2011
    @mouad
    Copy link
    Mannequin

    mouad mannequin commented Jun 26, 2011

    I took on consideration the two suggestion that was in the issue and create this patch that basically show only until the last character that differ and truncate the rest.

    I just included one test case, other test case if they should be included (and if my solution got accepted :)), should be more *complex* and check using Regex the correctness of the assertion message, but here is the output that we can see when applying the patch:

    >> tc.assertEqual("x\n" * 40 + "\n", "x\n" * 40 + "\r\n")

    'x\nx\nx\nx\nx\nx\nx\nx\nx\nx\nx\nx\nx\n...nx\nx\nx\nx\nx\nx\nx\nx\nx\nx\nx\nx\n\n [truncated]... != 'x\nx\nx\nx\nx\nx\nx\nx\nx\nx\nx\nx\nx\n...\nx\nx\nx\nx\nx\nx\nx\nx\nx\nx\nx\n\r [truncated]...
      x
      x
      x
      x
      x
      x
      x
      x
      x
      x
      x
      x
      x
      x
      x
      x
      x
      x
      x
      x
      x
      x
      x
      x
      x
      x
      x
      x
      x
      x
      x
      x
      x
      x
      x
      x
      x
      x
      x
      x
    - 
    + 

    >> tc.assertEqual('aaaa', 'abbb')

    AssertionError: 'aa [truncated]... != 'ab [truncated]...

    • aaaa
      + abbb

    >> tc.assertEqual('aaaa', 'bbbb')

    AssertionError: 'a [truncated]... != 'b [truncated]...

    • aaaa
      + bbbb

    >> tc.assertEqual("x\n" * 80 + "\n", "x\n" * 80 + "\r")

    AssertionError: 'x\nx\nx\nx\nx\nx\nx\nx\nx\nx\nx\nx\nx\n...nx\nx\nx\nx\nx\nx\nx\nx\nx\nx\nx\nx\n\n [truncated]... != 'x\nx\nx\nx\nx\nx\nx\nx\nx\nx\nx\nx\nx\n...nx\nx\nx\nx\nx\nx\nx\nx\nx\nx\nx\nx\n\r [truncated]...

    @iritkatriel
    Copy link
    Member

    Reproduced on 3.11.

    @iritkatriel iritkatriel added the 3.11 only security fixes label Dec 9, 2021
    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.11 only security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
    Projects
    Status: No status
    Development

    No branches or pull requests

    5 participants