gen_test_driver.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. #!/usr/bin/env python
  2. # Copyright (c) 2020 Project CHIP Authors
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. import optparse
  16. import re
  17. import sys
  18. TEMPLATE_PREFIX = '''/*
  19. *
  20. * Copyright (c) 2020 Project CHIP Authors
  21. * All rights reserved.
  22. *
  23. * Licensed under the Apache License, Version 2.0 (the \"License\");
  24. * you may not use this file except in compliance with the License.
  25. * You may obtain a copy of the License at
  26. *
  27. * http://www.apache.org/licenses/LICENSE-2.0
  28. *
  29. * Unless required by applicable law or agreed to in writing, software
  30. * distributed under the License is distributed on an \"AS IS\" BASIS,
  31. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  32. * See the License for the specific language governing permissions and
  33. * limitations under the License.
  34. *
  35. * THIS FILE WAS GENERATED AUTOMATICALLY BY THE BUILD SYSTEM.
  36. */
  37. #include <nlunit-test.h>
  38. '''
  39. # Forward declarations will be added here
  40. TEMPLATE_MAIN_START = '''
  41. int main()
  42. {
  43. int code = 0;
  44. nlTestSetOutputStyle(OUTPUT_CSV);
  45. '''
  46. # Test invokation will be added here
  47. TEMPLATE_SUFFIX = '''
  48. return code;
  49. }'''
  50. def main(argv):
  51. parser = optparse.OptionParser()
  52. parser.add_option('--input_file')
  53. parser.add_option('--output_file')
  54. options, _ = parser.parse_args(argv)
  55. tests = []
  56. TEST_SUITE_RE = re.compile(r'\s*CHIP_REGISTER_TEST_SUITE\(([^)]*)\)')
  57. with open(options.input_file, 'r') as input_file:
  58. for line in input_file.readlines():
  59. match = TEST_SUITE_RE.match(line)
  60. if not match:
  61. continue
  62. tests.append(match.group(1))
  63. if not tests:
  64. print("ERROR: no tests found in '%s'" % input_file)
  65. print("Did you forget to CHIP_REGISTER_TEST_SUITE?")
  66. return 1
  67. with open(options.output_file, 'w') as output_file:
  68. output_file.write(TEMPLATE_PREFIX)
  69. for test in tests:
  70. output_file.write("int %s();\n" % test)
  71. output_file.write("\n")
  72. output_file.write(TEMPLATE_MAIN_START)
  73. for test in tests:
  74. output_file.write(" code = code | (%s());\n" % test)
  75. output_file.write("\n")
  76. output_file.write(TEMPLATE_SUFFIX)
  77. return 0
  78. if __name__ == '__main__':
  79. sys.exit(main(sys.argv[1:]))