use indoc::indoc;
use quilt::langs::omni::Omni;
use quilt::prelude::*;
use quilt::term::STerm;
fn expand_py(code: &str) -> Result<String> {
let mut omni = Omni::default();
let q = omni.parse_lang("py", code)?;
Ok(omni.expand_lang("py", &q)?.coparse())
}
#[test]
fn simple() -> Result<()> {
let out = expand_py("x = ↖1 + 2↗")?;
insta::assert_snapshot!(out);
Ok(())
}
#[test]
fn quote_expr() -> Result<()> {
let out = expand_py("↖1 + 2↗")?;
insta::assert_snapshot!(out);
Ok(())
}
#[test]
fn variadic() -> Result<()> {
let out = expand_py(indoc! {r#"
↖def foo():
↙stmts↘
print("World")
↗
"#})?;
assert!(
out.contains(r#"tb("block").e("#),
"a block with a dynamic child should use a fluent .e() emit chain; got:\n{out}"
);
let literal = expand_py(indoc! {r#"
↖def foo():
print("Hello")
print("World")
↗
"#})?;
assert!(
literal.contains(r#"tb("block").c(tb("call")"#),
"a hole-free block should use plain .c() children; got:\n{literal}"
);
assert!(
literal.ends_with(".b()"),
"expansion should end with a .b() build call; got:\n{literal}"
);
Ok(())
}
#[test]
fn unquote() -> Result<()> {
let out = expand_py("↖↖↙1↘↗↗")?;
assert!(
out.contains("unquote("),
"nested unquote should emit an unquote(..) call; got:\n{out}"
);
Ok(())
}
#[test]
fn heterogeneous_lift_into_html() -> Result<()> {
let out = expand_py("t = html↖<p>↙↑(title)↘</p>↗")?;
assert!(
out.contains("qlift_html(title)"),
"lift into an html quote should spell qlift_html; got:\n{out}"
);
Ok(())
}
#[test]
fn homogeneous_lift_is_prefix() -> Result<()> {
let out = expand_py("t = ↖[↙↑(n)↘]↗")?;
assert!(
out.contains("qlift(n)"),
"homogeneous lift should spell the prefix qlift function; got:\n{out}"
);
Ok(())
}
#[test]
fn bare_tuple_quote() -> Result<()> {
let out = expand_py("p = ↖↙a↘, ↙b↘↗")?;
assert!(
out.contains(r#"tb("tuple_expression").e(a).e(sym(",")).w(" ").e(b)"#),
"a bare tuple quote should keep the tuple_expression whole; got:\n{out}"
);
Ok(())
}
#[test]
fn block_body_dedents_at_top_level() -> Result<()> {
let indented = expand_py("x = ↖\n a = 1\n b = 2\n↗")?;
let flush = expand_py("x = ↖\na = 1\nb = 2\n↗")?;
assert_eq!(
indented, flush,
"an indented block body should dedent to the flush-left expansion"
);
Ok(())
}
#[test]
fn inline_body_is_not_dedented() -> Result<()> {
let out = expand_py("x = ↖def f():\n return 1\n↗")?;
assert!(
out.contains(r#".p(" ").n().c(tb("block").c(tb("return_statement")"#),
"inline-opened body keeps its meaningful indentation (return nested in \
the function block, not dedented to a sibling); got:\n{out}"
);
Ok(())
}
#[test]
fn host_emit_unsupported() {
for code in [
indoc! {r#"
out = ↖def f():
print("start")
↙
for n in names:
↖print(↙name(n)↘)↗.←
↘
↗"#},
"x = ←",
] {
let msg = expand_py(code).unwrap_err().to_string();
assert!(msg.contains("python can't emit"), "{msg}");
assert!(msg.contains("your own `tb(..)` builder"), "{msg}");
}
}
#[test]
fn host_emit_deferred_in_quote() -> Result<()> {
insta::assert_snapshot!(expand_py("x = ↖y = ↖z = ←↗↗")?);
Ok(())
}
#[test]
fn variadic_chain_survives_emit_rejection() -> Result<()> {
let out = expand_py(indoc! {r#"
↖def foo():
↙stmts↘
print("World")
↗
"#})?;
assert!(
out.contains(r#"tb("block").e("#),
"wrap_child's .e() chain is independent of emit_str; got:\n{out}"
);
Ok(())
}
#[test]
fn a_backslash_in_a_quote_survives_the_lift() -> Result<()> {
let out = expand_py(r#"q = ↖x = "a\\b"↗"#)?;
assert!(
out.contains(r#"leaf("escape_sequence", "\\\\")"#),
"the escape sequence should be lifted as two backslashes; got:\n{out}"
);
Ok(())
}
#[test]
fn a_flush_left_string_line_is_lifted_as_an_escaped_newline() -> Result<()> {
let out = expand_py(indoc! {r#"
q = ↖def f():
return """a
b"""
↗
"#})?;
assert!(
out.contains(r#".w("a").w("\n").w("b")"#),
"the flush-left line should follow an escaped raw newline; got:\n{out}"
);
Ok(())
}