Package dogtail :: Module tc
[hide private]
[frames] | no frames]

Source Code for Module dogtail.tc

  1  # -*- coding: utf-8 -*- 
  2  """Test Case magic 
  3   
  4  Author: Ed Rousseau <rousseau@redhat.com>""" 
  5  __author__ = "Ed Rousseau <rousseau@redhat.com>" 
  6   
  7  import os 
  8  import os.path 
  9  from config import config 
 10  from logging import ResultsLogger, TimeStamp 
 11  from PIL import Image, ImageChops, ImageStat 
 12  from __builtin__ import unicode, long 
 13   
 14   
15 -class TC(object): # pragma: no cover
16 17 """ 18 The Test Case Superclass 19 """ 20 logger = ResultsLogger() 21
22 - def __init__(self):
23 self.encoding = config.encoding 24 # ascii + unicode. 8 bit extended char has been ripped out 25 self.supportedtypes = ( 26 "ascii", "utf-8", "utf-16", "utf-16-be", "utf-16-le", "unicode-escape", "raw-unicode-escape", 27 "big5", "gb18030", "eucJP", "eucKR", "shiftJIS")
28 29 # String comparison function
30 - def compare(self, label, baseline, undertest, encoding=config.encoding):
31 """ 32 Compares 2 strings to see if they are the same. The user may specify 33 the encoding to which the two strings are to be normalized for the 34 comparison. Default encoding is the default system encoding. 35 Normalization to extended 8 bit charactersets is not supported. 36 37 When the origin of either baseline or undertest is a text file whose 38 encoding is something other than ASCII, it is necessary to use 39 codecs.open() instead of open(), so the file's encoding may be 40 specified. 41 """ 42 self.label = label.strip() 43 self.baseline = baseline 44 self.undertest = undertest 45 for string in [self.baseline, self.undertest]: 46 try: 47 string = unicode(string, 'utf-8') 48 except TypeError: 49 pass 50 self.encoding = encoding 51 52 # Normalize the encoding type for the comparaison based on 53 # self.encoding 54 if self.encoding in self.supportedtypes: 55 self.baseline = (self.baseline).encode(self.encoding) 56 self.undertest = (self.undertest).encode(self.encoding) 57 # Compare the strings 58 if self.baseline == self.undertest: 59 self.result = {self.label: "Passed"} 60 else: 61 self.result = {self.label: "Failed - " + self.encoding + 62 " strings do not match. " + self.baseline + " expected: Got " + self.undertest} 63 # Pass the test result to the ResultsLogger for writing 64 TC.logger.log(self.result) 65 return self.result 66 67 else: 68 # We should probably raise an exception here 69 self.result = { 70 self.label: "ERROR - " + self.encoding + " is not a supported encoding type"} 71 TC.logger.log(self.result) 72 return self.result
73 74 75 # String Test Case subclass
76 -class TCString(TC): # pragma: no cover
77 78 """ 79 String Test Case Class 80 """ 81
82 - def __init__(self):
83 TC.__init__(self)
84 85 # Image test case subclass 86 87
88 -class TCImage(TC): # pragma: no cover
89 90 """ 91 Image Test Case Class. 92 """ 93
94 - def compare(self, label, baseline, undertest):
95 for _file in (baseline, undertest): 96 if type(_file) is not unicode and type(_file) is not str: 97 raise TypeError("Need filenames!") 98 self.label = label.strip() 99 self.baseline = baseline.strip() 100 self.undertest = undertest.strip() 101 diffName = TimeStamp().fileStamp("diff") + ".png" 102 self.diff = os.path.normpath( 103 os.path.sep.join((config.scratchDir, diffName))) 104 105 self.baseImage = Image.open(self.baseline) 106 self.testImage = Image.open(self.undertest) 107 try: 108 if self.baseImage.size != self.testImage.size: 109 self.result = { 110 self.label: "Failed - images are different sizes"} 111 raise StopIteration 112 113 self.diffImage = ImageChops.difference(self.baseImage, 114 self.testImage) 115 self.diffImage.save(self.diff) 116 result = False 117 for stat in ('stddev', 'mean', 'sum2'): 118 for item in getattr(ImageStat.Stat(self.diffImage), stat): 119 if item: 120 self.result = {self.label: "Failed - see %s" % 121 self.diff} 122 raise StopIteration 123 else: 124 result = True 125 except StopIteration: 126 result = False 127 128 if result: 129 self.result = {self.label: "Passed"} 130 131 TC.logger.log(self.result) 132 return self.result
133 134
135 -class TCNumber(TC): # pragma: no cover
136 137 """ 138 Number Comparaison Test Case Class 139 """ 140
141 - def __init__(self):
142 TC.__init__(self) 143 self.supportedtypes = ("int", "long", "float", "complex", "oct", "hex")
144 145 # Compare 2 numbers by the type provided in the type arg
146 - def compare(self, label, baseline, undertest, type):
147 """ 148 Compares 2 numbers to see if they are the same. The user may specify 149 how to normalize mixed type comparisons via the type argument. 150 """ 151 self.label = label.strip() 152 self.baseline = baseline 153 self.undertest = undertest 154 self.type = type.strip() 155 156 # If we get a valid type, convert to that type and compare 157 if self.type in self.supportedtypes: 158 # Normalize for comparison 159 if self.type == "int": 160 self.baseline = int(self.baseline) 161 self.undertest = int(self.undertest) 162 elif self.type == "long": 163 self.baseline = long(self.baseline) 164 self.undertest = long(self.undertest) 165 elif self.type == "float": 166 self.baseline = float(self.baseline) 167 self.undertest = float(self.undertest) 168 else: 169 self.baseline = complex(self.baseline) 170 self.undertest = complex(self.undertest) 171 172 # compare 173 if self.baseline == self.undertest: 174 self.result = {self.label: "Passed - numbers are the same"} 175 else: 176 self.result = {self.label: "Failed - " + str( 177 self.baseline) + " expected: Got " + str(self.undertest)} 178 TC.logger.log(self.result) 179 return self.result 180 else: 181 self.result = { 182 self.label: "Failed - " + self.type + " is not in list of supported types"} 183 TC.logger.log(self.result) 184 return self.result
185 186
187 -class TCBool(TC): # pragma: no cover
188
189 - def __init__(self):
190 pass
191
192 - def compare(self, label, _bool):
193 """ 194 If _bool is True, pass. 195 If _bool is False, fail. 196 """ 197 if type(_bool) is not bool: 198 raise TypeError 199 if _bool: 200 result = {label: "Passed"} 201 else: 202 result = {label: "Failed"} 203 TC.logger.log(result)
204 205 from tree import Node 206 207
208 -class TCNode(TC): # pragma: no cover
209
210 - def __init__(self):
211 pass
212
213 - def compare(self, label, baseline, undertest):
214 """ 215 If baseline is None, simply check that undertest is a Node. 216 If baseline is a Node, check that it is equal to undertest. 217 """ 218 if baseline is not None and not isinstance(baseline, Node): 219 raise TypeError 220 221 if not isinstance(undertest, Node): 222 result = {label: "Failed - %s is not a Node" % undertest} 223 elif baseline is None: 224 result = {label: "Passed - %s is a Node" % undertest} 225 elif isinstance(baseline, Node): 226 if baseline == undertest: 227 result = {label: "Passed - %s == %s" % (baseline, undertest)} 228 else: 229 result = {label: "Failed - %s != %s" % (baseline, undertest)} 230 TC.logger.log(result)
231