util.py 2.67 KB
Newer Older
Jerome Mariette's avatar
Jerome Mariette committed
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# Copyright (c) 2010- The University of Notre Dame.
# This software is distributed under the GNU General Public License.
# See the file COPYING for details.

""" Weaver utility test units """

import os
import sys

sys.path.insert(0, os.path.abspath(os.path.dirname(__file__) + '/../..'))

from unittest import TestCase, TestLoader, TestSuite, TextTestRunner

from weaver.compat import map

from weaver.util import WeaverError
from weaver.util import find_executable
from weaver.util import normalize_path
from weaver.util import parse_string_list

# Find executable test case ----------------------------------------------------

class FindExecutableTestCase(TestCase):

    def test_00_cat(self):
        self.assertEqual(find_executable('cat'), '/bin/cat')

    def test_01_bin_cat(self):
        self.assertEqual(find_executable('/bin/cat'), '/bin/cat')

    def test_02_bin_cat(self):
        self.assertEqual(find_executable('weaver.py'),
                         os.path.abspath('weaver.py'))

    def test_03_asdffdsa(self):
        with self.assertRaises(WeaverError):
            find_executable('asdffdsa')

# Normalize path test case -----------------------------------------------------

class NormalizePathTestCase(TestCase):

    DIR_NAME = os.path.dirname(os.path.abspath(__file__))

    def test_00_readme(self):
        self.assertEqual(
            normalize_path('README.rst', self.DIR_NAME),
            '../../README.rst')

    def test_01_weaver_nest(self):
        self.assertEqual(
            normalize_path('weaver/nest.py', self.DIR_NAME),
            '../nest.py')

    def test_02_weaver_test_util(self):
        self.assertEqual(
            normalize_path('weaver/test/util.py', self.DIR_NAME),
            'util.py')

# Parse string list test case --------------------------------------------------

class ParseStringListTestCase(TestCase):

    def test_00_string(self):
        self.assertEqual(parse_string_list('a'), ['a'])

    def test_01_list(self):
        l = ['a']
        self.assertEqual(parse_string_list(l), l)

    def test_02_tuple(self):
        t = ('a',)
        self.assertEqual(parse_string_list(t), ['a'])

    def test_03_generator(self):
        g = range(10)
        self.assertEqual(parse_string_list(g), list(map(str, g)))

# Main execution ---------------------------------------------------------------

TestCases = [
    FindExecutableTestCase,
    NormalizePathTestCase,
    ParseStringListTestCase,
]

if __name__ == '__main__':
    test_runner = TextTestRunner(verbosity = 2)
    test_suite  = TestSuite(map(TestLoader().loadTestsFromTestCase, TestCases))
    test_runner.run(test_suite)

Jerome Mariette's avatar
Jerome Mariette committed
92
# vim: set sts=4 sw=4 ts=8 expandtab ft=python: --------------------------------