use indoc::indoc;
use quilt::langs::omni::Omni;
use quilt::prelude::*;
use quilt::term::STerm;
fn roundtrip(code: &str) -> Result<()> {
let mut omni = Omni::default();
let q = omni.parse(code)?;
assert_eq!(code, q.coparse_quilt());
Ok(())
}
fn expand(code: &str) -> Result<String> {
let mut omni = Omni::default();
let q = omni.parse(code)?;
Ok(omni.expand(&q)?.coparse())
}
#[test]
fn roundtrip_statement() -> Result<()> {
roundtrip("const Q: T = sql↖SELECT id FROM t WHERE org = ↙org↘↗;\n")
}
#[test]
fn roundtrip_bare_expression() -> Result<()> {
roundtrip("const P: T = sql↖org = ↙name↘↗;\n")
}
#[test]
fn roundtrip_terminated_statement() -> Result<()> {
roundtrip("const Q: T = sql↖SELECT 1;↗;\n")
}
#[test]
fn roundtrip_statement_hole() -> Result<()> {
roundtrip(indoc! {r#"
const S: T = sql↖
SELECT 1;
↙stmt↘;
SELECT 2;
↗;
"#})
}
#[test]
fn roundtrip_final_statement_hole() -> Result<()> {
roundtrip(indoc! {r#"
const S: T = sql↖
SELECT 1;
↙stmt↘
↗;
"#})
}
#[test]
fn roundtrip_several_statement_holes() -> Result<()> {
roundtrip(indoc! {r#"
const S: T = sql↖
↙a↘;
SELECT 2;
↙b↘;
↗;
"#})
}
#[test]
fn statement_hole_expands_into_the_program() -> Result<()> {
let out = expand(indoc! {r#"
const S: T = sql↖
SELECT 1;
↙stmt↘;
↗;
"#})?;
assert!(
out.contains("stmt.emit(&mut b_)"),
"the hole should be emitted into the program builder, got:\n{out}"
);
assert!(
!out.contains("keyword_select\", \"SELECT\").w(\" \").c(&stmt"),
"no synthetic SELECT may survive around the hole:\n{out}"
);
assert_eq!(
out.matches("\"SELECT\"").count(),
1,
"the wrapper's SELECT leaked into the output:\n{out}"
);
Ok(())
}
#[test]
fn unterminated_mid_script_hole_is_still_an_error() {
let mut omni = Omni::default();
let err = omni
.parse(indoc! {r#"
const S: T = sql↖
SELECT 1;
↙stmt↘
SELECT 2;
↗;
"#})
.expect_err("a statement hole with no separator before the next statement must fail");
let msg = format!("{err}");
assert!(
msg.contains("__QUILT_HOLE__"),
"the original parse error should be reported, not a wrapper artefact: {msg}"
);
}
#[test]
fn expression_hole_on_a_shared_line_is_untouched() -> Result<()> {
roundtrip("const Q: T = sql↖SELECT ↙col↘, x FROM t↗;\n")
}
#[test]
fn authored_select_of_a_hole_survives() -> Result<()> {
let code = indoc! {r#"
const S: T = sql↖
SELECT ↙col↘;
↙stmt↘;
↗;
"#};
roundtrip(code)?;
let out = expand(code)?;
assert_eq!(
out.matches("\"SELECT\"").count(),
1,
"the authored SELECT should survive and no other should appear:\n{out}"
);
assert!(
out.contains("col"),
"the authored select's hole is gone:\n{out}"
);
Ok(())
}