/[thuban]/branches/WIP-pyshapelib-bramz/test/support.py
ViewVC logotype

Contents of /branches/WIP-pyshapelib-bramz/test/support.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 324 - (show annotations)
Fri Sep 20 13:55:42 2002 UTC (22 years, 5 months ago) by bh
Original Path: trunk/thuban/test/support.py
File MIME type: text/x-python
File size: 4605 byte(s)
(FloatComparisonMixin.assertFloatEqual): New.
Mixin class for float comparisons
(SubscriberMixin): New. Mixin class to test messages sent through
the Connector class

1 # Copyright (c) 2002 by Intevation GmbH
2 # Authors:
3 # Bernhard Herzog <[email protected]>
4 #
5 # This program is free software under the GPL (>=v2)
6 # Read the file COPYING coming with Thuban for details.
7
8 """
9 Support classes and function for the test suite
10 """
11
12 __version__ = "$Revision$"
13 # $Source$
14 # $Id$
15
16 import os, sys
17 import unittest
18
19 def thuban_dir():
20 """Return the directory containing the Thuban package"""
21 thisdir = os.path.dirname(__file__)
22 return os.path.join(thisdir, os.pardir)
23
24
25 def add_thuban_dir_to_path():
26 """Insert the Thuban directory at the beginning of the python path.
27
28 If it's already part of the path, remove later occurrences.
29 """
30 dir = thuban_dir()
31 while 1:
32 try:
33 sys.path.remove(dir)
34 except ValueError:
35 break
36 sys.path.insert(0, dir)
37
38
39 _initthuban_done = 0
40 def initthuban():
41 """Initialize the interpreter for using Thuban modules
42 """
43 global _initthuban_done
44 if not _initthuban_done:
45 add_thuban_dir_to_path()
46 import thubaninit
47 _initthuban_done = 1
48
49 def run_suite(suite):
50 """Run the test suite suite and return the result"""
51 runner = unittest.TextTestRunner(verbosity = 2)
52 return runner.run(suite)
53
54
55 def create_temp_dir():
56 """Create a temporary directory and return its name.
57
58 The temporary directory is always called temp and is created in the
59 directory where support module is located.
60
61 If the temp directory already exists, just return the name.
62 """
63 name = os.path.abspath(os.path.join(os.path.dirname(__file__), "temp"))
64
65 # if the directory already exists, we're done
66 if os.path.isdir(name):
67 return name
68
69 # create the directory
70 os.mkdir(name)
71 return name
72
73
74 class FileTestMixin:
75
76 """Mixin class for tests that use files in the temporary directory
77 """
78
79 def temp_file_name(self, basename):
80 """Return the full name of the file named basename in the temp. dir"""
81 return os.path.join(create_temp_dir(), basename)
82
83 def temp_dir(self):
84 """Return the name of the directory for the temporary files"""
85 return create_temp_dir()
86
87
88 class FloatComparisonMixin:
89
90 """
91 Mixin class for tests comparing floating point numbers.
92
93 This class provides a few methods for testing floating point
94 operations.
95 """
96
97 fp_epsilon = 1e-6
98
99 def assertFloatEqual(self, test, value):
100 """Assert equality of test and value with some tolerance.
101
102 Assert that the absolute difference between test and value is
103 less than self.fp_epsilon.
104 """
105 return self.assert_(self.fp_epsilon > abs(test - value))
106
107 def assertFloatSeqEqual(self, test, value, epsilon = None):
108 """Assert equality of the sequences test and value with some tolerance.
109
110 Assert that the absolute difference between each corresponding
111 value in test and value is less than the optional parameter
112 epsilon. If epsilon is not given use self.fp_epsilon.
113 """
114 if epsilon is None:
115 epsilon = self.fp_epsilon
116 for i in range(len(test)):
117 return self.assert_(epsilon > abs(test[i] - value[i]))
118
119
120 class SubscriberMixin:
121
122 """Mixin class for tests for messages sent through the Connector
123
124 The SubscriberMixin has some methods that can be used as subscribers
125 of events that when called append information about the message into
126 a list of messages received.
127
128 A derived class should call the clear_messages() method in both its
129 setUp and tearDown methods to clear the list of messages received.
130 """
131
132 def clear_messages(self):
133 """Clear the list of received messages.
134
135 Call this at least in the tests setUp and tearDown methods. It's
136 important to do it in tearDown too because otherwise there may
137 be cyclic references.
138 """
139 self.received_messages = []
140
141 def subscribe_no_params(self):
142 """Method for subscriptions without parameters.
143
144 Add an empty tuple to the list of received messages.
145 """
146 self.received_messages.append(())
147
148 def subscribe_with_params(self, *args):
149 """Method for subscriptions with parameters.
150
151 Append the tuple will all arguments to this function (except for
152 the self argument) to the list of received messages.
153 """
154 self.received_messages.append(args)
155
156 def check_messages(self, messages):
157 """Check whether the messages received match the list messages"""
158 self.assertEquals(messages, self.received_messages)

Properties

Name Value
svn:eol-style native
svn:keywords Author Date Id Revision

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26