Gnumed.timelinelib.test.cases.unit

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 random
     22  import unittest
     23
     24  import wx
     25
     26  from timelinelib.calendar.gregorian.timetype import GregorianTimeType
     27  from timelinelib.test.utils import svg_to_dict
     28
     29
     30 -class UnitTestCase(unittest.TestCase):
     31
     32      HALT_GUI = False
     33      AUTO_CLOSE = False
     34
     35 -    def assertSvgEqual(self, left, right):
     36          self.assertEqual(svg_to_dict(left), svg_to_dict(right), "\nLeft:  {!r}\nRight: {!r}".format(left, right))
     37
     38 -    def assertPeriodsEqual(self, first, second, time_type=GregorianTimeType()):
     39          message = "Periods not equal.\n  First:  %s \"%s\"\n  Second: %s \"%s\"" % (
     40              first,
     41              time_type.format_period(first),
     42              second,
     43              time_type.format_period(second),
     44          )
     45          self.assertEqual(first, second, message)
     46
     47 -    def assertInstanceNotIn(self, object_, list_):
     48          for element in list_:
     49              if element is object_:
     50                  self.fail("%r was in list" % object_)
     51
     52 -    def assertEqNeImplementationIsCorrect(self, create_fn, modifiers):
     53          (modification_description, modifier_fn) = get_random_modifier(modifiers)
     54          one = modifier_fn(create_fn())
     55          other = modifier_fn(create_fn())
     56          fail_message_one_other = "%r vs %r (%s)" % (one, other,
     57                                                      modification_description)
     58          self.assertTrue(type(one) == type(other), fail_message_one_other)
     59          self.assertFalse(one is None, fail_message_one_other)
     60          self.assertTrue(one is not None, fail_message_one_other)
     61          self.assertTrue(one is not other, fail_message_one_other)
     62          self.assertFalse(one is other, fail_message_one_other)
     63          self.assertTrue(one == other, fail_message_one_other)
     64          self.assertFalse(one != other, fail_message_one_other)
     65          self.assertTrue(one == one, fail_message_one_other)
     66          self.assertFalse(one != one, fail_message_one_other)
     67          (modification_description, modifier_fn) = get_random_modifier(modifiers)
     68          modified = modifier_fn(other)
     69          fail_message_modified_one = "%r vs %r (%s)" % (modified, one,
     70                                                         modification_description)
     71          self.assertTrue(type(modified) == type(one), fail_message_modified_one)
     72          self.assertTrue(modified is not one, fail_message_modified_one)
     73          self.assertFalse(modified is one, fail_message_modified_one)
     74          self.assertTrue(modified != one, fail_message_modified_one)
     75          self.assertFalse(modified == one, fail_message_modified_one)
     76
     77 -    def show_dialog(self, dialog_class, *args, **kwargs):
     78          with self.wxapp() as app:
     79              dialog = dialog_class(*args, **kwargs)
     80              try:
     81                  if self.HALT_GUI:
     82                      if self.AUTO_CLOSE:
     83                          wx.CallLater(2000, dialog.Close)
     84                      dialog.ShowModal()
     85              finally:
     86                  dialog.Destroy()
     87
     88      @contextlib.contextmanager
     89 -    def wxapp(self):
     90          app = self.get_wxapp()
     91          try:
     92              yield app
     93          finally:
     94              self.destroy_wxapp(app)
     95
     96 -    def get_wxapp(self):
     97          return wx.App(False)
     98
     99 -    def destroy_wxapp(self, app):
    100          if app.GetTopWindow():
    101              app.GetTopWindow().Destroy()
    102          app.Destroy()
    103          # A problem was seen when running these two tests in sequence:
    104          #
    105          # unit.calendar.num.periodpicker.TestNumPeriodPicker.test_show_manual_test_dialog
    106          # timelinelib.general.xmlparser
    107          #
    108          # The first one uses wx.App. The doctest failed because it did not seem
    109          # to capture stdout. I suspect that wx.App does something with stdout,
    110          # and a clean shutdown is needed to restore it. For some reason a
    111          # gc.collect() seems to fix it. I'm not sure why, but this seems to
    112          # work.
    113          #
    114          # The failure did not always happen, so I suspect it was a timing
    115          # issue. If the gc.collect() happened in time, the error did not
    116          # occur.
    117          gc.collect()
    118
    119
    120 -def get_random_modifier(modifiers):
    121      return random.choice(modifiers)
    122