2
# arch-tag: 3fe5d578-07de-406e-950e-6bd26dc93af0
3
# Copyright (C) 2004--2005 Canonical Limited
4
# Author: David Allouche <david@allouche.net>
6
# This program is free software; you can redistribute it and/or modify
7
# it under the terms of the GNU General Public License as published by
8
# the Free Software Foundation; either version 2 of the License, or
9
# (at your option) any later version.
11
# This program is distributed in the hope that it will be useful,
12
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
# GNU General Public License for more details.
16
# You should have received a copy of the GNU General Public License
17
# along with this program; if not, write to the Free Software
18
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20
"""Test cases for output parsing.
22
Unit tests for output classifiers and integration tests with actual
29
from framework import TestCase
32
class Classifiers(unittest.TestCase):
36
def _classify_chatter_helper(self, classify):
37
lines = ['* chatty', 'witty']
38
classifier = classify(lines)
39
chatter = classifier.next()
40
self.assertEqual('chatty', chatter.text)
41
self.assertEqual('* chatty', str(chatter))
42
self.assertEqual(arch.Chatter, type(chatter))
43
non_chatter = classifier.next()
44
self.assert_(non_chatter is lines[1])
45
self.assertRaises(StopIteration, classifier.next)
47
def classify_chatter(self):
48
"""classify_chatter works"""
49
self._classify_chatter_helper(arch.classify_chatter)
50
tests.append('classify_chatter')
52
def classify_changes(self):
53
"""classify_changes classifies chatter correctly"""
54
self._classify_chatter_helper(arch.classify_changeset_creation)
55
tests.append('classify_changes')
57
def _classify_tree_change_helper(self, line, type_,
58
nametype, name, **kwargs):
59
classifier = arch.classify_changeset_creation([line])
60
change = classifier.next()
61
self.assertRaises(StopIteration, classifier.next)
62
self.assertEqual(type_, type(change))
63
self.assertEqual(line, str(change))
64
self.assertEqual(name, change.name)
65
self.assertEqual(nametype, type(change.name))
66
for k, v in kwargs.items():
67
# do not use getattr so backtraces show the attribute name
68
if k == 'oldname': self.assertEqual(v, change.oldname)
69
elif k == 'binary': self.assertEqual(v, change.binary)
70
elif k == 'directory': self.assertEqual(v, change.directory)
71
else: raise AssertionError('bad argument: %s=%r' % (k, v))
72
for attr in ('oldname', 'binary', 'directory'):
73
if attr not in kwargs:
74
def thunk(): getattr(change, attr)
75
self.assertRaises(AttributeError, thunk)
77
def _classify_merge_outcome_helper(self, line, type_,
78
nametype, name, directory):
79
classifier = arch.classify_changeset_application([line])
80
change = classifier.next()
81
self.assertRaises(StopIteration, classifier.next)
82
self.assertEqual(type_, type(change))
83
self.assertEqual(line, str(change))
84
self.assertEqual(name, change.name)
85
self.assertEqual(nametype, type(change.name))
86
self.assertEqual(directory , change.directory)
89
class ClassifyFileAddition(Classifiers):
91
def classify_changes_addition(self):
92
"""classify_changes classifies FileAddition"""
93
type_ = arch.FileAddition
94
self._classify_tree_change_helper(
95
'A foo', type_, arch.FileName, 'foo', directory=False)
96
self._classify_tree_change_helper(
97
'A/ foo', type_, arch.DirName, 'foo', directory=True)
98
self._classify_tree_change_helper(
99
'A fo\(sp)o', type_, arch.FileName, 'fo o', directory=False)
100
self._classify_tree_change_helper(
101
'A/ fo\(sp)o', type_, arch.DirName, 'fo o', directory=True)
102
tests.append('classify_changes_addition')
105
class ClassifyFileDeletion(Classifiers):
107
def classify_changes_deletion(self):
108
"""classify_changes classifies FileDeletion"""
109
type_ = arch.FileDeletion
110
self._classify_tree_change_helper(
111
'D foo', type_, arch.FileName, 'foo', directory=False)
112
self._classify_tree_change_helper(
113
'D/ foo', type_, arch.DirName, 'foo', directory=True)
114
self._classify_tree_change_helper(
115
'D fo\(sp)o', type_, arch.FileName, 'fo o', directory=False)
116
self._classify_tree_change_helper(
117
'D/ fo\(sp)o', type_, arch.DirName, 'fo o', directory=True)
118
tests.append('classify_changes_deletion')
121
class ClassifyFileModification(Classifiers):
123
def classify_changes_modification(self):
124
"""classify_changes classifies FileModification"""
125
type_ = arch.FileModification
126
self._classify_tree_change_helper(
127
'M foo', type_, arch.FileName, 'foo', binary=False,
129
self._classify_tree_change_helper(
130
'Mb foo', type_, arch.FileName, 'foo', binary=True,
132
self._classify_tree_change_helper(
133
'M fo\(sp)o', type_, arch.FileName, 'fo o', binary=False,
135
self._classify_tree_change_helper(
136
'Mb fo\(sp)o', type_, arch.FileName, 'fo o', binary=True,
138
tests.append('classify_changes_modification')
141
class ClassifyFilePermissionChange(Classifiers):
143
def classify_changes_permissions(self):
144
"""classify_changes classifies FilePermissionsChange"""
145
type_ = arch.FilePermissionsChange
146
self._classify_tree_change_helper(
147
'-- foo', type_, arch.FileName, 'foo', directory=False)
148
self._classify_tree_change_helper(
149
'-/ foo', type_, arch.DirName, 'foo', directory=True)
150
self._classify_tree_change_helper(
151
'-- fo\(sp)o', type_, arch.FileName, 'fo o', directory=False)
152
self._classify_tree_change_helper(
153
'-/ fo\(sp)o', type_, arch.DirName, 'fo o', directory=True)
154
tests.append('classify_changes_permissions')
157
class ClassifyFileRename(Classifiers):
159
def classify_changes_rename(self):
160
"""classify_changes classifies FileRename"""
161
type_ = arch.FileRename
162
self._classify_tree_change_helper(
163
'=> bar\tfoo', type_, arch.FileName, 'foo',
164
directory=False, oldname='bar')
165
self._classify_tree_change_helper(
166
'/> bar\tfoo', type_, arch.DirName, 'foo',
167
directory=True, oldname='bar')
168
self._classify_tree_change_helper(
169
'=> b\(sp)ar\tfo\(sp)o', type_, arch.FileName, 'fo o',
170
directory=False, oldname='b ar')
171
self._classify_tree_change_helper(
172
'/> b\(sp)ar\tfo\(sp)o', type_, arch.DirName, 'fo o',
173
directory=True, oldname='b ar')
174
tests.append('classify_changes_rename')
177
class ClassifySymLinkModification(Classifiers):
179
def classify_changes_symlink(self):
180
"""classify_changes classifies SymlinkModification"""
181
self._classify_tree_change_helper(
182
'-> foo', arch.SymlinkModification, arch.FileName, 'foo',
184
self._classify_tree_change_helper(
185
'-> fo\(sp)o', arch.SymlinkModification, arch.FileName, 'fo o',
187
tests.append('classify_changes_symlink')
190
class ClassifyPatchConflict(Classifiers):
192
def classify_changes_conflict(self):
193
"""classify_changes classifies PatchConflict"""
194
self._classify_merge_outcome_helper(
195
'C foo', arch.PatchConflict, arch.FileName, 'foo',
197
self._classify_merge_outcome_helper(
198
'C fo\(sp)o', arch.PatchConflict, arch.FileName, 'fo o',
200
tests.append('classify_changes_conflict')
203
def test_suite(limit=()):
206
'ClassifyFileAddition',
207
'ClassifyFileDeletion',
208
'ClassifyFileModification',
209
'ClassifyFilePermissionChange',
210
'ClassifyFileRename',
211
'ClassifySymLinkModification',
212
'ClassifyPatchConflict',
214
return framework.make_test_suite(globals(), classes, limit)
217
return framework.run_test_suite(argv, test_suite)
219
if __name__ == "__main__":
220
sys.exit(main(sys.argv))