You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

90 lines
3.2 KiB

  1. import re, sys
  2. from pathlib import Path
  3. COPYRIGHT = """/*
  4. * This program source code file is part of KiCad, a free EDA CAD application.
  5. *
  6. * Copyright (C) 2022 KiCad Developers, see AUTHORS.txt for contributors.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version 2
  11. * of the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, you may find one here:
  20. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  21. * or you may search the http://www.gnu.org website for the version 2 license,
  22. * or you may write to the Free Software Foundation, Inc.,
  23. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  24. */
  25. """
  26. class MockGen:
  27. def __init__(self, path):
  28. self.members = []
  29. self.path = path
  30. with open(self.path) as f:
  31. self.lines = f.readlines()
  32. def __parseHeader(self):
  33. # group 1: retval and function
  34. # group 2: arguments
  35. # group 3: other keywords (const, override...)
  36. regex = re.compile("virtual\s+([^\(]+)\(([^\)]*)\)(.*)")
  37. for line in self.lines:
  38. virtualFcn = regex.search(line)
  39. if virtualFcn:
  40. function = {
  41. 'name':virtualFcn[1],
  42. 'args':virtualFcn[2],
  43. 'extra':virtualFcn[3],
  44. }
  45. self.members.append(function)
  46. def __generateMockDirective(self, virtualMember):
  47. if "const" in virtualMember['extra']:
  48. mockDirective = str("MOCK_CONST_METHOD( ")
  49. else:
  50. mockDirective = str("MOCK_METHOD( ")
  51. fcnName = virtualMember['name'].split()[-1]
  52. arguments = virtualMember['args'].split(',')
  53. arity = 0 if len(arguments) == 1 and not arguments[0] else len(arguments)
  54. mockDirective += fcnName + ", " + str(arity) + ", "
  55. fcnRetval = virtualMember['name'].strip().removesuffix(fcnName).strip()
  56. mockDirective += fcnRetval + "("
  57. if arity != 0:
  58. for i, arg in enumerate(arguments):
  59. mockDirective += " " if i == 0 else ", "
  60. mockDirective += arg.strip()
  61. mockDirective += " "
  62. return mockDirective + ") );\n"
  63. def generateMock(self):
  64. self.__parseHeader()
  65. with open('qa/mocks/out.txt', 'w') as output:
  66. output.write(COPYRIGHT)
  67. for virtualMember in self.members:
  68. directive = self.__generateMockDirective(virtualMember)
  69. output.write(directive)
  70. if __name__ == "__main__":
  71. header = Path(sys.argv[1])
  72. if header.exists() and len(sys.argv) == 2:
  73. mockGen = MockGen(header)
  74. mockGen.generateMock()
  75. else:
  76. print("This script generates mocks for Turtle mock framework")
  77. print(f"Usage: {sys.argv[0]} path/to/header.hpp")