Package Gnumed :: Package timelinelib :: Package test :: Package cases :: Module unit
[frames] | no frames]

Source Code for Module Gnumed.timelinelib.test.cases.unit

  1  # Copyright (C) 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018  Rickard Lindberg, Roger Lindberg 
  2  # 
  3  # This file is part of Timeline. 
  4  # 
  5  # Timeline is free software: you can redistribute it and/or modify 
  6  # it under the terms of the GNU General Public License as published by 
  7  # the Free Software Foundation, either version 3 of the License, or 
  8  # (at your option) any later version. 
  9  # 
 10  # Timeline is distributed in the hope that it will be useful, 
 11  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
 12  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 13  # GNU General Public License for more details. 
 14  # 
 15  # You should have received a copy of the GNU General Public License 
 16  # along with Timeline.  If not, see <http://www.gnu.org/licenses/>. 
 17   
 18   
 19  import contextlib 
 20  import gc 
 21  import platform 
 22  import random 
 23  import unittest 
 24   
 25  import wx 
 26   
 27  from timelinelib.calendar.gregorian.timetype import GregorianTimeType 
 28  from timelinelib.test.utils import svg_to_dict 
29 30 31 -class UnitTestCase(unittest.TestCase):
32 33 HALT_GUI = False 34 AUTO_CLOSE = False 35
36 - def assertSvgEqual(self, left, right):
37 self.assertEqual(svg_to_dict(left), svg_to_dict(right), "\nLeft: {!r}\nRight: {!r}".format(left, right))
38
39 - def assertPeriodsEqual(self, first, second, time_type=GregorianTimeType()):
40 message = "Periods not equal.\n First: %s \"%s\"\n Second: %s \"%s\"" % ( 41 first, 42 time_type.format_period(first), 43 second, 44 time_type.format_period(second), 45 ) 46 self.assertEqual(first, second, message)
47
48 - def assertInstanceNotIn(self, object_, list_):
49 for element in list_: 50 if element is object_: 51 self.fail("%r was in list" % object_)
52
53 - def assertEqNeImplementationIsCorrect(self, create_fn, modifiers):
54 (modification_description, modifier_fn) = get_random_modifier(modifiers) 55 one = modifier_fn(create_fn()) 56 other = modifier_fn(create_fn()) 57 fail_message_one_other = "%r vs %r (%s)" % (one, other, 58 modification_description) 59 self.assertTrue(type(one) == type(other), fail_message_one_other) 60 self.assertFalse(one is None, fail_message_one_other) 61 self.assertTrue(one is not None, fail_message_one_other) 62 self.assertTrue(one is not other, fail_message_one_other) 63 self.assertFalse(one is other, fail_message_one_other) 64 self.assertTrue(one == other, fail_message_one_other) 65 self.assertFalse(one != other, fail_message_one_other) 66 self.assertTrue(one == one, fail_message_one_other) 67 self.assertFalse(one != one, fail_message_one_other) 68 (modification_description, modifier_fn) = get_random_modifier(modifiers) 69 modified = modifier_fn(other) 70 fail_message_modified_one = "%r vs %r (%s)" % (modified, one, 71 modification_description) 72 self.assertTrue(type(modified) == type(one), fail_message_modified_one) 73 self.assertTrue(modified is not one, fail_message_modified_one) 74 self.assertFalse(modified is one, fail_message_modified_one) 75 self.assertTrue(modified != one, fail_message_modified_one) 76 self.assertFalse(modified == one, fail_message_modified_one)
77
78 - def show_dialog(self, dialog_class, *args, **kwargs):
79 with self.wxapp() as app: 80 dialog = dialog_class(*args, **kwargs) 81 try: 82 if self.HALT_GUI: 83 if self.AUTO_CLOSE: 84 wx.CallLater(2000, dialog.Close) 85 dialog.ShowModal() 86 finally: 87 dialog.Destroy()
88 89 @contextlib.contextmanager
90 - def wxapp(self):
91 app = self.get_wxapp() 92 try: 93 yield app 94 finally: 95 self.destroy_wxapp(app)
96
97 - def get_wxapp(self):
98 app = wx.App(False) 99 if platform.system() == "Windows": 100 import locale 101 locale.setlocale(locale.LC_ALL, 'C') 102 self.locale = wx.Locale() 103 self.locale.Init(wx.LANGUAGE_DEFAULT) 104 return app
105
106 - def destroy_wxapp(self, app):
107 if app.GetTopWindow(): 108 app.GetTopWindow().Destroy() 109 app.Destroy() 110 # A problem was seen when running these two tests in sequence: 111 # 112 # unit.calendar.num.periodpicker.TestNumPeriodPicker.test_show_manual_test_dialog 113 # timelinelib.general.xmlparser 114 # 115 # The first one uses wx.App. The doctest failed because it did not seem 116 # to capture stdout. I suspect that wx.App does something with stdout, 117 # and a clean shutdown is needed to restore it. For some reason a 118 # gc.collect() seems to fix it. I'm not sure why, but this seems to 119 # work. 120 # 121 # The failure did not always happen, so I suspect it was a timing 122 # issue. If the gc.collect() happened in time, the error did not 123 # occur. 124 gc.collect()
125
126 127 -def get_random_modifier(modifiers):
128 return random.choice(modifiers)
129