67 lines
2.3 KiB
Rust
67 lines
2.3 KiB
Rust
use std::fs;
|
|
use std::path::PathBuf;
|
|
|
|
use mlua::{Function, Lua, Table};
|
|
|
|
fn fixture_path(name: &str) -> PathBuf {
|
|
PathBuf::from(env!("CARGO_MANIFEST_DIR"))
|
|
.join("tests")
|
|
.join("fixtures")
|
|
.join(name)
|
|
}
|
|
|
|
fn load_fixture(name: &str) -> (Lua, Table) {
|
|
let lua = Lua::new();
|
|
let src = fs::read_to_string(fixture_path(name)).unwrap();
|
|
let table: Table = lua.load(&src).set_name(name).eval().unwrap();
|
|
(lua, table)
|
|
}
|
|
|
|
#[test]
|
|
fn lua_line_framer_fixture_splits_crlf_and_lf() {
|
|
let (lua, table) = load_fixture("lua_line_framer.lua");
|
|
let feed: Function = table.get("feed").unwrap();
|
|
|
|
let input = lua.create_string(b"hello\r\nworld\n").unwrap();
|
|
let frames: Vec<String> = feed.call(input).unwrap();
|
|
assert_eq!(frames, vec!["hello", "world"]);
|
|
}
|
|
|
|
#[test]
|
|
fn lua_line_framer_fixture_handles_split_chunks_and_flush() {
|
|
let (lua, table) = load_fixture("lua_line_framer.lua");
|
|
let feed: Function = table.get("feed").unwrap();
|
|
let flush: Function = table.get("flush").unwrap();
|
|
let pending_len: Function = table.get("pending_len").unwrap();
|
|
let reset: Function = table.get("reset").unwrap();
|
|
|
|
let frames: Vec<String> = feed.call(lua.create_string(b"par").unwrap()).unwrap();
|
|
assert!(frames.is_empty());
|
|
assert_eq!(pending_len.call::<usize>(()).unwrap(), 3);
|
|
|
|
let frames: Vec<String> = feed.call(lua.create_string(b"tial\n").unwrap()).unwrap();
|
|
assert_eq!(frames, vec!["partial"]);
|
|
|
|
let flushed: Option<String> = flush.call(()).unwrap();
|
|
assert_eq!(flushed.as_deref(), None);
|
|
|
|
reset.call::<()>(()).unwrap();
|
|
assert_eq!(pending_len.call::<usize>(()).unwrap(), 0);
|
|
}
|
|
|
|
#[test]
|
|
fn lua_text_decoder_fixture_decodes_text_and_rejects_empty() {
|
|
let (lua, table) = load_fixture("lua_text_decoder.lua");
|
|
let decode: Function = table.get("decode").unwrap();
|
|
|
|
let decoded: Option<Table> = decode.call(lua.create_string(b"abc").unwrap()).unwrap();
|
|
let decoded = decoded.expect("non-empty frame should decode");
|
|
let kind: String = decoded.get("kind").unwrap();
|
|
let data: String = decoded.get("data").unwrap();
|
|
assert_eq!(kind, "text");
|
|
assert_eq!(data, "abc");
|
|
|
|
let empty: Option<Table> = decode.call(lua.create_string(b"").unwrap()).unwrap();
|
|
assert!(empty.is_none(), "empty frame should be rejected");
|
|
}
|