/usr/lib/python2.7/dist-packages/wxglade/tests/__init__.py is in python-wxglade 0.6.8-2.2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 | """
@copyright: 2012 Carsten Grohmann
@license: MIT (see license.txt) - THIS PROGRAM COMES WITH NO WARRANTY
"""
# import general python modules
import cStringIO
import difflib
import glob
import os.path
import re
import unittest
# import project modules
import codegen
import common
import wxglade
from xml_parse import CodeWriter
class WXGladeBaseTest(unittest.TestCase):
"""\
Provide basic functions for all tests
All test cases uses an own implementation to L{common.save_file()} to
catch the results. This behaviour is limitied to single file
creation.
"""
vFiles = {}
"""\
Dictionary to store the content of the files generated by the code
generators.
The filename is the key and the content is a StringIO instance.
"""
orig_file_exists = None
"""\
Reference to the original L{codegen.BaseCodeWriter._file_exists()}
implementation
"""
orig_load_file = None
"""\
Reference to the original L{codegen.BaseSourceFileContent._load_file()}
implementation
"""
orig_os_access = None
"""\
Reference to original C{os.access()} implementation
@see: L{_os_access()}
"""
orig_os_makedirs = None
"""\
Reference to original C{os.makedirs()} implementation
@see: L{_os_makedirs()}
"""
orig_os_path_isdir = None
"""\
Reference to original C{os.path.isdir()} implementation
@see: L{_os_path_isdir()}
"""
orig_save_file = None
"""\
Reference to the original L{common.save_file()} implementation
"""
caseDirectory = 'casefiles'
"""\
Directory with input files and result files
"""
init_stage1 = False
"""\
Initialise the first stage of wxGlade e.g. path settings
"""
init_use_gui = False
"""\
Initialise the GUI part of wxGlade
"""
def setUp(self):
"""\
Initialise parts of wxGlade only
"""
# initialise path settings
if self.init_stage1:
wxglade.init_stage1()
# initialise sizers, widgets, codewriter
wxglade.init_stage2(use_gui=self.init_use_gui)
# initialise wxGlade configuration
import config
config.init_preferences()
# set some useful preferences
config.preferences.autosave = False
config.preferences.write_timestamp = False
config.preferences.show_progress = False
# initiate empty structure to store files and there content
self.vFiles = {}
# replace some original implementations by test specific implementation
self.orig_save_file = common.save_file
common.save_file = self._save_file
self.orig_load_file = codegen.BaseSourceFileContent._load_file
codegen.BaseSourceFileContent._load_file = self._load_lines
self.orig_file_exists = codegen.BaseCodeWriter._file_exists
self.orig_os_access = os.access
os.access = self._os_access
self.orig_os_makedirs = os.makedirs
os.makedirs = self._os_makedirs
self.orig_os_path_isdir = os.path.isdir
os.path.isdir = self._os_path_isdir
codegen.BaseCodeWriter._file_exists = self._file_exists
codegen.BaseCodeWriter._show_warnings = False
# set own version string to prevent diff mismatches
common.version = '"faked test version"'
# Determinate case directory
self.caseDirectory = os.path.join(
os.path.dirname(__file__),
self.caseDirectory,
)
def tearDown(self):
"""\
Cleanup
"""
# cleanup virtual files
for filename in self.vFiles:
self.vFiles[filename].close()
self.vFiles = {}
# restore original implementations
common.save_file = self.orig_save_file
codegen.BaseSourceFileContent._load_file = self.orig_load_file
codegen.BaseCodeWriter._file_exists = self.orig_file_exists
os.access = self.orig_os_access
os.makedirs = self.orig_os_makedirs
os.path.isdir = self._os_path_isdir
def _generate_code(self, language, document, filename):
"""\
Generate code for the given language.
@param language: Language to generate code for
@type language: String
@param document: XML document to generate code for
@type document: String
@param filename: Name of the virtual output file
@type filename: String
"""
self.failUnless(
language in common.code_writers,
"No codewriter loaded for %s" % language
)
document = self._prepare_wxg(language, document)
# generate code
CodeWriter(
writer=common.code_writers[language],
input=document,
from_string=True,
out_path=filename,
)
return
def _file_exists(self, filename):
"""\
Check if the file is a test case file
@rtype: Boolean
"""
fullpath = os.path.join(self.caseDirectory, filename)
exists = os.path.isfile(fullpath)
self.failIf(
not exists,
'Case file %s does not exist' % filename
)
return exists
def _load_file(self, filename):
"""\
Load a file need by a test case.
@param filename: Name of the file to load
@type filename: String
@return: File content
@rtype: String
"""
casename, extension = os.path.splitext(filename)
if extension == '.wxg':
filetype = 'input'
else:
filetype = 'result'
file_list = glob.glob(
os.path.join(self.caseDirectory, "%s%s" % (casename, extension))
)
self.failIf(
len(file_list) == 0,
'No %s file "%s" for case "%s" found!' % (
filetype,
filename,
casename,
)
)
self.failIf(
len(file_list) > 1,
'More than one %s file "%s" for case "%s" found!' % (
filetype,
filename,
casename,
)
)
fh = open(file_list[0])
content = fh.read()
fh.close()
# replacing path entries
content = content % {
'wxglade_path': common.wxglade_path,
'docs_path': common.docs_path,
'icons_path': common.icons_path,
'widgets_path': common.widgets_path,
'templates_path': common.templates_path,
'tutorial_file': common.tutorial_file,
}
return content
def _load_lines(self, filename):
"""\
Return file content as a list of lines
"""
casename, extension = os.path.splitext(filename)
if extension == '.wxg':
filetype = 'input'
else:
filetype = 'result'
file_list = glob.glob(
os.path.join(self.caseDirectory, "%s%s" % (casename, extension))
)
self.failIf(
len(file_list) == 0,
'No %s file for case "%s" found!' % (filetype, casename)
)
self.failIf(
len(file_list) > 1,
'More than one %s file for case "%s" found!' % (filetype, casename)
)
fh = open(file_list[0])
content = fh.readlines()
fh.close()
return content
def _modify_attrs(self, content, **kwargs):
"""\
Modify general options inside a wxg (XML) file
"""
modified = content
for option in kwargs:
# create regexp first
pattern = r'%s=".*?"' % option
modified = re.sub(
pattern,
'%s="%s"' % (option, kwargs[option]),
modified,
1
)
return modified
def _os_access(self, path, mode):
"""\
Fake implementation for C{os.access()} - returns always C{True}
"""
return True
def _os_makedirs(self, path, mode):
"""\
Fake implementation for C{os.makedirs()} - do nothing
"""
pass
def _os_path_isdir(self, s):
"""\
Fake implementation for C{os.path.isdir()}
"""
if s in [".", "./"]:
return True
return False
def _prepare_wxg(self, language, document):
"""\
Set test specific options inside a wxg (XML) file
@param language: Language to generate code for
@type language: String
@param document: XML document to generate code for
@type document: String
@return: Modified XML document
@rtype: String
"""
if language == "perl":
_document = self._modify_attrs(
document,
language='perl',
indent_amount='8',
indent_symbol='space',
)
else:
_document = self._modify_attrs(
document,
language=language,
indent_amount='4',
indent_symbol='space',
)
return _document
def _save_file(self, filename, content, which='wxg'):
"""\
Test specific implementation of L{common.save_file()} to get the
result of the code generation without file creation.
The file content is stored in a StringIO instance. It's
accessible at L{self.vFiles} using the filename as key.
@note: The signature is as same as L{wxglade.common.save_file()} but
the functionality differs.
@param filename: Name of the file to create
@param content: String to store into 'filename'
@param which: Kind of backup: 'wxg' or 'codegen'
"""
self.failIf(
filename in self.vFiles,
"Virtual file %s already exists" % filename
)
self.failUnless(
filename,
"No filename given",
)
outfile = cStringIO.StringIO()
outfile.write(content)
self.vFiles[filename] = outfile
def _test_all(self, base):
"""\
Generate code for all languages based on the base file name
@param base: Base name of the test files
@type base: String
"""
for lang, ext in [
['lisp', '.lisp'],
['perl', '.pl'],
['python', '.py'],
['XRC', '.xrc'],
['C++', ''],
]:
name_wxg = '%s.wxg' % base
name_lang = '%s%s' % (base, ext)
if lang == 'C++':
self._generate_and_compare_cpp(name_wxg, name_lang)
else:
self._generate_and_compare(lang, name_wxg, name_lang)
def _diff(self, text1, text2):
"""\
Compare two lists, tailing spaces will be removed
@param text1: Expected text
@type text1: String
@param text2: Generated text
@type text2: String
@return: Changes formatted as unified diff
@rtype: String
"""
self.assertEqual(type(text1), type(""))
self.assertEqual(type(text2), type(""))
# split into lists, because difflib needs lists and remove
# tailing spaces
list1 = [x.rstrip() for x in text1.splitlines()]
list2 = [x.rstrip() for x in text2.splitlines()]
# compare source files
diff_gen = difflib.unified_diff(
list1,
list2,
fromfile='expected source',
tofile='created source',
lineterm=''
)
return '\n'.join(diff_gen)
def _generate_and_compare(self, lang, inname, outname):
"""\
Generate code and compare generated and expected code
@param lang: Language to generate code for
@type lang: String
@param inname: Name of the XML input file
@type inname: String
@param outname: Name of the output file
@type outname: String
"""
# load XML input file
source = self._load_file(inname)
expected = self._load_file(outname)
# generate code
self._generate_code(lang, source, outname)
generated = self.vFiles[outname].getvalue()
self._compare(expected, generated)
def _generate_and_compare_cpp(self, inname, outname):
"""\
Generate C++ code and compare generated and expected code
@param inname: Name of the XML input file
@type inname: String
@param outname: Name of the output file without extension
@type outname: String
"""
name_h = '%s.h' % outname
name_cpp = '%s.cpp' % outname
# load XML input file
source = self._load_file(inname)
result_cpp = self._load_file(name_cpp)
result_h = self._load_file(name_h)
# generate and compare C++ code
self._generate_code('C++', source, outname)
generated_cpp = self.vFiles[name_cpp].getvalue()
generated_h = self.vFiles[name_h].getvalue()
self._compare(result_cpp, generated_cpp, 'C++ source')
self._compare(result_h, generated_h, 'C++ header')
def _compare(self, expected, generated, filetype=None):
"""\
Compare expected and generated content using a diff algorithm
@param expected: Expected content
@type expected: Multiline String
@param generated: Generated content
@type generated: Multiline String
@param filetype: Short description of the content
@type filetype: String
"""
# compare files
delta = self._diff(expected, generated)
if filetype:
self.failIf(
delta,
"Generated %s file and expected result differs:\n%s" % (
filetype,
delta,
)
)
else:
self.failIf(
delta,
"Generated file and expected result differs:\n%s" % delta
)
|