use super::*;
#[test]
fn test_event_handler() {
let script = S2sParser::parse(r#"on:start { msg "Hello!"; }"#).unwrap();
assert_eq!(script.statements.len(), 1);
match &script.statements[0] {
Stmt::EventHandler { event, body } => {
assert_eq!(event, "start");
assert_eq!(body.len(), 1);
match &body[0] {
Stmt::Command { name, args, suppress } => {
assert_eq!(name, "msg");
assert!(!suppress);
assert_eq!(args.len(), 1);
match &args[0] {
Expr::String(s) => assert_eq!(s, "Hello!"),
_ => panic!("expected string arg"),
}
}
_ => panic!("expected Command"),
}
}
_ => panic!("expected EventHandler"),
}
}
#[test]
fn test_if_condition() {
let script = S2sParser::parse(
r#"if ((x==5) && (y>3)) { msg "yes"; } else { msg "no"; }"#,
)
.unwrap();
assert_eq!(script.statements.len(), 1);
match &script.statements[0] {
Stmt::If {
condition,
then_body,
else_ifs,
else_body,
} => {
assert!(!then_body.is_empty());
assert!(else_ifs.is_empty());
assert!(!else_body.is_empty());
match condition {
Expr::Logical { op, .. } => {
assert!(matches!(op, LogicalOp::And));
}
_ => panic!("expected Logical"),
}
}
_ => panic!("expected If"),
}
}
#[test]
fn test_loop() {
let script = S2sParser::parse(r#"loop count,10 { msg loop_id(); }"#).unwrap();
assert_eq!(script.statements.len(), 1);
match &script.statements[0] {
Stmt::Loop { mode, arg, body } => {
assert_eq!(mode, "count");
assert_eq!(arg, "10");
assert_eq!(body.len(), 1);
}
_ => panic!("expected Loop"),
}
}
#[test]
fn test_assignment() {
let script = S2sParser::parse(r#"$count = 5; $name = "hello";"#).unwrap();
assert_eq!(script.statements.len(), 2);
match &script.statements[0] {
Stmt::Assign { var, value } => {
assert_eq!(var, "count");
match value.as_ref() {
Expr::Number(n) => assert_eq!(n, "5"),
_ => panic!("expected Number"),
}
}
_ => panic!("expected Assign"),
}
match &script.statements[1] {
Stmt::Assign { var, value } => {
assert_eq!(var, "name");
match value.as_ref() {
Expr::String(s) => assert_eq!(s, "hello"),
_ => panic!("expected String"),
}
}
_ => panic!("expected Assign"),
}
}
#[test]
fn test_compound_assign() {
let script =
S2sParser::parse(r#"$count += 3; $count -= 1; $count++; $count--;"#).unwrap();
assert_eq!(script.statements.len(), 4);
match &script.statements[0] {
Stmt::CompoundAssign { var, op, value } => {
assert_eq!(var, "count");
assert!(matches!(op, AssignOp::Add));
match value.as_ref() {
Expr::Number(n) => assert_eq!(n, "3"),
_ => panic!("expected Number"),
}
}
_ => panic!("expected CompoundAssign"),
}
}
#[test]
fn test_return_command() {
let script = S2sParser::parse(r#"$id = create("item", 42);"#).unwrap();
assert_eq!(script.statements.len(), 1);
match &script.statements[0] {
Stmt::Assign { var, value } => {
assert_eq!(var, "id");
match value.as_ref() {
Expr::CommandCall { name, args } => {
assert_eq!(name, "create");
assert_eq!(args.len(), 2);
match &args[0] {
Expr::String(s) => assert_eq!(s, "item"),
_ => panic!("expected String"),
}
match &args[1] {
Expr::Number(n) => assert_eq!(n, "42"),
_ => panic!("expected Number"),
}
}
_ => panic!("expected CommandCall"),
}
}
_ => panic!("expected Assign"),
}
}
#[test]
fn test_suppressed_command() {
let script = S2sParser::parse(r#"@create "item", 999;"#).unwrap();
assert_eq!(script.statements.len(), 1);
match &script.statements[0] {
Stmt::Command { name, args, suppress } => {
assert_eq!(name, "create");
assert!(*suppress);
assert_eq!(args.len(), 2);
}
_ => panic!("expected Command"),
}
}
#[test]
fn test_exit_skip() {
let script = S2sParser::parse(r#"exit; skip; skipevent;"#).unwrap();
assert_eq!(script.statements.len(), 3);
assert!(matches!(script.statements[0], Stmt::Exit));
assert!(matches!(script.statements[1], Stmt::Skip));
assert!(matches!(script.statements[2], Stmt::SkipEvent));
}
#[test]
fn test_real_random_events() {
let input = r#"on:changeday {
if (menu()==0){
$event=random(0,6);
if ($event==1){
if (count("object",136)<10){
$id=randomcreate("object",136,-100,-30);
if ($id>0){
setpos "object",$id,"self",random(-15,-5),"self";
$itemid=create("item",44);
setamount $itemid,random(1,5);
store $itemid,"object",$id;
}
}
}
}
}"#;
let script = S2sParser::parse(input).unwrap();
assert_eq!(script.statements.len(), 1);
match &script.statements[0] {
Stmt::EventHandler { event, body } => {
assert_eq!(event, "changeday");
assert!(!body.is_empty());
match &body[0] {
Stmt::If {
condition,
then_body,
..
} => {
match condition {
Expr::Condition { op, .. } => assert_eq!(op, "=="),
_ => panic!("expected Condition"),
}
assert!(!then_body.is_empty());
}
_ => panic!("expected If"),
}
}
_ => panic!("expected EventHandler"),
}
}
#[test]
fn test_simple_command() {
let script = S2sParser::parse(r#"msg "Hello World";"#).unwrap();
assert_eq!(script.statements.len(), 1);
match &script.statements[0] {
Stmt::Command { name, args, .. } => {
assert_eq!(name, "msg");
assert_eq!(args.len(), 1);
match &args[0] {
Expr::String(s) => assert_eq!(s, "Hello World"),
_ => panic!("expected string"),
}
}
_ => panic!("expected Command"),
}
}
#[test]
fn test_empty_script() {
let script = S2sParser::parse("").unwrap();
assert_eq!(script.statements.len(), 0);
}
#[test]
fn test_comments_preserved() {
let script = S2sParser::parse(
r#"// this is a comment
msg "hello";
/* block comment */
msg "world";"#,
)
.unwrap();
assert_eq!(script.statements.len(), 4);
assert!(matches!(&script.statements[0], Stmt::Comment(_)));
assert!(matches!(&script.statements[1], Stmt::Command { .. }));
assert!(matches!(&script.statements[2], Stmt::Comment(_)));
assert!(matches!(&script.statements[3], Stmt::Command { .. }));
}
#[test]
fn test_multiple_args() {
let script = S2sParser::parse(r#"setpos "object", 5, 100, 0, 200;"#).unwrap();
assert_eq!(script.statements.len(), 1);
match &script.statements[0] {
Stmt::Command { name, args, .. } => {
assert_eq!(name, "setpos");
assert_eq!(args.len(), 5);
match &args[0] {
Expr::String(s) => assert_eq!(s, "object"),
_ => panic!("expected string"),
}
match &args[1] {
Expr::Number(n) => assert_eq!(n, "5"),
_ => panic!("expected number"),
}
}
_ => panic!("expected Command"),
}
}
#[test]
fn test_loop_objects_syntax() {
let script = S2sParser::parse(r#"loop ("objects", 165) { msg loop_id(); }"#).unwrap();
match &script.statements[0] {
Stmt::Loop { mode, arg, body } => {
eprintln!("mode: {:?}, arg: {:?}, body.len: {}", mode, arg, body.len());
assert_eq!(mode, "objects");
assert_eq!(arg, "165");
assert_eq!(body.len(), 1);
}
_ => panic!("expected Loop"),
}
}