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
26import sys
27import os
28
29# Add current directory to path for imports
30sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
31
32def test_preprocessor_patch():
33    """Test the SCons PreProcessor patch functionality"""
34    try:
35        from scons_preprocessor_patch import SConsPreProcessorPatch, create_preprocessor_instance
36
37        print("Testing SCons PreProcessor patch...")
38
39        # Test creating patch instance
40        patch = SConsPreProcessorPatch()
41        print("✓ SConsPreProcessorPatch instance created successfully")
42
43        # Test getting patched preprocessor
44        patched_class = patch.get_patched_preprocessor()
45        print("✓ Patched PreProcessor class retrieved successfully")
46
47        # Test creating preprocessor instance
48        preprocessor = create_preprocessor_instance()
49        print("✓ PreProcessor instance created successfully")
50
51        # Test basic functionality
52        test_content = """
53        #define TEST_MACRO 1
54        #ifdef TEST_MACRO
55        #define ENABLED_FEATURE 1
56        #else
57        #define DISABLED_FEATURE 1
58        #endif
59        """
60
61        preprocessor.process_contents(test_content)
62        namespace = preprocessor.cpp_namespace
63
64        print("✓ PreProcessor processed test content successfully")
65        print(f"  - TEST_MACRO: {namespace.get('TEST_MACRO', 'Not found')}")
66        print(f"  - ENABLED_FEATURE: {namespace.get('ENABLED_FEATURE', 'Not found')}")
67        print(f"  - DISABLED_FEATURE: {namespace.get('DISABLED_FEATURE', 'Not found')}")
68
69        print("\n✓ All tests passed! SCons PreProcessor patch is working correctly.")
70        return True
71
72    except ImportError as e:
73        print(f"✗ Import error: {e}")
74        print("Make sure SCons is available in the environment")
75        return False
76    except Exception as e:
77        print(f"✗ Test failed: {e}")
78        return False
79
80def test_building_integration():
81    """Test integration with building.py"""
82    try:
83        # Test that the function is available from the patch module
84        from scons_preprocessor_patch import create_preprocessor_instance
85
86        print("\nTesting scons_preprocessor_patch integration...")
87
88        # Test that the function is available
89        preprocessor = create_preprocessor_instance()
90        print("✓ create_preprocessor_instance function works from scons_preprocessor_patch")
91
92        # Test basic processing
93        test_content = "#define BUILD_TEST 1"
94        preprocessor.process_contents(test_content)
95        namespace = preprocessor.cpp_namespace
96
97        print(f"✓ Integration test passed: BUILD_TEST = {namespace.get('BUILD_TEST', 'Not found')}")
98        return True
99
100    except Exception as e:
101        print(f"✗ Integration test failed: {e}")
102        return False
103
104if __name__ == "__main__":
105    print("SCons PreProcessor Patch Test Suite")
106    print("=" * 40)
107
108    success1 = test_preprocessor_patch()
109    success2 = test_building_integration()
110
111    if success1 and success2:
112        print("\n�� All tests passed! The refactoring was successful.")
113        sys.exit(0)
114    else:
115        print("\n❌ Some tests failed. Please check the implementation.")
116        sys.exit(1)