aboutsummaryrefslogtreecommitdiff
path: root/lib/oe/test_types.py
blob: 367cc30e454f23934fa97b4ba84870f323d65be5 (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
54
55
56
57
58
59
60
61
62
import unittest
from oe.maketype import create, factory

class TestTypes(unittest.TestCase):
    def assertIsInstance(self, obj, cls):
        return self.assertTrue(isinstance(obj, cls))

    def assertIsNot(self, obj, other):
        return self.assertFalse(obj is other)

    def assertFactoryCreated(self, value, type, **flags):
        cls = factory(type)
        self.assertIsNot(cls, None)
        self.assertIsInstance(create(value, type, **flags), cls)

class TestBooleanType(TestTypes):
    def test_invalid(self):
        self.assertRaises(ValueError, create, '', 'boolean')
        self.assertRaises(ValueError, create, 'foo', 'boolean')
        self.assertRaises(TypeError, create, object(), 'boolean')

    def test_true(self):
        self.assertTrue(create('y', 'boolean'))
        self.assertTrue(create('yes', 'boolean'))
        self.assertTrue(create('1', 'boolean'))
        self.assertTrue(create('t', 'boolean'))
        self.assertTrue(create('true', 'boolean'))
        self.assertTrue(create('TRUE', 'boolean'))
        self.assertTrue(create('truE', 'boolean'))

    def test_false(self):
        self.assertFalse(create('n', 'boolean'))
        self.assertFalse(create('no', 'boolean'))
        self.assertFalse(create('0', 'boolean'))
        self.assertFalse(create('f', 'boolean'))
        self.assertFalse(create('false', 'boolean'))
        self.assertFalse(create('FALSE', 'boolean'))
        self.assertFalse(create('faLse', 'boolean'))

    def test_bool_equality(self):
        self.assertEqual(create('n', 'boolean'), False)
        self.assertNotEqual(create('n', 'boolean'), True)
        self.assertEqual(create('y', 'boolean'), True)
        self.assertNotEqual(create('y', 'boolean'), False)

class TestList(TestTypes):
    def assertListEqual(self, value, valid, sep=None):
        obj = create(value, 'list', separator=sep)
        self.assertEqual(obj, valid)
        if sep is not None:
            self.assertEqual(obj.separator, sep)
        self.assertEqual(str(obj), obj.separator.join(obj))

    def test_list_nosep(self):
        testlist = ['alpha', 'beta', 'theta']
        self.assertListEqual('alpha beta theta', testlist)
        self.assertListEqual('alpha  beta\ttheta', testlist)
        self.assertListEqual('alpha', ['alpha'])

    def test_list_usersep(self):
        self.assertListEqual('foo:bar', ['foo', 'bar'], ':')
        self.assertListEqual('foo:bar:baz', ['foo', 'bar', 'baz'], ':')