interface_tests.py 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. """Tests for cement.core.interface."""
  2. from cement.core import exc, interface, output, meta
  3. from cement.core.handler import CementBaseHandler
  4. from cement.utils import test
  5. class TestInterface(interface.Interface):
  6. class IMeta:
  7. label = 'test'
  8. class TestHandler(CementBaseHandler):
  9. class Meta:
  10. interface = TestInterface
  11. label = 'test'
  12. class TestHandler2(CementBaseHandler):
  13. class Meta:
  14. interface = output.IOutput
  15. label = 'test2'
  16. class TestHandler3():
  17. pass
  18. class InterfaceTestCase(test.CementCoreTestCase):
  19. def setUp(self):
  20. self.app = self.make_app()
  21. @test.raises(exc.InterfaceError)
  22. def test_interface_class(self):
  23. try:
  24. i = interface.Interface()
  25. except exc.InterfaceError as e:
  26. self.eq(e.msg, "Interfaces can not be used directly.")
  27. raise
  28. def test_attribute_class(self):
  29. i = interface.Attribute('Test attribute')
  30. self.eq(i.__repr__(), "<interface.Attribute - 'Test attribute'>")
  31. def test_validator(self):
  32. interface.validate(TestInterface, TestHandler(), [])
  33. @test.raises(exc.InterfaceError)
  34. def test_validate_bad_interface(self):
  35. han = TestHandler2()
  36. try:
  37. interface.validate(TestInterface, han, [])
  38. except exc.InterfaceError as e:
  39. self.eq(e.msg, "%s does not implement %s." % (han, TestInterface))
  40. raise
  41. @test.raises(exc.InterfaceError)
  42. def test_validate_bad_interface_no_meta(self):
  43. han = TestHandler3()
  44. try:
  45. interface.validate(TestInterface, han, [])
  46. except exc.InterfaceError as e:
  47. self.eq(e.msg, "Invalid or missing: ['_meta'] in %s" % han)
  48. raise
  49. @test.raises(exc.InterfaceError)
  50. def test_validate_bad_interface_missing_meta(self):
  51. han = TestHandler()
  52. try:
  53. interface.validate(TestInterface, han, [], ['missing_meta'])
  54. except exc.InterfaceError as e:
  55. self.eq(
  56. e.msg, "Invalid or missing: ['_meta.missing_meta'] in %s" % han)
  57. raise