test_xxlimited.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import unittest
  2. from test.support import import_helper
  3. import types
  4. xxlimited = import_helper.import_module('xxlimited')
  5. xxlimited_35 = import_helper.import_module('xxlimited_35')
  6. class CommonTests:
  7. module: types.ModuleType
  8. def test_xxo_new(self):
  9. xxo = self.module.Xxo()
  10. def test_xxo_attributes(self):
  11. xxo = self.module.Xxo()
  12. with self.assertRaises(AttributeError):
  13. xxo.foo
  14. with self.assertRaises(AttributeError):
  15. del xxo.foo
  16. xxo.foo = 1234
  17. self.assertEqual(xxo.foo, 1234)
  18. del xxo.foo
  19. with self.assertRaises(AttributeError):
  20. xxo.foo
  21. def test_foo(self):
  22. # the foo function adds 2 numbers
  23. self.assertEqual(self.module.foo(1, 2), 3)
  24. def test_str(self):
  25. self.assertTrue(issubclass(self.module.Str, str))
  26. self.assertIsNot(self.module.Str, str)
  27. custom_string = self.module.Str("abcd")
  28. self.assertEqual(custom_string, "abcd")
  29. self.assertEqual(custom_string.upper(), "ABCD")
  30. def test_new(self):
  31. xxo = self.module.new()
  32. self.assertEqual(xxo.demo("abc"), "abc")
  33. class TestXXLimited(CommonTests, unittest.TestCase):
  34. module = xxlimited
  35. def test_xxo_demo(self):
  36. xxo = self.module.Xxo()
  37. other = self.module.Xxo()
  38. self.assertEqual(xxo.demo("abc"), "abc")
  39. self.assertEqual(xxo.demo(xxo), xxo)
  40. self.assertEqual(xxo.demo(other), other)
  41. self.assertEqual(xxo.demo(0), None)
  42. def test_error(self):
  43. with self.assertRaises(self.module.Error):
  44. raise self.module.Error
  45. def test_buffer(self):
  46. xxo = self.module.Xxo()
  47. self.assertEqual(xxo.x_exports, 0)
  48. b1 = memoryview(xxo)
  49. self.assertEqual(xxo.x_exports, 1)
  50. b2 = memoryview(xxo)
  51. self.assertEqual(xxo.x_exports, 2)
  52. b1[0] = 1
  53. self.assertEqual(b1[0], 1)
  54. self.assertEqual(b2[0], 1)
  55. class TestXXLimited35(CommonTests, unittest.TestCase):
  56. module = xxlimited_35
  57. def test_xxo_demo(self):
  58. xxo = self.module.Xxo()
  59. other = self.module.Xxo()
  60. self.assertEqual(xxo.demo("abc"), "abc")
  61. self.assertEqual(xxo.demo(0), None)
  62. def test_roj(self):
  63. # the roj function always fails
  64. with self.assertRaises(SystemError):
  65. self.module.roj(0)
  66. def test_null(self):
  67. null1 = self.module.Null()
  68. null2 = self.module.Null()
  69. self.assertNotEqual(null1, null2)