Kyle Marek-Spartz
kyle.marek.spartz@gmail.com
http://kyle.marek-spartz.org
February 13, 2014
public interface CountFishInterface {
public void one();
public void two();
}
public class OurFish implements CountFishInterface {
public void one() { ... }
public void two() { ... }
}
javac
should complain if the interface is not satisfied.NotImplementedError
NotImplementedError
, cont.NotImplementedError
.AttributeError
AttributeError
, so why bother?This:
is approximately equivalent to:
def AbstractInterfaceTest(test_name, method_names):
def abstract_interface_test_helper(method_name):
def test_method(self):
try:
getattr(self.obj, method_name)()
except NotImplementedError:
self.fail(
type(self.obj).__name__ +
' does not implement ' +
method_name
)
return test_method
methods = {
'test_' + method_name: abstract_interface_test_helper(method_name)
for method_name
in method_names
}
return type(test_name, (object,), methods)
interfaces = {
'CountFish': ['one', 'two'],
'ColorFish': ['red', 'blue']
}
for interface_name, methods in interfaces.iteritems():
interface_name += 'Interface'
globals()[interface_name] = Interface(interface_name, methods)
test_name = 'AbstractTest' + interface_name
globals()[test_name] = AbstractInterfaceTest(test_name, methods)