Merge branch 'ts-pack'

This commit is contained in:
Paul Gauthier 2024-08-23 10:16:34 -07:00
commit 69106200ff
4 changed files with 226 additions and 0 deletions

View file

@ -1,6 +1,7 @@
import os
import time
import unittest
from pathlib import Path
import git
@ -424,6 +425,58 @@ class TestRepoMapAllLanguages(unittest.TestCase):
# close the open cache files, so Windows won't error
del repo_map
def test_repo_map_sample_code_base(self):
# Path to the sample code base
sample_code_base = Path(__file__).parent.parent / "fixtures" / "sample-code-base"
# Path to the expected repo map file
expected_map_file = (
Path(__file__).parent.parent / "fixtures" / "sample-code-base-repo-map.txt"
)
# Ensure the paths exist
self.assertTrue(sample_code_base.exists(), "Sample code base directory not found")
self.assertTrue(expected_map_file.exists(), "Expected repo map file not found")
# Initialize RepoMap with the sample code base as root
io = InputOutput()
repomap_root = Path(__file__).parent.parent.parent
repo_map = RepoMap(
main_model=self.GPT35,
root=str(repomap_root),
io=io,
)
# Get all files in the sample code base
other_files = [str(f) for f in sample_code_base.rglob("*") if f.is_file()]
# Generate the repo map
generated_map_str = repo_map.get_repo_map([], other_files).strip()
# Read the expected map from the file
with open(expected_map_file, "r") as f:
expected_map = f.read().strip()
# Compare the generated map with the expected map
if generated_map_str != expected_map:
# If they differ, show the differences and fail the test
import difflib
diff = list(
difflib.unified_diff(
expected_map.splitlines(),
generated_map_str.splitlines(),
fromfile="expected",
tofile="generated",
lineterm="",
)
)
diff_str = "\n".join(diff)
self.fail(f"Generated map differs from expected map:\n{diff_str}")
# If we reach here, the maps are identical
self.assertEqual(generated_map_str, expected_map, "Generated map matches expected map")
if __name__ == "__main__":
unittest.main()

View file

@ -0,0 +1,55 @@
tests/fixtures/sample-code-base/sample.js:
⋮...
│function greet(name) {
│ return `Hello, ${name}!`;
⋮...
│function calculateCircleArea(radius) {
│ return Math.PI * radius * radius;
⋮...
│function isPrime(number) {
│ if (number <= 1) return false;
│ for (let i = 2; i <= Math.sqrt(number); i++) {
│ if (number % i === 0) return false;
│ }
│ return true;
⋮...
│function reverseString(str) {
│ return str.split('').reverse().join('');
⋮...
│function getRandomNumber(min, max) {
│ return Math.floor(Math.random() * (max - min + 1)) + min;
⋮...
│function filterEvenNumbers(numbers) {
│ return numbers.filter(num => num % 2 !== 0);
⋮...
│function factorial(n) {
│ if (n === 0 || n === 1) return 1;
│ return n * factorial(n - 1);
⋮...
tests/fixtures/sample-code-base/sample.py:
│class Car:
│ def __init__(self, make, model, year):
│ self.make = make
│ self.model = model
│ self.year = year
⋮...
│ def accelerate(self, increment):
⋮...
│ def brake(self, decrement):
⋮...
│ def honk(self):
⋮...
│class Garage:
│ def __init__(self):
⋮...
│ def add_car(self, car):
⋮...
│ def remove_car(self, car):
⋮...
│ def list_cars(self):
⋮...
│def main():
⋮...

View file

@ -0,0 +1,50 @@
// Sample JavaScript script with 7 functions
// 1. A simple greeting function
function greet(name) {
return `Hello, ${name}!`;
}
// 2. A function to calculate the area of a circle
function calculateCircleArea(radius) {
return Math.PI * radius * radius;
}
// 3. A function to check if a number is prime
function isPrime(number) {
if (number <= 1) return false;
for (let i = 2; i <= Math.sqrt(number); i++) {
if (number % i === 0) return false;
}
return true;
}
// 4. A function to reverse a string
function reverseString(str) {
return str.split('').reverse().join('');
}
// 5. A function to generate a random number within a range
function getRandomNumber(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
// 6. A function to filter out even numbers from an array
function filterEvenNumbers(numbers) {
return numbers.filter(num => num % 2 !== 0);
}
// 7. A function to calculate the factorial of a number
function factorial(n) {
if (n === 0 || n === 1) return 1;
return n * factorial(n - 1);
}
// Example usage
console.log(greet("Alice"));
console.log(calculateCircleArea(5));
console.log(isPrime(17));
console.log(reverseString("JavaScript"));
console.log(getRandomNumber(1, 100));
console.log(filterEvenNumbers([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]));
console.log(factorial(5));

View file

@ -0,0 +1,68 @@
class Car:
def __init__(self, make, model, year):
self.make = make
self.model = model
self.year = year
self.speed = 0
def accelerate(self, increment):
self.speed += increment
print(f"{self.make} {self.model} is now going {self.speed} mph.")
def brake(self, decrement):
self.speed = max(0, self.speed - decrement)
print(f"{self.make} {self.model} slowed down to {self.speed} mph.")
def honk(self):
print(f"{self.make} {self.model}: Beep beep!")
class Garage:
def __init__(self):
self.cars = []
def add_car(self, car):
self.cars.append(car)
print(f"Added {car.make} {car.model} to the garage.")
def remove_car(self, car):
if car in self.cars:
self.cars.remove(car)
print(f"Removed {car.make} {car.model} from the garage.")
else:
print(f"{car.make} {car.model} is not in the garage.")
def list_cars(self):
if self.cars:
print("Cars in the garage:")
for car in self.cars:
print(f"- {car.year} {car.make} {car.model}")
else:
print("The garage is empty.")
def main():
# Create some cars
car1 = Car("Toyota", "Corolla", 2020)
car2 = Car("Tesla", "Model 3", 2022)
# Demonstrate car methods
car1.accelerate(30)
car1.honk()
car1.brake(10)
# Create a garage and add cars
my_garage = Garage()
my_garage.add_car(car1)
my_garage.add_car(car2)
# List cars in the garage
my_garage.list_cars()
# Remove a car and list again
my_garage.remove_car(car1)
my_garage.list_cars()
if __name__ == "__main__":
main()