use rustine::exec::{execute, serialize_execution, RuntimeFormat};
use rustine::parser::lexer::lex;
use rustine::parser::syntax::parse_gel_document;
const SYNTAX: &str = include_str!("../fixtures/parity/linesplit/syntax1.gel");
const INPUT: &str = include_str!("../fixtures/parity/linesplit/input1.txt");
fn run_linesplit(format: RuntimeFormat) -> String {
let tokens = lex(SYNTAX).expect("lex syntax");
let mut doc = parse_gel_document(&tokens).expect("parse syntax");
let exec = execute(&mut doc, "input", INPUT).expect("execute");
assert!(exec.error.is_none(), "unexpected error: {:?}", exec.error);
serialize_execution(&exec, format)
}
#[test]
fn parity_linesplit_json_line_count() {
let json = run_linesplit(RuntimeFormat::Json);
let parsed: serde_json::Value = serde_json::from_str(&json).unwrap_or_else(|e| panic!("valid JSON: {e}\n{json}"));
let output = parsed
.get("output")
.unwrap_or_else(|| panic!("missing 'output' key\n{json}"));
let lines = output
.get("line")
.unwrap_or_else(|| panic!("missing 'line' key\n{json}"));
let lines = lines
.as_array()
.unwrap_or_else(|| panic!("line is not array: {lines}\nfull: {json}"));
assert_eq!(lines.len(), 10, "expected 10 lines, got {}\n{}", lines.len(), json);
let non_empty = lines
.iter()
.filter(|l| l.get("#text").and_then(|t| t.as_str()).is_some_and(|s| !s.is_empty()))
.count();
assert_eq!(non_empty, 8, "expected 8 non-empty lines, got {non_empty}\n{json}");
}
#[test]
fn parity_linesplit_json_content() {
let json = run_linesplit(RuntimeFormat::Json);
assert!(json.contains("a nice line"), "missing 'a nice line'\n{json}");
assert!(json.contains("another & line"), "missing 'another & line'\n{json}");
assert!(json.contains("something else"), "missing 'something else'\n{json}");
assert!(json.contains("my name is foo"), "missing 'my name is foo'\n{json}");
assert!(json.contains("your name is bar"), "missing 'your name is bar'\n{json}");
assert!(json.contains("yet a nice line"), "missing 'yet a nice line'\n{json}");
assert!(json.contains("yet another line"), "missing 'yet another line'\n{json}");
assert!(
json.contains("yet something else"),
"missing 'yet something else'\n{json}"
);
}
#[test]
fn parity_linesplit_json_structure() {
let json = run_linesplit(RuntimeFormat::Json);
assert!(json.contains("\"line\""), "missing 'line' key\n{json}");
}
#[test]
fn parity_linesplit_xml() {
let xml = run_linesplit(RuntimeFormat::Xml);
assert!(xml.contains("a nice line"), "missing 'a nice line'\n{xml}");
assert!(xml.contains("another & line"), "missing escaped ampersand\n{xml}");
assert!(
xml.contains("yet something else"),
"missing 'yet something else'\n{xml}"
);
}
#[test]
fn parity_linesplit_yaml() {
let yaml = run_linesplit(RuntimeFormat::Yaml);
assert!(yaml.contains("a nice line"), "missing 'a nice line'\n{yaml}");
assert!(
yaml.contains("yet something else"),
"missing 'yet something else'\n{yaml}"
);
}