Skip to content

Commit

Permalink
Output actual character differences when tests fail
Browse files Browse the repository at this point in the history
  • Loading branch information
dan-blanchard committed Sep 19, 2015
1 parent 02edb35 commit 11bef25
Showing 1 changed file with 26 additions and 7 deletions.
33 changes: 26 additions & 7 deletions test.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@

from __future__ import with_statement

from hypothesis import given, assume, Settings, Verbosity
import hypothesis.strategies as st
import textwrap
from difflib import ndiff
from io import open
from os import listdir
from os.path import dirname, isdir, join, realpath, relpath, splitext

import hypothesis.strategies as st
from hypothesis import given, assume, Settings, Verbosity
from nose.tools import eq_, assert_raises

import chardet
Expand All @@ -27,12 +30,28 @@
def check_file_encoding(file_name, encoding):
""" Ensure that we detect the encoding for file_name correctly. """
with open(file_name, 'rb') as f:
result = chardet.detect(f.read())
input_bytes = f.read()
result = chardet.detect(input_bytes)
try:
expected_unicode = input_bytes.decode(encoding)
except LookupError:
expected_unicode = ''
try:
detected_unicode = input_bytes.decode(result['encoding'])
except (LookupError, UnicodeDecodeError):
detected_unicode = ''
encoding = EQUIVALENT_ENCODINGS.get(encoding, encoding)
eq_(result['encoding'].lower(), encoding, ("Expected %s, but got %s for "
"%s" % (encoding,
result['encoding'],
file_name)))
if result['encoding'].lower() != encoding:
wrapped_expected = '\n'.join(textwrap.wrap(expected_unicode, 100)) + '\n'
wrapped_detected = '\n'.join(textwrap.wrap(detected_unicode, 100)) + '\n'
diff = ''.join(ndiff(wrapped_expected.splitlines(keepends=True),
wrapped_detected.splitlines(keepends=True)))
else:
diff = ''
eq_(result['encoding'].lower(), encoding,
("Expected %s, but got %s for %s. Character differences: \n%s" %
(encoding, result['encoding'], file_name, diff)))



def test_encoding_detection():
Expand Down

0 comments on commit 11bef25

Please sign in to comment.