test_preprocessor.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. #
  4. # File : test_preprocessor_patch.py
  5. # This file is part of RT-Thread RTOS
  6. # COPYRIGHT (C) 2006 - 2025, RT-Thread Development Team
  7. #
  8. # This program is free software; you can redistribute it and/or modify
  9. # it under the terms of the GNU General Public License as published by
  10. # the Free Software Foundation; either version 2 of the License, or
  11. # (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 along
  19. # with this program; if not, write to the Free Software Foundation, Inc.,
  20. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  21. #
  22. # Change Logs:
  23. # Date Author Notes
  24. # 2025-01-05 Assistant Test file for SCons PreProcessor patch
  25. import sys
  26. import os
  27. # Add current directory to path for imports
  28. sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
  29. def test_preprocessor_patch():
  30. """Test the SCons PreProcessor patch functionality"""
  31. try:
  32. from scons_preprocessor_patch import SConsPreProcessorPatch, create_preprocessor_instance
  33. print("Testing SCons PreProcessor patch...")
  34. # Test creating patch instance
  35. patch = SConsPreProcessorPatch()
  36. print("✓ SConsPreProcessorPatch instance created successfully")
  37. # Test getting patched preprocessor
  38. patched_class = patch.get_patched_preprocessor()
  39. print("✓ Patched PreProcessor class retrieved successfully")
  40. # Test creating preprocessor instance
  41. preprocessor = create_preprocessor_instance()
  42. print("✓ PreProcessor instance created successfully")
  43. # Test basic functionality
  44. test_content = """
  45. #define TEST_MACRO 1
  46. #ifdef TEST_MACRO
  47. #define ENABLED_FEATURE 1
  48. #else
  49. #define DISABLED_FEATURE 1
  50. #endif
  51. """
  52. preprocessor.process_contents(test_content)
  53. namespace = preprocessor.cpp_namespace
  54. print("✓ PreProcessor processed test content successfully")
  55. print(f" - TEST_MACRO: {namespace.get('TEST_MACRO', 'Not found')}")
  56. print(f" - ENABLED_FEATURE: {namespace.get('ENABLED_FEATURE', 'Not found')}")
  57. print(f" - DISABLED_FEATURE: {namespace.get('DISABLED_FEATURE', 'Not found')}")
  58. print("\n✓ All tests passed! SCons PreProcessor patch is working correctly.")
  59. return True
  60. except ImportError as e:
  61. print(f"✗ Import error: {e}")
  62. print("Make sure SCons is available in the environment")
  63. return False
  64. except Exception as e:
  65. print(f"✗ Test failed: {e}")
  66. return False
  67. def test_building_integration():
  68. """Test integration with building.py"""
  69. try:
  70. # Test that the function is available from the patch module
  71. from scons_preprocessor_patch import create_preprocessor_instance
  72. print("\nTesting scons_preprocessor_patch integration...")
  73. # Test that the function is available
  74. preprocessor = create_preprocessor_instance()
  75. print("✓ create_preprocessor_instance function works from scons_preprocessor_patch")
  76. # Test basic processing
  77. test_content = "#define BUILD_TEST 1"
  78. preprocessor.process_contents(test_content)
  79. namespace = preprocessor.cpp_namespace
  80. print(f"✓ Integration test passed: BUILD_TEST = {namespace.get('BUILD_TEST', 'Not found')}")
  81. return True
  82. except Exception as e:
  83. print(f"✗ Integration test failed: {e}")
  84. return False
  85. if __name__ == "__main__":
  86. print("SCons PreProcessor Patch Test Suite")
  87. print("=" * 40)
  88. success1 = test_preprocessor_patch()
  89. success2 = test_building_integration()
  90. if success1 and success2:
  91. print("\n🎉 All tests passed! The refactoring was successful.")
  92. sys.exit(0)
  93. else:
  94. print("\n❌ Some tests failed. Please check the implementation.")
  95. sys.exit(1)