summaryrefslogtreecommitdiff
path: root/tests/utils.py
blob: b25503587a99fdf7dbcbb249f508a1c625daadbf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# -*- coding: utf-8 -*-

"""Helpers for testing."""

import difflib
import io
import os
import unittest

from sqlparse.utils import split_unquoted_newlines
from sqlparse.compat import u, StringIO

DIR_PATH = os.path.dirname(__file__)
FILES_DIR = os.path.join(DIR_PATH, 'files')


def load_file(filename, encoding='utf-8'):
    """Opens filename with encoding and return its contents."""
    with io.open(os.path.join(FILES_DIR, filename), encoding=encoding) as f:
        return f.read()


class TestCaseBase(unittest.TestCase):
    """Base class for test cases with some additional checks."""

    # Adopted from Python's tests.
    def ndiffAssertEqual(self, first, second):
        """Like failUnlessEqual except use ndiff for readable output."""
        if first != second:
            # Using the built-in .splitlines() method here will cause incorrect
            # results when splitting statements that have quoted CR/CR+LF
            # characters.
            sfirst = split_unquoted_newlines(u(first))
            ssecond = split_unquoted_newlines(u(second))
            diff = difflib.ndiff(sfirst, ssecond)

            fp = StringIO()
            fp.write('\n')
            fp.write('\n'.join(diff))

            raise self.failureException(fp.getvalue())