Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions AI Code Reviewer/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
ast
pycodestyle
AI_Code_Reviewer
pycodestyle
pytest
155 changes: 155 additions & 0 deletions AI Code Reviewer/test_CodeReviewerAnalyzePythonCode.py
Original file line number Diff line number Diff line change
@@ -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"
117 changes: 117 additions & 0 deletions AI Code Reviewer/test_CodeReviewerCheckCodeStyle.py
Original file line number Diff line number Diff line change
@@ -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"
Loading