summaryrefslogtreecommitdiff
path: root/tests/common.py
blob: adc3ec2366561336a751e2b2a4ff6bbaab8875fc (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
42
43
44
45
46
47
48
49
50
51
52
53
# coding: utf-8

"""
Provides test-related code that can be used by all tests.

"""

import os

import examples


DATA_DIR = 'tests/data'
EXAMPLES_DIR = os.path.dirname(examples.__file__)


def get_data_path(file_name):
    return os.path.join(DATA_DIR, file_name)


class AssertStringMixin:

    """A unittest.TestCase mixin to check string equality."""

    def assertString(self, actual, expected):
        """
        Assert that the given strings are equal and have the same type.

        """
        # Show both friendly and literal versions.
        message = """String mismatch: %%s\


        Expected: \"""%s\"""
        Actual:   \"""%s\"""

        Expected: %s
        Actual:   %s""" % (expected, actual, repr(expected), repr(actual))

        self.assertEquals(actual, expected, message % "different characters")

        details = "types different: %s != %s" % (repr(type(expected)), repr(type(actual)))
        self.assertEquals(type(expected), type(actual), message % details)


class AssertIsMixin:

    """A unittest.TestCase mixin adding assertIs()."""

    # unittest.assertIs() is not available until Python 2.7:
    #   http://docs.python.org/library/unittest.html#unittest.TestCase.assertIsNone
    def assertIs(self, first, second):
        self.assertTrue(first is second, msg="%s is not %s" % (repr(first), repr(second)))