From ef91086462e8f754953dfa4a908823804f3ee46b Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 5 Jan 2025 13:50:31 +0000 Subject: [PATCH] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- .../backend/DirectX/DirectxPreprocessor.py | 3 +- .../test_directx/test_preprocessor.py | 69 +++++++++++++------ 2 files changed, 49 insertions(+), 23 deletions(-) diff --git a/crosstl/backend/DirectX/DirectxPreprocessor.py b/crosstl/backend/DirectX/DirectxPreprocessor.py index 581da72..784cca6 100644 --- a/crosstl/backend/DirectX/DirectxPreprocessor.py +++ b/crosstl/backend/DirectX/DirectxPreprocessor.py @@ -1,5 +1,6 @@ import os + class DirectxPreprocessor: def __init__(self): self.macros = {} @@ -68,4 +69,4 @@ def expand_macros(self, line): if any(macro not in self.macros for macro in line.split()): raise ValueError(f"Undefined macro encountered in line: {line}") - return line \ No newline at end of file + return line diff --git a/tests/test_backend/test_directx/test_preprocessor.py b/tests/test_backend/test_directx/test_preprocessor.py index 474113f..e08e5e6 100644 --- a/tests/test_backend/test_directx/test_preprocessor.py +++ b/tests/test_backend/test_directx/test_preprocessor.py @@ -1,32 +1,40 @@ import unittest from unittest.mock import patch, mock_open from DirectxPreprocessor import DirectxPreprocessor -import os + class TestDirectxPreprocessor(unittest.TestCase): def setUp(self): """Set up common test variables.""" self.preprocessor = DirectxPreprocessor() - @patch("os.path.exists", return_value=True) # Mock os.path.exists to always return True for included files + @patch( + "os.path.exists", return_value=True + ) # Mock os.path.exists to always return True for included files def test_include_directive(self, mock_exists): """Test proper handling of #include directives.""" - shader_code = '#include "common.hlsl"\nfloat4 main() : SV_POSITION { return 0; }' - include_content = 'float4 commonFunc() { return float4(1.0); }' + shader_code = ( + '#include "common.hlsl"\nfloat4 main() : SV_POSITION { return 0; }' + ) + include_content = "float4 commonFunc() { return float4(1.0); }" with patch("builtins.open", mock_open(read_data=include_content)): result = self.preprocessor.preprocess(shader_code) self.assertIn("float4 commonFunc()", result) self.assertIn("float4 main()", result) - @patch("os.path.exists", return_value=True) # Mock os.path.exists to always return True for included files + @patch( + "os.path.exists", return_value=True + ) # Mock os.path.exists to always return True for included files def test_define_macro(self, mock_exists): """Test macro definition and substitution.""" - shader_code = '#define PI 3.14\nfloat piValue = PI;' + shader_code = "#define PI 3.14\nfloat piValue = PI;" result = self.preprocessor.preprocess(shader_code) self.assertIn("float piValue = 3.14;", result) - @patch("os.path.exists", return_value=True) # Mock os.path.exists to always return True for included files + @patch( + "os.path.exists", return_value=True + ) # Mock os.path.exists to always return True for included files def test_conditional_compilation(self, mock_exists): """Test conditional compilation blocks.""" shader_code = """ @@ -47,36 +55,48 @@ def test_undefined_macro_error(self): with self.assertRaises(ValueError): self.preprocessor.preprocess(shader_code) - @patch("os.path.exists", return_value=True) # Mock os.path.exists to always return True for included files + @patch( + "os.path.exists", return_value=True + ) # Mock os.path.exists to always return True for included files def test_nested_includes(self, mock_exists): """Test handling of nested include directives.""" main_shader = '#include "file1.hlsl"' file1_content = '#include "file2.hlsl"\nfloat file1Value = 1.0;' - file2_content = 'float file2Value = 2.0;' + file2_content = "float file2Value = 2.0;" - with patch("builtins.open", side_effect=[ - mock_open(read_data=file1_content).return_value, - mock_open(read_data=file2_content).return_value, - ]): + with patch( + "builtins.open", + side_effect=[ + mock_open(read_data=file1_content).return_value, + mock_open(read_data=file2_content).return_value, + ], + ): result = self.preprocessor.preprocess(main_shader) self.assertIn("float file1Value = 1.0;", result) self.assertIn("float file2Value = 2.0;", result) - @patch("os.path.exists", return_value=True) # Mock os.path.exists to always return True for included files + @patch( + "os.path.exists", return_value=True + ) # Mock os.path.exists to always return True for included files def test_recursive_includes(self, mock_exists): """Test detection of recursive include loops.""" main_shader = '#include "file1.hlsl"' file1_content = '#include "file2.hlsl"' file2_content = '#include "file1.hlsl"' - with patch("builtins.open", side_effect=[ - mock_open(read_data=file1_content).return_value, - mock_open(read_data=file2_content).return_value, - ]): + with patch( + "builtins.open", + side_effect=[ + mock_open(read_data=file1_content).return_value, + mock_open(read_data=file2_content).return_value, + ], + ): with self.assertRaises(RecursionError): self.preprocessor.preprocess(main_shader) - @patch("os.path.exists", return_value=True) # Mock os.path.exists to always return True for included files + @patch( + "os.path.exists", return_value=True + ) # Mock os.path.exists to always return True for included files def test_macro_expansion(self, mock_exists): """Test macro expansion within shader code.""" shader_code = """ @@ -86,7 +106,9 @@ def test_macro_expansion(self, mock_exists): result = self.preprocessor.preprocess(shader_code) self.assertIn("int value = 10 + 5;", result) - @patch("os.path.exists", return_value=True) # Mock os.path.exists to always return True for included files + @patch( + "os.path.exists", return_value=True + ) # Mock os.path.exists to always return True for included files def test_condition_stack(self, mock_exists): """Test the condition stack during #ifdef/#endif.""" shader_code = """ @@ -96,7 +118,9 @@ def test_condition_stack(self, mock_exists): float featureValue = 0.0; #endif """ - self.preprocessor.handle_define("#define FEATURE 1") # Ensure the macro is defined + self.preprocessor.handle_define( + "#define FEATURE 1" + ) # Ensure the macro is defined result = self.preprocessor.preprocess(shader_code) self.assertIn("float featureValue = 1.0;", result) self.assertNotIn("float featureValue = 0.0;", result) @@ -107,5 +131,6 @@ def test_empty_shader(self): result = self.preprocessor.preprocess(shader_code) self.assertEqual(result, "") + if __name__ == "__main__": - unittest.main() \ No newline at end of file + unittest.main()