Skip to content

Commit f59f8e6

Browse files
authored
Merge pull request #382 from MashyBasker/rust-example-unittest
2 parents 83fff52 + 968441b commit f59f8e6

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed

examples/unittest/rust.py

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
from kaizen.generator.unit_test import UnitTestGenerator
2+
3+
generator = UnitTestGenerator()
4+
code = '''
5+
struct Calculator {
6+
result: i32,
7+
}
8+
9+
impl Calculator {
10+
fn new() -> Calculator {
11+
Calculator { result: 0 }
12+
}
13+
14+
fn add(&mut self, a: i32, b: i32) -> i32 {
15+
self.result = a + b;
16+
self.result
17+
}
18+
19+
fn subtract(&mut self, a: i32, b: i32) -> i32 {
20+
self.result = a - b;
21+
self.result
22+
}
23+
24+
fn get_result(&self) -> i32 {
25+
self.result
26+
}
27+
}
28+
29+
fn greet(name: &str) -> String {
30+
format!("Hello, {}!", name)
31+
}
32+
33+
fn main() {
34+
let mut calc = Calculator::new();
35+
println!("{}", calc.add(5, 3)); // Should print 8
36+
println!("{}", calc.subtract(10, 4)); // Should print 6
37+
println!("{}", calc.get_result()); // Should print 6
38+
println!("{}", greet("Alice")); // Should print "Hello, Alice!"
39+
}
40+
'''
41+
42+
test_results = generator.run_tests()
43+
44+
for file_path, result in test_results.items():
45+
print(f"Results for {file_path}:")
46+
if "error" in result:
47+
print(f" Error: {result['error']}")
48+
else:
49+
print(f" Tests run: {result.get('tests_run', 'N/A')}")
50+
print(f" Failures: {result.get('failures', 'N/A')}")
51+
print(f" Errors: {result.get('errors', 'N/A')}")
52+
print()

0 commit comments

Comments
 (0)