use pikopiko::composer::edit::{format, FormatOptions};
use pikopiko::{compile_with, CompileOptions};
fn opts() -> CompileOptions<'static> {
CompileOptions {
synths: pikopiko::synth::ALL,
..Default::default()
}
}
fn events(src: &str) -> Vec<(u64, u8, String)> {
let (song, _) = compile_with(src, &opts());
song.expect("コンパイルできる")
.events
.iter()
.map(|e| (e.tick, e.track, format!("{:?}", e.kind)))
.collect()
}
fn fmt(src: &str, width: usize) -> String {
format(
src,
&FormatOptions {
width,
..FormatOptions::default()
},
&opts(),
)
.expect("整形できる")
}
#[test]
fn trims_blank_lines_and_trailing_spaces() {
let src = "\n\n#title x \n\n\n\nA c d\t\n";
let out = fmt(src, 100);
assert_eq!(out, "#title x\n\nA c d\n");
assert_eq!(events(src), events(&out));
}
#[cfg(feature = "va")]
#[test]
fn keeps_quotes_and_comments() {
let src = "#synth va\nA o4 'c e g'4 d ; メモ です\n";
let out = fmt(src, 100);
assert_eq!(out, "#synth va\nA o4 'c e g'4 d ; メモ です\n");
assert_eq!(events(src), events(&out));
}
#[test]
fn wraps_long_track_lines_with_the_same_track_name() {
let body = "cdefgab>c<".repeat(8);
let notes: Vec<String> = body.chars().map(|c| c.to_string()).collect();
let src = format!("A l8 o4 {}\n", notes.join(" "));
let out = fmt(&src, 40);
assert!(out.lines().count() > 1);
assert!(out
.lines()
.all(|l| l.chars().count() <= 40 && l.starts_with("A ")));
assert_eq!(events(&src), events(&out));
}
#[test]
fn is_idempotent() {
let src = "A l8 c d e f\nB c\n";
let once = fmt(src, 100);
assert_eq!(fmt(&once, 100), once);
}
#[test]
fn rejects_songs_with_errors() {
assert!(format("A c999\n", &FormatOptions::default(), &opts()).is_err());
}
#[test]
fn demo_songs_keep_their_output() {
let mut checked = 0;
for dir in ["songs/basic", "songs/va"] {
let Ok(rd) = std::fs::read_dir(dir) else {
continue;
};
for entry in rd.flatten() {
let path = entry.path();
if path.extension().and_then(|e| e.to_str()) != Some("mml") {
continue;
}
let src = std::fs::read_to_string(&path).unwrap();
let files = pikopiko::compiler::include::Files::beside(path.to_str().unwrap());
let o = CompileOptions {
synths: pikopiko::synth::ALL,
include: Some(&files),
..Default::default()
};
let Ok(out) = format(&src, &FormatOptions::default(), &o) else {
continue;
};
let a = compile_with(&src, &o).0.unwrap();
let b = compile_with(&out, &o).0.expect("整形後もコンパイルできる");
let key = |s: &pikopiko::Song| {
s.events
.iter()
.map(|e| (e.tick, e.track, format!("{:?}", e.kind)))
.collect::<Vec<_>>()
};
assert_eq!(key(&a), key(&b), "{}", path.display());
assert_eq!(a.length_ticks, b.length_ticks, "{}", path.display());
assert_eq!(
format(&out, &FormatOptions::default(), &o).unwrap(),
out,
"{}",
path.display()
);
checked += 1;
}
}
let least = if cfg!(feature = "va") { 10 } else { 1 };
assert!(checked >= least, "デモ曲が足りない: {checked}");
}
#[test]
fn normalize_rewrites_abbreviations_without_changing_the_song() {
use pikopiko::composer::edit::{format, FormatOptions};
let src = "@vib slow = 0 55 30\n@detune up = 0 20\n@arp maj = * 0 4 7\n\
A t120 o4 l8 c cmp slow d ep up e mpof f epof g en maj a enof b\n";
let opts = pikopiko::CompileOptions::default();
let out = format(
src,
&FormatOptions {
normalize: true,
..FormatOptions::default()
},
&opts,
)
.unwrap();
assert!(out.contains("c @vib slow"), "{out}");
assert!(out.contains("@detune up"), "{out}");
assert!(out.contains("@vib off"), "{out}");
assert!(out.contains("@detune off"), "{out}");
assert!(out.contains("@arp maj"), "{out}");
assert!(out.contains("@arp off"), "{out}");
assert!(
!out.contains(" mp") && !out.contains(" ep ") && !out.contains(" en "),
"{out}"
);
let strip = |t: &str| {
let mut s = pikopiko::compile_with(t, &opts).0.unwrap();
for e in &mut s.events {
e.span = Default::default();
}
s
};
assert_eq!(strip(src), strip(&out));
let again = format(
&out,
&FormatOptions {
normalize: true,
..FormatOptions::default()
},
&opts,
)
.unwrap();
assert_eq!(out, again);
let plain = format(src, &FormatOptions::default(), &opts).unwrap();
assert!(plain.contains("cmp slow"));
}
#[test]
fn normalize_leaves_names_and_comments_alone() {
use pikopiko::composer::edit::{format, FormatOptions};
let src = "$mp = c d\nA t120 o4 $mp ; mp en ep\n";
let opts = pikopiko::CompileOptions::default();
let out = format(
src,
&FormatOptions {
normalize: true,
..FormatOptions::default()
},
&opts,
)
.unwrap();
assert_eq!(out, "$mp = c d\nA t120 o4 $mp ; mp en ep\n");
}