Skip to content

Commit

Permalink
Add Python file support in codemap and associated tests
Browse files Browse the repository at this point in the history
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
spachava753 committed Oct 11, 2024
1 parent b8ba1a4 commit f3c98a6
Show file tree
Hide file tree
Showing 5 changed files with 434 additions and 2 deletions.
2 changes: 2 additions & 0 deletions codemap/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ func generateFileOutput(fsys fs.FS, path string, maxLiteralLen int) (string, err
output, err = generateGoFileOutput(src, maxLiteralLen)
case ".java":
output, err = generateJavaFileOutput(src, maxLiteralLen)
case ".py":
output, err = generatePythonFileOutput(src, maxLiteralLen)
default:
output = string(src)
}
Expand Down
292 changes: 292 additions & 0 deletions codemap/output_python_test.go
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)
}
})
}
}
Loading

0 comments on commit f3c98a6

Please sign in to comment.