Revision 98093aac kamaki/cli/test.py

b/kamaki/cli/test.py
33 33

  
34 34
from unittest import makeSuite, TestSuite, TextTestRunner, TestCase
35 35
from inspect import getmembers, isclass
36
from tempfile import NamedTemporaryFile
36 37

  
37 38
from kamaki.cli.command_tree.test import Command, CommandTree
38 39
from kamaki.cli.argument.test import (
......
41 42
    KeyValueArgument, ProgressBarArgument, ArgumentParseManager)
42 43

  
43 44

  
45
class History(TestCase):
46

  
47
    def setUp(self):
48
        from kamaki.cli.history import History as HClass
49
        self.HCLASS = HClass
50
        self.file = NamedTemporaryFile()
51

  
52
    def tearDown(self):
53
        self.file.close()
54

  
55
    def test__match(self):
56
        self.assertRaises(AttributeError, self.HCLASS._match, 'ok', 42)
57
        self.assertRaises(TypeError, self.HCLASS._match, 2.71, 'ok')
58
        for args, expected in (
59
                (('XXX', None), True),
60
                ((None, None), True),
61
                (('this line has some terms', 'some terms'), True),
62
                (('this line has some terms', 'some bad terms'), False),
63
                (('small line', 'not so small line terms'), False),
64
                ((['line', 'with', 'some', 'terms'], 'some terms'), True),
65
                ((['line', 'with', 'some terms'], 'some terms'), False)):
66
            self.assertEqual(self.HCLASS._match(*args), expected)
67

  
68
    def test_get(self):
69
        history = self.HCLASS(self.file.name)
70
        self.assertEqual(history.get(), [])
71

  
72
        sample_history = (
73
            'kamaki history show\n',
74
            'kamaki file list\n',
75
            'kamaki touch pithos:f1\n',
76
            'kamaki file info pithos:f1\n')
77
        self.file.write(''.join(sample_history))
78
        self.file.flush()
79

  
80
        expected = ['%s.  \t%s' % (
81
            i + 1, event) for i, event in enumerate(sample_history)]
82
        self.assertEqual(history.get(), expected)
83
        self.assertEqual(history.get('kamaki'), expected)
84
        self.assertEqual(history.get('file kamaki'), expected[1::2])
85
        self.assertEqual(history.get('pithos:f1'), expected[2:])
86
        self.assertEqual(history.get('touch pithos:f1'), expected[2:3])
87

  
88
        for limit in range(len(sample_history)):
89
            self.assertEqual(history.get(limit=limit), expected[-limit:])
90
            self.assertEqual(
91
                history.get('kamaki', limit=limit), expected[-limit:])
92

  
93
    def test_add(self):
94
        history = self.HCLASS(self.file.name)
95
        some_strings = ('a brick', 'two bricks', 'another brick', 'A wall!')
96
        for i, line in enumerate(some_strings):
97
            history.add(line)
98
            self.file.seek(0)
99
            self.assertEqual(
100
                self.file.read(), '\n'.join(some_strings[:(i + 1)]) + '\n')
101

  
102
    def test_clean(self):
103
        content = 'a brick\ntwo bricks\nanother brick\nA wall!\n'
104
        self.file.write(content)
105
        self.file.flush()
106
        self.file.seek(0)
107
        self.assertEqual(self.file.read(), content)
108
        history = self.HCLASS(self.file.name)
109
        history.clean()
110
        self.file.seek(0)
111
        self.assertEqual(self.file.read(), '')
112

  
113
    def test_retrieve(self):
114
        sample_history = (
115
            'kamaki history show\n',
116
            'kamaki file list\n',
117
            'kamaki touch pithos:f1\n',
118
            'kamaki file info pithos:f1\n',
119
            'current / last command is always excluded')
120
        self.file.write(''.join(sample_history))
121
        self.file.flush()
122

  
123
        history = self.HCLASS(self.file.name)
124
        self.assertRaises(ValueError, history.retrieve, 'must be number')
125
        self.assertRaises(TypeError, history.retrieve, [1, 2, 3])
126

  
127
        for i in (0, len(sample_history), -len(sample_history)):
128
            self.assertEqual(history.retrieve(i), None)
129
        for i in range(1, len(sample_history)):
130
            self.assertEqual(history.retrieve(i), sample_history[i - 1])
131
            self.assertEqual(history.retrieve(- i), sample_history[- i - 1])
132

  
133

  
44 134
#  TestCase auxiliary methods
45 135

  
46 136
def runTestCase(cls, test_name, args=[], failure_collector=[]):

Also available in: Unified diff