Add test files (indexers, search, CLI, integration)
This commit is contained in:
209
tests/fixtures/sample_code.py
vendored
Normal file
209
tests/fixtures/sample_code.py
vendored
Normal file
@@ -0,0 +1,209 @@
|
||||
"""Sample Python module for testing the code indexer."""
|
||||
|
||||
|
||||
def add(a, b):
|
||||
"""Add two numbers together.
|
||||
|
||||
Args:
|
||||
a: First number to add
|
||||
b: Second number to add
|
||||
|
||||
Returns:
|
||||
The sum of a and b
|
||||
|
||||
Example:
|
||||
>>> add(2, 3)
|
||||
5
|
||||
"""
|
||||
return a + b
|
||||
|
||||
|
||||
def multiply(a, b):
|
||||
"""Multiply two numbers.
|
||||
|
||||
Args:
|
||||
a: First number
|
||||
b: Second number
|
||||
|
||||
Returns:
|
||||
The product of a and b
|
||||
"""
|
||||
return a * b
|
||||
|
||||
|
||||
def greet(name: str, greeting: str = "Hello") -> str:
|
||||
"""Generate a greeting message.
|
||||
|
||||
Args:
|
||||
name: Name of the person to greet
|
||||
greeting: Greeting word to use
|
||||
|
||||
Returns:
|
||||
A formatted greeting string
|
||||
|
||||
Raises:
|
||||
ValueError: If name is empty
|
||||
"""
|
||||
if not name:
|
||||
raise ValueError("Name cannot be empty")
|
||||
return f"{greeting}, {name}!"
|
||||
|
||||
|
||||
class Calculator:
|
||||
"""A simple calculator class for basic arithmetic operations.
|
||||
|
||||
This class provides methods for performing addition, subtraction,
|
||||
multiplication, and division operations.
|
||||
|
||||
Attributes:
|
||||
memory: Current memory value for accumulator operations
|
||||
|
||||
Example:
|
||||
>>> calc = Calculator()
|
||||
>>> calc.add(5)
|
||||
>>> calc.multiply(2)
|
||||
>>> calc.get_memory()
|
||||
10
|
||||
"""
|
||||
|
||||
def __init__(self, initial_value: float = 0.0) -> None:
|
||||
"""Initialize the calculator with an optional starting value.
|
||||
|
||||
Args:
|
||||
initial_value: The starting value for the calculator
|
||||
"""
|
||||
self.memory = initial_value
|
||||
|
||||
def add(self, value: float) -> None:
|
||||
"""Add a value to the current memory.
|
||||
|
||||
Args:
|
||||
value: Number to add to memory
|
||||
"""
|
||||
self.memory += value
|
||||
|
||||
def subtract(self, value: float) -> None:
|
||||
"""Subtract a value from the current memory.
|
||||
|
||||
Args:
|
||||
value: Number to subtract from memory
|
||||
"""
|
||||
self.memory -= value
|
||||
|
||||
def multiply(self, value: float) -> None:
|
||||
"""Multiply the current memory by a value.
|
||||
|
||||
Args:
|
||||
value: Number to multiply by
|
||||
"""
|
||||
self.memory *= value
|
||||
|
||||
def divide(self, value: float) -> None:
|
||||
"""Divide the current memory by a value.
|
||||
|
||||
Args:
|
||||
value: Number to divide by
|
||||
|
||||
Raises:
|
||||
ZeroDivisionError: If value is zero
|
||||
"""
|
||||
if value == 0:
|
||||
raise ZeroDivisionError("Cannot divide by zero")
|
||||
self.memory /= value
|
||||
|
||||
def get_memory(self) -> float:
|
||||
"""Get the current memory value.
|
||||
|
||||
Returns:
|
||||
The current memory value
|
||||
"""
|
||||
return self.memory
|
||||
|
||||
def reset(self) -> None:
|
||||
"""Reset the memory to zero."""
|
||||
self.memory = 0.0
|
||||
|
||||
|
||||
class DataProcessor:
|
||||
"""A class for processing data with various operations.
|
||||
|
||||
This class supports filtering, mapping, and aggregating data
|
||||
from various input sources.
|
||||
|
||||
Attributes:
|
||||
data: Internal data storage
|
||||
processed_count: Number of items processed
|
||||
|
||||
Methods:
|
||||
load: Load data from a source
|
||||
filter: Filter data based on criteria
|
||||
map: Transform data elements
|
||||
aggregate: Calculate aggregate statistics
|
||||
"""
|
||||
|
||||
def __init__(self) -> None:
|
||||
"""Initialize the data processor."""
|
||||
self.data = []
|
||||
self.processed_count = 0
|
||||
|
||||
def load(self, items: list) -> None:
|
||||
"""Load data into the processor.
|
||||
|
||||
Args:
|
||||
items: List of items to process
|
||||
"""
|
||||
self.data = list(items)
|
||||
|
||||
def filter(self, predicate) -> list:
|
||||
"""Filter data based on a predicate function.
|
||||
|
||||
Args:
|
||||
predicate: Function that returns True for items to keep
|
||||
|
||||
Returns:
|
||||
Filtered list of items
|
||||
"""
|
||||
result = [item for item in self.data if predicate(item)]
|
||||
self.processed_count += len(result)
|
||||
return result
|
||||
|
||||
def map(self, transform) -> list:
|
||||
"""Transform data using a function.
|
||||
|
||||
Args:
|
||||
transform: Function to apply to each item
|
||||
|
||||
Returns:
|
||||
List of transformed items
|
||||
"""
|
||||
result = [transform(item) for item in self.data]
|
||||
self.processed_count += len(result)
|
||||
return result
|
||||
|
||||
def aggregate(self, func, initial=None):
|
||||
"""Aggregate data using a function.
|
||||
|
||||
Args:
|
||||
func: Aggregation function (e.g., sum, max, min)
|
||||
initial: Initial value for the aggregation
|
||||
|
||||
Returns:
|
||||
Aggregated result
|
||||
"""
|
||||
if initial is not None:
|
||||
result = func(self.data, initial)
|
||||
else:
|
||||
result = func(self.data)
|
||||
self.processed_count += 1
|
||||
return result
|
||||
|
||||
def get_stats(self) -> dict:
|
||||
"""Get processing statistics.
|
||||
|
||||
Returns:
|
||||
Dictionary with processing stats
|
||||
"""
|
||||
return {
|
||||
"total_items": len(self.data),
|
||||
"processed_count": self.processed_count,
|
||||
}
|
||||
Reference in New Issue
Block a user