use pikopiko::compiler::include::{Files, Source, Sources};
use pikopiko::{compile, compile_with, CompileOptions, Diagnostic, EventKind};
fn with(sources: &Sources, src: &str) -> (Option<pikopiko::Song>, Vec<Diagnostic>) {
let opts = CompileOptions {
include: Some(sources),
..Default::default()
};
compile_with(src, &opts)
}
fn codes(d: &[Diagnostic]) -> Vec<&'static str> {
d.iter().map(|d| d.code).collect()
}
const LIB: &str =
"; 共有の音色\n@env pluck = 15 12 9 6 3 0\n@vib w = 6 55 30\n$riff = c d e\n$unused = f g\n";
#[test]
fn includes_definitions() {
let lib = Sources::new([("lib.mml", LIB)]);
let (song, diags) = with(&lib, "#include \"lib.mml\"\nA @env pluck @vib w $riff");
let song = song.unwrap_or_else(|| panic!("{diags:?}"));
assert_eq!(codes(&diags), Vec::<&str>::new());
let plain = compile(&format!("{LIB}A @env pluck @vib w $riff")).unwrap();
let kinds = |s: &pikopiko::Song| -> Vec<(u64, EventKind)> {
s.events.iter().map(|e| (e.tick, e.kind)).collect()
};
assert_eq!(kinds(&song), kinds(&plain));
let note = song
.events
.iter()
.find(|e| matches!(e.kind, EventKind::NoteOn { .. }))
.unwrap();
assert_eq!((note.span.source, note.span.line), (1, 4));
}
#[test]
fn prelude_is_read_before_the_song() {
let prelude = [Source {
name: "common",
text: "$riff = c d e",
}];
let opts = CompileOptions {
prelude: &prelude,
..Default::default()
};
let (song, diags) = compile_with("A $riff", &opts);
assert!(song.is_some(), "{diags:?}");
}
#[test]
fn errors_inside_included_text_name_the_file() {
let lib = Sources::new([("lib.mml", "$ok = c\n@env bad = 99\n")]);
let (song, diags) = with(&lib, "#include \"lib.mml\"\nA $ok");
assert!(song.is_none());
let d = diags.iter().find(|d| d.code == "E0301").unwrap();
assert_eq!(d.file.as_deref(), Some("lib.mml"));
assert_eq!((d.span.source, d.span.line), (1, 2));
assert!(d.to_string().contains("lib.mml:2:"), "{d}");
}
#[test]
fn only_definitions_can_be_included() {
let lib = Sources::new([("lib.mml", "#synth ay\n$a = c\nA c d\n")]);
let (_, diags) = with(&lib, "#include \"lib.mml\"\nA $a");
assert_eq!(codes(&diags), ["E0603", "E0603"]);
assert!(diags.iter().all(|d| d.file.as_deref() == Some("lib.mml")));
}
#[test]
fn nested_cycles_and_twice() {
let lib = Sources::new([
("a.mml", "#include \"b.mml\"\n$a = c"),
("b.mml", "$b = d"),
("loop1.mml", "#include \"loop2.mml\""),
("loop2.mml", "#include \"loop1.mml\""),
]);
let (song, diags) = with(&lib, "#include \"a.mml\"\n#include \"b.mml\"\nA $a $b");
assert!(song.is_some(), "{diags:?}");
let (_, diags) = with(&lib, "#include \"loop1.mml\"\nA c");
assert_eq!(codes(&diags), ["E0602"]);
}
#[test]
fn duplicate_definitions_are_errors() {
let lib = Sources::new([("lib.mml", "$riff = c\n@env e = 15")]);
let (_, diags) = with(
&lib,
"#include \"lib.mml\"\n$riff = d\n@env e = 9\nA $riff @env e c",
);
let mut c = codes(&diags);
c.sort();
assert_eq!(c, ["E0403", "E0408"]);
}
#[test]
fn loading_errors() {
let (_, d) = compile_with("#include \"lib.mml\"\nA c", &CompileOptions::default());
assert_eq!(codes(&d), ["E0601"]);
let (_, d) = with(&Sources::default(), "#include \"lib.mml\"\nA c");
assert_eq!(codes(&d), ["E0601"]);
let lib = Sources::new([("lib.mml", "$a = c")]);
assert_eq!(codes(&with(&lib, "#include lib.mml\nA c").1), ["E0228"]);
assert_eq!(codes(&with(&lib, "A c\n#include \"lib.mml\"").1), ["E0217"]);
}
#[test]
fn files_resolve_relative_to_the_including_file() {
let dir = std::env::temp_dir().join(format!("pikopiko-include-{}", std::process::id()));
let sub = dir.join("lib");
std::fs::create_dir_all(&sub).unwrap();
std::fs::write(
sub.join("drums.mml"),
"#include \"env.mml\"\n$kick = @env k c16",
)
.unwrap();
std::fs::write(sub.join("env.mml"), "@env k = 15 9 3 0").unwrap();
let files = Files::new(&dir);
let opts = CompileOptions {
include: Some(&files),
..Default::default()
};
let (song, diags) = compile_with("#include \"lib/drums.mml\"\nA $kick", &opts);
assert!(song.is_some(), "{diags:?}");
let (_, diags) = compile_with("#include \"lib/none.mml\"\nA c", &opts);
assert_eq!(codes(&diags), ["E0601"]);
std::fs::remove_dir_all(&dir).ok();
}
#[cfg(feature = "serde")]
#[test]
fn json_has_file_only_for_included_text() {
let lib = Sources::new([("lib.mml", "@env bad = 99")]);
let (_, diags) = with(&lib, "#include \"lib.mml\"\nA c c#");
let v = serde_json::to_value(&diags).unwrap();
let inc = v
.as_array()
.unwrap()
.iter()
.find(|d| d["code"] == "E0301")
.unwrap();
assert_eq!(inc["file"], "lib.mml");
assert_eq!(inc["span"]["source"], 1);
let main = v
.as_array()
.unwrap()
.iter()
.find(|d| d["code"] == "E0104")
.unwrap();
assert!(main.get("file").is_none() && main["span"].get("source").is_none());
}