def test_addmethod(): foomethod = MultiMethod('foomethod', identity) foomethod.addmethod(lambda x: "The Answer", 42) foomethod.addmethod(lambda x: "2^10", 1024) foomethod.addmethod(lambda x: "Nothing", Default) assert_equal(foomethod(42), "The Answer") assert_equal(foomethod(1024), "2^10") assert_equal(foomethod(Default), "Nothing")
def test_removemethod(): barmethod = MultiMethod('barmethod', identity) @barmethod.method(1) def barmethod(x): return 123 assert_equal(barmethod(1), 123) barmethod.removemethod(1) assert_raises(Exception, lambda: barmethod(1)) @barmethod.method(Default) def barmethod(x): return 42 assert_equal(barmethod("whatever"), 42) assert_equal(barmethod("something"), 42) barmethod.removemethod(Default) assert_raises(Exception, lambda: barmethod("whatever")) assert_raises(Exception, lambda: barmethod(1))