-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Python file support in codemap and associated tests
This update includes the addition of Python file support in the 'codemap' feature. It introduces the ability to generate Python file outputs by adding it as a case in the switch statement in the 'output.go' file. It also provides new tests ensuring that this feature works correctly, through the 'output_python_test.go' file. Finally, it also adds a new 'python_output.go' file, contributing to the Python support.
- Loading branch information
1 parent
b8ba1a4
commit f3c98a6
Showing
5 changed files
with
434 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,292 @@ | ||
package codemap | ||
|
||
import ( | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
) | ||
|
||
func TestGeneratePythonOutput(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
files map[string]string | ||
maxLen int | ||
expected string | ||
}{ | ||
{ | ||
name: "Basic Python function and class", | ||
maxLen: 50, | ||
files: map[string]string{ | ||
"main.py": ` | ||
def greet(name): | ||
return f"Hello, {name}!" | ||
class Person: | ||
def __init__(self, name, age): | ||
self.name = name | ||
self.age = age | ||
def introduce(self): | ||
return f"My name is {self.name} and I'm {self.age} years old." | ||
if __name__ == "__main__": | ||
person = Person("Alice", 30) | ||
print(greet(person.name)) | ||
print(person.introduce()) | ||
`, | ||
}, | ||
expected: `<code_map> | ||
<file> | ||
<path>main.py</path> | ||
<file_map> | ||
def greet(name): | ||
pass | ||
class Person: | ||
def __init__(self, name, age): | ||
pass | ||
def introduce(self): | ||
pass | ||
if __name__ == "__main__": | ||
person = Person("Alice", 30) | ||
print(greet(person.name)) | ||
print(person.introduce()) | ||
</file_map> | ||
</file> | ||
</code_map> | ||
`, | ||
}, | ||
{ | ||
name: "Python class with decorators and docstrings", | ||
maxLen: 50, | ||
files: map[string]string{ | ||
"user.py": ` | ||
import datetime | ||
class User: | ||
""" | ||
Represents a user in the system. | ||
""" | ||
def __init__(self, username, email): | ||
self.username = username | ||
self.email = email | ||
self.created_at = datetime.datetime.now() | ||
@property | ||
def display_name(self): | ||
""" | ||
Returns the user's display name. | ||
""" | ||
return self.username.capitalize() | ||
@classmethod | ||
def from_dict(cls, data): | ||
""" | ||
Creates a User instance from a dictionary. | ||
""" | ||
return cls(data['username'], data['email']) | ||
def __str__(self): | ||
return f"User(username={self.username}, email={self.email})" | ||
def __repr__(self): | ||
return self.__str__() | ||
`, | ||
}, | ||
expected: `<code_map> | ||
<file> | ||
<path>user.py</path> | ||
<file_map> | ||
import datetime | ||
class User: | ||
""" | ||
Represents a user in the system. | ||
""" | ||
def __init__(self, username, email): | ||
pass | ||
@property | ||
def display_name(self): | ||
""" | ||
Returns the user's display name. | ||
""" | ||
pass | ||
@classmethod | ||
def from_dict(cls, data): | ||
""" | ||
Creates a User instance from a dictionary. | ||
""" | ||
pass | ||
def __str__(self): | ||
pass | ||
def __repr__(self): | ||
pass | ||
</file_map> | ||
</file> | ||
</code_map> | ||
`, | ||
}, | ||
{ | ||
name: "Python with multiple classes and functions", | ||
maxLen: 50, | ||
files: map[string]string{ | ||
"shapes.py": ` | ||
import math | ||
class Shape: | ||
def area(self): | ||
raise NotImplementedError("Subclass must implement abstract method") | ||
def perimeter(self): | ||
raise NotImplementedError("Subclass must implement abstract method") | ||
class Circle(Shape): | ||
def __init__(self, radius): | ||
self.radius = radius | ||
def area(self): | ||
return math.pi * self.radius ** 2 | ||
def perimeter(self): | ||
return 2 * math.pi * self.radius | ||
class Rectangle(Shape): | ||
def __init__(self, width, height): | ||
self.width = width | ||
self.height = height | ||
def area(self): | ||
return self.width * self.height | ||
def perimeter(self): | ||
return 2 * (self.width + self.height) | ||
def calculate_total_area(shapes): | ||
return sum(shape.area() for shape in shapes) | ||
if __name__ == "__main__": | ||
circle = Circle(5) | ||
rectangle = Rectangle(4, 6) | ||
print(f"Circle area: {circle.area()}") | ||
print(f"Rectangle perimeter: {rectangle.perimeter()}") | ||
print(f"Total area: {calculate_total_area([circle, rectangle])}") | ||
`, | ||
}, | ||
expected: `<code_map> | ||
<file> | ||
<path>shapes.py</path> | ||
<file_map> | ||
import math | ||
class Shape: | ||
def area(self): | ||
pass | ||
def perimeter(self): | ||
pass | ||
class Circle(Shape): | ||
def __init__(self, radius): | ||
pass | ||
def area(self): | ||
pass | ||
def perimeter(self): | ||
pass | ||
class Rectangle(Shape): | ||
def __init__(self, width, height): | ||
pass | ||
def area(self): | ||
pass | ||
def perimeter(self): | ||
pass | ||
def calculate_total_area(shapes): | ||
pass | ||
if __name__ == "__main__": | ||
circle = Circle(5) | ||
rectangle = Rectangle(4, 6) | ||
print(f"Circle area: {circle.area()}") | ||
print(f"Rectangle perimeter: {rectangle.perimeter()}") | ||
print(f"Total area: {calculate_total_area([circle, rectangle])}") | ||
</file_map> | ||
</file> | ||
</code_map> | ||
`, | ||
}, | ||
{ | ||
name: "Python with nested functions and lambda", | ||
maxLen: 50, | ||
files: map[string]string{ | ||
"nested.py": ` | ||
def outer_function(x): | ||
def inner_function(y): | ||
return x + y | ||
return inner_function | ||
lambda_func = lambda x: x * 2 | ||
def higher_order_function(func, value): | ||
return func(value) | ||
if __name__ == "__main__": | ||
closure = outer_function(10) | ||
print(closure(5)) | ||
print(lambda_func(3)) | ||
print(higher_order_function(lambda x: x ** 2, 4)) | ||
`, | ||
}, | ||
expected: `<code_map> | ||
<file> | ||
<path>nested.py</path> | ||
<file_map> | ||
def outer_function(x): | ||
pass | ||
lambda_func = lambda x: x * 2 | ||
def higher_order_function(func, value): | ||
pass | ||
if __name__ == "__main__": | ||
closure = outer_function(10) | ||
print(closure(5)) | ||
print(lambda_func(3)) | ||
print(higher_order_function(lambda x: x ** 2, 4)) | ||
</file_map> | ||
</file> | ||
</code_map> | ||
`, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
// Set up the in-memory file system | ||
memFS := setupInMemoryFS(tt.files) | ||
|
||
// Generate the output using GenerateOutput | ||
output, err := GenerateOutput(memFS, tt.maxLen) | ||
if err != nil { | ||
t.Fatalf("Failed to generate output: %v", err) | ||
} | ||
|
||
// Compare the output with the expected result | ||
if !assert.Equal(t, tt.expected, output) { | ||
t.Errorf("Unexpected output.\nExpected:\n%s\nGot:\n%s", tt.expected, output) | ||
} | ||
}) | ||
} | ||
} |
Oops, something went wrong.