-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_complete_implementation.py
More file actions
153 lines (120 loc) · 5.6 KB
/
test_complete_implementation.py
File metadata and controls
153 lines (120 loc) · 5.6 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
153
#!/usr/bin/env python3
"""Comprehensive test for the complete RegexGenerator implementation."""
import sys
import os
sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'src'))
def test_full_implementation():
"""Test the complete implementation end-to-end."""
try:
# Test imports
import regexgen
from regexgen.patterns.ast import PatternAST, LiteralNode, CharacterClassNode
from regexgen.algorithms.simulated_annealing import SimulatedAnnealing, SAConfig
from regexgen.scoring.fitness import MultiCriteriaScorer, ScoringMode
from regexgen.validation.validator import PatternValidator
from regexgen.patterns.mutations import PatternMutator
print("✓ All imports successful")
# Test basic AST functionality
literal = LiteralNode("test")
ast = PatternAST(literal)
assert ast.to_regex() == "test"
assert ast.complexity() == 4
print("✓ AST functionality works")
# Test character class
char_class = CharacterClassNode(characters={'a', 'b', 'c'})
char_ast = PatternAST(char_class)
regex_str = char_ast.to_regex()
assert '[' in regex_str and ']' in regex_str
print(f"✓ Character class works: {regex_str}")
# Test pattern mutation
mutator = PatternMutator()
mutated = mutator.mutate(ast)
assert mutated.to_regex() != "" # Should produce some pattern
print("✓ Pattern mutation works")
# Test fitness scoring
scorer = MultiCriteriaScorer(mode=ScoringMode.BALANCED)
positive_examples = ["test", "best", "rest"]
negative_examples = ["hello", "world"]
fitness = scorer.score(ast, positive_examples, negative_examples)
assert 0 <= fitness.total_score <= 1
assert fitness.positive_total == 3
assert fitness.negative_total == 2
print(f"✓ Fitness scoring works: score={fitness.total_score:.3f}")
# Test validation
validator = PatternValidator()
validation = validator.validate(ast, positive_examples, negative_examples)
assert validation.regex_string == "test"
assert not validation.timeout_occurred
print("✓ Pattern validation works")
# Test simple optimization
config = SAConfig(max_iterations=10, max_complexity=20, timeout_seconds=5)
optimizer = SimulatedAnnealing(config)
simple_positives = ["abc", "def", "ghi"]
simple_negatives = ["123", "xyz"]
print("✓ Running simple optimization test...")
result = optimizer.optimize(simple_positives, simple_negatives, scorer)
assert result.best_pattern is not None
assert result.best_fitness.total_score >= 0
assert result.iterations > 0
print(f"✓ Optimization works: {result.best_pattern.to_regex()} (score: {result.best_fitness.total_score:.3f})")
return True
except Exception as e:
print(f"✗ Test failed: {e}")
import traceback
traceback.print_exc()
return False
def test_cli_integration():
"""Test CLI integration without actually running subprocess."""
try:
# Test that CLI imports work
from regexgen.cli.main import cli, Algorithm, ScoringMode as CLIScoringMode
print("✓ CLI imports work")
# Test enum values
assert Algorithm.SIMULATED_ANNEALING.value == "sa"
assert CLIScoringMode.BALANCED.value == "balanced"
print("✓ CLI enums work")
return True
except Exception as e:
print(f"✗ CLI integration test failed: {e}")
return False
def test_pattern_examples():
"""Test with realistic pattern examples."""
try:
from regexgen.algorithms.simulated_annealing import SimulatedAnnealing, SAConfig
from regexgen.scoring.fitness import MultiCriteriaScorer, ScoringMode
# Test simple digit patterns
positives = ["123", "456", "789"]
negatives = ["abc", "12a", "1234"]
config = SAConfig(max_iterations=50, max_complexity=30, timeout_seconds=10)
optimizer = SimulatedAnnealing(config)
scorer = MultiCriteriaScorer(mode=ScoringMode.MINIMAL)
print("✓ Testing digit pattern generation...")
result = optimizer.optimize(positives, negatives, scorer)
generated_regex = result.best_pattern.to_regex()
print(f"✓ Generated pattern: {generated_regex}")
print(f"✓ Score: {result.best_fitness.total_score:.3f}")
print(f"✓ Positive matches: {result.best_fitness.positive_matches}/{len(positives)}")
print(f"✓ Negative rejections: {result.best_fitness.negative_matches}/{len(negatives)}")
return True
except Exception as e:
print(f"✗ Pattern example test failed: {e}")
return False
if __name__ == "__main__":
print("🧪 Testing complete RegexGenerator implementation...")
print()
success = True
print("1. Testing full implementation...")
success &= test_full_implementation()
print()
print("2. Testing CLI integration...")
success &= test_cli_integration()
print()
print("3. Testing realistic examples...")
success &= test_pattern_examples()
print()
if success:
print("🎉 All tests passed! Complete implementation is working correctly.")
print("RegexGenerator is ready for use!")
else:
print("❌ Some tests failed. Check the errors above.")
sys.exit(1)