TestUtilityLoad.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. import unittest
  2. from Cython.Compiler import Code, UtilityCode
  3. def strip_2tup(tup):
  4. return tup[0] and tup[0].strip(), tup[1] and tup[1].strip()
  5. class TestUtilityLoader(unittest.TestCase):
  6. """
  7. Test loading UtilityCodes
  8. """
  9. expected = "test {{loader}} prototype", "test {{loader}} impl"
  10. required = "req {{loader}} proto", "req {{loader}} impl"
  11. context = dict(loader='Loader')
  12. name = "TestUtilityLoader"
  13. filename = "TestUtilityLoader.c"
  14. cls = Code.UtilityCode
  15. def test_load_as_string(self):
  16. got = strip_2tup(self.cls.load_as_string(self.name))
  17. self.assertEqual(got, self.expected)
  18. got = strip_2tup(self.cls.load_as_string(self.name, self.filename))
  19. self.assertEqual(got, self.expected)
  20. def test_load(self):
  21. utility = self.cls.load(self.name)
  22. got = strip_2tup((utility.proto, utility.impl))
  23. self.assertEqual(got, self.expected)
  24. required, = utility.requires
  25. got = strip_2tup((required.proto, required.impl))
  26. self.assertEqual(got, self.required)
  27. utility = self.cls.load(self.name, from_file=self.filename)
  28. got = strip_2tup((utility.proto, utility.impl))
  29. self.assertEqual(got, self.expected)
  30. utility = self.cls.load_cached(self.name, from_file=self.filename)
  31. got = strip_2tup((utility.proto, utility.impl))
  32. self.assertEqual(got, self.expected)
  33. class TestTempitaUtilityLoader(TestUtilityLoader):
  34. """
  35. Test loading UtilityCodes with Tempita substitution
  36. """
  37. expected_tempita = (TestUtilityLoader.expected[0].replace('{{loader}}', 'Loader'),
  38. TestUtilityLoader.expected[1].replace('{{loader}}', 'Loader'))
  39. required_tempita = (TestUtilityLoader.required[0].replace('{{loader}}', 'Loader'),
  40. TestUtilityLoader.required[1].replace('{{loader}}', 'Loader'))
  41. cls = Code.TempitaUtilityCode
  42. def test_load_as_string(self):
  43. got = strip_2tup(self.cls.load_as_string(self.name, context=self.context))
  44. self.assertEqual(got, self.expected_tempita)
  45. def test_load(self):
  46. utility = self.cls.load(self.name, context=self.context)
  47. got = strip_2tup((utility.proto, utility.impl))
  48. self.assertEqual(got, self.expected_tempita)
  49. required, = utility.requires
  50. got = strip_2tup((required.proto, required.impl))
  51. self.assertEqual(got, self.required_tempita)
  52. utility = self.cls.load(self.name, from_file=self.filename, context=self.context)
  53. got = strip_2tup((utility.proto, utility.impl))
  54. self.assertEqual(got, self.expected_tempita)
  55. class TestCythonUtilityLoader(TestTempitaUtilityLoader):
  56. """
  57. Test loading CythonUtilityCodes
  58. """
  59. # Just change the attributes and run the same tests
  60. expected = None, "test {{cy_loader}} impl"
  61. expected_tempita = None, "test CyLoader impl"
  62. required = None, "req {{cy_loader}} impl"
  63. required_tempita = None, "req CyLoader impl"
  64. context = dict(cy_loader='CyLoader')
  65. name = "TestCyUtilityLoader"
  66. filename = "TestCyUtilityLoader.pyx"
  67. cls = UtilityCode.CythonUtilityCode
  68. # Small hack to pass our tests above
  69. cls.proto = None
  70. test_load = TestUtilityLoader.test_load
  71. test_load_tempita = TestTempitaUtilityLoader.test_load