1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 """File-related test classes."""
17
18
19
20
21
22 import qm.fields
23 import qm.test.base
24 from qm.test.result import *
25 from qm.test.test import *
26 import qm.web
27 import re
28 import string
29
30
31
32
33
35 """A rule for performing a text substitution.
36
37 A 'SubstitutionField' consists of a regular expression pattern and a
38 corresponding replacement string. When the substitution is applied
39 to a body of text, all substrings that match the pattern are
40 replaced with the substitution string.
41
42 The syntax for the regular expression and the substitution string is
43 that of the standard Python 're' (regular expression) module."""
44
56
57
59 return """
60 A substitution consists of a regular expression pattern and a
61 substitution string. When the substitution is applied, all
62 subtrings matching the pattern are replaced with the
63 substitution string. The substitution string may reference
64 matched groups in the pattern.
65
66 The regular expression and substitution syntax are those of
67 Python's standard "'re' regular expression module"."""
68
69
70
72 """Check that the contents of a file match the expected value.
73
74 A 'FileContentsTest' examines the contents of a file. The test
75 passes if and only if the contents exactly match the expected value.
76
77 The path to the file itself is not specified explicitly in the test.
78 Instead, it is taken from a contex property; the name of that
79 variable is specified in the **Path Property** field.
80
81 Optionally, the test may specify one or more substitutions. Each
82 substitution consists of a regular expression pattern and a
83 replacement string. Both the actual file contents and the expected
84 file contents are processed with these substitutions, with all
85 pattern matches replaced with the corresponding substitutions,
86 before the comparison is performed."""
87
88 arguments = [
89 qm.fields.TextField(
90 name="path_property",
91 title="Path Property",
92 description="""The context property naming the file.
93
94 The context property given here will contain the path name
95 of the file.""",
96 not_empty_text=1,
97 default_value="path"),
98
99 qm.fields.TextField(
100 name="expected_contents",
101 title="Expected Contents",
102 description="""The expected contents of the file.""",
103 verbatim="true",
104 multiline="true",
105 default_value=""),
106
107 qm.fields.SetField(SubstitutionField(
108 name="substitutions",
109 title="Substitutions",
110 description="""Regular expression substitutions.
111
112 Each substitution will be applied to both the expected and
113 actual contents of the file. The comparison will be
114 performed after the substitutions have been performed.
115
116 You can use substitutions to ignore insignificant
117 differences between the expected and autual contents."""))
118 ]
119
120
121 - def Run(self, context, result):
122
123 path = context[self.path_property]
124
125 contents = open(path, "r").read()
126
127 self.expected_contents = \
128 self.__PerformSubstitutions(self.expected_contents)
129 contents = self.__PerformSubstitutions(contents)
130
131 if contents != self.expected_contents:
132 result.Fail("Contents do not match expected contents.",
133 { "FileContentsTest.contents" : contents,
134 "FileContentsTest.expected_contents" :
135 self.expected_contents })
136
137
139 """Perform substitutions on a body of text.
140
141 returns -- The string 'text', processed with the substitutions
142 configured for this test instance."""
143
144 for pattern, replacement in self.substitutions:
145 text = re.sub(pattern, replacement, text)
146 return text
147
148
149
150
151
152
153
154
155
156