コード例 #1
0
 def test_add_method_coderef_exception(self):
     smobj = SelfMutilatingObject()
     exception = None
     try:
         smobj.add_method('walla', 'walla')
     except TypeError as exception:
         pass
     assert exception
コード例 #2
0
    def test_add_method_name_exception(self):
        smobj = SelfMutilatingObject()
        def thing():
            print "thing works"

        exception = None
        try:
            smobj.add_method(42, thing)
        except TypeError as exception:
            pass
        assert exception
コード例 #3
0
    def test_add_method_params(self):
        smobj = SelfMutilatingObject()

        def concat(*params):
            concat = ""
            for word in params:
                if concat:
                    concat + " and "
                concat += word
            return concat

        smobj.add_method('concatinate', concat)

        smobj.concatinate('one', 'two', 'three') == "one and two and three"
コード例 #4
0
    def test_add_method_kwargs(self):
        smobj = SelfMutilatingObject()
        
        def translate(**kwargs):
            answer = ""
            for (k, v) in kwargs.items():
                if answer:
                    answer += ", "
                answer += "%s %s" % (v, k)
            return answer

        smobj.add_method('translate', translate)

        answer = smobj.translate(cat='siamese', dog='terrier', mouse='field')
        assert "siamese cat" in answer
        assert "terrier dog" in answer
        assert "field mouse" in answer
コード例 #5
0
    def test_add_method_args(self):

        smobj = SelfMutilatingObject()
        smobj.add_method('multiply', lambda a, b: a * b)

        assert smobj.multiply(2, 2) == 4