bottest/tests/unit/basic/basic_compiler_goto_transform.rs

147 lines
3.5 KiB
Rust
Raw Normal View History

2025-12-23 18:41:29 -03:00
#![allow(unused_imports)]
#![allow(unused_variables)]
#![allow(dead_code)]
2025-12-23 18:41:29 -03:00
#[test]
2025-12-23 18:41:29 -03:00
fn test_is_label_line() {
assert!(is_label_line("start:"));
assert!(is_label_line(" mainLoop:"));
assert!(is_label_line("my_label:"));
assert!(is_label_line("label123:"));
assert!(!is_label_line("TALK \"hello:\""));
assert!(!is_label_line("' comment:"));
assert!(!is_label_line("CASE:"));
assert!(!is_label_line("123label:"));
assert!(!is_label_line("has space:"));
}
#[test]
2025-12-23 18:41:29 -03:00
fn test_extract_goto_target() {
assert_eq!(extract_goto_target("GOTO start"), Some("start".to_string()));
assert_eq!(
extract_goto_target(" GOTO myLabel"),
Some("myLabel".to_string())
);
assert_eq!(
extract_goto_target("IF x > 5 THEN GOTO done"),
Some("done".to_string())
);
assert_eq!(extract_goto_target("TALK \"hello\""), None);
}
#[test]
2025-12-23 18:41:29 -03:00
fn test_transform_line_simple_goto() {
assert_eq!(
transform_line("GOTO start"),
"__goto_label = \"start\"; continue;"
);
assert_eq!(
transform_line(" GOTO myLoop "),
"__goto_label = \"myLoop\"; continue;"
);
}
#[test]
2025-12-23 18:41:29 -03:00
fn test_transform_line_if_then_goto() {
let result = transform_line("IF x < 10 THEN GOTO start");
assert!(result.contains("if x < 10"));
assert!(result.contains("__goto_label = \"start\""));
assert!(result.contains("continue"));
}
#[test]
2025-12-23 18:41:29 -03:00
fn test_transform_line_if_goto_no_then() {
let result = transform_line("IF x < 10 GOTO start");
assert!(result.contains("if x < 10"));
assert!(result.contains("__goto_label = \"start\""));
}
#[test]
2025-12-23 18:41:29 -03:00
fn test_transform_line_not_goto() {
assert_eq!(transform_line("TALK \"Hello\""), "TALK \"Hello\"");
assert_eq!(transform_line("x = x + 1"), "x = x + 1");
assert_eq!(transform_line("ON ERROR GOTO 0"), "ON ERROR GOTO 0");
}
#[test]
2025-12-23 18:41:29 -03:00
fn test_has_goto_constructs() {
assert!(has_goto_constructs("start:\nTALK \"hi\"\nGOTO start"));
assert!(has_goto_constructs("IF x > 0 THEN GOTO done"));
assert!(!has_goto_constructs("TALK \"hello\"\nWAIT 1"));
2025-12-23 18:41:29 -03:00
assert!(!has_goto_constructs("ON ERROR GOTO 0"));
}
#[test]
2025-12-23 18:41:29 -03:00
fn test_transform_goto_simple() {
let input = r#"start:
TALK "Hello"
x = x + 1
IF x < 3 THEN GOTO start
TALK "Done""#;
let output = transform_goto(input);
assert!(output.contains("__goto_label"));
assert!(output.contains("while"));
assert!(output.contains("\"start\""));
assert!(output.contains("WARNING"));
}
#[test]
2025-12-23 18:41:29 -03:00
fn test_transform_goto_no_goto() {
let input = "TALK \"Hello\"\nTALK \"World\"";
let output = transform_goto(input);
2025-12-23 18:41:29 -03:00
assert_eq!(output, input);
}
#[test]
2025-12-23 18:41:29 -03:00
fn test_transform_goto_multiple_labels() {
let input = r#"start:
TALK "Start"
GOTO middle
middle:
TALK "Middle"
GOTO done
done:
TALK "Done""#;
let output = transform_goto(input);
assert!(output.contains("\"start\""));
assert!(output.contains("\"middle\""));
assert!(output.contains("\"done\""));
}
#[test]
2025-12-23 18:41:29 -03:00
fn test_infinite_loop_protection() {
let output = transform_goto("loop:\nGOTO loop");
assert!(output.contains("__goto_max_iterations"));
assert!(output.contains("throw"));
}