Package qm :: Package test :: Package classes :: Module file
[hide private]
[frames] | no frames]

Source Code for Module qm.test.classes.file

  1  ######################################################################## 
  2  # 
  3  # File:   file.py 
  4  # Author: Alex Samuel 
  5  # Date:   2001-06-21 
  6  # 
  7  # Contents: 
  8  #   Test classes involving file contents. 
  9  # 
 10  # Copyright (c) 2001, 2002 by CodeSourcery, LLC.  All rights reserved.  
 11  # 
 12  # For license terms see the file COPYING. 
 13  # 
 14  ######################################################################## 
 15   
 16  """File-related test classes.""" 
 17   
 18  ######################################################################## 
 19  # imports 
 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  # classes 
 32  ######################################################################## 
 33   
34 -class SubstitutionField(qm.fields.TupleField):
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
45 - def __init__(self, name, **properties):
46 """Create a new 'SubstitutionField'. 47 48 By default, the pattern and replacement string are empty.""" 49 50 # Initialize the base class. 51 fields = (qm.fields.TextField(name = "pattern", 52 title = "Pattern",), 53 qm.fields.TextField(name = "replacement", 54 title = "Replacement")) 55 qm.fields.TupleField.__init__(self, name, fields, **properties)
56 57
58 - def GetHelp(self):
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
71 -class FileContentsTest(Test):
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 # Extract the path to the file we're testing. 123 path = context[self.path_property] 124 # Read the contents of the file. 125 contents = open(path, "r").read() 126 # Perform substitutions on the file contents. 127 self.expected_contents = \ 128 self.__PerformSubstitutions(self.expected_contents) 129 contents = self.__PerformSubstitutions(contents) 130 # Compare the contents to the expected contents. 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
138 - def __PerformSubstitutions(self, text):
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 # Local Variables: 152 # mode: python 153 # indent-tabs-mode: nil 154 # fill-column: 72 155 # End: 156