s2s2lua 0.1.0

Transpile Stranded II .s2s scripts to Lua for OpenStranded
Documentation
// s2s2lua – transpile Stranded II .s2s scripts to Lua for OpenStranded
// Copyright (C) 2025  s2s2lua contributors
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.

use super::*;
use crate::parser::S2sParser;

fn compile(s2s: &str) -> String {
    let script = S2sParser::parse(s2s).unwrap();
    let opts = GenOptions::default();
    LuaGenerator::generate(&script, opts).unwrap()
}

#[test]
fn test_event_handler_output() {
    let lua = compile(r#"on:start { msg "Hello!"; }"#);
    assert!(lua.contains("core_api.on_event(\"start\", function()"));
    assert!(lua.contains("core_api.msg(\"Hello!\")"));
    assert!(lua.contains("end)"));
}

#[test]
fn test_command_output() {
    let lua = compile(r#"setpos "object", 5, 100, 0, 200;"#);
    assert!(lua.contains("core_api.setpos(\"object\", 5, 100, 0, 200)"));
}

#[test]
fn test_assignment_output() {
    let lua = compile(r#"$count = 5;"#);
    assert!(lua.contains("local count = 5"));
}

#[test]
fn test_compound_assign_output() {
    let lua = compile(r#"$count += 3; $count -= 1; $count++; $count--;"#);
    assert!(lua.contains("count = count + 3"));
    assert!(lua.contains("count = count - 1"));
    assert!(lua.contains("count = count + 1"));
    assert!(lua.contains("count = count - 1"));
}

#[test]
fn test_if_output() {
    let lua = compile(r#"if ((x==5) && (y>3)) { msg "yes"; } else { msg "no"; }"#);
    assert!(lua.contains("if ((x == 5) and (y > 3)) then"));
    assert!(lua.contains("else"));
    assert!(lua.contains("core_api.msg(\"yes\")"));
    assert!(lua.contains("core_api.msg(\"no\")"));
    assert!(lua.contains("end"));
}

#[test]
fn test_loop_count_output() {
    let lua = compile(r#"loop count,10 { msg loop_id(); }"#);
    assert!(lua.contains("core_api.loop(\"count\", 10, function(id)"));
    assert!(lua.contains("core_api.msg(id)"));
}

#[test]
fn test_return_command_output() {
    let lua = compile(r#"$id = create("item", 42);"#);
    assert!(lua.contains("local id = core_api.create(\"item\", 42)"));
}

#[test]
fn test_suppress_output() {
    let lua = compile(r#"@create "item", 999;"#);
    assert!(lua.contains("xpcall(function() core_api.create(\"item\", 999) end"));
}

#[test]
fn test_exit_output() {
    let lua = compile(r#"loop count,10 { exit; }"#);
    assert!(lua.contains("return true"));
}

#[test]
fn test_skip_skipevent() {
    let lua = compile(r#"skip; skipevent;"#);
    assert!(lua.contains("core_api.skip()"));
    assert!(lua.contains("core_api.skipevent()"));
}

#[test]
fn test_elseif() {
    let lua =
        compile(r#"if ((x==1)) { msg "one"; } elseif ((x==2)) { msg "two"; } else { msg "other"; }"#);
    assert!(lua.contains("elseif (x == 2) then"));
}

#[test]
fn test_type_rename() {
    let lua = compile(r#"type "object", 5;"#);
    assert!(lua.contains("core_api.type_of(\"object\", 5)"));
}