# Copyright (c) 2002-2004 LOGILAB S.A. (Paris, FRANCE). # http://www.logilab.fr/ -- mailto:contact@logilab.fr # # This program is free software; you can redistribute it and/or modify it under # the terms of the GNU General Public License as published by the Free Software # Foundation; either version 2 of the License, or (at your option) any later # version. # # This program is distributed in the hope that it will be useful, but WITHOUT # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. # # You should have received a copy of the GNU General Public License along with # this program; if not, write to the Free Software Foundation, Inc., # 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. """pyreverse unittest utilities""" __revision__ = "$Id: utils.py,v 1.8 2005/01/05 15:36:45 syt Exp $" import unittest import os import sys from os.path import join from logilab.common.astng import ASTNGManager def _astng_wrapper(func, modname): return func(modname) def _sorted_file(path): lines = [line.strip() for line in open(path).readlines() if (line.find('squeleton generated by ') == -1 and not line.startswith('__revision__ = "$Id:'))] lines = [line for line in lines if line] lines.sort() return '\n'.join(lines) def get_project(module, name=None): """return a astng project representation """ manager = ASTNGManager() # flush cache manager._modules_by_name = {} return manager.project_from_files([module], _astng_wrapper, project_name=name) class FileTC(unittest.TestCase): """base test case for testing file output""" generated_files = () def setUp(self): self.expected_files = [join('expected', file) for file in self.generated_files] def tearDown(self): for fname in self.generated_files: try: os.remove(fname) except: continue def _test_same_file(self, index): file = self.generated_files[index] expected_file = self.expected_files[index] value = _sorted_file(file) expected = _sorted_file(expected_file) try: self.assertEqual(value, expected, '%s != %s' % (file, expected_file)) os.remove(file) except AssertionError: print '!'*80 print '%s / %s' % (file, expected_file) values = value.splitlines() expecteds = expected.splitlines() for i in range(len(values)): got = values[i] try: exp = expecteds[i] except IndexError: print '%r != None' % got break if got != exp: print 'Around line %s' % (i+1), print '%r != %r' % (got, exp) break raise def build_file_case(filetc): for i in range(len(filetc.generated_files)): setattr(filetc, 'test_same_file_%s' %i, lambda self, index=i: self._test_same_file(index))