#[cfg(test)]
mod tests {
use crate::{compile_and_run_with_config, VMConfig};
use glyph_types::Value;
#[test]
fn test_simple_literal_match() {
let code = r#"
@program(name="test_literal_match", version="1.0", requires=[])
def main() -> int:
let x = 42
match x:
case 42:
return 1
case 0:
return 2
case _:
return 3
"#;
let config = VMConfig::default();
let result = compile_and_run_with_config(code, config).unwrap();
assert_eq!(result, Value::Int(1));
}
#[test]
fn test_variable_pattern() {
let code = r#"
@program(name="test_variable_pattern", version="1.0", requires=[])
def main() -> int:
let x = 100
match x:
case 42:
return 1
case y:
return y
"#;
let config = VMConfig::default();
let result = compile_and_run_with_config(code, config).unwrap();
assert_eq!(result, Value::Int(100));
}
#[test]
fn test_wildcard_pattern() {
let code = r#"
@program(name="test_wildcard_pattern", version="1.0", requires=[])
def main() -> int:
let x = 999
match x:
case 42:
return 1
case 100:
return 2
case _:
return 99
"#;
let config = VMConfig::default();
let result = compile_and_run_with_config(code, config).unwrap();
assert_eq!(result, Value::Int(99));
}
#[test]
fn test_string_pattern() {
let code = r#"
@program(name="test_string_pattern", version="1.0", requires=[])
def main() -> str:
let msg = "hello"
match msg:
case "hi":
return "greeting"
case "hello":
return "world"
case _:
return "unknown"
"#;
let config = VMConfig::default();
let result = compile_and_run_with_config(code, config).unwrap();
assert_eq!(result, Value::Str("world".to_string()));
}
#[test]
fn test_bool_pattern() {
let code = r#"
@program(name="test_bool_pattern", version="1.0", requires=[])
def main() -> str:
let flag = True
match flag:
case True:
return "yes"
case False:
return "no"
"#;
let config = VMConfig::default();
let result = compile_and_run_with_config(code, config).unwrap();
assert_eq!(result, Value::Str("yes".to_string()));
}
#[test]
fn test_none_pattern() {
let code = r#"
@program(name="test_none_pattern", version="1.0", requires=[])
def main() -> int:
let val = None
match val:
case None:
return 0
case _:
return 1
"#;
let config = VMConfig::default();
let result = compile_and_run_with_config(code, config).unwrap();
assert_eq!(result, Value::Int(0));
}
#[test]
#[ignore] fn test_constructor_pattern() {
let code = r#"
@program(name="test_constructor_pattern", version="1.0", requires=[])
def main() -> int:
let result = Ok(42)
match result:
case Ok(value):
return value
case Err(msg):
return -1
"#;
let config = VMConfig::default();
let result = compile_and_run_with_config(code, config).unwrap();
assert_eq!(result, Value::Int(42));
}
}