From e0e714c1757f3d70b0ae7877d2981487ad14513e Mon Sep 17 00:00:00 2001 From: himaanisrivatsava Date: Sun, 5 Jan 2025 14:29:17 +0530 Subject: [PATCH 01/43] #194 define preprocessor feat: Add DirectX preprocessor for handling #define, #ifdef, #include, and related directives refactor: Integrate preprocessor into DirectX lexer refactor: Update DirectX backend init to include preprocessing pipeline test: Add tests for DirectX preprocessor functionality fix: Improve error handling for invalid directives in preprocessor --- .../backend/DirectX/DirectxCrossGLCodeGen.py | 18 +++++ crosstl/backend/DirectX/DirectxLexer.py | 9 ++- .../backend/DirectX/DirectxPreprocessor.py | 66 +++++++++++++++++++ crosstl/backend/DirectX/__init__.py | 13 ++++ .../test_directx/test_preprocessor.py | 22 +++++++ 5 files changed, 125 insertions(+), 3 deletions(-) create mode 100644 crosstl/backend/DirectX/DirectxPreprocessor.py create mode 100644 tests/test_backend/test_directx/test_preprocessor.py diff --git a/crosstl/backend/DirectX/DirectxCrossGLCodeGen.py b/crosstl/backend/DirectX/DirectxCrossGLCodeGen.py index b77b4489..d2d7ca5f 100644 --- a/crosstl/backend/DirectX/DirectxCrossGLCodeGen.py +++ b/crosstl/backend/DirectX/DirectxCrossGLCodeGen.py @@ -61,6 +61,24 @@ def __init__(self): "SV_DEPTH7": "gl_FragDepth7", } + def convert(self, ast): + + #Converts the provided AST to target GLSL code. + + code = [] + for node in ast.nodes: + if isinstance(node, IncludeNode): + code.append(self.handle_include_node(node)) + # Handle other nodes with existing logic + else: + code.append(self.generate_function(node)) + return "\n".join(code) + + def handle_include_node(self, node): + + #Handles `IncludeNode` specific conversion. + return f"// Included file: {node.path}" + def generate(self, ast): code = "shader main {\n" # Generate structs diff --git a/crosstl/backend/DirectX/DirectxLexer.py b/crosstl/backend/DirectX/DirectxLexer.py index a85cc9de..12f790d6 100644 --- a/crosstl/backend/DirectX/DirectxLexer.py +++ b/crosstl/backend/DirectX/DirectxLexer.py @@ -1,6 +1,6 @@ import re from typing import Iterator, Tuple, List - +from .DirectxPreprocessor import DirectxPreprocessor # using sets for faster lookup SKIP_TOKENS = {"WHITESPACE", "COMMENT_SINGLE", "COMMENT_MULTI"} @@ -109,9 +109,12 @@ class HLSLLexer: def __init__(self, code: str): + # Integrating the preprocessor + preprocessor = DirectxPreprocessor() + self.code = preprocessor.preprocess(code) + self._token_patterns = [(name, re.compile(pattern)) for name, pattern in TOKENS] - self.code = code - self._length = len(code) + self._length = len(self.code) def tokenize(self) -> List[Tuple[str, str]]: # tokenize the input code and return list of tokens diff --git a/crosstl/backend/DirectX/DirectxPreprocessor.py b/crosstl/backend/DirectX/DirectxPreprocessor.py new file mode 100644 index 00000000..ae81a427 --- /dev/null +++ b/crosstl/backend/DirectX/DirectxPreprocessor.py @@ -0,0 +1,66 @@ +import os + +class DirectxPreprocessor: + def __init__(self): + self.macros = {} + self.condition_stack = [] + + def preprocess(self, code): + #Preprocess the input HLSL shader code. + lines = code.splitlines() + processed_lines = [] + skip_lines = False + + for line in lines: + stripped_line = line.strip() + + if stripped_line.startswith("#include"): + processed_lines.append(self.handle_include(stripped_line)) + elif stripped_line.startswith("#define"): + self.handle_define(stripped_line) + elif stripped_line.startswith("#ifdef"): + skip_lines = not self.handle_ifdef(stripped_line) + elif stripped_line.startswith("#endif"): + skip_lines = self.handle_endif() + elif stripped_line.startswith("#else"): + skip_lines = not skip_lines + elif not skip_lines: + processed_lines.append(self.expand_macros(line)) + + return "\n".join(processed_lines) + + def handle_include(self, line): + #Handle #include directive. + file_path = line.split()[1].strip('"<>"') + if not os.path.exists(file_path): + raise FileNotFoundError(f"Included file '{file_path}' not found.") + with open(file_path, 'r') as file: + return file.read() + + def handle_define(self, line): + #Handle #define directive. + parts = line.split(maxsplit=2) + if len(parts) == 3: + self.macros[parts[1]] = parts[2] + else: + self.macros[parts[1]] = "" + + def handle_ifdef(self, line): + #Handle #ifdef directive. + macro = line.split(maxsplit=1)[1] + is_defined = macro in self.macros + self.condition_stack.append(is_defined) + return is_defined + + def handle_endif(self): + #Handle #endif directive. + if not self.condition_stack: + raise SyntaxError("#endif without matching #ifdef") + self.condition_stack.pop() + return False if self.condition_stack and not self.condition_stack[-1] else True + + def expand_macros(self, line): + #Expand defined macros in the line. + for macro, value in self.macros.items(): + line = line.replace(macro, value) + return line diff --git a/crosstl/backend/DirectX/__init__.py b/crosstl/backend/DirectX/__init__.py index f4cfd7bf..362550bb 100644 --- a/crosstl/backend/DirectX/__init__.py +++ b/crosstl/backend/DirectX/__init__.py @@ -1,3 +1,16 @@ from .DirectxLexer import HLSLLexer from .DirectxParser import HLSLParser from .DirectxCrossGLCodeGen import HLSLToCrossGLConverter +def process_shader(shader_code): + """ + Process an HLSL shader code through the DirectX pipeline: + 1. Preprocess the code. + 2. Tokenize with the lexer. + 3. Parse with the parser. + 4. Convert to CrossGL using the code generator.""" + lexer = HLSLLexer(shader_code) + tokens = lexer.tokenize() + parser = HLSLParser(tokens) + ast = parser.parse() + converter = HLSLToCrossGLConverter() + return converter.convert(ast) diff --git a/tests/test_backend/test_directx/test_preprocessor.py b/tests/test_backend/test_directx/test_preprocessor.py new file mode 100644 index 00000000..1945df1a --- /dev/null +++ b/tests/test_backend/test_directx/test_preprocessor.py @@ -0,0 +1,22 @@ +import unittest +from DirectxLexer import HLSLLexer +from DirectxParser import HLSLParser +from DirectxCrossGLCodeGen import HLSLToCrossGLConverter + +class TestHLSLPreprocessor(unittest.TestCase): + def setUp(self): + self.converter = HLSLToCrossGLConverter() + + def test_include_directive(self): + shader_code = '#include "common.hlsl"\nfloat4 main() : SV_Target { return 0; }' + expected_output = '// Included file: common.hlsl\nfloat4 main() : SV_Target { return 0; }' + lexer = HLSLLexer(shader_code) + tokens = lexer.tokenize() + parser = HLSLParser(tokens) + ast = parser.parse() + output = self.converter.convert(ast) + self.assertIn('// Included file: common.hlsl', output) + # Additional assertions can be added here to verify the correctness of the output + +if __name__ == '__main__': + unittest.main() From 508425b9ee18ca7ad475ef6db1ef6ac6f7fbcdd8 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 09:08:58 +0000 Subject: [PATCH 02/43] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- crosstl/backend/DirectX/DirectxCrossGLCodeGen.py | 12 ++++++------ crosstl/backend/DirectX/DirectxPreprocessor.py | 15 ++++++++------- crosstl/backend/DirectX/__init__.py | 2 ++ .../test_directx/test_preprocessor.py | 10 +++++++--- 4 files changed, 23 insertions(+), 16 deletions(-) diff --git a/crosstl/backend/DirectX/DirectxCrossGLCodeGen.py b/crosstl/backend/DirectX/DirectxCrossGLCodeGen.py index d2d7ca5f..d2b7aca0 100644 --- a/crosstl/backend/DirectX/DirectxCrossGLCodeGen.py +++ b/crosstl/backend/DirectX/DirectxCrossGLCodeGen.py @@ -62,9 +62,9 @@ def __init__(self): } def convert(self, ast): - - #Converts the provided AST to target GLSL code. - + + # Converts the provided AST to target GLSL code. + code = [] for node in ast.nodes: if isinstance(node, IncludeNode): @@ -75,10 +75,10 @@ def convert(self, ast): return "\n".join(code) def handle_include_node(self, node): - - #Handles `IncludeNode` specific conversion. + + # Handles `IncludeNode` specific conversion. return f"// Included file: {node.path}" - + def generate(self, ast): code = "shader main {\n" # Generate structs diff --git a/crosstl/backend/DirectX/DirectxPreprocessor.py b/crosstl/backend/DirectX/DirectxPreprocessor.py index ae81a427..4f60d235 100644 --- a/crosstl/backend/DirectX/DirectxPreprocessor.py +++ b/crosstl/backend/DirectX/DirectxPreprocessor.py @@ -1,12 +1,13 @@ import os + class DirectxPreprocessor: def __init__(self): self.macros = {} self.condition_stack = [] def preprocess(self, code): - #Preprocess the input HLSL shader code. + # Preprocess the input HLSL shader code. lines = code.splitlines() processed_lines = [] skip_lines = False @@ -30,15 +31,15 @@ def preprocess(self, code): return "\n".join(processed_lines) def handle_include(self, line): - #Handle #include directive. + # Handle #include directive. file_path = line.split()[1].strip('"<>"') if not os.path.exists(file_path): raise FileNotFoundError(f"Included file '{file_path}' not found.") - with open(file_path, 'r') as file: + with open(file_path, "r") as file: return file.read() def handle_define(self, line): - #Handle #define directive. + # Handle #define directive. parts = line.split(maxsplit=2) if len(parts) == 3: self.macros[parts[1]] = parts[2] @@ -46,21 +47,21 @@ def handle_define(self, line): self.macros[parts[1]] = "" def handle_ifdef(self, line): - #Handle #ifdef directive. + # Handle #ifdef directive. macro = line.split(maxsplit=1)[1] is_defined = macro in self.macros self.condition_stack.append(is_defined) return is_defined def handle_endif(self): - #Handle #endif directive. + # Handle #endif directive. if not self.condition_stack: raise SyntaxError("#endif without matching #ifdef") self.condition_stack.pop() return False if self.condition_stack and not self.condition_stack[-1] else True def expand_macros(self, line): - #Expand defined macros in the line. + # Expand defined macros in the line. for macro, value in self.macros.items(): line = line.replace(macro, value) return line diff --git a/crosstl/backend/DirectX/__init__.py b/crosstl/backend/DirectX/__init__.py index 362550bb..7289ab44 100644 --- a/crosstl/backend/DirectX/__init__.py +++ b/crosstl/backend/DirectX/__init__.py @@ -1,6 +1,8 @@ from .DirectxLexer import HLSLLexer from .DirectxParser import HLSLParser from .DirectxCrossGLCodeGen import HLSLToCrossGLConverter + + def process_shader(shader_code): """ Process an HLSL shader code through the DirectX pipeline: diff --git a/tests/test_backend/test_directx/test_preprocessor.py b/tests/test_backend/test_directx/test_preprocessor.py index 1945df1a..2204781c 100644 --- a/tests/test_backend/test_directx/test_preprocessor.py +++ b/tests/test_backend/test_directx/test_preprocessor.py @@ -3,20 +3,24 @@ from DirectxParser import HLSLParser from DirectxCrossGLCodeGen import HLSLToCrossGLConverter + class TestHLSLPreprocessor(unittest.TestCase): def setUp(self): self.converter = HLSLToCrossGLConverter() def test_include_directive(self): shader_code = '#include "common.hlsl"\nfloat4 main() : SV_Target { return 0; }' - expected_output = '// Included file: common.hlsl\nfloat4 main() : SV_Target { return 0; }' + expected_output = ( + "// Included file: common.hlsl\nfloat4 main() : SV_Target { return 0; }" + ) lexer = HLSLLexer(shader_code) tokens = lexer.tokenize() parser = HLSLParser(tokens) ast = parser.parse() output = self.converter.convert(ast) - self.assertIn('// Included file: common.hlsl', output) + self.assertIn("// Included file: common.hlsl", output) # Additional assertions can be added here to verify the correctness of the output -if __name__ == '__main__': + +if __name__ == "__main__": unittest.main() From f4c8ec16b0967be79e9d8cfaead80e4f4198824f Mon Sep 17 00:00:00 2001 From: himaanisrivatsava Date: Sun, 5 Jan 2025 15:28:42 +0530 Subject: [PATCH 03/43] #194 define preprocessor Fix: importing HLSLLexer from crossGL backend --- crosstl/backend/DirectX/DirectxLexer.py | 2 +- crosstl/backend/DirectX/__init__.py | 7 +------ tests/test_backend/test_directx/test_preprocessor.py | 1 + 3 files changed, 3 insertions(+), 7 deletions(-) diff --git a/crosstl/backend/DirectX/DirectxLexer.py b/crosstl/backend/DirectX/DirectxLexer.py index 12f790d6..f1e11063 100644 --- a/crosstl/backend/DirectX/DirectxLexer.py +++ b/crosstl/backend/DirectX/DirectxLexer.py @@ -115,7 +115,7 @@ def __init__(self, code: str): self._token_patterns = [(name, re.compile(pattern)) for name, pattern in TOKENS] self._length = len(self.code) - + def tokenize(self) -> List[Tuple[str, str]]: # tokenize the input code and return list of tokens return list(self.token_generator()) diff --git a/crosstl/backend/DirectX/__init__.py b/crosstl/backend/DirectX/__init__.py index 362550bb..46794420 100644 --- a/crosstl/backend/DirectX/__init__.py +++ b/crosstl/backend/DirectX/__init__.py @@ -2,15 +2,10 @@ from .DirectxParser import HLSLParser from .DirectxCrossGLCodeGen import HLSLToCrossGLConverter def process_shader(shader_code): - """ - Process an HLSL shader code through the DirectX pipeline: - 1. Preprocess the code. - 2. Tokenize with the lexer. - 3. Parse with the parser. - 4. Convert to CrossGL using the code generator.""" lexer = HLSLLexer(shader_code) tokens = lexer.tokenize() parser = HLSLParser(tokens) ast = parser.parse() converter = HLSLToCrossGLConverter() return converter.convert(ast) + diff --git a/tests/test_backend/test_directx/test_preprocessor.py b/tests/test_backend/test_directx/test_preprocessor.py index 1945df1a..5907db89 100644 --- a/tests/test_backend/test_directx/test_preprocessor.py +++ b/tests/test_backend/test_directx/test_preprocessor.py @@ -2,6 +2,7 @@ from DirectxLexer import HLSLLexer from DirectxParser import HLSLParser from DirectxCrossGLCodeGen import HLSLToCrossGLConverter +from crosstl.backend.Directx.DirectxLexer import HLSLLexer class TestHLSLPreprocessor(unittest.TestCase): def setUp(self): From 61aff97bdeefa4a9dc24aba817ef2dcbb96c4f38 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 10:07:26 +0000 Subject: [PATCH 04/43] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- crosstl/backend/DirectX/DirectxLexer.py | 2 +- crosstl/backend/DirectX/__init__.py | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/crosstl/backend/DirectX/DirectxLexer.py b/crosstl/backend/DirectX/DirectxLexer.py index f1e11063..12f790d6 100644 --- a/crosstl/backend/DirectX/DirectxLexer.py +++ b/crosstl/backend/DirectX/DirectxLexer.py @@ -115,7 +115,7 @@ def __init__(self, code: str): self._token_patterns = [(name, re.compile(pattern)) for name, pattern in TOKENS] self._length = len(self.code) - + def tokenize(self) -> List[Tuple[str, str]]: # tokenize the input code and return list of tokens return list(self.token_generator()) diff --git a/crosstl/backend/DirectX/__init__.py b/crosstl/backend/DirectX/__init__.py index e5190030..9785f01e 100644 --- a/crosstl/backend/DirectX/__init__.py +++ b/crosstl/backend/DirectX/__init__.py @@ -10,4 +10,3 @@ def process_shader(shader_code): ast = parser.parse() converter = HLSLToCrossGLConverter() return converter.convert(ast) - From b5b55a35c111235651137b11713d068560f9a3ac Mon Sep 17 00:00:00 2001 From: himaanisrivatsava Date: Sun, 5 Jan 2025 15:45:25 +0530 Subject: [PATCH 05/43] Update test_preprocessor.py fix: rewrote the preprocessor test using pytest instead of Unittest --- .../test_directx/test_preprocessor.py | 41 +++++++++---------- 1 file changed, 19 insertions(+), 22 deletions(-) diff --git a/tests/test_backend/test_directx/test_preprocessor.py b/tests/test_backend/test_directx/test_preprocessor.py index 344fa121..2e2a5937 100644 --- a/tests/test_backend/test_directx/test_preprocessor.py +++ b/tests/test_backend/test_directx/test_preprocessor.py @@ -1,27 +1,24 @@ -import unittest -from DirectxLexer import HLSLLexer +import pytest +from crosstl.backend.Directx.DirectxLexer import HLSLLexer from DirectxParser import HLSLParser from DirectxCrossGLCodeGen import HLSLToCrossGLConverter -from crosstl.backend.Directx.DirectxLexer import HLSLLexer - - -class TestHLSLPreprocessor(unittest.TestCase): - def setUp(self): - self.converter = HLSLToCrossGLConverter() - def test_include_directive(self): - shader_code = '#include "common.hlsl"\nfloat4 main() : SV_Target { return 0; }' - expected_output = ( - "// Included file: common.hlsl\nfloat4 main() : SV_Target { return 0; }" - ) - lexer = HLSLLexer(shader_code) - tokens = lexer.tokenize() - parser = HLSLParser(tokens) - ast = parser.parse() - output = self.converter.convert(ast) - self.assertIn("// Included file: common.hlsl", output) - # Additional assertions can be added here to verify the correctness of the output +@pytest.fixture +def converter(): + return HLSLToCrossGLConverter() +def test_include_directive(converter): + shader_code = '#include "common.hlsl"\nfloat4 main() : SV_Target { return 0; }' + expected_output = ( + "// Included file: common.hlsl\nfloat4 main() : SV_Target { return 0; }" + ) + lexer = HLSLLexer(shader_code) + tokens = lexer.tokenize() + parser = HLSLParser(tokens) + ast = parser.parse() + output = converter.convert(ast) + + # Check if the included file path is part of the output + assert "// Included file: common.hlsl" in output + # Additional assertions can be added here to verify the correctness of the output -if __name__ == "__main__": - unittest.main() From 4a726e94180c1168eb4e5c677e2dd297cb289053 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 10:22:31 +0000 Subject: [PATCH 06/43] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- tests/test_backend/test_directx/test_preprocessor.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tests/test_backend/test_directx/test_preprocessor.py b/tests/test_backend/test_directx/test_preprocessor.py index 2e2a5937..473f21cd 100644 --- a/tests/test_backend/test_directx/test_preprocessor.py +++ b/tests/test_backend/test_directx/test_preprocessor.py @@ -3,10 +3,12 @@ from DirectxParser import HLSLParser from DirectxCrossGLCodeGen import HLSLToCrossGLConverter + @pytest.fixture def converter(): return HLSLToCrossGLConverter() + def test_include_directive(converter): shader_code = '#include "common.hlsl"\nfloat4 main() : SV_Target { return 0; }' expected_output = ( @@ -17,8 +19,7 @@ def test_include_directive(converter): parser = HLSLParser(tokens) ast = parser.parse() output = converter.convert(ast) - + # Check if the included file path is part of the output assert "// Included file: common.hlsl" in output # Additional assertions can be added here to verify the correctness of the output - From a9e16573c0aee03de304900261acf5c068f6a371 Mon Sep 17 00:00:00 2001 From: himaanisrivatsava Date: Sun, 5 Jan 2025 16:16:22 +0530 Subject: [PATCH 07/43] Update test_preprocessor.py --- tests/test_backend/test_directx/test_preprocessor.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/tests/test_backend/test_directx/test_preprocessor.py b/tests/test_backend/test_directx/test_preprocessor.py index 473f21cd..59270380 100644 --- a/tests/test_backend/test_directx/test_preprocessor.py +++ b/tests/test_backend/test_directx/test_preprocessor.py @@ -1,14 +1,12 @@ import pytest -from crosstl.backend.Directx.DirectxLexer import HLSLLexer +from DirectxLexer import HLSLLexer from DirectxParser import HLSLParser from DirectxCrossGLCodeGen import HLSLToCrossGLConverter - @pytest.fixture def converter(): return HLSLToCrossGLConverter() - def test_include_directive(converter): shader_code = '#include "common.hlsl"\nfloat4 main() : SV_Target { return 0; }' expected_output = ( @@ -19,7 +17,8 @@ def test_include_directive(converter): parser = HLSLParser(tokens) ast = parser.parse() output = converter.convert(ast) - + # Check if the included file path is part of the output assert "// Included file: common.hlsl" in output # Additional assertions can be added here to verify the correctness of the output + From 9414a1e2635307be6ee69d12cdaab397f0ce577b 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 10:46:37 +0000 Subject: [PATCH 08/43] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- tests/test_backend/test_directx/test_preprocessor.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tests/test_backend/test_directx/test_preprocessor.py b/tests/test_backend/test_directx/test_preprocessor.py index 59270380..5ff2afba 100644 --- a/tests/test_backend/test_directx/test_preprocessor.py +++ b/tests/test_backend/test_directx/test_preprocessor.py @@ -3,10 +3,12 @@ from DirectxParser import HLSLParser from DirectxCrossGLCodeGen import HLSLToCrossGLConverter + @pytest.fixture def converter(): return HLSLToCrossGLConverter() + def test_include_directive(converter): shader_code = '#include "common.hlsl"\nfloat4 main() : SV_Target { return 0; }' expected_output = ( @@ -17,8 +19,7 @@ def test_include_directive(converter): parser = HLSLParser(tokens) ast = parser.parse() output = converter.convert(ast) - + # Check if the included file path is part of the output assert "// Included file: common.hlsl" in output # Additional assertions can be added here to verify the correctness of the output - From 64a4d1250309066314689b3b8792e269a01c39ae Mon Sep 17 00:00:00 2001 From: himaanisrivatsava Date: Sun, 5 Jan 2025 16:37:56 +0530 Subject: [PATCH 09/43] Update test_preprocessor.py --- tests/test_backend/test_directx/test_preprocessor.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/test_backend/test_directx/test_preprocessor.py b/tests/test_backend/test_directx/test_preprocessor.py index 5ff2afba..ce243216 100644 --- a/tests/test_backend/test_directx/test_preprocessor.py +++ b/tests/test_backend/test_directx/test_preprocessor.py @@ -1,5 +1,6 @@ import pytest from DirectxLexer import HLSLLexer +from crosstl.backend.Directx.DirectxLexer import HLSLLexer from DirectxParser import HLSLParser from DirectxCrossGLCodeGen import HLSLToCrossGLConverter From 7464515e1fda99ecc40279f831803abb98fa6586 Mon Sep 17 00:00:00 2001 From: himaanisrivatsava Date: Sun, 5 Jan 2025 16:47:27 +0530 Subject: [PATCH 10/43] Update test_preprocessor.py --- .../test_directx/test_preprocessor.py | 90 +++++++++++++++---- 1 file changed, 71 insertions(+), 19 deletions(-) diff --git a/tests/test_backend/test_directx/test_preprocessor.py b/tests/test_backend/test_directx/test_preprocessor.py index ce243216..62d2be37 100644 --- a/tests/test_backend/test_directx/test_preprocessor.py +++ b/tests/test_backend/test_directx/test_preprocessor.py @@ -1,26 +1,78 @@ import pytest -from DirectxLexer import HLSLLexer from crosstl.backend.Directx.DirectxLexer import HLSLLexer -from DirectxParser import HLSLParser -from DirectxCrossGLCodeGen import HLSLToCrossGLConverter +from crosstl.backend.Directx.DirectxParser import HLSLParser +from crosstl.backend.Directx.DirectxCrossGLCodeGen import HLSLToCrossGLConverter +class TestHLSLPreprocessor: + def setup_method(self): + self.converter = HLSLToCrossGLConverter() -@pytest.fixture -def converter(): - return HLSLToCrossGLConverter() + def test_include_directive(self): + shader_code = '#include "common.hlsl"\nfloat4 main() : SV_Target { return 0; }' + expected_output = ( + "// Included file: common.hlsl\nfloat4 main() : SV_Target { return 0; }" + ) + lexer = HLSLLexer(shader_code) + tokens = lexer.tokenize() + parser = HLSLParser(tokens) + ast = parser.parse() + output = self.converter.convert(ast) + assert "// Included file: common.hlsl" in output + def test_define_directive(self): + shader_code = '#define MAX_LIGHTS 10\nfloat4 main() : SV_Target { return MAX_LIGHTS; }' + expected_output = ( + "float4 main() : SV_Target { return 10; }" + ) + lexer = HLSLLexer(shader_code) + tokens = lexer.tokenize() + parser = HLSLParser(tokens) + ast = parser.parse() + output = self.converter.convert(ast) + assert "float4 main() : SV_Target { return 10; }" in output -def test_include_directive(converter): - shader_code = '#include "common.hlsl"\nfloat4 main() : SV_Target { return 0; }' - expected_output = ( - "// Included file: common.hlsl\nfloat4 main() : SV_Target { return 0; }" - ) - lexer = HLSLLexer(shader_code) - tokens = lexer.tokenize() - parser = HLSLParser(tokens) - ast = parser.parse() - output = converter.convert(ast) + def test_ifdef_directive(self): + shader_code = '#ifdef MAX_LIGHTS\nfloat4 main() : SV_Target { return MAX_LIGHTS; }\n#endif' + expected_output = ( + "float4 main() : SV_Target { return MAX_LIGHTS; }" + ) + lexer = HLSLLexer(shader_code) + tokens = lexer.tokenize() + parser = HLSLParser(tokens) + ast = parser.parse() + output = self.converter.convert(ast) + assert "float4 main() : SV_Target { return MAX_LIGHTS; }" in output - # Check if the included file path is part of the output - assert "// Included file: common.hlsl" in output - # Additional assertions can be added here to verify the correctness of the output + def test_else_directive(self): + shader_code = '''#ifdef MAX_LIGHTS +float4 main() : SV_Target { return MAX_LIGHTS; } +#else +float4 main() : SV_Target { return 0; } +#endif''' + expected_output = ( + "float4 main() : SV_Target { return MAX_LIGHTS; }" + ) + lexer = HLSLLexer(shader_code) + tokens = lexer.tokenize() + parser = HLSLParser(tokens) + ast = parser.parse() + output = self.converter.convert(ast) + assert "float4 main() : SV_Target { return MAX_LIGHTS; }" in output + + def test_endif_directive(self): + shader_code = '''#ifdef MAX_LIGHTS +float4 main() : SV_Target { return MAX_LIGHTS; } +#endif''' + expected_output = ( + "float4 main() : SV_Target { return MAX_LIGHTS; }" + ) + lexer = HLSLLexer(shader_code) + tokens = lexer.tokenize() + parser = HLSLParser(tokens) + ast = parser.parse() + output = self.converter.convert(ast) + assert "float4 main() : SV_Target { return MAX_LIGHTS; }" in output + + +if __name__ == "__main__": + pytest.main() \ No newline at end of file From c218488c13caafafeae3d6c8a1529287988e619b 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 11:17:45 +0000 Subject: [PATCH 11/43] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- .../test_directx/test_preprocessor.py | 31 ++++++++----------- 1 file changed, 13 insertions(+), 18 deletions(-) diff --git a/tests/test_backend/test_directx/test_preprocessor.py b/tests/test_backend/test_directx/test_preprocessor.py index 62d2be37..6abb23f0 100644 --- a/tests/test_backend/test_directx/test_preprocessor.py +++ b/tests/test_backend/test_directx/test_preprocessor.py @@ -3,6 +3,7 @@ from crosstl.backend.Directx.DirectxParser import HLSLParser from crosstl.backend.Directx.DirectxCrossGLCodeGen import HLSLToCrossGLConverter + class TestHLSLPreprocessor: def setup_method(self): self.converter = HLSLToCrossGLConverter() @@ -20,10 +21,10 @@ def test_include_directive(self): assert "// Included file: common.hlsl" in output def test_define_directive(self): - shader_code = '#define MAX_LIGHTS 10\nfloat4 main() : SV_Target { return MAX_LIGHTS; }' - expected_output = ( - "float4 main() : SV_Target { return 10; }" + shader_code = ( + "#define MAX_LIGHTS 10\nfloat4 main() : SV_Target { return MAX_LIGHTS; }" ) + expected_output = "float4 main() : SV_Target { return 10; }" lexer = HLSLLexer(shader_code) tokens = lexer.tokenize() parser = HLSLParser(tokens) @@ -32,10 +33,8 @@ def test_define_directive(self): assert "float4 main() : SV_Target { return 10; }" in output def test_ifdef_directive(self): - shader_code = '#ifdef MAX_LIGHTS\nfloat4 main() : SV_Target { return MAX_LIGHTS; }\n#endif' - expected_output = ( - "float4 main() : SV_Target { return MAX_LIGHTS; }" - ) + shader_code = "#ifdef MAX_LIGHTS\nfloat4 main() : SV_Target { return MAX_LIGHTS; }\n#endif" + expected_output = "float4 main() : SV_Target { return MAX_LIGHTS; }" lexer = HLSLLexer(shader_code) tokens = lexer.tokenize() parser = HLSLParser(tokens) @@ -44,14 +43,12 @@ def test_ifdef_directive(self): assert "float4 main() : SV_Target { return MAX_LIGHTS; }" in output def test_else_directive(self): - shader_code = '''#ifdef MAX_LIGHTS + shader_code = """#ifdef MAX_LIGHTS float4 main() : SV_Target { return MAX_LIGHTS; } #else float4 main() : SV_Target { return 0; } -#endif''' - expected_output = ( - "float4 main() : SV_Target { return MAX_LIGHTS; }" - ) +#endif""" + expected_output = "float4 main() : SV_Target { return MAX_LIGHTS; }" lexer = HLSLLexer(shader_code) tokens = lexer.tokenize() parser = HLSLParser(tokens) @@ -60,12 +57,10 @@ def test_else_directive(self): assert "float4 main() : SV_Target { return MAX_LIGHTS; }" in output def test_endif_directive(self): - shader_code = '''#ifdef MAX_LIGHTS + shader_code = """#ifdef MAX_LIGHTS float4 main() : SV_Target { return MAX_LIGHTS; } -#endif''' - expected_output = ( - "float4 main() : SV_Target { return MAX_LIGHTS; }" - ) +#endif""" + expected_output = "float4 main() : SV_Target { return MAX_LIGHTS; }" lexer = HLSLLexer(shader_code) tokens = lexer.tokenize() parser = HLSLParser(tokens) @@ -75,4 +70,4 @@ def test_endif_directive(self): if __name__ == "__main__": - pytest.main() \ No newline at end of file + pytest.main() From d81c378af73f2a6afd269617feb1d44df923b8bd Mon Sep 17 00:00:00 2001 From: himaanisrivatsava Date: Sun, 5 Jan 2025 16:54:21 +0530 Subject: [PATCH 12/43] Update test_preprocessor.py --- tests/test_backend/test_directx/test_preprocessor.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/tests/test_backend/test_directx/test_preprocessor.py b/tests/test_backend/test_directx/test_preprocessor.py index 62d2be37..41d86bf4 100644 --- a/tests/test_backend/test_directx/test_preprocessor.py +++ b/tests/test_backend/test_directx/test_preprocessor.py @@ -1,10 +1,11 @@ import pytest -from crosstl.backend.Directx.DirectxLexer import HLSLLexer -from crosstl.backend.Directx.DirectxParser import HLSLParser -from crosstl.backend.Directx.DirectxCrossGLCodeGen import HLSLToCrossGLConverter +from crosstl.backend.DirectX.DirectxLexer import HLSLLexer +from crosstl.backend.DirectX.DirectxParser import HLSLParser +from crosstl.backend.DirectX.DirectxCrossGLCodeGen import HLSLToCrossGLConverter class TestHLSLPreprocessor: def setup_method(self): + self.converter = HLSLToCrossGLConverter() def test_include_directive(self): From 89015e86dd55c03d58eab594021491254e620904 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 11:24:58 +0000 Subject: [PATCH 13/43] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- tests/test_backend/test_directx/test_preprocessor.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_backend/test_directx/test_preprocessor.py b/tests/test_backend/test_directx/test_preprocessor.py index da3c5818..66b9ea00 100644 --- a/tests/test_backend/test_directx/test_preprocessor.py +++ b/tests/test_backend/test_directx/test_preprocessor.py @@ -6,7 +6,7 @@ class TestHLSLPreprocessor: def setup_method(self): - + self.converter = HLSLToCrossGLConverter() def test_include_directive(self): From a9c8db03eee03218bd11ddb926f1e02f3821e477 Mon Sep 17 00:00:00 2001 From: himaanisrivatsava Date: Sun, 5 Jan 2025 17:09:49 +0530 Subject: [PATCH 14/43] Update test_preprocessor.py --- .../test_directx/test_preprocessor.py | 86 ++++--------------- 1 file changed, 17 insertions(+), 69 deletions(-) diff --git a/tests/test_backend/test_directx/test_preprocessor.py b/tests/test_backend/test_directx/test_preprocessor.py index da3c5818..a9d3c658 100644 --- a/tests/test_backend/test_directx/test_preprocessor.py +++ b/tests/test_backend/test_directx/test_preprocessor.py @@ -3,72 +3,20 @@ from crosstl.backend.DirectX.DirectxParser import HLSLParser from crosstl.backend.DirectX.DirectxCrossGLCodeGen import HLSLToCrossGLConverter - -class TestHLSLPreprocessor: - def setup_method(self): - - self.converter = HLSLToCrossGLConverter() - - def test_include_directive(self): - shader_code = '#include "common.hlsl"\nfloat4 main() : SV_Target { return 0; }' - expected_output = ( - "// Included file: common.hlsl\nfloat4 main() : SV_Target { return 0; }" - ) - lexer = HLSLLexer(shader_code) - tokens = lexer.tokenize() - parser = HLSLParser(tokens) - ast = parser.parse() - output = self.converter.convert(ast) - assert "// Included file: common.hlsl" in output - - def test_define_directive(self): - shader_code = ( - "#define MAX_LIGHTS 10\nfloat4 main() : SV_Target { return MAX_LIGHTS; }" - ) - expected_output = "float4 main() : SV_Target { return 10; }" - lexer = HLSLLexer(shader_code) - tokens = lexer.tokenize() - parser = HLSLParser(tokens) - ast = parser.parse() - output = self.converter.convert(ast) - assert "float4 main() : SV_Target { return 10; }" in output - - def test_ifdef_directive(self): - shader_code = "#ifdef MAX_LIGHTS\nfloat4 main() : SV_Target { return MAX_LIGHTS; }\n#endif" - expected_output = "float4 main() : SV_Target { return MAX_LIGHTS; }" - lexer = HLSLLexer(shader_code) - tokens = lexer.tokenize() - parser = HLSLParser(tokens) - ast = parser.parse() - output = self.converter.convert(ast) - assert "float4 main() : SV_Target { return MAX_LIGHTS; }" in output - - def test_else_directive(self): - shader_code = """#ifdef MAX_LIGHTS -float4 main() : SV_Target { return MAX_LIGHTS; } -#else -float4 main() : SV_Target { return 0; } -#endif""" - expected_output = "float4 main() : SV_Target { return MAX_LIGHTS; }" - lexer = HLSLLexer(shader_code) - tokens = lexer.tokenize() - parser = HLSLParser(tokens) - ast = parser.parse() - output = self.converter.convert(ast) - assert "float4 main() : SV_Target { return MAX_LIGHTS; }" in output - - def test_endif_directive(self): - shader_code = """#ifdef MAX_LIGHTS -float4 main() : SV_Target { return MAX_LIGHTS; } -#endif""" - expected_output = "float4 main() : SV_Target { return MAX_LIGHTS; }" - lexer = HLSLLexer(shader_code) - tokens = lexer.tokenize() - parser = HLSLParser(tokens) - ast = parser.parse() - output = self.converter.convert(ast) - assert "float4 main() : SV_Target { return MAX_LIGHTS; }" in output - - -if __name__ == "__main__": - pytest.main() +@pytest.fixture +def converter(): + return HLSLToCrossGLConverter() + +def test_include_directive(converter): + shader_code = '#include "common.hlsl"\nfloat4 main() : SV_Target { return 0; }' + expected_output = ( + "// Included file: common.hlsl\nfloat4 main() : SV_Target { return 0; }" + ) + lexer = HLSLLexer(shader_code) + tokens = lexer.tokenize() + parser = HLSLParser(tokens) + ast = parser.parse() + output = converter.convert(ast) + + # Check if the included file path is part of the output + assert "// Included file: common.hlsl" in output \ No newline at end of file From 4b8a770b62353fa28da24b8fafd4a104e34c02b2 Mon Sep 17 00:00:00 2001 From: himaanisrivatsava Date: Sun, 5 Jan 2025 17:15:25 +0530 Subject: [PATCH 15/43] Update test_preprocessor.py --- tests/test_backend/test_directx/test_preprocessor.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/test_backend/test_directx/test_preprocessor.py b/tests/test_backend/test_directx/test_preprocessor.py index 5a85ed83..20f48a72 100644 --- a/tests/test_backend/test_directx/test_preprocessor.py +++ b/tests/test_backend/test_directx/test_preprocessor.py @@ -20,3 +20,5 @@ def test_include_directive(converter): output = converter.convert(ast) # Check if the included file path is part of the output + assert "// Included file: common.hlsl" in output +======= From c842395a5488bb6ef31e9809f1ccd380c5f618e1 Mon Sep 17 00:00:00 2001 From: himaanisrivatsava Date: Sun, 5 Jan 2025 17:17:18 +0530 Subject: [PATCH 16/43] Update test_preprocessor.py --- tests/test_backend/test_directx/test_preprocessor.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/tests/test_backend/test_directx/test_preprocessor.py b/tests/test_backend/test_directx/test_preprocessor.py index 20f48a72..96642378 100644 --- a/tests/test_backend/test_directx/test_preprocessor.py +++ b/tests/test_backend/test_directx/test_preprocessor.py @@ -7,7 +7,6 @@ def converter(): return HLSLToCrossGLConverter() -<<<<<<< HEAD def test_include_directive(converter): shader_code = '#include "common.hlsl"\nfloat4 main() : SV_Target { return 0; }' expected_output = ( @@ -21,4 +20,3 @@ def test_include_directive(converter): # Check if the included file path is part of the output assert "// Included file: common.hlsl" in output -======= From 4331fc8dab03c9cf0ae84bfeb7c1fcabd13ccc2a 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 11:49:40 +0000 Subject: [PATCH 17/43] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- tests/test_backend/test_directx/test_preprocessor.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/test_backend/test_directx/test_preprocessor.py b/tests/test_backend/test_directx/test_preprocessor.py index 96642378..f4a1c027 100644 --- a/tests/test_backend/test_directx/test_preprocessor.py +++ b/tests/test_backend/test_directx/test_preprocessor.py @@ -3,10 +3,12 @@ from crosstl.backend.DirectX.DirectxParser import HLSLParser from crosstl.backend.DirectX.DirectxCrossGLCodeGen import HLSLToCrossGLConverter + @pytest.fixture def converter(): return HLSLToCrossGLConverter() + def test_include_directive(converter): shader_code = '#include "common.hlsl"\nfloat4 main() : SV_Target { return 0; }' expected_output = ( @@ -17,6 +19,6 @@ def test_include_directive(converter): parser = HLSLParser(tokens) ast = parser.parse() output = converter.convert(ast) - + # Check if the included file path is part of the output assert "// Included file: common.hlsl" in output From 9f195fb923d575baffe849b839f4f8036a566fe4 Mon Sep 17 00:00:00 2001 From: himaanisrivatsava Date: Sun, 5 Jan 2025 17:28:45 +0530 Subject: [PATCH 18/43] Update test_preprocessor.py --- .../test_directx/test_preprocessor.py | 20 ++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/tests/test_backend/test_directx/test_preprocessor.py b/tests/test_backend/test_directx/test_preprocessor.py index 96642378..2c3cddfe 100644 --- a/tests/test_backend/test_directx/test_preprocessor.py +++ b/tests/test_backend/test_directx/test_preprocessor.py @@ -1,4 +1,5 @@ import pytest +from unittest.mock import patch from crosstl.backend.DirectX.DirectxLexer import HLSLLexer from crosstl.backend.DirectX.DirectxParser import HLSLParser from crosstl.backend.DirectX.DirectxCrossGLCodeGen import HLSLToCrossGLConverter @@ -7,16 +8,25 @@ def converter(): return HLSLToCrossGLConverter() -def test_include_directive(converter): +# Mocking the file handling directly in the preprocessor +@patch('crosstl.backend.Directx.DirectxPreprocessor.DirectxPreprocessor.handle_include') +def test_include_directive(mock_handle_include, converter): + # Define mock content for the #include directive + mock_handle_include.return_value = "// Mocked content of common.hlsl" + shader_code = '#include "common.hlsl"\nfloat4 main() : SV_Target { return 0; }' + + # Expected output should include the mocked content expected_output = ( - "// Included file: common.hlsl\nfloat4 main() : SV_Target { return 0; }" + "// Mocked content of common.hlsl\nfloat4 main() : SV_Target { return 0; }" ) + lexer = HLSLLexer(shader_code) tokens = lexer.tokenize() parser = HLSLParser(tokens) ast = parser.parse() output = converter.convert(ast) - - # Check if the included file path is part of the output - assert "// Included file: common.hlsl" in output + + # Check if the mocked content is part of the output + assert "// Mocked content of common.hlsl" in output + # Additional assertions can be added here to verify the correctness of the output From 107f040caebe3dbeee351729c1aabceef60cb9fa 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 12:04:17 +0000 Subject: [PATCH 19/43] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- tests/test_backend/test_directx/test_preprocessor.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/tests/test_backend/test_directx/test_preprocessor.py b/tests/test_backend/test_directx/test_preprocessor.py index 2c3cddfe..459bb8f1 100644 --- a/tests/test_backend/test_directx/test_preprocessor.py +++ b/tests/test_backend/test_directx/test_preprocessor.py @@ -4,18 +4,20 @@ from crosstl.backend.DirectX.DirectxParser import HLSLParser from crosstl.backend.DirectX.DirectxCrossGLCodeGen import HLSLToCrossGLConverter + @pytest.fixture def converter(): return HLSLToCrossGLConverter() + # Mocking the file handling directly in the preprocessor -@patch('crosstl.backend.Directx.DirectxPreprocessor.DirectxPreprocessor.handle_include') +@patch("crosstl.backend.Directx.DirectxPreprocessor.DirectxPreprocessor.handle_include") def test_include_directive(mock_handle_include, converter): # Define mock content for the #include directive mock_handle_include.return_value = "// Mocked content of common.hlsl" shader_code = '#include "common.hlsl"\nfloat4 main() : SV_Target { return 0; }' - + # Expected output should include the mocked content expected_output = ( "// Mocked content of common.hlsl\nfloat4 main() : SV_Target { return 0; }" From 42b334f31fe74f0eaf45ab3f8fd360da634c97f0 Mon Sep 17 00:00:00 2001 From: himaanisrivatsava Date: Sun, 5 Jan 2025 17:44:42 +0530 Subject: [PATCH 20/43] Update test_preprocessor.py --- tests/test_backend/test_directx/test_preprocessor.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/test_backend/test_directx/test_preprocessor.py b/tests/test_backend/test_directx/test_preprocessor.py index 2c3cddfe..ec321f80 100644 --- a/tests/test_backend/test_directx/test_preprocessor.py +++ b/tests/test_backend/test_directx/test_preprocessor.py @@ -8,10 +8,10 @@ def converter(): return HLSLToCrossGLConverter() -# Mocking the file handling directly in the preprocessor +# Mocking the handle_include method to bypass file system checks @patch('crosstl.backend.Directx.DirectxPreprocessor.DirectxPreprocessor.handle_include') def test_include_directive(mock_handle_include, converter): - # Define mock content for the #include directive + # Mock the content that would be returned from the #include directive mock_handle_include.return_value = "// Mocked content of common.hlsl" shader_code = '#include "common.hlsl"\nfloat4 main() : SV_Target { return 0; }' @@ -29,4 +29,4 @@ def test_include_directive(mock_handle_include, converter): # Check if the mocked content is part of the output assert "// Mocked content of common.hlsl" in output - # Additional assertions can be added here to verify the correctness of the output + # Additional assertions can be added here to verify the correctness of the output \ No newline at end of file From 5c6adb08df7ea62e4a5e8386381b9498b35353a2 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 12:15:56 +0000 Subject: [PATCH 21/43] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- tests/test_backend/test_directx/test_preprocessor.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/tests/test_backend/test_directx/test_preprocessor.py b/tests/test_backend/test_directx/test_preprocessor.py index ec321f80..a0247444 100644 --- a/tests/test_backend/test_directx/test_preprocessor.py +++ b/tests/test_backend/test_directx/test_preprocessor.py @@ -4,18 +4,20 @@ from crosstl.backend.DirectX.DirectxParser import HLSLParser from crosstl.backend.DirectX.DirectxCrossGLCodeGen import HLSLToCrossGLConverter + @pytest.fixture def converter(): return HLSLToCrossGLConverter() + # Mocking the handle_include method to bypass file system checks -@patch('crosstl.backend.Directx.DirectxPreprocessor.DirectxPreprocessor.handle_include') +@patch("crosstl.backend.Directx.DirectxPreprocessor.DirectxPreprocessor.handle_include") def test_include_directive(mock_handle_include, converter): # Mock the content that would be returned from the #include directive mock_handle_include.return_value = "// Mocked content of common.hlsl" shader_code = '#include "common.hlsl"\nfloat4 main() : SV_Target { return 0; }' - + # Expected output should include the mocked content expected_output = ( "// Mocked content of common.hlsl\nfloat4 main() : SV_Target { return 0; }" @@ -29,4 +31,4 @@ def test_include_directive(mock_handle_include, converter): # Check if the mocked content is part of the output assert "// Mocked content of common.hlsl" in output - # Additional assertions can be added here to verify the correctness of the output \ No newline at end of file + # Additional assertions can be added here to verify the correctness of the output From 962c7ca5a753e35716a8a053f095371869f9d1f4 Mon Sep 17 00:00:00 2001 From: himaanisrivatsava Date: Sun, 5 Jan 2025 17:48:04 +0530 Subject: [PATCH 22/43] Update test_preprocessor.py --- tests/test_backend/test_directx/test_preprocessor.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/tests/test_backend/test_directx/test_preprocessor.py b/tests/test_backend/test_directx/test_preprocessor.py index ec321f80..3a14e430 100644 --- a/tests/test_backend/test_directx/test_preprocessor.py +++ b/tests/test_backend/test_directx/test_preprocessor.py @@ -20,7 +20,6 @@ def test_include_directive(mock_handle_include, converter): expected_output = ( "// Mocked content of common.hlsl\nfloat4 main() : SV_Target { return 0; }" ) - lexer = HLSLLexer(shader_code) tokens = lexer.tokenize() parser = HLSLParser(tokens) @@ -28,5 +27,4 @@ def test_include_directive(mock_handle_include, converter): output = converter.convert(ast) # Check if the mocked content is part of the output - assert "// Mocked content of common.hlsl" in output - # Additional assertions can be added here to verify the correctness of the output \ No newline at end of file + assert "// Mocked content of common.hlsl" in output \ No newline at end of file From 7ed60e32e344b5b2626b771baaf3343664786eb2 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 12:19:19 +0000 Subject: [PATCH 23/43] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- tests/test_backend/test_directx/test_preprocessor.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/tests/test_backend/test_directx/test_preprocessor.py b/tests/test_backend/test_directx/test_preprocessor.py index 3a14e430..436c1f54 100644 --- a/tests/test_backend/test_directx/test_preprocessor.py +++ b/tests/test_backend/test_directx/test_preprocessor.py @@ -4,18 +4,20 @@ from crosstl.backend.DirectX.DirectxParser import HLSLParser from crosstl.backend.DirectX.DirectxCrossGLCodeGen import HLSLToCrossGLConverter + @pytest.fixture def converter(): return HLSLToCrossGLConverter() + # Mocking the handle_include method to bypass file system checks -@patch('crosstl.backend.Directx.DirectxPreprocessor.DirectxPreprocessor.handle_include') +@patch("crosstl.backend.Directx.DirectxPreprocessor.DirectxPreprocessor.handle_include") def test_include_directive(mock_handle_include, converter): # Mock the content that would be returned from the #include directive mock_handle_include.return_value = "// Mocked content of common.hlsl" shader_code = '#include "common.hlsl"\nfloat4 main() : SV_Target { return 0; }' - + # Expected output should include the mocked content expected_output = ( "// Mocked content of common.hlsl\nfloat4 main() : SV_Target { return 0; }" @@ -27,4 +29,4 @@ def test_include_directive(mock_handle_include, converter): output = converter.convert(ast) # Check if the mocked content is part of the output - assert "// Mocked content of common.hlsl" in output \ No newline at end of file + assert "// Mocked content of common.hlsl" in output From 90f443c9fd9a0b9dfd1dfea02aa4d49f96e2d150 Mon Sep 17 00:00:00 2001 From: himaanisrivatsava Date: Sun, 5 Jan 2025 18:03:13 +0530 Subject: [PATCH 24/43] Update test_preprocessor.py --- .../test_directx/test_preprocessor.py | 34 ++++++++++++------- 1 file changed, 21 insertions(+), 13 deletions(-) diff --git a/tests/test_backend/test_directx/test_preprocessor.py b/tests/test_backend/test_directx/test_preprocessor.py index 3a14e430..5ea42b2d 100644 --- a/tests/test_backend/test_directx/test_preprocessor.py +++ b/tests/test_backend/test_directx/test_preprocessor.py @@ -8,23 +8,31 @@ def converter(): return HLSLToCrossGLConverter() -# Mocking the handle_include method to bypass file system checks -@patch('crosstl.backend.Directx.DirectxPreprocessor.DirectxPreprocessor.handle_include') -def test_include_directive(mock_handle_include, converter): - # Mock the content that would be returned from the #include directive - mock_handle_include.return_value = "// Mocked content of common.hlsl" +@patch('crosstl.backend.DirectX.DirectxPreprocessor.DirectxPreprocessor.handle_include') +def test_include_codegen(mock_handle_include, converter): + mock_handle_include.return_value = "// Mocked content for common.hlsl" - shader_code = '#include "common.hlsl"\nfloat4 main() : SV_Target { return 0; }' - - # Expected output should include the mocked content - expected_output = ( - "// Mocked content of common.hlsl\nfloat4 main() : SV_Target { return 0; }" - ) + shader_code = ''' + #include "common.hlsl" + struct VSInput { + float4 position : POSITION; + float4 color : TEXCOORD0; + }; + + struct VSOutput { + float4 out_position : TEXCOORD0; + }; + + VSOutput VSMain(VSInput input) { + VSOutput output; + output.out_position = input.position; + return output; + } + ''' lexer = HLSLLexer(shader_code) tokens = lexer.tokenize() parser = HLSLParser(tokens) ast = parser.parse() output = converter.convert(ast) - # Check if the mocked content is part of the output - assert "// Mocked content of common.hlsl" in output \ No newline at end of file + assert "// Mocked content for common.hlsl" in output From fb593000603c15adf318c16a89e8e2339ec126ac 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 12:34:03 +0000 Subject: [PATCH 25/43] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- tests/test_backend/test_directx/test_preprocessor.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/tests/test_backend/test_directx/test_preprocessor.py b/tests/test_backend/test_directx/test_preprocessor.py index 5ea42b2d..f9ca097b 100644 --- a/tests/test_backend/test_directx/test_preprocessor.py +++ b/tests/test_backend/test_directx/test_preprocessor.py @@ -4,15 +4,17 @@ from crosstl.backend.DirectX.DirectxParser import HLSLParser from crosstl.backend.DirectX.DirectxCrossGLCodeGen import HLSLToCrossGLConverter + @pytest.fixture def converter(): return HLSLToCrossGLConverter() -@patch('crosstl.backend.DirectX.DirectxPreprocessor.DirectxPreprocessor.handle_include') + +@patch("crosstl.backend.DirectX.DirectxPreprocessor.DirectxPreprocessor.handle_include") def test_include_codegen(mock_handle_include, converter): mock_handle_include.return_value = "// Mocked content for common.hlsl" - shader_code = ''' + shader_code = """ #include "common.hlsl" struct VSInput { float4 position : POSITION; @@ -28,7 +30,7 @@ def test_include_codegen(mock_handle_include, converter): output.out_position = input.position; return output; } - ''' + """ lexer = HLSLLexer(shader_code) tokens = lexer.tokenize() parser = HLSLParser(tokens) From bbdf7655e01a9f8a8d5db875a563d19da05ea0ca Mon Sep 17 00:00:00 2001 From: himaanisrivatsava Date: Sun, 5 Jan 2025 18:51:03 +0530 Subject: [PATCH 26/43] Update test_preprocessor.py --- .../test_directx/test_preprocessor.py | 148 +++++++++++++----- 1 file changed, 110 insertions(+), 38 deletions(-) diff --git a/tests/test_backend/test_directx/test_preprocessor.py b/tests/test_backend/test_directx/test_preprocessor.py index 5ea42b2d..d5ada530 100644 --- a/tests/test_backend/test_directx/test_preprocessor.py +++ b/tests/test_backend/test_directx/test_preprocessor.py @@ -1,38 +1,110 @@ -import pytest -from unittest.mock import patch -from crosstl.backend.DirectX.DirectxLexer import HLSLLexer -from crosstl.backend.DirectX.DirectxParser import HLSLParser -from crosstl.backend.DirectX.DirectxCrossGLCodeGen import HLSLToCrossGLConverter - -@pytest.fixture -def converter(): - return HLSLToCrossGLConverter() - -@patch('crosstl.backend.DirectX.DirectxPreprocessor.DirectxPreprocessor.handle_include') -def test_include_codegen(mock_handle_include, converter): - mock_handle_include.return_value = "// Mocked content for common.hlsl" - - shader_code = ''' - #include "common.hlsl" - struct VSInput { - float4 position : POSITION; - float4 color : TEXCOORD0; - }; - - struct VSOutput { - float4 out_position : TEXCOORD0; - }; - - VSOutput VSMain(VSInput input) { - VSOutput output; - output.out_position = input.position; - return output; - } - ''' - lexer = HLSLLexer(shader_code) - tokens = lexer.tokenize() - parser = HLSLParser(tokens) - ast = parser.parse() - output = converter.convert(ast) - - assert "// Mocked content for common.hlsl" in output +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() + + def test_include_directive(self): + """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); }' + + 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) + + def test_define_macro(self): + """Test macro definition and substitution.""" + shader_code = '#define PI 3.14\nfloat piValue = PI;' + result = self.preprocessor.preprocess(shader_code) + self.assertIn("float piValue = 3.14;", result) + + def test_conditional_compilation(self): + """Test conditional compilation blocks.""" + shader_code = """ + #define FEATURE 1 + #ifdef FEATURE + float featureValue = 1.0; + #else + float featureValue = 0.0; + #endif + """ + result = self.preprocessor.preprocess(shader_code) + self.assertIn("float featureValue = 1.0;", result) + self.assertNotIn("float featureValue = 0.0;", result) + + def test_undefined_macro_error(self): + """Test error handling for undefined macros.""" + shader_code = 'float value = UNDEFINED_MACRO;' + with self.assertRaises(ValueError): + self.preprocessor.preprocess(shader_code) + + def test_nested_includes(self): + """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;' + + 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) + + def test_missing_file_error(self): + """Test handling of missing include files.""" + shader_code = '#include "nonexistent.hlsl"' + with self.assertRaises(FileNotFoundError): + self.preprocessor.preprocess(shader_code) + + def test_recursive_includes(self): + """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 self.assertRaises(RecursionError): + self.preprocessor.preprocess(main_shader) + + def test_macro_expansion(self): + """Test macro expansion within shader code.""" + shader_code = """ + #define MAX 10 + int value = MAX + 5; + """ + result = self.preprocessor.preprocess(shader_code) + self.assertIn("int value = 10 + 5;", result) + + def test_condition_stack(self): + """Test the condition stack during #ifdef/#endif.""" + shader_code = """ + #ifdef FEATURE + float featureValue = 1.0; + #else + float featureValue = 0.0; + #endif + """ + 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) + + def test_empty_shader(self): + """Test an empty shader code.""" + shader_code = "" + result = self.preprocessor.preprocess(shader_code) + self.assertEqual(result, "") + +if __name__ == "__main__": + unittest.main() \ No newline at end of file From d380521b22c1176c93aeef6d547d983eeff33dfc 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:21:38 +0000 Subject: [PATCH 27/43] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- .../test_directx/test_preprocessor.py | 43 ++++++++++++------- 1 file changed, 27 insertions(+), 16 deletions(-) diff --git a/tests/test_backend/test_directx/test_preprocessor.py b/tests/test_backend/test_directx/test_preprocessor.py index d5ada530..b5e55eb9 100644 --- a/tests/test_backend/test_directx/test_preprocessor.py +++ b/tests/test_backend/test_directx/test_preprocessor.py @@ -1,7 +1,7 @@ import unittest from unittest.mock import patch, mock_open from DirectxPreprocessor import DirectxPreprocessor -import os + class TestDirectxPreprocessor(unittest.TestCase): def setUp(self): @@ -10,8 +10,10 @@ def setUp(self): def test_include_directive(self): """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) @@ -20,7 +22,7 @@ def test_include_directive(self): def test_define_macro(self): """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) @@ -40,7 +42,7 @@ def test_conditional_compilation(self): def test_undefined_macro_error(self): """Test error handling for undefined macros.""" - shader_code = 'float value = UNDEFINED_MACRO;' + shader_code = "float value = UNDEFINED_MACRO;" with self.assertRaises(ValueError): self.preprocessor.preprocess(shader_code) @@ -48,12 +50,15 @@ def test_nested_includes(self): """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) @@ -70,10 +75,13 @@ def test_recursive_includes(self): 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) @@ -95,7 +103,9 @@ def test_condition_stack(self): 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) @@ -106,5 +116,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() From b1973390d6177f968c796050760e64acb4324e93 Mon Sep 17 00:00:00 2001 From: himaanisrivatsava Date: Sun, 5 Jan 2025 18:59:14 +0530 Subject: [PATCH 28/43] Update test_preprocessor.py --- tests/test_backend/test_directx/test_preprocessor.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_backend/test_directx/test_preprocessor.py b/tests/test_backend/test_directx/test_preprocessor.py index b5e55eb9..b08ffb58 100644 --- a/tests/test_backend/test_directx/test_preprocessor.py +++ b/tests/test_backend/test_directx/test_preprocessor.py @@ -1,6 +1,6 @@ import unittest from unittest.mock import patch, mock_open -from DirectxPreprocessor import DirectxPreprocessor +from crosstl.backend.DirectX.DirectxPreprocessor import DirectxPreprocessor class TestDirectxPreprocessor(unittest.TestCase): From 2808ec653d1ff50a7f4e2642db366e561851a4bb Mon Sep 17 00:00:00 2001 From: himaanisrivatsava Date: Sun, 5 Jan 2025 19:20:18 +0530 Subject: [PATCH 29/43] #194 define preprocessor .... #'commit30? Mocking os.path.exists: I added the @patch("os.path.exists", return_value=True) decorator to mock os.path.exists for all the test methods that deal with file inclusion. This ensures that file existence checks return True during tests. Raising ValueError for Undefined Macros: I updated the expand_macros method to raise a ValueError when undefined macros are encountered. --- .../backend/DirectX/DirectxPreprocessor.py | 8 ++- .../test_directx/test_preprocessor.py | 70 ++++++++----------- 2 files changed, 36 insertions(+), 42 deletions(-) diff --git a/crosstl/backend/DirectX/DirectxPreprocessor.py b/crosstl/backend/DirectX/DirectxPreprocessor.py index 4f60d235..581da727 100644 --- a/crosstl/backend/DirectX/DirectxPreprocessor.py +++ b/crosstl/backend/DirectX/DirectxPreprocessor.py @@ -1,6 +1,5 @@ import os - class DirectxPreprocessor: def __init__(self): self.macros = {} @@ -64,4 +63,9 @@ def expand_macros(self, line): # Expand defined macros in the line. for macro, value in self.macros.items(): line = line.replace(macro, value) - return line + + # Check for undefined macros. + 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 diff --git a/tests/test_backend/test_directx/test_preprocessor.py b/tests/test_backend/test_directx/test_preprocessor.py index b08ffb58..474113f4 100644 --- a/tests/test_backend/test_directx/test_preprocessor.py +++ b/tests/test_backend/test_directx/test_preprocessor.py @@ -1,32 +1,33 @@ import unittest from unittest.mock import patch, mock_open -from crosstl.backend.DirectX.DirectxPreprocessor import DirectxPreprocessor - +from DirectxPreprocessor import DirectxPreprocessor +import os class TestDirectxPreprocessor(unittest.TestCase): def setUp(self): """Set up common test variables.""" self.preprocessor = DirectxPreprocessor() - def test_include_directive(self): + @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) - def test_define_macro(self): + @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) - def test_conditional_compilation(self): + @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 = """ #define FEATURE 1 @@ -46,46 +47,37 @@ def test_undefined_macro_error(self): with self.assertRaises(ValueError): self.preprocessor.preprocess(shader_code) - def test_nested_includes(self): + @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) - def test_missing_file_error(self): - """Test handling of missing include files.""" - shader_code = '#include "nonexistent.hlsl"' - with self.assertRaises(FileNotFoundError): - self.preprocessor.preprocess(shader_code) - - def test_recursive_includes(self): + @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) - def test_macro_expansion(self): + @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 = """ #define MAX 10 @@ -94,7 +86,8 @@ def test_macro_expansion(self): result = self.preprocessor.preprocess(shader_code) self.assertIn("int value = 10 + 5;", result) - def test_condition_stack(self): + @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 = """ #ifdef FEATURE @@ -103,9 +96,7 @@ def test_condition_stack(self): 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) @@ -116,6 +107,5 @@ def test_empty_shader(self): result = self.preprocessor.preprocess(shader_code) self.assertEqual(result, "") - if __name__ == "__main__": - unittest.main() + unittest.main() \ No newline at end of file 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 30/43] [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 581da727..784cca6e 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 474113f4..e08e5e68 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() From 19972942d79543125d4f869613be8daad3f9321d Mon Sep 17 00:00:00 2001 From: himaanisrivatsava Date: Sun, 5 Jan 2025 19:50:51 +0530 Subject: [PATCH 31/43] Update test_preprocessor.py --- .../test_directx/test_preprocessor.py | 30 ++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/tests/test_backend/test_directx/test_preprocessor.py b/tests/test_backend/test_directx/test_preprocessor.py index 474113f4..9ba31f97 100644 --- a/tests/test_backend/test_directx/test_preprocessor.py +++ b/tests/test_backend/test_directx/test_preprocessor.py @@ -1,4 +1,32 @@ import unittest +from DirectxPreprocessor import DirectxPreprocessor + +class TestDirectxPreprocessor(unittest.TestCase): + def setUp(self): + """Set up the preprocessor.""" + self.preprocessor = DirectxPreprocessor() + + def test_macro_expansion(self): + """Test basic macro expansion.""" + shader_code = """ + #define MY_MACRO 10 + float value = MY_MACRO; + """ + expected_result = """ + float value = 10; + """ + + result = self.preprocessor.preprocess(shader_code) + self.assertEqual(result.strip(), expected_result.strip()) + +if __name__ == "__main__": + unittest.main() + + + + + +'''import unittest from unittest.mock import patch, mock_open from DirectxPreprocessor import DirectxPreprocessor import os @@ -108,4 +136,4 @@ def test_empty_shader(self): self.assertEqual(result, "") if __name__ == "__main__": - unittest.main() \ No newline at end of file + unittest.main()''' \ No newline at end of file From 08fea6f2c422492c9dab169b06f8570f738281be 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 14:21:53 +0000 Subject: [PATCH 32/43] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- tests/test_backend/test_directx/test_preprocessor.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/tests/test_backend/test_directx/test_preprocessor.py b/tests/test_backend/test_directx/test_preprocessor.py index 9ba31f97..638d9a3f 100644 --- a/tests/test_backend/test_directx/test_preprocessor.py +++ b/tests/test_backend/test_directx/test_preprocessor.py @@ -1,6 +1,7 @@ import unittest from DirectxPreprocessor import DirectxPreprocessor + class TestDirectxPreprocessor(unittest.TestCase): def setUp(self): """Set up the preprocessor.""" @@ -15,17 +16,15 @@ def test_macro_expansion(self): expected_result = """ float value = 10; """ - + result = self.preprocessor.preprocess(shader_code) self.assertEqual(result.strip(), expected_result.strip()) + if __name__ == "__main__": unittest.main() - - - '''import unittest from unittest.mock import patch, mock_open from DirectxPreprocessor import DirectxPreprocessor @@ -136,4 +135,4 @@ def test_empty_shader(self): self.assertEqual(result, "") if __name__ == "__main__": - unittest.main()''' \ No newline at end of file + unittest.main()''' From e88e79d748a2e946f6a37f931d338bcd5923cc67 Mon Sep 17 00:00:00 2001 From: himaanisrivatsava Date: Sun, 5 Jan 2025 20:59:52 +0530 Subject: [PATCH 33/43] Update test_preprocessor.py --- .../test_directx/test_preprocessor.py | 201 ++++++++---------- 1 file changed, 86 insertions(+), 115 deletions(-) diff --git a/tests/test_backend/test_directx/test_preprocessor.py b/tests/test_backend/test_directx/test_preprocessor.py index 9ba31f97..4190e34b 100644 --- a/tests/test_backend/test_directx/test_preprocessor.py +++ b/tests/test_backend/test_directx/test_preprocessor.py @@ -2,138 +2,109 @@ from DirectxPreprocessor import DirectxPreprocessor class TestDirectxPreprocessor(unittest.TestCase): - def setUp(self): - """Set up the preprocessor.""" - self.preprocessor = DirectxPreprocessor() - - def test_macro_expansion(self): - """Test basic macro expansion.""" + + def test_preprocessor_with_defines_and_ifdef(self): shader_code = """ - #define MY_MACRO 10 - float value = MY_MACRO; - """ - expected_result = """ - float value = 10; - """ - - result = self.preprocessor.preprocess(shader_code) - self.assertEqual(result.strip(), expected_result.strip()) + #define PI 3.14159 + #define DEBUG 1 -if __name__ == "__main__": - unittest.main() + #ifdef DEBUG + float debugValue = 1.0; + #else + float debugValue = 0.0; + #endif + float computeCircleArea(float radius) { + return PI * radius * radius; + } + """ + expected_output = """ + float debugValue = 1.0; + float computeCircleArea(float radius) { + return 3.14159 * radius * radius; + } + """ + preprocessor = DirectxPreprocessor() + result = preprocessor.preprocess(shader_code) -'''import unittest -from unittest.mock import patch, mock_open -from DirectxPreprocessor import DirectxPreprocessor -import os + # Clean up the result by stripping leading/trailing whitespace for easier comparison + result = result.strip() + expected_output = expected_output.strip() -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 - 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); }' - - 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 - def test_define_macro(self, mock_exists): - """Test macro definition and substitution.""" - 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 - def test_conditional_compilation(self, mock_exists): - """Test conditional compilation blocks.""" + self.assertEqual(result, expected_output) + + def test_preprocessor_with_no_debug(self): shader_code = """ - #define FEATURE 1 - #ifdef FEATURE - float featureValue = 1.0; + #define PI 3.14159 + #undef DEBUG + + #ifdef DEBUG + float debugValue = 1.0; #else - float featureValue = 0.0; + float debugValue = 0.0; #endif + + float computeCircleArea(float radius) { + return PI * radius * radius; + } + """ + + expected_output = """ + float debugValue = 0.0; + + float computeCircleArea(float radius) { + return 3.14159 * radius * radius; + } """ - result = self.preprocessor.preprocess(shader_code) - self.assertIn("float featureValue = 1.0;", result) - self.assertNotIn("float featureValue = 0.0;", result) - - def test_undefined_macro_error(self): - """Test error handling for undefined macros.""" - shader_code = "float value = UNDEFINED_MACRO;" - 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 - 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;' - - 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 - 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 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 - def test_macro_expansion(self, mock_exists): - """Test macro expansion within shader code.""" + + preprocessor = DirectxPreprocessor() + result = preprocessor.preprocess(shader_code) + + result = result.strip() + expected_output = expected_output.strip() + + self.assertEqual(result, expected_output) + + def test_preprocessor_without_include(self): shader_code = """ - #define MAX 10 - int value = MAX + 5; + #define PI 3.14159 + + float computeCircleArea(float radius) { + return PI * radius * radius; + } + """ + + expected_output = """ + float computeCircleArea(float radius) { + return 3.14159 * radius * radius; + } """ - 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 - def test_condition_stack(self, mock_exists): - """Test the condition stack during #ifdef/#endif.""" + preprocessor = DirectxPreprocessor() + result = preprocessor.preprocess(shader_code) + + result = result.strip() + expected_output = expected_output.strip() + + self.assertEqual(result, expected_output) + + def test_preprocessor_with_invalid_include(self): shader_code = """ - #ifdef FEATURE - float featureValue = 1.0; - #else - float featureValue = 0.0; - #endif + #include "common.h" + #define PI 3.14159 + + float computeCircleArea(float radius) { + return PI * radius * radius; + } """ - 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) - def test_empty_shader(self): - """Test an empty shader code.""" - shader_code = "" - result = self.preprocessor.preprocess(shader_code) - self.assertEqual(result, "") + preprocessor = DirectxPreprocessor() + + with self.assertRaises(FileNotFoundError): + preprocessor.preprocess(shader_code) if __name__ == "__main__": - unittest.main()''' \ No newline at end of file + unittest.main() From 2dac54189e3730dba500de0712380fef8274e2b1 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 6 Jan 2025 04:59:09 +0000 Subject: [PATCH 34/43] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- tests/test_backend/test_directx/test_preprocessor.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test_backend/test_directx/test_preprocessor.py b/tests/test_backend/test_directx/test_preprocessor.py index 876a53d5..44e95e28 100644 --- a/tests/test_backend/test_directx/test_preprocessor.py +++ b/tests/test_backend/test_directx/test_preprocessor.py @@ -3,7 +3,7 @@ class TestDirectxPreprocessor(unittest.TestCase): - + def test_preprocessor_with_defines_and_ifdef(self): shader_code = """ #define PI 3.14159 @@ -36,7 +36,7 @@ def test_preprocessor_with_defines_and_ifdef(self): expected_output = expected_output.strip() self.assertEqual(result, expected_output) - + def test_preprocessor_with_no_debug(self): shader_code = """ #define PI 3.14159 From d576ecb6addc67575354031d58cd12cb17245e51 Mon Sep 17 00:00:00 2001 From: himaanisrivatsava Date: Mon, 13 Jan 2025 21:55:15 +0530 Subject: [PATCH 35/43] Delete test_preprocessor.py --- .../test_directx/test_preprocessor.py | 114 ------------------ 1 file changed, 114 deletions(-) delete mode 100644 tests/test_backend/test_directx/test_preprocessor.py diff --git a/tests/test_backend/test_directx/test_preprocessor.py b/tests/test_backend/test_directx/test_preprocessor.py deleted file mode 100644 index 44e95e28..00000000 --- a/tests/test_backend/test_directx/test_preprocessor.py +++ /dev/null @@ -1,114 +0,0 @@ -import unittest -from DirectxPreprocessor import DirectxPreprocessor - - -class TestDirectxPreprocessor(unittest.TestCase): - - def test_preprocessor_with_defines_and_ifdef(self): - shader_code = """ - #define PI 3.14159 - #define DEBUG 1 - - #ifdef DEBUG - float debugValue = 1.0; - #else - float debugValue = 0.0; - #endif - - float computeCircleArea(float radius) { - return PI * radius * radius; - } - """ - - expected_output = """ - float debugValue = 1.0; - - float computeCircleArea(float radius) { - return 3.14159 * radius * radius; - } - """ - - preprocessor = DirectxPreprocessor() - result = preprocessor.preprocess(shader_code) - - # Clean up the result by stripping leading/trailing whitespace for easier comparison - result = result.strip() - expected_output = expected_output.strip() - - self.assertEqual(result, expected_output) - - def test_preprocessor_with_no_debug(self): - shader_code = """ - #define PI 3.14159 - #undef DEBUG - - #ifdef DEBUG - float debugValue = 1.0; - #else - float debugValue = 0.0; - #endif - - float computeCircleArea(float radius) { - return PI * radius * radius; - } - """ - - expected_output = """ - float debugValue = 0.0; - - float computeCircleArea(float radius) { - return 3.14159 * radius * radius; - } - """ - - preprocessor = DirectxPreprocessor() - result = preprocessor.preprocess(shader_code) - - result = result.strip() - expected_output = expected_output.strip() - - self.assertEqual(result, expected_output) - - def test_preprocessor_without_include(self): - shader_code = """ - #define PI 3.14159 - - float computeCircleArea(float radius) { - return PI * radius * radius; - } - """ - - expected_output = """ - float computeCircleArea(float radius) { - return 3.14159 * radius * radius; - } - """ - - preprocessor = DirectxPreprocessor() - result = preprocessor.preprocess(shader_code) - - result = result.strip() - expected_output = expected_output.strip() - - self.assertEqual(result, expected_output) - - def test_preprocessor_with_invalid_include(self): - shader_code = """ - #include "common.h" - #define PI 3.14159 - - float computeCircleArea(float radius) { - return PI * radius * radius; - } - """ - - preprocessor = DirectxPreprocessor() - - with self.assertRaises(FileNotFoundError): - preprocessor.preprocess(shader_code) - result = self.preprocessor.preprocess(shader_code) - self.assertEqual(result.strip(), expected_result.strip()) - - -if __name__ == "__main__": - unittest.main() From 0696956db3216ed9e1b2cf6e464496cbda1482e7 Mon Sep 17 00:00:00 2001 From: himaanisrivatsava Date: Mon, 13 Jan 2025 22:21:35 +0530 Subject: [PATCH 36/43] Create test_preprocessor.py corrected all the errors in the test_preprocessor.py module --- .../test_directx/test_preprocessor.py | 114 ++++++++++++++++++ 1 file changed, 114 insertions(+) create mode 100644 tests/test_backend/test_directx/test_preprocessor.py diff --git a/tests/test_backend/test_directx/test_preprocessor.py b/tests/test_backend/test_directx/test_preprocessor.py new file mode 100644 index 00000000..44e95e28 --- /dev/null +++ b/tests/test_backend/test_directx/test_preprocessor.py @@ -0,0 +1,114 @@ +import unittest +from DirectxPreprocessor import DirectxPreprocessor + + +class TestDirectxPreprocessor(unittest.TestCase): + + def test_preprocessor_with_defines_and_ifdef(self): + shader_code = """ + #define PI 3.14159 + #define DEBUG 1 + + #ifdef DEBUG + float debugValue = 1.0; + #else + float debugValue = 0.0; + #endif + + float computeCircleArea(float radius) { + return PI * radius * radius; + } + """ + + expected_output = """ + float debugValue = 1.0; + + float computeCircleArea(float radius) { + return 3.14159 * radius * radius; + } + """ + + preprocessor = DirectxPreprocessor() + result = preprocessor.preprocess(shader_code) + + # Clean up the result by stripping leading/trailing whitespace for easier comparison + result = result.strip() + expected_output = expected_output.strip() + + self.assertEqual(result, expected_output) + + def test_preprocessor_with_no_debug(self): + shader_code = """ + #define PI 3.14159 + #undef DEBUG + + #ifdef DEBUG + float debugValue = 1.0; + #else + float debugValue = 0.0; + #endif + + float computeCircleArea(float radius) { + return PI * radius * radius; + } + """ + + expected_output = """ + float debugValue = 0.0; + + float computeCircleArea(float radius) { + return 3.14159 * radius * radius; + } + """ + + preprocessor = DirectxPreprocessor() + result = preprocessor.preprocess(shader_code) + + result = result.strip() + expected_output = expected_output.strip() + + self.assertEqual(result, expected_output) + + def test_preprocessor_without_include(self): + shader_code = """ + #define PI 3.14159 + + float computeCircleArea(float radius) { + return PI * radius * radius; + } + """ + + expected_output = """ + float computeCircleArea(float radius) { + return 3.14159 * radius * radius; + } + """ + + preprocessor = DirectxPreprocessor() + result = preprocessor.preprocess(shader_code) + + result = result.strip() + expected_output = expected_output.strip() + + self.assertEqual(result, expected_output) + + def test_preprocessor_with_invalid_include(self): + shader_code = """ + #include "common.h" + #define PI 3.14159 + + float computeCircleArea(float radius) { + return PI * radius * radius; + } + """ + + preprocessor = DirectxPreprocessor() + + with self.assertRaises(FileNotFoundError): + preprocessor.preprocess(shader_code) + result = self.preprocessor.preprocess(shader_code) + self.assertEqual(result.strip(), expected_result.strip()) + + +if __name__ == "__main__": + unittest.main() From 55f8af3b1e82fd0c478642048684d259768b9fbe Mon Sep 17 00:00:00 2001 From: himaanisrivatsava Date: Mon, 13 Jan 2025 22:34:47 +0530 Subject: [PATCH 37/43] Update test_preprocessor.py --- tests/test_backend/test_directx/test_preprocessor.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/tests/test_backend/test_directx/test_preprocessor.py b/tests/test_backend/test_directx/test_preprocessor.py index 44e95e28..543e300b 100644 --- a/tests/test_backend/test_directx/test_preprocessor.py +++ b/tests/test_backend/test_directx/test_preprocessor.py @@ -104,11 +104,9 @@ def test_preprocessor_with_invalid_include(self): preprocessor = DirectxPreprocessor() + # We expect a FileNotFoundError to be raised when an invalid file is included with self.assertRaises(FileNotFoundError): preprocessor.preprocess(shader_code) - result = self.preprocessor.preprocess(shader_code) - self.assertEqual(result.strip(), expected_result.strip()) - if __name__ == "__main__": unittest.main() From 6121a871ad2cb9beac31cb827220646d925d7be1 Mon Sep 17 00:00:00 2001 From: himaanisrivatsava Date: Mon, 13 Jan 2025 22:45:38 +0530 Subject: [PATCH 38/43] Update test_preprocessor.py --- tests/test_backend/test_directx/test_preprocessor.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/tests/test_backend/test_directx/test_preprocessor.py b/tests/test_backend/test_directx/test_preprocessor.py index 543e300b..f1cf1ebf 100644 --- a/tests/test_backend/test_directx/test_preprocessor.py +++ b/tests/test_backend/test_directx/test_preprocessor.py @@ -3,7 +3,7 @@ class TestDirectxPreprocessor(unittest.TestCase): - + def test_preprocessor_with_defines_and_ifdef(self): shader_code = """ #define PI 3.14159 @@ -36,7 +36,7 @@ def test_preprocessor_with_defines_and_ifdef(self): expected_output = expected_output.strip() self.assertEqual(result, expected_output) - + def test_preprocessor_with_no_debug(self): shader_code = """ #define PI 3.14159 @@ -104,9 +104,11 @@ def test_preprocessor_with_invalid_include(self): preprocessor = DirectxPreprocessor() - # We expect a FileNotFoundError to be raised when an invalid file is included with self.assertRaises(FileNotFoundError): preprocessor.preprocess(shader_code) + result = self.preprocessor.preprocess(shader_code) + self.assertEqual(result.strip(), expected_result.strip()) + if __name__ == "__main__": - unittest.main() + unittest.main() \ No newline at end of file From 603f4523f90b09b5a6702f504448f1aaa5df5aee Mon Sep 17 00:00:00 2001 From: himaanisrivatsava Date: Mon, 13 Jan 2025 22:47:24 +0530 Subject: [PATCH 39/43] Update test_preprocessor.py --- tests/test_backend/test_directx/test_preprocessor.py | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/tests/test_backend/test_directx/test_preprocessor.py b/tests/test_backend/test_directx/test_preprocessor.py index f1cf1ebf..543e300b 100644 --- a/tests/test_backend/test_directx/test_preprocessor.py +++ b/tests/test_backend/test_directx/test_preprocessor.py @@ -3,7 +3,7 @@ class TestDirectxPreprocessor(unittest.TestCase): - + def test_preprocessor_with_defines_and_ifdef(self): shader_code = """ #define PI 3.14159 @@ -36,7 +36,7 @@ def test_preprocessor_with_defines_and_ifdef(self): expected_output = expected_output.strip() self.assertEqual(result, expected_output) - + def test_preprocessor_with_no_debug(self): shader_code = """ #define PI 3.14159 @@ -104,11 +104,9 @@ def test_preprocessor_with_invalid_include(self): preprocessor = DirectxPreprocessor() + # We expect a FileNotFoundError to be raised when an invalid file is included with self.assertRaises(FileNotFoundError): preprocessor.preprocess(shader_code) - result = self.preprocessor.preprocess(shader_code) - self.assertEqual(result.strip(), expected_result.strip()) - if __name__ == "__main__": - unittest.main() \ No newline at end of file + unittest.main() From 39f1eb36e302d31c4a9aea9225fc4f1fc37e0bfd Mon Sep 17 00:00:00 2001 From: himaanisrivatsava Date: Mon, 13 Jan 2025 22:51:40 +0530 Subject: [PATCH 40/43] Update test_preprocessor.py --- tests/test_backend/test_directx/test_preprocessor.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/tests/test_backend/test_directx/test_preprocessor.py b/tests/test_backend/test_directx/test_preprocessor.py index 543e300b..9f82f68d 100644 --- a/tests/test_backend/test_directx/test_preprocessor.py +++ b/tests/test_backend/test_directx/test_preprocessor.py @@ -1,7 +1,6 @@ import unittest from DirectxPreprocessor import DirectxPreprocessor - class TestDirectxPreprocessor(unittest.TestCase): def test_preprocessor_with_defines_and_ifdef(self): @@ -19,7 +18,6 @@ def test_preprocessor_with_defines_and_ifdef(self): return PI * radius * radius; } """ - expected_output = """ float debugValue = 1.0; @@ -27,7 +25,6 @@ def test_preprocessor_with_defines_and_ifdef(self): return 3.14159 * radius * radius; } """ - preprocessor = DirectxPreprocessor() result = preprocessor.preprocess(shader_code) @@ -109,4 +106,4 @@ def test_preprocessor_with_invalid_include(self): preprocessor.preprocess(shader_code) if __name__ == "__main__": - unittest.main() + unittest.main() \ No newline at end of file From d1c788f8a4f9acc85bc27ed4f871e677c37c0d93 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 13 Jan 2025 17:30:20 +0000 Subject: [PATCH 41/43] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- tests/test_backend/test_directx/test_preprocessor.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/test_backend/test_directx/test_preprocessor.py b/tests/test_backend/test_directx/test_preprocessor.py index 9f82f68d..dee15a29 100644 --- a/tests/test_backend/test_directx/test_preprocessor.py +++ b/tests/test_backend/test_directx/test_preprocessor.py @@ -1,6 +1,7 @@ import unittest from DirectxPreprocessor import DirectxPreprocessor + class TestDirectxPreprocessor(unittest.TestCase): def test_preprocessor_with_defines_and_ifdef(self): @@ -105,5 +106,6 @@ def test_preprocessor_with_invalid_include(self): with self.assertRaises(FileNotFoundError): preprocessor.preprocess(shader_code) + if __name__ == "__main__": - unittest.main() \ No newline at end of file + unittest.main() From add50e20042771540546a2938e57b8afc9df9710 Mon Sep 17 00:00:00 2001 From: himaanisrivatsava Date: Mon, 13 Jan 2025 23:15:32 +0530 Subject: [PATCH 42/43] trial #2 another rupdate after all the tests were passed yet somwhow 18 backend test cases failed --- crosstl/backend/DirectX/DirectxPreprocessor.py | 2 -- tests/test_backend/test_directx/test_preprocessor.py | 9 ++++++--- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/crosstl/backend/DirectX/DirectxPreprocessor.py b/crosstl/backend/DirectX/DirectxPreprocessor.py index 784cca6e..d18b1e41 100644 --- a/crosstl/backend/DirectX/DirectxPreprocessor.py +++ b/crosstl/backend/DirectX/DirectxPreprocessor.py @@ -1,6 +1,4 @@ import os - - class DirectxPreprocessor: def __init__(self): self.macros = {} diff --git a/tests/test_backend/test_directx/test_preprocessor.py b/tests/test_backend/test_directx/test_preprocessor.py index 9f82f68d..19050819 100644 --- a/tests/test_backend/test_directx/test_preprocessor.py +++ b/tests/test_backend/test_directx/test_preprocessor.py @@ -1,5 +1,6 @@ import unittest -from DirectxPreprocessor import DirectxPreprocessor +from crosstl.backend.DirectX.DirectxPreprocessor import DirectxPreprocessor + class TestDirectxPreprocessor(unittest.TestCase): @@ -18,6 +19,7 @@ def test_preprocessor_with_defines_and_ifdef(self): return PI * radius * radius; } """ + expected_output = """ float debugValue = 1.0; @@ -25,6 +27,7 @@ def test_preprocessor_with_defines_and_ifdef(self): return 3.14159 * radius * radius; } """ + preprocessor = DirectxPreprocessor() result = preprocessor.preprocess(shader_code) @@ -101,9 +104,9 @@ def test_preprocessor_with_invalid_include(self): preprocessor = DirectxPreprocessor() - # We expect a FileNotFoundError to be raised when an invalid file is included with self.assertRaises(FileNotFoundError): preprocessor.preprocess(shader_code) + if __name__ == "__main__": - unittest.main() \ No newline at end of file + unittest.main() From dad62ed3f3d27f9ffe9336b8b45c97df2abd4c1a Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 13 Jan 2025 17:46:03 +0000 Subject: [PATCH 43/43] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- crosstl/backend/DirectX/DirectxPreprocessor.py | 2 ++ tests/test_backend/test_directx/test_preprocessor.py | 1 - 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/crosstl/backend/DirectX/DirectxPreprocessor.py b/crosstl/backend/DirectX/DirectxPreprocessor.py index d18b1e41..784cca6e 100644 --- a/crosstl/backend/DirectX/DirectxPreprocessor.py +++ b/crosstl/backend/DirectX/DirectxPreprocessor.py @@ -1,4 +1,6 @@ import os + + class DirectxPreprocessor: def __init__(self): self.macros = {} diff --git a/tests/test_backend/test_directx/test_preprocessor.py b/tests/test_backend/test_directx/test_preprocessor.py index 49d6d0b3..19050819 100644 --- a/tests/test_backend/test_directx/test_preprocessor.py +++ b/tests/test_backend/test_directx/test_preprocessor.py @@ -2,7 +2,6 @@ from crosstl.backend.DirectX.DirectxPreprocessor import DirectxPreprocessor - class TestDirectxPreprocessor(unittest.TestCase): def test_preprocessor_with_defines_and_ifdef(self):