-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample-script.py
More file actions
152 lines (120 loc) · 4.27 KB
/
Copy pathexample-script.py
File metadata and controls
152 lines (120 loc) · 4.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
"""
Example script demonstrating the mutation-guided test generator on a simple calculator module.
"""
import os
import shutil
import tempfile
def setup_example_project():
"""Set up an example project with a calculator module and basic tests."""
# Create a temporary directory
temp_dir = tempfile.mkdtemp()
src_dir = os.path.join(temp_dir, "src")
tests_dir = os.path.join(temp_dir, "tests")
# Create directories
os.makedirs(src_dir)
os.makedirs(tests_dir)
# Create calculator.py
calc_content = """# calculator.py
def add(a, b):
"""Add two numbers and return the result."""
return a + b
def subtract(a, b):
"""Subtract b from a and return the result."""
return a - b
def multiply(a, b):
"""Multiply two numbers and return the result."""
return a * b
def divide(a, b):
"""Divide a by b and return the result."""
if b == 0:
raise ValueError("Cannot divide by zero")
return a / b
def calculate_discount(price, discount_percent):
"""Calculate the final price after applying a discount."""
if not (0 <= discount_percent <= 100):
raise ValueError("Discount percentage must be between 0 and 100")
discount = price * (discount_percent / 100)
return price - discount
def is_prime(n):
"""Check if a number is prime."""
if n <= 1:
return False
if n <= 3:
return True
if n % 2 == 0 or n % 3 == 0:
return False
i = 5
while i * i <= n:
if n % i == 0 or n % (i + 2) == 0:
return False
i += 6
return True
"""
# Create test_calculator.py
test_content = """# test_calculator.py
import unittest
import sys
import os
# Add the src directory to the path so we can import calculator
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "src")))
import calculator
class TestCalculator(unittest.TestCase):
def test_add(self):
self.assertEqual(calculator.add(2, 3), 5)
self.assertEqual(calculator.add(-1, 1), 0)
def test_subtract(self):
self.assertEqual(calculator.subtract(5, 3), 2)
self.assertEqual(calculator.subtract(10, 10), 0)
def test_multiply(self):
self.assertEqual(calculator.multiply(3, 4), 12)
self.assertEqual(calculator.multiply(0, 5), 0)
def test_divide(self):
self.assertEqual(calculator.divide(10, 2), 5)
self.assertEqual(calculator.divide(7, 2), 3.5)
with self.assertRaises(ValueError):
calculator.divide(5, 0)
def test_calculate_discount(self):
self.assertEqual(calculator.calculate_discount(100, 20), 80)
if __name__ == "__main__":
unittest.main()
"""
# Write the files
with open(os.path.join(src_dir, "calculator.py"), 'w') as f:
f.write(calc_content)
with open(os.path.join(tests_dir, "test_calculator.py"), 'w') as f:
f.write(test_content)
return temp_dir, src_dir, tests_dir
def run_test_generator(src_dir, tests_dir):
"""Run the mutation-guided test generator on the example project."""
from mutation_guided_test_generator import MutationGuidedTestGenerator
# Create the generator
generator = MutationGuidedTestGenerator(
source_dir=src_dir,
test_dir=tests_dir
)
# Run the generator
result = generator.run()
print(result)
# Print the updated test file
with open(os.path.join(tests_dir, "test_calculator.py"), 'r') as f:
print("\nUpdated test file:")
print(f.read())
def cleanup(temp_dir):
"""Clean up the temporary directory."""
shutil.rmtree(temp_dir)
def main():
"""Main function to demonstrate the test generator."""
temp_dir, src_dir, tests_dir = setup_example_project()
try:
print(f"Created example project in {temp_dir}")
print(f"Source directory: {src_dir}")
print(f"Test directory: {tests_dir}")
print("\nRunning mutation-guided test generator...")
run_test_generator(src_dir, tests_dir)
finally:
# Uncomment to clean up the temporary directory
# cleanup(temp_dir)
print(f"\nExample project files are in {temp_dir}")
print("You can inspect them and then delete the directory manually.")
if __name__ == "__main__":
main()