__init__.py 3.35 KB
Newer Older
1
# -*- coding: utf-8 -*-  IGNORE:C0111
Matěj Cepl's avatar
Matěj Cepl committed
2 3 4 5
from __future__ import absolute_import, print_function, unicode_literals
import logging
import yamlish
import yaml
Matěj Cepl's avatar
Matěj Cepl committed
6 7
import tempfile
import textwrap
Matěj Cepl's avatar
Matěj Cepl committed
8

Matěj Cepl's avatar
Matěj Cepl committed
9 10 11
INPUT = 1
OUTPUT = 2

Matěj Cepl's avatar
Matěj Cepl committed
12 13 14
if yamlish.py3k:
    unicode = str

15
#logging.basicConfig(level=logging.DEBUG)
16

Matěj Cepl's avatar
Matěj Cepl committed
17

18 19 20 21
def _generate_test_name(source):
    """
    Clean up human-friendly test name into a method name.
    """
Matěj Cepl's avatar
Matěj Cepl committed
22
    out = source.replace(' ', '_').replace(':', '').replace(',', '').lower()
Matěj Cepl's avatar
Matěj Cepl committed
23 24
    return "test_%s" % out

25

26
def _create_input_test(test_src, tested_function, options=None):
27 28 29
    """
    Decorate tested function to be used as a method for TestCase.
    """
Matěj Cepl's avatar
Matěj Cepl committed
30
    def do_test_expected(self):
31 32 33
        """
        Execute a test by calling a tested_function on test_src data.
        """
Matěj Cepl's avatar
Matěj Cepl committed
34
        self.maxDiff = None
Matěj Cepl's avatar
Matěj Cepl committed
35 36 37

        got = ""
        if 'error' in test_src:
38
            self.assertRaises(test_src['error'], tested_function,
39
                              test_src['in'], options)
Matěj Cepl's avatar
Matěj Cepl committed
40 41
        else:
            want = test_src['out']
42 43
            got = tested_function(test_src['in'], options)
            logging.debug('got = type %s', type(got))
Matěj Cepl's avatar
Matěj Cepl committed
44 45
            logging.debug("test_src['out'] = %s",
                          unicode(test_src['out']))
Matěj Cepl's avatar
Matěj Cepl committed
46 47
            self.assertEqual(got, want, """Result matches
            expected = %s
48

Matěj Cepl's avatar
Matěj Cepl committed
49 50 51 52 53 54
            observed = %s
            """ % (want, got))

    return do_test_expected


55
def _create_output_test(test_src, tested_function, options=None):
Matěj Cepl's avatar
Matěj Cepl committed
56 57 58 59 60 61 62 63 64 65 66 67
    """
    Decorate tested function to be used as a method for TestCase.
    """
    def do_test_expected(self):
        """
        Execute a test by calling a tested_function on test_src data.
        """
        self.maxDiff = None

        # We currently don't throw any exceptions in Writer, so this
        # this is always false
        if 'error' in test_src:
Matěj Cepl's avatar
Matěj Cepl committed
68 69
            self.assertRaises(test_src['error'], yamlish.dumps,
                              test_src['in'], options)
Matěj Cepl's avatar
Matěj Cepl committed
70 71 72 73
        else:
            logging.debug("out:\n%s", textwrap.dedent(test_src['out']))
            want = yaml.load(textwrap.dedent(test_src['out']))
            logging.debug("want:\n%s", want)
74
            with tempfile.NamedTemporaryFile() as test_file:
Matěj Cepl's avatar
Matěj Cepl committed
75 76 77 78 79 80 81 82 83 84
                tested_function(test_src['in'], test_file)
                test_file.seek(0)
                got_str = test_file.read()
                logging.debug("got_str = %s", got_str)
                got = yaml.load(got_str)
                self.assertEqual(got, want, "Result matches")

    return do_test_expected


85
def generate_testsuite(test_data, test_case_shell, test_fce, direction=INPUT,
Matěj Cepl's avatar
Matěj Cepl committed
86
                       options=None):
87
    """
88 89
    Generate tests from the test data, class to build upon and function
    to use for testing.
90
    """
Matěj Cepl's avatar
Matěj Cepl committed
91 92
    for in_test in test_data:
        if ('skip' in in_test) and in_test['skip']:
93
            logging.debug("test %s skipped!", in_test['name'])
Matěj Cepl's avatar
Matěj Cepl committed
94
            continue
95
        name = _generate_test_name(in_test['name'])
Matěj Cepl's avatar
Matěj Cepl committed
96
        if direction == INPUT:
Matěj Cepl's avatar
Matěj Cepl committed
97 98
            test_method = _create_input_test(in_test, test_fce,
                                             options=options)
Matěj Cepl's avatar
Matěj Cepl committed
99
        elif direction == OUTPUT:
Matěj Cepl's avatar
Matěj Cepl committed
100 101
            test_method = _create_output_test(in_test, test_fce,
                                              options=options)
102 103
        test_method.__name__ = str('test_%s' % name)
        setattr(test_case_shell, test_method.__name__, test_method)