use indoc::indoc;
use pretty_assertions::assert_eq;
use quilt::lang::{one_liner, Language};
use quilt::langs::omni::Omni;
use quilt::langs::python::lang::PythonLanguage;
use quilt::prelude::*;
use quilt::term::STerm;
fn expand(lang: &str, code: &str) -> Result<String> {
let mut omni = Omni::default();
let term = omni.parse_lang(lang, code.trim())?;
let expanded = omni.expand_lang(lang, &term)?;
Ok(expanded.coparse())
}
fn roundtrip(code: &str) -> Result<()> {
roundtrip_lang("rs", code)
}
fn roundtrip_lang(lang: &str, code: &str) -> Result<()> {
let code = code.trim();
let mut omni = Omni::default();
let term = omni.parse_lang(lang, code)?;
let code2 = term.coparse_quilt();
assert_eq!(code, code2);
Ok(())
}
#[test]
fn rust_hello() -> Result<()> {
roundtrip(indoc! {r#"
fn hello() {
println!("Hello, world!");
}
"#})
}
#[test]
fn python_hello() -> Result<()> {
roundtrip_lang(
"py",
indoc! {r#"
def hello():
print("Hello, world!")
"#},
)
}
#[test]
fn rs_py() -> Result<()> {
roundtrip(indoc! {r#"
fn hello() {
let code = py↖
def hello():
print("Hello, world!")
↗;
println!(code);
}
"#})
}
#[test]
fn rs_py_minimal() -> Result<()> {
roundtrip(indoc! {r#"
const X: T = py↖1↗;
"#})
}
#[test]
fn squash() -> Result<()> {
let rs = "def f(): pass";
let mut py = PythonLanguage::default();
let qterm = py.parse_file(&one_liner(rs))?;
let block = qterm[4].clone();
dbg!(&block);
assert_eq!(block.coparse(), "pass");
let squashed = block.squash();
assert_eq!(squashed.coparse(), "pass");
Ok(())
}
#[test]
fn whitespace_ownership() -> Result<()> {
let rs = indoc! {r#"
const X: T = py↖ 1 ↗;
"#};
let mut omni = Omni::default();
let term = omni.parse(rs)?;
assert_eq!(rs, term.coparse());
assert_eq!("py↖ 1 ↗", term[5].coparse());
assert_eq!(" 1 ", term[5][0].coparse());
Ok(())
}
#[test]
fn rs_py_multiline() -> Result<()> {
roundtrip(indoc! {r#"
const X: T = py↖
123
↗;
"#})
}
#[test]
fn rs_py_multiline_no_indent() -> Result<()> {
roundtrip(indoc! {r#"
const X: T = py↖
123
↗;
"#})
}
#[test]
fn expr() -> Result<()> {
roundtrip_lang("py", "1 ")
}
#[test]
fn rs_empty() -> Result<()> {
roundtrip(indoc! {r#"
const X: T = ↖↗;
"#})
}
#[test]
fn rs_empty_nl() -> Result<()> {
roundtrip(indoc! {r#"
const X: T = ↖
↗;
"#})
}
#[test]
fn rs_empty_nl_2() -> Result<()> {
roundtrip(indoc! {r#"
const X: T = ↖
↗;
"#})
}
#[test]
fn rs_py_empty() -> Result<()> {
roundtrip(indoc! {r#"
const X: T = py↖123
↗;
"#})
}
#[test]
fn rs_stmt() -> Result<()> {
roundtrip(indoc! {r#"
let code = ↖println!("Hello, world!");↗;
"#})
}
#[test]
fn rs_var() -> Result<()> {
roundtrip(indoc! {r#"
let code = ↖let ↙foo↘ = "bar";↗;
"#})
}
#[test]
fn reduce_expands_homogeneous() -> Result<()> {
let out = expand(
"rs",
indoc! {r#"
fn main() {
let result = program.↓;
}
"#},
)?;
assert_eq!(
out.trim(),
"fn main() {\n let result = program.reduce();\n}"
);
Ok(())
}
#[test]
fn hetero_reduce_py_coparse() -> Result<()> {
let out = expand(
"rs",
indoc! {r#"
fn main() {
let result = program.py↓;
}
"#},
)?;
assert_eq!(
out.trim(),
"fn main() {\n let result = program.reduce_py();\n}"
);
Ok(())
}
#[test]
fn hetero_reduce_py_expands_to_reduce_py() -> Result<()> {
let out = expand(
"rs",
indoc! {r#"
fn main() {
let result = program.py↓;
}
"#},
)?;
assert!(
out.contains("reduce_py()"),
"expected `reduce_py()` in output, got: {out}"
);
Ok(())
}
#[test]
fn homo_reduce_expands_to_reduce() -> Result<()> {
let out = expand(
"rs",
indoc! {r#"
fn main() {
let result = program.↓;
}
"#},
)?;
assert!(
out.contains("reduce()"),
"expected `reduce()` in output, got: {out}"
);
assert!(
!out.contains("reduce_py()"),
"should not contain `reduce_py()`, got: {out}"
);
Ok(())
}
#[test]
fn py_homo_reduce_expands_to_reduce() -> Result<()> {
let out = expand("py", "result = program.↓")?;
assert!(
out.contains("reduce()"),
"expected `reduce()` in py output, got: {out}"
);
Ok(())
}
#[test]
fn py_hetero_reduce_rs_expands_to_reduce_rs() -> Result<()> {
let out = expand("py", "result = program.rs↓")?;
assert!(
out.contains("reduce_rs()"),
"expected `reduce_rs()` in py output, got: {out}"
);
Ok(())
}
#[test]
fn multiline_plain_block_comment_round_trips() -> Result<()> {
roundtrip(indoc! {r#"
/* a
b */
fn f() {
println!("hi");
}
"#})
}
#[test]
fn indented_multiline_plain_block_comment_round_trips() -> Result<()> {
roundtrip(indoc! {r#"
fn f() {
/* one
two
three */
let x = 1;
}
"#})
}
#[test]
fn flush_left_string_continuation_is_not_reindented() -> Result<()> {
roundtrip_lang(
"rs",
indoc! {r#"
fn f() {
if true {
let s = "one
two
three";
}
}
"#},
)?;
roundtrip_lang(
"py",
indoc! {r#"
def f():
s = """one
two
three"""
return s
"#},
)?;
roundtrip_lang(
"ts",
indoc! {r#"
function f() {
const s = `one
two
three`;
return s;
}
"#},
)
}
#[test]
fn expand_keeps_a_host_strings_contents() -> Result<()> {
let out = expand(
"rs",
indoc! {r#"
fn f() {
if true {
let s = "one
two";
let q = rs↖1↗;
}
}
"#},
)?;
assert!(
out.contains("let s = \"one\ntwo\";"),
"the host string should be untouched, got:\n{out}"
);
Ok(())
}
#[test]
fn every_escaped_glyph_round_trips_at_ground_level() -> Result<()> {
for g in quilt::glyphs::GLYPHS {
roundtrip(&format!("let s = \"\\{g}\";"))?;
}
Ok(())
}
#[test]
fn escaped_brackets_round_trip_inside_a_quote() -> Result<()> {
for g in ['↖', '↗', '↙', '↘', '⟨', '⟩'] {
roundtrip(&format!("let q = rs↖let s = \"\\{g}\";↗;"))?;
}
Ok(())
}
#[test]
fn deferred_operators_are_not_escaped() -> Result<()> {
roundtrip("let q = rs↖let y = ↑;↗;")?;
roundtrip("let q = rs↖let y = ↓;↗;")?;
roundtrip("let q = rs↖let y = py↓;↗;")?;
roundtrip("let q = rs↖fn f() { ←; }↗;")?;
roundtrip("let q = lean↖def n := ↑↗;")?;
roundtrip("let q = rs↖let z = rs↖let y = ↑;↗;↗;")
}
#[test]
fn an_escaped_operator_inside_a_quoted_string_is_the_known_residual() -> Result<()> {
let mut omni = Omni::default();
let src = "let q = rs↖let s = \"\\↑\";↗;";
let back = omni.parse(src)?.coparse_quilt();
assert_eq!(
back, "let q = rs↖let s = \"↑\";↗;",
"if this now round-trips, the residual on #223 is closed — replace this \
test with `roundtrip({src:?})`"
);
Ok(())
}
#[test]
fn a_line_comment_does_not_eat_the_closing_bracket() -> Result<()> {
roundtrip("let b = rs↖let x = 1; // hi↗;")?;
roundtrip("let u = rs↖let y = ↙f()↘; // hi↗;")?;
roundtrip("let n = rs↖let y = ↙f() // why↘;↗;")?;
let mut omni = Omni::default();
let out = omni
.parse_lang("rs", "let q = rs↖let x = 1; ⟨//⟩ note↗;")?
.coparse();
assert_eq!(out, "let q = rs↖let x = 1; ↗;");
Ok(())
}
#[test]
fn a_ground_level_line_comment_still_reaches_end_of_line() -> Result<()> {
let mut omni = Omni::default();
roundtrip(indoc! {r"
// prose about the ↙…↘ hole and the ↖…↗ brackets, with ↑ ↓ ← for good measure
let x = 1;"})?;
let out = omni
.parse_lang("rs", "⟨//⟩ a quilt comment about ↙…↘\nlet x = 1;")?
.coparse();
assert!(
!out.contains("quilt comment"),
"a `⟨//⟩` comment is stripped, not passed through: {out:?}"
);
assert!(
out.contains("let x = 1;"),
"the code after the comment survived: {out:?}"
);
Ok(())
}
#[test]
fn a_c_style_comment_is_raw_and_is_not_escaped() -> Result<()> {
roundtrip("// prose about ↙…↘ and ↑ ↓ ←\nlet x = 1;")?;
roundtrip("/* block ↙…↘ with ⟨ ⟩ */\nlet x = 1;")?;
roundtrip("let q = rs↖let x = 1; // hi ↑↗;")?;
roundtrip_lang("ts", "// ts comment ↙…↘\nconst x = 1;")
}
#[test]
fn a_hash_or_dash_comment_is_not_raw_and_keeps_its_escapes() -> Result<()> {
roundtrip_lang("py", "# python comment with \\↙…\\↘ and \\↑\nx = 1")?;
roundtrip_lang("nix", "{ } # nix comment \\↑")?;
roundtrip_lang("bash", "# shell comment \\↓\necho hi")
}
#[test]
fn a_block_quote_left_of_its_host_expands_as_if_indented() -> Result<()> {
let flush = expand(
"rs",
indoc! {r#"
fn f() {
if true {
let g = rs↖
fn g() -> u32 {
1
}
↗;
}
}
"#},
)?;
let indented = expand(
"rs",
indoc! {r#"
fn f() {
if true {
let g = rs↖
fn g() -> u32 {
1
}
↗;
}
}
"#},
)?;
assert_eq!(flush, indented);
Ok(())
}
#[test]
fn a_quote_body_partly_left_of_its_host_keeps_its_shape() -> Result<()> {
let partly = expand(
"rs",
indoc! {r#"
fn f() {
if true {
let g = rs↖
fn g() -> u32 {
1
}
↗;
}
}
"#},
)?;
let indented = expand(
"rs",
indoc! {r#"
fn f() {
if true {
let g = rs↖
fn g() -> u32 {
1
}
↗;
}
}
"#},
)?;
assert_eq!(partly, indented);
Ok(())
}
#[test]
fn an_inline_quote_left_of_its_host_expands() -> Result<()> {
let flush = expand(
"rs",
indoc! {r#"
fn f() {
if true {
let g = rs↖fn g() -> u32 {
1
}↗;
}
}
"#},
)?;
let indented = expand(
"rs",
indoc! {r#"
fn f() {
if true {
let g = rs↖fn g() -> u32 {
1
}↗;
}
}
"#},
)?;
assert_eq!(flush, indented);
Ok(())
}
#[test]
fn a_short_blank_line_in_a_quote_does_not_hold_back_the_dedent() -> Result<()> {
let blank = " fn a() {}\n \n fn b() {}";
let src = format!("fn f() {{\n let g = rs↖\n{blank}\n ↗;\n}}");
let out = expand("rs", &src)?;
let clean = expand(
"rs",
"fn f() {\n let g = rs↖\n fn a() {}\n\n fn b() {}\n ↗;\n}",
)?;
assert_eq!(out, clean);
Ok(())
}
fn first_quote_body(src: &str) -> Result<String> {
first_quote_body_in("ts", src)
}
fn first_quote_body_in(host: &str, src: &str) -> Result<String> {
fn find(t: &Arc<QTerm>) -> Option<Arc<QTerm>> {
match &**t {
QTerm::Quote { term, .. } => Some(term.clone()),
QTerm::Unquote { term, .. } => find(term),
QTerm::Tuple { terms, .. } => terms.iter().find_map(find),
}
}
let mut omni = Omni::default();
let term = omni.parse_lang(host, src.trim())?;
Ok(find(&term).expect("a quote").coparse())
}
#[test]
fn a_block_quote_closed_left_of_its_body_dedents_by_the_body() -> Result<()> {
let body = first_quote_body(indoc! {r#"
{
prog.push(↖
F[1] = {
a: 1,
};
↗);
}
"#})?;
assert_eq!(body, "F[1] = {\n a: 1,\n};");
Ok(())
}
#[test]
fn an_inline_quote_closed_by_a_bracket_dedents_to_the_closer() -> Result<()> {
let body = first_quote_body(indoc! {r#"
{
prog.push(↖F[1] = {
a: 1,
};↗);
}
"#})?;
assert_eq!(body, "F[1] = {\n a: 1,\n};");
Ok(())
}
#[test]
fn an_inline_quote_aligned_to_its_opener_dedents_to_the_closer() -> Result<()> {
let body = first_quote_body(indoc! {r#"
{
prog.push(↖F[1] = {
a: 1,
};↗);
}
"#})?;
assert_eq!(body, "F[1] = {\n a: 1,\n};");
Ok(())
}
#[test]
fn an_inline_quote_keeps_lines_deeper_than_its_closer() -> Result<()> {
let body = first_quote_body(indoc! {r#"
{
prog.push(↖F[1] = {
b: [
1,
],
};↗);
}
"#})?;
assert_eq!(body, "F[1] = {\n b: [\n 1,\n ],\n};");
Ok(())
}
#[test]
fn an_inline_quote_at_its_host_indent_is_unchanged() -> Result<()> {
let body = first_quote_body(indoc! {r#"
{
prog.push(↖F[1] = {
a: 1,
};↗);
}
"#})?;
assert_eq!(body, "F[1] = {\n a: 1,\n};");
Ok(())
}
#[test]
fn an_inline_python_def_keeps_its_body_indent() -> Result<()> {
let body = first_quote_body_in(
"rs",
"fn f() {\n let q = py↖def f():\n return 1↗;\n}",
)?;
assert_eq!(body, "def f():\n return 1");
Ok(())
}