Package pytilities :: Package test :: Package overloading :: Module overloaded
[hide private]
[frames] | no frames]

Source Code for Module pytilities.test.overloading.overloaded

  1  # Copyright (C) 2010 Tim Diels <limyreth@users.sourceforge.net> 
  2  #  
  3  # This file is part of pytilities. 
  4  #  
  5  # pytilities 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  # pytilities 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 pytilities.  If not, see <http://www.gnu.org/licenses/>. 
 17  # 
 18   
 19  __docformat__ = 'reStructuredText' 
 20   
 21  import unittest 
 22  from pytilities.overloading import overloaded, Overload, Param 
 23  from pytilities.types import NumberType 
24 25 -class Storage(object):
26 """Something with an x and y""" 27 x = 1 28 y = 1
29
30 -class A(object):
31 """Some class to test with""" 32
33 - def __init_xy(self, x, y):
34 return "xy", self, x, y
35
36 - def __init_storage(self, storage):
37 return "storage", self, storage
38 39 @overloaded(( 40 Overload(__init_xy, 41 Param("x", NumberType, default=0), 42 Param("y", NumberType, default=0)), 43 Overload(__init_storage, 44 Param("storage"))))
45 - def say_ni(self):
46 """docstring is docstring""" 47 pass
48
49 50 -class MethodTestCase(unittest.TestCase):
51 '''Test overloading of a method'''
52 - def setUp(self):
53 self.a = A() 54 self.s = Storage()
55
56 - def test_xy(self):
57 """Calls that should end up at xy""" 58 self.assertEquals(self.a.say_ni(), ("xy", self.a, 0, 0)) 59 self.assertEquals(self.a.say_ni(3), ("xy", self.a, 3, 0)) 60 self.assertEquals(self.a.say_ni(x=3), ("xy", self.a, 3, 0)) 61 self.assertEquals(self.a.say_ni(3, 6), ("xy", self.a, 3, 6)) 62 self.assertEquals(self.a.say_ni(x=3, y=6), ("xy", self.a, 3, 6)) 63 self.assertEquals(self.a.say_ni(y=6), ("xy", self.a, 0, 6))
64
65 - def test_storage(self):
66 """Calls that should end up at storage""" 67 self.assertEquals(self.a.say_ni(self.s), ("storage", self.a, self.s))
68
69 - def test_bad(self):
70 """Calls that should cause assertion errors""" 71 self.assertRaises(AssertionError, lambda: self.a.say_ni(0, self.s))
72
73 74 -def __init_xy(x, y):
75 return "xy", x, y
76
77 -def __init_storage(storage):
78 return "storage", storage
79 80 @overloaded(( 81 Overload(__init_xy, 82 Param("x", NumberType, default=0), 83 Param("y", NumberType, default=0)), 84 Overload(__init_storage, 85 Param("storage"))), 86 False)
87 -def say_it(self):
88 """docstring is docstring""" 89 pass
90
91 92 -class FunctionTestCase(unittest.TestCase):
93 '''Test overloading of a function'''
94 - def setUp(self):
95 self.s = Storage()
96
97 - def test_xy(self):
98 """Calls that should end up at xy""" 99 self.assertEquals(say_it(), ("xy", 0, 0)) 100 self.assertEquals(say_it(3), ("xy", 3, 0)) 101 self.assertEquals(say_it(x=3), ("xy", 3, 0)) 102 self.assertEquals(say_it(3, 6), ("xy", 3, 6)) 103 self.assertEquals(say_it(x=3, y=6), ("xy", 3, 6)) 104 self.assertEquals(say_it(y=6), ("xy", 0, 6))
105
106 - def test_storage(self):
107 """Calls that should end up at storage""" 108 self.assertEquals(say_it(self.s), ("storage", self.s))
109
110 - def test_bad(self):
111 """Calls that should cause assertion errors""" 112 self.assertRaises(AssertionError, lambda: say_it(0, self.s))
113 114 # TODO test matcher functions 115