From e880c0c771b5a317cfcd607037db70eb625cc8a1 Mon Sep 17 00:00:00 2001 From: roost-io Date: Mon, 8 Jul 2024 08:44:54 +0000 Subject: [PATCH] Unit test generated by RoostGPT Using AI Model gpt-4-turbo --- AI Code Reviewer/requirements.txt | 5 +- .../test_CodeReviewerAnalyzePythonCode.py | 155 ++++++++++++++ .../test_CodeReviewerCheckCodeStyle.py | 117 ++++++++++ .../test_CodeReviewerCheckComments.py | 126 +++++++++++ .../test_CodeReviewerCheckIndentation.py | 200 ++++++++++++++++++ .../test_CodeReviewerCheckUndefinedVars.py | 151 +++++++++++++ .../test_CodeReviewerGetFeedback.py | 112 ++++++++++ pytest.ini | 10 + 8 files changed, 874 insertions(+), 2 deletions(-) create mode 100644 AI Code Reviewer/test_CodeReviewerAnalyzePythonCode.py create mode 100644 AI Code Reviewer/test_CodeReviewerCheckCodeStyle.py create mode 100644 AI Code Reviewer/test_CodeReviewerCheckComments.py create mode 100644 AI Code Reviewer/test_CodeReviewerCheckIndentation.py create mode 100644 AI Code Reviewer/test_CodeReviewerCheckUndefinedVars.py create mode 100644 AI Code Reviewer/test_CodeReviewerGetFeedback.py create mode 100644 pytest.ini diff --git a/AI Code Reviewer/requirements.txt b/AI Code Reviewer/requirements.txt index e9489e5444..bafc96700e 100644 --- a/AI Code Reviewer/requirements.txt +++ b/AI Code Reviewer/requirements.txt @@ -1,2 +1,3 @@ -ast -pycodestyle \ No newline at end of file +AI_Code_Reviewer +pycodestyle +pytest diff --git a/AI Code Reviewer/test_CodeReviewerAnalyzePythonCode.py b/AI Code Reviewer/test_CodeReviewerAnalyzePythonCode.py new file mode 100644 index 0000000000..0efb2bd9d4 --- /dev/null +++ b/AI Code Reviewer/test_CodeReviewerAnalyzePythonCode.py @@ -0,0 +1,155 @@ +# ********RoostGPT******** +""" +Test generated by RoostGPT for test python-ai-codereviewer using AI Type Open AI and AI Model gpt-4-turbo + +ROOST_METHOD_HASH=analyze_python_code_d3e4d47a3c +ROOST_METHOD_SIG_HASH=analyze_python_code_9a11aca9e3 + + +### Scenario 1: Valid Python Code with Proper Indentation and Definitions +Details: + TestName: test_valid_python_code_no_feedback + Description: Ensure that properly formatted Python code with correct indentation, defined variables, and good comments does not generate any feedback. +Execution: + Arrange: Provide a valid Python code snippet. + Act: Call the `analyze_python_code` function with the code snippet. + Assert: Check that the feedback list remains empty. +Validation: + Validates that the function correctly identifies well-formatted code and that no unnecessary feedback is given, aligning with the function's purpose to find issues. + +### Scenario 2: Python Code with Syntax Errors +Details: + TestName: test_syntax_error_handling + Description: Verify that the function correctly identifies syntax errors and provides appropriate feedback. +Execution: + Arrange: Provide a Python code snippet with a deliberate syntax error. + Act: Call the `analyze_python_code` function with the erroneous code. + Assert: Check that the feedback contains a message about the syntax error. +Validation: + Ensures that syntax errors are caught and reported, which is crucial for a code analysis tool to prevent runtime errors. + +### Scenario 3: Indentation Errors in Function Definitions +Details: + TestName: test_indentation_error_in_function + Description: Check if indentation errors within Python functions are detected and reported. +Execution: + Arrange: Provide a Python code snippet where a function's body is incorrectly indented. + Act: Call the `analyze_python_code` function with the code. + Assert: Check that the feedback includes an indentation error message for the function. +Validation: + Tests the ability of the function to detect common logical errors in Python code, ensuring code quality and maintainability. + +### Scenario 4: Undefined Variable Usage +Details: + TestName: test_undefined_variable_usage + Description: Verify that the use of undefined variables is detected and reported. +Execution: + Arrange: Provide a Python code snippet that uses a variable before it is defined. + Act: Call the `analyze_python_code` function with the code. + Assert: Check that the feedback includes a message about the undefined variable. +Validation: + Critical for ensuring that all variables are defined before use, which is a basic requirement for functional code. + +### Scenario 5: Code Style Issues Detected by pycodestyle +Details: + TestName: test_code_style_issues_detection + Description: Ensure that code style issues are detected using pycodestyle and appropriate feedback is given. +Execution: + Arrange: Provide a Python code snippet with style issues like missing whitespace around operators. + Act: Call the `analyze_python_code` function with the code. + Assert: Check that feedback includes a message about code style issues. +Validation: + Validates the integration with pycodestyle and the ability to enforce PEP 8 code style standards, promoting better code readability and maintainability. + +### Scenario 6: Inadequate Comment Style +Details: + TestName: test_inadequate_comment_style + Description: Test if comments without space after the '#' or empty comments are flagged. +Execution: + Arrange: Provide a Python code snippet with one or more poorly formatted comments. + Act: Call the `analyze_python_code` function with the code. + Assert: Check that feedback includes messages pointing out the specific comment issues. +Validation: + Ensures that comments in the code meet a basic standard of readability and formatting, which is important for code maintainability. + +These scenarios collectively ensure comprehensive testing of the `analyze_python_code` function, covering typical errors and style issues that developers might encounter. +""" + +# ********RoostGPT******** +import pytest +from AI_Code_Reviewer.ai_code_reviewer import CodeReviewer + +class Test_CodeReviewerAnalyzePythonCode: + + @pytest.mark.valid + def test_valid_python_code_no_feedback(self): + code_reviewer = CodeReviewer() + valid_code = """ +def example_function(): + # This is a well-commented function + variable = 123 + print(variable) +""" + code_reviewer.analyze_python_code(valid_code) + assert not code_reviewer.feedback, "Expected no feedback for valid Python code" + + @pytest.mark.invalid + @pytest.mark.syntax + def test_syntax_error_handling(self): + code_reviewer = CodeReviewer() + code_with_syntax_error = """ +def example_function() + variable = 123 + print(variable) +""" + code_reviewer.analyze_python_code(code_with_syntax_error) + assert any("Syntax Error" in feedback for feedback in code_reviewer.feedback), "Expected feedback about syntax error" + + @pytest.mark.invalid + @pytest.mark.negative + def test_indentation_error_in_function(self): + code_reviewer = CodeReviewer() + code_with_indentation_error = """ +def example_function(): +variable = 123 +print(variable) +""" + code_reviewer.analyze_python_code(code_with_indentation_error) + assert any("Indentation Error" in feedback for feedback in code_reviewer.feedback), "Expected feedback about indentation error" + + @pytest.mark.invalid + @pytest.mark.negative + def test_undefined_variable_usage(self): + code_reviewer = CodeReviewer() + code_with_undefined_variable = """ +def example_function(): + print(undeclared_variable) +""" + code_reviewer.analyze_python_code(code_with_undefined_variable) + assert any("Variable 'undeclared_variable' is used but not defined" in feedback for feedback in code_reviewer.feedback), "Expected feedback about undefined variable" + + @pytest.mark.invalid + @pytest.mark.regression + def test_code_style_issues_detection(self): + code_reviewer = CodeReviewer() + code_with_style_issues = """ +def example_function(): + variable=123 #Missing whitespace around operator + print(variable) +""" + code_reviewer.analyze_python_code(code_with_style_issues) + assert any("Code style issues found" in feedback for feedback in code_reviewer.feedback), "Expected feedback about code style issues" + + @pytest.mark.invalid + @pytest.mark.regression + def test_inadequate_comment_style(self): + code_reviewer = CodeReviewer() + code_with_poor_comments = """ +def example_function(): + #bad comment + # anotherbadcomment + variable = 123 + print(variable) +""" + code_reviewer.analyze_python_code(code_with_poor_comments) + assert any("Improve comment style" in feedback for feedback in code_reviewer.feedback), "Expected feedback about comment style issues" diff --git a/AI Code Reviewer/test_CodeReviewerCheckCodeStyle.py b/AI Code Reviewer/test_CodeReviewerCheckCodeStyle.py new file mode 100644 index 0000000000..5087c8fd40 --- /dev/null +++ b/AI Code Reviewer/test_CodeReviewerCheckCodeStyle.py @@ -0,0 +1,117 @@ +# ********RoostGPT******** +""" +Test generated by RoostGPT for test python-ai-codereviewer using AI Type Open AI and AI Model gpt-4-turbo + +ROOST_METHOD_HASH=_check_code_style_1512c7e89b +ROOST_METHOD_SIG_HASH=_check_code_style_671231205e + + +### Scenario 1: No Code Style Errors +Details: + TestName: test_check_code_style_no_errors + Description: Tests if the method correctly identifies that there are no code style errors in the provided code snippet. +Execution: + Arrange: Create an instance of the class and prepare a code snippet that adheres to the PEP8 style guide. + Act: Call the `_check_code_style` method with the compliant code snippet. + Assert: Verify that the `feedback` list remains empty. +Validation: + This test ensures that the method correctly identifies code snippets without style issues, which is crucial for not flagging compliant code as erroneous. It validates the method's ability to differentiate between compliant and non-compliant code. + +### Scenario 2: Code Style Errors Present +Details: + TestName: test_check_code_style_with_errors + Description: Tests if the method correctly identifies code style errors and appends an appropriate message to the feedback list. +Execution: + Arrange: Create an instance of the class and prepare a code snippet that violates the PEP8 style guide. + Act: Call the `_check_code_style` method with the non-compliant code snippet. + Assert: Verify that the `feedback` list contains the specific message about code style issues. +Validation: + This test ensures that the method can detect when a code snippet violates style guidelines and appropriately notifies the user. This is essential for guiding users to improve code quality and adhere to best practices. + +### Scenario 3: Empty Code Input +Details: + TestName: test_check_code_style_empty_code + Description: Verifies the behavior of the method when an empty string is passed as code. +Execution: + Arrange: Create an instance of the class and prepare an empty code snippet. + Act: Call the `_check_code_style` method with the empty string. + Assert: Check that the `feedback` list remains empty. +Validation: + This test checks the method's robustness in handling edge cases where no code is provided. Ensuring that no false positives (errors) are reported in such cases is critical for accurate feedback. + +### Scenario 4: Code with Multiple Style Errors +Details: + TestName: test_check_code_style_multiple_errors + Description: Tests the method's ability to handle a code snippet with multiple style errors and whether it still appends only one generic message to the feedback. +Execution: + Arrange: Create an instance of the class and prepare a code snippet with several PEP8 violations. + Act: Call the `_check_code_style` method with this code. + Assert: Verify that the feedback list contains only one entry about code style issues. +Validation: + This scenario is essential to ensure that the feedback mechanism does not overwhelm the user with multiple messages for a single check, maintaining a clear and concise feedback system. + +### Scenario 5: Non-String Code Input +Details: + TestName: test_check_code_style_non_string_input + Description: Evaluates the method's behavior when an object other than a string (like a list or a dict) is passed as code. +Execution: + Arrange: Create an instance of the class and prepare a non-string code input (e.g., a list). + Act: Call the `_check_code_style` method with the non-string input. + Assert: Expect an appropriate handling of the type error, ideally without crashing and without appending to the feedback. +Validation: + While Python is dynamically typed, this test ensures that the method can gracefully handle unexpected data types without causing runtime errors, which enhances the robustness of the application. +""" + +# ********RoostGPT******** +import pytest +import pycodestyle +from AI_Code_Reviewer.ai_code_reviewer import CodeReviewer + +class Test_CodeReviewerCheckCodeStyle: + + @pytest.mark.valid + def test_check_code_style_no_errors(self): + reviewer = CodeReviewer() + pep8_compliant_code = """ +def example_function(): + return 'hello, world' +""" + reviewer._check_code_style(pep8_compliant_code) + assert len(reviewer.feedback) == 0, "Feedback should be empty for compliant code" + + @pytest.mark.invalid + def test_check_code_style_with_errors(self): + reviewer = CodeReviewer() + non_compliant_code = """ +def example_function(): +return 'hello, world' +""" + reviewer._check_code_style(non_compliant_code) + assert len(reviewer.feedback) == 1, "Feedback should contain exactly one message about style issues" + assert "Code style issues found" in reviewer.feedback[0], "Feedback message should mention code style issues" + + @pytest.mark.negative + def test_check_code_style_empty_code(self): + reviewer = CodeReviewer() + empty_code = "" + reviewer._check_code_style(empty_code) + assert len(reviewer.feedback) == 0, "Feedback should be empty for empty code input" + + @pytest.mark.invalid + def test_check_code_style_multiple_errors(self): + reviewer = CodeReviewer() + multiple_errors_code = """ +def example_function(): +return 'hello, world' + print('error') +""" + reviewer._check_code_style(multiple_errors_code) + assert len(reviewer.feedback) == 1, "Feedback should contain only one message even with multiple errors" + + @pytest.mark.negative + def test_check_code_style_non_string_input(self): + reviewer = CodeReviewer() + non_string_input = ["def example_function():", " return 'hello, world'"] + with pytest.raises(TypeError): + reviewer._check_code_style(non_string_input) + assert len(reviewer.feedback) == 0, "Feedback should be empty when a non-string input is provided" diff --git a/AI Code Reviewer/test_CodeReviewerCheckComments.py b/AI Code Reviewer/test_CodeReviewerCheckComments.py new file mode 100644 index 0000000000..9ae4017712 --- /dev/null +++ b/AI Code Reviewer/test_CodeReviewerCheckComments.py @@ -0,0 +1,126 @@ +# ********RoostGPT******** +""" +Test generated by RoostGPT for test python-ai-codereviewer using AI Type Open AI and AI Model gpt-4-turbo + +ROOST_METHOD_HASH=_check_comments_b162e87713 +ROOST_METHOD_SIG_HASH=_check_comments_1609749a3d + + +### Scenario 1: Test with a Properly Formatted Comment +Details: + TestName: test_comment_with_proper_format + Description: Verify that the function does not append feedback for a properly formatted comment (a space after the '#'). +Execution: + Arrange: Instantiate the object and prepare a string with a properly formatted comment. + Act: Call the _check_comments method with the prepared string. + Assert: Check that the feedback list remains empty. +Validation: + Rationalize the importance of the test by confirming that well-formed comments are not flagged incorrectly, ensuring the function correctly identifies style issues without generating false positives. + +### Scenario 2: Test with an Improperly Formatted Comment (no space) +Details: + TestName: test_comment_without_space_after_hash + Description: Ensure that the function appends appropriate feedback for a comment without a space after the '#'. +Execution: + Arrange: Instantiate the object and prepare a string with a comment lacking a space after the '#'. + Act: Call the _check_comments method with the prepared string. + Assert: Check that the feedback list contains the expected message regarding the improper comment format. +Validation: + The test verifies that the function enforces styling rules by identifying comments that do not adhere to expected formatting standards, which is crucial for maintaining readability and consistency in code comments. + +### Scenario 3: Test with an Empty Comment +Details: + TestName: test_empty_comment + Description: Validate that the function appends feedback for a comment that consists only of a '#'. +Execution: + Arrange: Instantiate the object and prepare a string containing only a '#'. + Act: Call the _check_comments method with the prepared string. + Assert: Check that the feedback list includes a message about the empty comment. +Validation: + This test ensures that the function can identify and flag completely empty comments, which do not contribute to understanding the code and should either be expanded or removed. + +### Scenario 4: Test with Multiple Comments, Including Both Correct and Incorrect Formats +Details: + TestName: test_mixed_comment_styles + Description: Check that the function correctly handles multiple comments in various styles within a single input. +Execution: + Arrange: Instantiate the object and prepare a string with multiple comments, some correctly formatted and others not. + Act: Call the _check_comments method with the prepared string. + Assert: Verify that the feedback list only contains entries for the incorrectly formatted comments. +Validation: + This scenario is important for ensuring that the function operates correctly in realistic scenarios where code may contain a mix of well-formed and poorly-formed comments. + +### Scenario 5: Test with Non-Comment Code Present +Details: + TestName: test_code_with_non_comment_lines + Description: Ensure that the function does not append feedback for lines of code that are not comments. +Execution: + Arrange: Instantiate the object and prepare a string that includes both comments and regular lines of code. + Act: Call the _check_comments method with the prepared string. + Assert: Confirm that only the comment lines are evaluated and feedback is correctly managed. +Validation: + This test confirms that the function discriminates between comment lines and code lines, focusing its evaluations only on comments, which is crucial for its intended purpose without affecting non-comment code lines. + +### Scenario 6: Test with Comments Following Code on the Same Line +Details: + TestName: test_inline_comments + Description: Verify that the function handles inline comments correctly, even when they follow code on the same line. +Execution: + Arrange: Instantiate the object and prepare a string where comments are inline following some code. + Act: Call the _check_comments method with the prepared string. + Assert: Check that the function does not erroneously flag these comments if they are formatted correctly. +Validation: + This test ensures that the function can accurately handle and evaluate inline comments, which are common in real-world code, without misidentifying them based on their placement. +""" + +# ********RoostGPT******** +import pytest +from AI_Code_Reviewer.ai_code_reviewer import CodeReviewer + +class Test_CodeReviewerCheckComments: + @pytest.mark.valid + def test_comment_with_proper_format(self): + code_reviewer = CodeReviewer() + test_code = "# This is a properly formatted comment." + code_reviewer._check_comments(test_code) + assert len(code_reviewer.feedback) == 0, "Feedback should be empty for properly formatted comments." + + @pytest.mark.invalid + def test_comment_without_space_after_hash(self): + code_reviewer = CodeReviewer() + test_code = "#This is an improperly formatted comment." + code_reviewer._check_comments(test_code) + assert len(code_reviewer.feedback) == 1 + assert "Improve comment style" in code_reviewer.feedback[0] + + @pytest.mark.invalid + def test_empty_comment(self): + code_reviewer = CodeReviewer() + test_code = "#" + code_reviewer._check_comments(test_code) + assert len(code_reviewer.feedback) == 1 + assert "Improve comment style" in code_reviewer.feedback[0] + + @pytest.mark.valid + @pytest.mark.invalid + def test_mixed_comment_styles(self): + code_reviewer = CodeReviewer() + test_code = "#Good comment\n#badcomment\n# Another good comment" + code_reviewer._check_comments(test_code) + assert len(code_reviewer.feedback) == 1 + assert "Improve comment style in line 2" in code_reviewer.feedback[0] + + @pytest.mark.valid + def test_code_with_non_comment_lines(self): + code_reviewer = CodeReviewer() + test_code = "print('Hello, world!')\n#Comment here" + code_reviewer._check_comments(test_code) + assert len(code_reviewer.feedback) == 0, "Feedback should only include comments." + + @pytest.mark.valid + def test_inline_comments(self): + code_reviewer = CodeReviewer() + test_code = "x = 10 # This is an inline comment" + code_reviewer._check_comments(test_code) + assert len(code_reviewer.feedback) == 0, "Inline comments should not be flagged if correctly formatted." + diff --git a/AI Code Reviewer/test_CodeReviewerCheckIndentation.py b/AI Code Reviewer/test_CodeReviewerCheckIndentation.py new file mode 100644 index 0000000000..a97adb252d --- /dev/null +++ b/AI Code Reviewer/test_CodeReviewerCheckIndentation.py @@ -0,0 +1,200 @@ +# ********RoostGPT******** +""" +Test generated by RoostGPT for test python-ai-codereviewer using AI Type Open AI and AI Model gpt-4-turbo + +ROOST_METHOD_HASH=_check_indentation_4bea7efa9b +ROOST_METHOD_SIG_HASH=_check_indentation_a78db5f72b + + +### Scenario 1: Function with Missing Docstring +**Details:** + - TestName: test_function_missing_docstring + - Description: Tests whether the feedback list contains the appropriate message when a function definition lacks a docstring. + +**Execution:** + - Arrange: Create an AST tree of a Python function without a docstring. + - Act: Call `_check_indentation` with the created AST tree. + - Assert: Check if the feedback list contains the specific message about the missing docstring. + +**Validation:** + - The test validates that the method correctly identifies functions without docstrings and appends the appropriate message to the feedback list, ensuring that code style guidelines are enforced. + +### Scenario 2: Function with Docstring +**Details:** + - TestName: test_function_with_docstring + - Description: Validate that no feedback is given for a function that includes a docstring. + +**Execution:** + - Arrange: Create an AST tree of a Python function with a docstring. + - Act: Call `_check_indentation` with this tree. + - Assert: Ensure the feedback list remains empty. + +**Validation:** + - This scenario checks that the method correctly ignores functions that comply with having a docstring, affirming the function's ability to discern properly documented functions. + +### Scenario 3: Loop with Missing Pass Statement +**Details:** + - TestName: test_loop_missing_pass_statement + - Description: Ensures that the feedback list flags loops (for, while) that should contain a 'pass' statement but do not. + +**Execution:** + - Arrange: Create an AST tree of a loop (for or while) without any inner statements or with a non-pass statement. + - Act: Call `_check_indentation` with the created tree. + - Assert: Confirm that the feedback list contains a message about the missing 'pass' statement. + +**Validation:** + - This test is crucial for maintaining code quality and readability by ensuring that empty control structures are explicitly marked with 'pass', avoiding potential misunderstandings of the code's intent. + +### Scenario 4: If Condition with Proper Body +**Details:** + - TestName: test_if_condition_with_proper_body + - Description: Checks that an if statement with a valid body does not trigger any feedback. + +**Execution:** + - Arrange: Create an AST tree where an if statement has a valid body (like an expression or another valid statement). + - Act: Call `_check_indentation` on this tree. + - Assert: Verify that the feedback list remains empty. + +**Validation:** + - This test ensures that the method correctly identifies and ignores properly constructed if conditions, which is essential for not raising false positives in style checks. + +### Scenario 5: With Statement Missing Pass +**Details:** + - TestName: test_with_statement_missing_pass + - Description: Tests whether a 'with' statement without a 'pass' statement or any other valid inner statement is flagged. + +**Execution:** + - Arrange: Create an AST tree of a 'with' statement that lacks inner statements. + - Act: Call `_check_indentation` with this tree. + - Assert: Check for an appropriate error message in the feedback list. + +**Validation:** + - Ensures that the method can identify and report potentially problematic 'with' statements that could lead to errors or misinterpretations in the code's logic, thus maintaining code clarity and correctness. + +### Scenario 6: Nested Structures Compliance +**Details:** + - TestName: test_nested_structures_compliance + - Description: Verify that nested structures (like a function within a loop) are individually checked and correctly reported if they violate indentation rules. + +**Execution:** + - Arrange: Create an AST tree with nested structures, e.g., a loop without a 'pass' inside a function without a docstring. + - Act: Call `_check_indentation` using this tree. + - Assert: Confirm that feedback is generated for both the function and the loop. + +**mValidation:** + - This test checks the method's ability to handle and accurately report issues in complex, nested code structures, which is vital for maintaining high code quality in larger codebases. +""" + +# ********RoostGPT******** +import pytest +import ast +from AI_Code_Rewriter.ai_code_reviewer import CodeReviewer + +class Test_CodeReviewerCheckIndentation: + + @pytest.mark.smoke + def test_function_missing_docstring(self): + # Arrange + code = """ +def function_without_docstring(): + a = 1 + return a +""" + tree = ast.parse(code) + code_reviewer = CodeReviewer() + + # Act + code_reviewer._check_indentation(tree) + + # Assert + expected_message = "Function 'function_without_docstring' should have a docstring or 'pass' statement." + assert expected_message in code_reviewer.feedback + + @pytest.mark.valid + def test_function_with_docstring(self): + # Arrange + code = """ +def function_with_docstring(): + \"\"\"This is a docstring.\"\"\" + a = 1 + return a +""" + tree = ast.parse(code) + code_reviewer = CodeReviewer() + + # Act + code_reviewer._check_indentation(tree) + + # Assert + assert not code_reviewer.feedback + + @pytest.mark.negative + def test_loop_missing_pass_statement(self): + # Arrange + code = """ +def some_function(): + for i in range(10): + # No pass or any other statement +""" + tree = ast.parse(code) + code_reviewer = CodeReviewer() + + # Act + code_reviewer._check_indentation(tree) + + # Assert + expected_message = "Indentation Error: Missing 'pass' statement for 'For(target=Name(id='i', ctx=Store()), iter=Call(func=Name(id='range', ctx=Load()), args=[Constant(value=10)], keywords=[]), body=[], orelse=[])'" + assert expected_message in code_reviewer.feedback + + @pytest.mark.positive + def test_if_condition_with_proper_body(self): + # Arrange + code = """ +def check_condition(): + if True: + print("Condition met") +""" + tree = ast.parse(code) + code_reviewer = CodeReviewer() + + # Act + code_reviewer._check_indentation(tree) + + # Assert + assert not code_reviewer.feedback + + @pytest.mark.smoke + def test_with_statement_missing_pass(self): + # Arrange + code = """ +def with_example(): + with open('file.txt', 'r') as file: + # No pass or any other statement +""" + tree = ast.parse(code) + code_reviewer = CodeReviewer() + + # Act + code_reviewer._check_indentation(tree) + + # Assert + expected_message = "Indentation Error: Missing 'pass' statement for 'With(items=[withitem(context_expr=Call(func=Name(id='open', ctx=Load()), args=[Constant(value='file.txt'), Constant(value='r')], keywords=[]), optional_vars=Name(id='file', ctx=Store()))], body=[], type_comment=None)'" + assert expected_message in code_reviewer.feedback + + @pytest.mark.regression + def test_nested_structures_compliance(self): + # Arrange + code = """ +def outer_function(): + for i in range(5): + if i > 2: + print(i) +""" + tree = ast.parse(code) + code_reviewer = CodeReviewer() + + # Act + code_reviewer._check_indentation(tree) + + # Assert + assert not code_reviewer.feedback diff --git a/AI Code Reviewer/test_CodeReviewerCheckUndefinedVars.py b/AI Code Reviewer/test_CodeReviewerCheckUndefinedVars.py new file mode 100644 index 0000000000..3e4afd47ab --- /dev/null +++ b/AI Code Reviewer/test_CodeReviewerCheckUndefinedVars.py @@ -0,0 +1,151 @@ +# ********RoostGPT******** +""" +Test generated by RoostGPT for test python-ai-codereviewer using AI Type Open AI and AI Model gpt-4-turbo + +ROOST_METHOD_HASH=_check_undefined_vars_8c9d170008 +ROOST_METHOD_SIG_HASH=_check_undefined_vars_88edf1e4ff + + +### Scenario 1: No Undefined Variables +Details: + TestName: test_no_undefined_variables + Description: Verify that the function identifies no undefined variables when all variables used are properly defined beforehand. +Execution: + Arrange: Create an AST of a code snippet where variables are defined before use. + Act: Call the `_check_undefined_vars` method with the AST. + Assert: Check that the `feedback` list remains empty. +Validation: + Rationalize the importance of this test to ensure that the method correctly handles cases where all variables are defined, thus not falsely reporting errors. + +### Scenario 2: Single Undefined Variable +Details: + TestName: test_single_undefined_variable + Description: Test the detection of a single undefined variable used in the code. +Execution: + Arrange: Generate an AST from a code snippet where one variable is used before it is defined. + Act: Invoke the `_check_undefined_vars` method with this AST. + Assert: Verify that the `feedback` list contains the appropriate error message for the undefined variable. +Validation: + This test confirms that the function can accurately identify and report individual instances of undefined variables, which is critical for debugging and maintaining code quality. + +### Scenario 3: Multiple Undefined Variables +Details: + TestName: test_multiple_undefined_variables + Description: Ensure that the function can identify multiple undefined variables used in a code snippet. +Execution: + Arrange: Produce an AST for a snippet with several variables used before definitions. + Act: Execute the `_check_undefined_vars` method on this AST. + Assert: Check that the `feedback` list correctly reports all undefined variables. +Validation: + This scenario tests the function's capability to handle and report multiple errors simultaneously, a common situation in larger or complex code bases. + +### Scenario 4: Mixed Defined and Undefined Variables +Details: + TestName: test_mixed_defined_and_undefined_variables + Description: Check the method's ability to distinguish between defined and undefined variables when both are present. +Execution: + Arrange: Create an AST where some variables are defined before use and others are not. + Act: Call the `_check_undefined_vars` method with this AST. + Assert: Confirm that only the undefined variables are reported in the `feedback`. +Validation: + Validates the precision of the method in a realistic coding scenario, ensuring it does not misidentify defined variables as undefined. + +### Scenario 5: Variables Defined After Use +Details: + TestName: test_variables_defined_after_use + Description: Test the scenario where variables are used before they are defined but defined later in the same scope. +Execution: + Arrange: Construct an AST for a code snippet where variables are used and then defined later in the code. + Act: Invoke the `_check_undefined_vars` method with this AST. + Assert: Ensure that the variables used before definition are reported as undefined. +Validation: + This scenario checks the function's strict compliance with variable declaration before use, a key aspect of many programming styles and necessary for avoiding runtime errors in some languages. + +### Scenario 6: No Variables Used +Details: + TestName: test_no_variables_used + Description: Ensures that the function handles the case where no variables are used in the code gracefully. +Execution: + Arrange: Provide an AST of a code snippet that contains no variable usage. + Act: Call the `_check_undefined_vars` method. + Assert: Confirm that the `feedback` list remains empty. +Validation: + This test verifies that the function does not produce incorrect feedback in the absence of variables, avoiding false positives in trivial or empty code blocks. +""" + +# ********RoostGPT******** +import pytest +import ast +from AI_Code_Reviewer.ai_code_reviewer import CodeReviewer + +class Test_CodeReviewerCheckUndefinedVars: + @pytest.mark.valid + def test_no_undefined_variables(self): + code_reviewer = CodeReviewer() + code_snippet = """ + x = 10 + y = 20 + result = x + y + print(result) + """ + tree = ast.parse(code_snippet) + code_reviewer._check_undefined_vars(tree) + assert not code_reviewer.feedback, "There should be no feedback for properly defined variables" + + @pytest.mark.invalid + def test_single_undefined_variable(self): + code_reviewer = CodeReviewer() + code_snippet = """ + print(x) + """ + tree = ast.parse(code_snippet) + code_reviewer._check_undefined_vars(tree) + assert len(code_reviewer.feedback) == 1 + assert "Variable 'x' is used but not defined." in code_reviewer.feedback + + @pytest.mark.invalid + def test_multiple_undefined_variables(self): + code_reviewer = CodeReviewer() + code_snippet = """ + a = b + c + print(d) + """ + tree = ast.parse(code_snippet) + code_reviewer._check_undefined_vars(tree) + expected_errors = {"Variable 'b' is used but not defined.", "Variable 'c' is used but not defined.", "Variable 'd' is used but not defined."} + assert len(code_reviewer.feedback) == 3 + assert set(code_reviewer.feedback) == expected_errors + + @pytest.mark.mixed + def test_mixed_defined_and_undefined_variables(self): + code_reviewer = CodeReviewer() + code_snippet = """ + x = 10 + y = x + z + """ + tree = ast.parse(code_snippet) + code_reviewer._check_undefined_vars(tree) + assert len(code_reviewer.feedback) == 1 + assert "Variable 'z' is used but not defined." in code_reviewer.feedback + + @pytest.mark.invalid + def test_variables_defined_after_use(self): + code_reviewer = CodeReviewer() + code_snippet = """ + print(x) + x = 10 + """ + tree = ast.parse(code_snippet) + code_reviewer._check_undefined_vars(tree) + assert len(code_reviewer.feedback) == 1 + assert "Variable 'x' is used but not defined." in code_reviewer.feedback + + @pytest.mark.valid + def test_no_variables_used(self): + code_reviewer = CodeReviewer() + code_snippet = """ + # Just a comment, no variables + """ + tree = ast.parse(code_snippet) + code_reviewer._check_undefined_vars(tree) + assert not code_reviewer.feedback, "There should be no feedback when no variables are used" diff --git a/AI Code Reviewer/test_CodeReviewerGetFeedback.py b/AI Code Reviewer/test_CodeReviewerGetFeedback.py new file mode 100644 index 0000000000..ac737db90b --- /dev/null +++ b/AI Code Reviewer/test_CodeReviewerGetFeedback.py @@ -0,0 +1,112 @@ +# ********RoostGPT******** +""" +Test generated by RoostGPT for test python-ai-codereviewer using AI Type Open AI and AI Model gpt-4-turbo + +ROOST_METHOD_HASH=get_feedback_72992f81e1 +ROOST_METHOD_SIG_HASH=get_feedback_30ef9e4490 + + +### Scenario 1: Test Empty Feedback List +Details: + TestName: test_empty_feedback_list + Description: Verify that calling `get_feedback` on a newly initialized object returns an empty list. +Execution: + Arrange: Create an instance of the class. + Act: Call the `get_feedback` method. + Assert: Check that the returned value is an empty list. +Validation: + This test is important because it checks the default state of the feedback list. The expected behavior, according to the class definition, is that a new instance should have an empty feedback list. Ensuring this behavior is correct helps validate that the feedback list is being initialized properly. + +### Scenario 2: Test Non-Empty Feedback List +Details: + TestName: test_non_empty_feedback_list + Description: Verify that `get_feedback` correctly returns a non-empty list after feedback has been added to the list. +Execution: + Arrange: Create an instance of the class and add several feedback entries to the `feedback` list. + Act: Call the `get_feedback` method. + Assert: Check that the returned list matches the feedback entries added. +Validation: + This test ensures that the `get_feedback` method accurately reflects the current state of the feedback list. It is crucial for the method to return all current entries in the list, as this confirms the list's dynamic nature and the method's ability to retrieve its current state correctly. + +### Scenario 3: Test Feedback List Integrity +Details: + TestName: test_feedback_list_integrity + Description: Ensure that the list returned by `get_feedback` is a copy of the original list, preventing accidental modification of the internal state. +Execution: + Arrange: Create an instance of the class and add feedback to the list. + Act: Retrieve the feedback list and modify the returned list. + Assert: Check that the original feedback list inside the class instance remains unchanged. +Validation: + This test verifies that modifications to the returned list do not affect the internal state of the class. Ensuring this behavior is crucial for maintaining data integrity and encapsulation, which prevents accidental data corruption through external modifications of mutable objects. + +### Scenario 4: Test Feedback List After Multiple Modifications +Details: + TestName: test_feedback_list_after_multiple_modifications + Description: Verify that `get_feedback` reflects multiple changes to the feedback list over time. +Execution: + Arrange: Create an instance of the class, add feedback, remove feedback, and modify feedback entries over several operations. + Act: Call the `get_feedback` method after each modification. + Assert: Check that each call to `get_feedback` accurately reflects the list's state at that point in time. +Validation: + This scenario tests the method's reliability and accuracy in a dynamic environment where changes to the feedback list occur frequently. It's important to confirm that the method consistently provides an accurate snapshot of the list's state, supporting reliable and predictable application behavior. + +These scenarios provide a comprehensive assessment of the `get_feedback` method's functionality, focusing on its ability to accurately and reliably return the current state of the feedback list while ensuring the integrity and isolation of the class's internal state. +""" + +# ********RoostGPT******** +import pytest +from AI_Code_Reviewer.ai_code_reviewer import CodeReviewer + +class Test_CodeReviewerGetFeedback: + @pytest.mark.valid + def test_empty_feedback_list(self): + # Arrange + code_reviewer = CodeReviewer() + # Act + feedback = code_reviewer.get_feedback() + # Assert + assert feedback == [], "Expected feedback list to be empty for a newly initialized CodeReviewer." + + @pytest.mark.valid + def test_non_empty_feedback_list(self): + # Arrange + code_reviewer = CodeReviewer() + code_reviewer.feedback.append("Invalid variable name") + code_reviewer.feedback.append("Line too long") + # Act + feedback = code_reviewer.get_feedback() + # Assert + assert feedback == ["Invalid variable name", "Line too long"], "Expected feedback list to match the entries added." + + @pytest.mark.negative + def test_feedback_list_integrity(self): + # Arrange + code_reviewer = CodeReviewer() + code_reviewer.feedback.append("Missing docstring") + # Act + feedback = code_reviewer.get_feedback() + feedback.append("Extra space") + # Assert + assert "Extra space" not in code_reviewer.feedback, "Modifying the returned list should not affect the original feedback list." + + @pytest.mark.regression + def test_feedback_list_after_multiple_modifications(self): + # Arrange + code_reviewer = CodeReviewer() + modifications = [ + ("Add", "Use proper indentation"), + ("Remove", "Use proper indentation"), + ("Add", "Inconsistent variable names") + ] + expected_feedback = [] + # Act and Assert + for action, message in modifications: + if action == "Add": + code_reviewer.feedback.append(message) + expected_feedback.append(message) + elif action == "Remove" and message in code_reviewer.feedback: + code_reviewer.feedback.remove(message) + expected_feedback.remove(message) + feedback = code_reviewer.get_feedback() + assert feedback == expected_feedback, f"Feedback list should be {expected_feedback} after {action} operation." + diff --git a/pytest.ini b/pytest.ini new file mode 100644 index 0000000000..cbfb36810e --- /dev/null +++ b/pytest.ini @@ -0,0 +1,10 @@ +[pytest] +markers = + valid: mark a test as valid scenario + invalid: mark a test as invalid scenario + syntax: mark a test as syntax scenario + negative: mark a test as negative scenario + regression: mark a test as regression scenario + smoke: mark a test as smoke scenario + positive: mark a test as positive scenario + mixed: mark a test as mixed scenario