header_reader_test.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. # Copyright 2022 DeepMind Technologies Limited
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. # ==============================================================================
  15. """Tests for MuJoCo API header reader."""
  16. from absl.testing import absltest
  17. from absl.testing import parameterized
  18. import header_reader
  19. _EXAMPLE = """
  20. //------- My favorite section --------
  21. // My function
  22. MJAPI void mj_function(int a, int b);
  23. // My other function
  24. // This one has multiple lines
  25. MJAPI const char* mj_other_function(int a, int b,
  26. const char* a);
  27. typedef enum mjEnum_ {
  28. mjVALUE1 // Some value
  29. mjVALUE2 // Some other value
  30. } mjEnum;
  31. struct mjStruct_ {
  32. int value1 // Some value
  33. int value2 // Some other value
  34. };
  35. typedef struct mjStruct_ mjStruct;
  36. // My favorite struct
  37. typedef struct mjStruct2_ {
  38. int value1 // Another value
  39. int value2 // More more value
  40. } mjStruct2;
  41. MJAPI void mj_no_doc(int a, void* b);
  42. //------------ MJAPI FUNCTIONS --------------
  43. void mj_stripped(int a, int b,
  44. int c);
  45. """
  46. _API = header_reader.read([f'{line}\n' for line in _EXAMPLE.split('\n')])
  47. class MuJoCoApiGeneratorTest(parameterized.TestCase):
  48. def test_enums_line_numbers(self):
  49. self.assertEqual(_API['mjEnum'].start, 12)
  50. self.assertEqual(_API['mjEnum'].end, 15)
  51. def test_structs_line_numbers(self):
  52. self.assertEqual(_API['mjStruct'].start, 17)
  53. self.assertEqual(_API['mjStruct'].end, 21)
  54. def test_structs2_line_numbers(self):
  55. self.assertEqual(_API['mjStruct2'].start, 23)
  56. self.assertEqual(_API['mjStruct2'].end, 26)
  57. def test_function_line_numbers(self):
  58. self.assertEqual(_API['mj_function'].start, 5)
  59. self.assertEqual(_API['mj_function'].end, 5)
  60. def test_function_code(self):
  61. self.assertEqual(_API['mj_function'].code,
  62. 'void mj_function(int a, int b);\n')
  63. def test_function_section(self):
  64. self.assertEqual(_API['mj_function'].section, 'My favorite section')
  65. def test_function_doc(self):
  66. self.assertEqual(_API['mj_function'].doc, 'My function\n')
  67. def test_multi_line_doc(self):
  68. self.assertEqual(_API['mj_other_function'].doc,
  69. 'My other function\nThis one has multiple lines\n')
  70. def test_multi_line_function(self):
  71. self.assertEqual(_API['mj_other_function'].start, 9)
  72. self.assertEqual(_API['mj_other_function'].end, 10)
  73. def test_no_doc_function(self):
  74. self.assertEqual(_API['mj_no_doc'].start, 28)
  75. self.assertEqual(_API['mj_no_doc'].end, 28)
  76. def test_stripped_functions(self):
  77. self.assertEqual(_API['mj_stripped'].start, 32)
  78. if __name__ == '__main__':
  79. absltest.main()