File: test_utils.py

package info (click to toggle)
python-ptk 1.3.8%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 416 kB
  • sloc: python: 3,616; makefile: 200
file content (67 lines) | stat: -rw-r--r-- 1,711 bytes parent folder | download | duplicates (3)
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
#!/usr/bin/env python3

import base, unittest, copy
from ptk.utils import memoize, Singleton, chars


# I don't know what discover does, but it makes this test fail...

## class MemoizeTestCase(unittest.TestCase):
##     def setUp(self):
##         self.calls = list()

##     @memoize
##     def compute(self, v):
##         self.calls.append(v)
##         return v

##     def test_memoize(self):
##         self.compute(42)
##         self.compute(13)
##         self.assertEqual(self.compute(42), 42)
##         self.assertEqual(self.calls, [42, 13])


class SingletonUnderTest(metaclass=Singleton):
    __reprval__ = '$'

    def __init__(self):
        self.value = 42


class SingletonTestCase(unittest.TestCase):
    def test_instance(self):
        self.assertFalse(isinstance(SingletonUnderTest, type))

    def test_init(self):
        self.assertEqual(SingletonUnderTest.value, 42)

    def test_order(self):
        values = ['spam', SingletonUnderTest]
        values.sort()
        self.assertEqual(values, [SingletonUnderTest, 'spam'])

    def test_copy(self):
        self.assertTrue(copy.copy(SingletonUnderTest) is SingletonUnderTest)

    def test_deepcopy(self):
        self.assertTrue(copy.deepcopy(SingletonUnderTest) is SingletonUnderTest)

    def test_repr(self):
        self.assertEqual(repr(SingletonUnderTest), '$')

    def test_eq(self):
        self.assertNotEqual(SingletonUnderTest, SingletonUnderTest.__class__())


class CharsTestCase(unittest.TestCase):
    def test_str(self):
        self.assertTrue('*' in chars('*'))

    def test_bytes(self):
        for byte in b'*':
            self.assertTrue(byte in chars('*'))


if __name__ == '__main__':
    unittest.main()