Skip to content

Commit b6f55e6

Browse files
committed
Implementados Mais testes e agora cada Função de teste executa no seu próprio escopo
1 parent 01648fe commit b6f55e6

3 files changed

Lines changed: 311 additions & 180 deletions

File tree

src/interpreter/statement_execute.rs

Lines changed: 87 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ pub fn run_tests(stmt: &Statement) -> Result<Vec<TestResult>, String> {
4242
let mut results = Vec::new();
4343

4444
for test in env.scrape_tests() {
45-
let test_env = env.clone();
45+
let mut test_env = env.clone();
46+
test_env.push();
4647

4748
let stmt = match &test.body {
4849
Some(body) => *body.clone(),
@@ -64,6 +65,7 @@ pub fn run_tests(stmt: &Statement) -> Result<Vec<TestResult>, String> {
6465
));
6566
}
6667
}
68+
test_env.pop();
6769
}
6870

6971
Ok(results)
@@ -1260,6 +1262,46 @@ mod tests {
12601262
}
12611263
}
12621264

1265+
#[test]
1266+
fn test_run_tests_with_second_assert_fail() {
1267+
let teste1 = Statement::TestDef(Function {
1268+
name: "test_fail".to_string(),
1269+
kind: Type::TVoid,
1270+
params: Vec::new(),
1271+
body: Some(Box::new(Statement::Block(vec![
1272+
Statement::Assert(
1273+
Box::new(Expression::CTrue),
1274+
Box::new(Expression::CString("This test should pass".to_string())),
1275+
),
1276+
Statement::Assert(
1277+
Box::new(Expression::CFalse),
1278+
Box::new(Expression::CString(
1279+
"This second test should fail".to_string(),
1280+
)),
1281+
),
1282+
Statement::Assert(
1283+
Box::new(Expression::CTrue),
1284+
Box::new(Expression::CString(
1285+
"This test shouldn't run, but should pass".to_string(),
1286+
)),
1287+
),
1288+
]))),
1289+
});
1290+
let programa = Statement::Block(vec![teste1]);
1291+
match run_tests(&programa) {
1292+
Ok(resultados) => {
1293+
assert_eq!(resultados.len(), 1);
1294+
assert_eq!(resultados[0].name, "test_fail");
1295+
assert!(!resultados[0].result);
1296+
assert_eq!(
1297+
resultados[0].error,
1298+
Some("This second test should fail".to_string())
1299+
);
1300+
}
1301+
Err(e) => panic!("Test execution failed: {}", e),
1302+
}
1303+
}
1304+
12631305
#[test]
12641306
fn test_run_tests_without_asserts() {
12651307
let teste = Statement::TestDef(Function {
@@ -1332,5 +1374,49 @@ mod tests {
13321374
Err(e) => panic!("Test execution failed: {}", e),
13331375
}
13341376
}
1377+
#[test]
1378+
fn test_test_scope_isolation() {
1379+
// test_one: define x = 1, passa se x == 1
1380+
let test_one = Statement::TestDef(Function {
1381+
name: "test_one".to_string(),
1382+
kind: Type::TBool,
1383+
params: vec![],
1384+
body: Some(Box::new(Statement::Block(vec![
1385+
Statement::VarDeclaration("x".to_string(), Box::new(Expression::CInt(1))),
1386+
Statement::AssertEQ(
1387+
Box::new(Expression::Var("x".to_string())),
1388+
Box::new(Expression::CInt(1)),
1389+
Box::new(Expression::CString("x should be 1".to_string())),
1390+
),
1391+
]))),
1392+
});
1393+
1394+
// test_two: espera que x NÃO exista
1395+
let test_two = Statement::TestDef(Function {
1396+
name: "test_two".to_string(),
1397+
kind: Type::TBool,
1398+
params: vec![],
1399+
body: Some(Box::new(Statement::Block(vec![Statement::AssertFalse(
1400+
Box::new(Expression::Var("x".to_string())),
1401+
Box::new(Expression::CString("x should not be visible".to_string())),
1402+
)]))),
1403+
});
1404+
1405+
let stmt = Statement::Block(vec![test_one, test_two]);
1406+
1407+
let results = run_tests(&stmt).unwrap();
1408+
1409+
assert_eq!(results.len(), 2);
1410+
1411+
let r1 = &results[0];
1412+
let r2 = &results[1];
1413+
1414+
assert_eq!(r1.name, "test_one");
1415+
assert!(r1.result);
1416+
1417+
assert_eq!(r2.name, "test_two");
1418+
assert!(!r2.result);
1419+
assert_eq!(r2.error, Some("Variable 'x' not found".to_string())); // Erro é propagado de Expression::Var
1420+
}
13351421
}
13361422
}

src/parser/parser_stmt.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -505,6 +505,33 @@ mod tests {
505505
assert_eq!(parsed, expected);
506506
}
507507

508+
#[test]
509+
fn test_parse_test_function_definition_statement_valid_multiple_statements() {
510+
let input = r#"test test_example():
511+
x = 1;
512+
y = 2;
513+
assert(x == 1, "x deveria ser 1");
514+
end"#;
515+
let expected = Statement::TestDef(Function {
516+
name: "test_example".to_string(),
517+
kind: Type::TVoid,
518+
params: vec![],
519+
body: Some(Box::new(Statement::Block(vec![
520+
Statement::Assignment("x".to_string(), Box::new(Expression::CInt(1))),
521+
Statement::Assignment("y".to_string(), Box::new(Expression::CInt(2))),
522+
Statement::Assert(
523+
Box::new(Expression::EQ(
524+
Box::new(Expression::Var("x".to_string())),
525+
Box::new(Expression::CInt(1)),
526+
)),
527+
Box::new(Expression::CString("x deveria ser 1".to_string())),
528+
),
529+
]))),
530+
});
531+
let parsed = parse_test_function_definition_statement(input).unwrap().1;
532+
assert_eq!(parsed, expected);
533+
}
534+
508535
#[test]
509536
fn test_parse_test_function_definition_statement_with_spaces() {
510537
let input = "test test_spaces( ): x = 2; end";

0 commit comments

Comments
 (0)