Skip to content

Commit 5b57e07

Browse files
Merge pull request #36 from subalterngames/child_paths_tests
Child paths tests
2 parents 08f10dc + 3c4bb86 commit 5b57e07

File tree

5 files changed

+103
-3
lines changed

5 files changed

+103
-3
lines changed

changelog.md

+4-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44

55
- Fixed: If note is played via a qwerty key press, and then an octave is changed via a qwerty key press, there won't be a note-off event.
66
- Fixed: Cacophony can't found files (saves, soundfonts, etc.) if the file extension contains uppercase characters.
7-
- Fixed clippy warnings for Rust 1.78
8-
- The GitHub workflow for building Cacophony now uses the latest version of Rust.
7+
- Fixed: ChildPaths sometimes doesn't set the correct directory when moving up a directory.
8+
- (Backend) Fixed clippy warnings for Rust 1.78
9+
- (Backend) The GitHub workflow for building Cacophony now uses the latest version of Rust.
10+
- (Backend) Added tests for ChildPaths.
911

1012
## 0.2.3
1113

common/src/open_file/child_paths.rs

+96-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,16 @@ impl ChildPaths {
3333
self.selected = children
3434
.iter()
3535
.enumerate()
36-
.filter(|p| p.1.path == previous_directory)
36+
.filter(|p| {
37+
p.1.stem
38+
== previous_directory
39+
.components()
40+
.last()
41+
.unwrap()
42+
.as_os_str()
43+
.to_str()
44+
.unwrap()
45+
})
3746
.map(|p| p.0)
3847
.next();
3948
}
@@ -91,3 +100,89 @@ impl ChildPaths {
91100
paths
92101
}
93102
}
103+
104+
#[cfg(test)]
105+
mod tests {
106+
use std::{fs::canonicalize, path::PathBuf};
107+
108+
use super::ChildPaths;
109+
use crate::open_file::{Extension, FileOrDirectory};
110+
111+
#[test]
112+
fn test_sf2_child_paths() {
113+
let sf_directory = PathBuf::from("../data");
114+
assert!(sf_directory.exists());
115+
let mut child_paths = ChildPaths::default();
116+
child_paths.set(&sf_directory, &Extension::Sf2, None);
117+
assert_eq!(child_paths.children.len(), 1);
118+
let f = &child_paths.children[0];
119+
assert!(f.is_file);
120+
assert_eq!(f.stem, "CT1MBGMRSV1.06.sf2");
121+
assert!(child_paths.selected.is_some());
122+
assert_eq!(child_paths.selected.unwrap(), 0);
123+
// There shouldn't be any save files.
124+
child_paths.set(&sf_directory, &Extension::Cac, None);
125+
assert!(child_paths.children.is_empty());
126+
assert!(child_paths.selected.is_some());
127+
assert_eq!(child_paths.selected.unwrap(), 0);
128+
let parent_directory = canonicalize(sf_directory.parent().unwrap()).unwrap();
129+
assert_eq!(
130+
parent_directory
131+
.components()
132+
.last()
133+
.unwrap()
134+
.as_os_str()
135+
.to_str()
136+
.unwrap(),
137+
"cacophony"
138+
);
139+
// Set a different directory.
140+
child_paths.set(&parent_directory, &Extension::Sf2, None);
141+
assert!(!child_paths.children.is_empty());
142+
assert!(child_paths
143+
.children
144+
.iter()
145+
.filter(|c| c.is_file)
146+
.collect::<Vec<&FileOrDirectory>>()
147+
.is_empty());
148+
// Ignore any folders that have names beginning with a period because they won't all appear in the GitHub workflow.
149+
child_paths
150+
.children
151+
.retain(|f| match f.stem.chars().next() {
152+
Some(ch) => ch != '.',
153+
None => false,
154+
});
155+
assert_eq!(child_paths.children.len(), 14);
156+
assert!(child_paths.selected.is_some());
157+
assert_eq!(child_paths.selected.unwrap(), 0);
158+
// Go "up" a directory.
159+
child_paths.set(
160+
&parent_directory,
161+
&Extension::Sf2,
162+
Some(sf_directory.clone()),
163+
);
164+
// Test the selection.
165+
assert_eq!(
166+
child_paths.children[child_paths.selected.unwrap()].stem,
167+
"data"
168+
);
169+
}
170+
171+
#[test]
172+
fn test_cac_child_paths() {
173+
let cac_directory = PathBuf::from("../test_files/child_paths");
174+
assert!(cac_directory.exists());
175+
let mut child_paths = ChildPaths::default();
176+
child_paths.set(&cac_directory, &Extension::Cac, None);
177+
assert_eq!(child_paths.children.len(), 3);
178+
test_cac_file(&child_paths, child_paths.selected.unwrap(), "test_0.cac");
179+
test_cac_file(&child_paths, 1, "test_1.cac");
180+
test_cac_file(&child_paths, 2, "test_2.CAC");
181+
}
182+
183+
fn test_cac_file(child_paths: &ChildPaths, index: usize, filename: &str) {
184+
let f = &child_paths.children[index];
185+
assert!(f.is_file);
186+
assert_eq!(f.stem, filename);
187+
}
188+
}

test_files/child_paths/test_0.cac

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"state":{"music":{"midi_tracks":[{"channel":0,"gain":127,"notes":[[60,127,0,192],[62,127,192,384],[64,127,384,576]],"mute":false,"solo":false}],"selected":0},"view":{"dt":[0,25536],"dn":[75,45],"mode":{"values":["Normal","Quick","Precise"],"index":{"index":0,"length":3}},"single_track":true,"zoom_levels":[404,462,528,604,691,790,903,1033,1181,1350,1543,1764,2016,2304,2634,3011,3442,3934,4497,5140,5875,6715,7675,8772,10026,11459,13097,14968,17107,19551,22344,25536,29184,33353,38117,43562,49785,56897,65025,74314,84930,97062,110928,126774,144884,165581,189235,216268,247163,282472,322825,368942,421648,481883,550723,629397,719310,822068,939506,1073721,1227109,1402410,1602754,1831718],"zoom_index":{"index":31,"length":64},"zoom_increments":{"Quick":4,"Normal":2,"Precise":1},"initial_zoom_index":31},"time":{"cursor":576,"playback":0,"bpm":120,"mode":{"values":["Normal","Quick","Precise"],"index":{"index":0,"length":3}}},"input":{"armed":true,"alphanumeric_input":false,"volume":{"index":127,"length":128},"use_volume":true,"beat":192},"panels":["Music","Tracks","PianoRoll"],"focus":{"index":2,"length":3},"music_panel_field":{"values":["Name","BPM","Gain"],"index":{"index":0,"length":3}},"piano_roll_mode":"Time","edit_mode":{"values":["Normal","Quick","Precise"],"index":{"index":0,"length":3}},"select_mode":{"Single":null}},"synth_state":{"programs":{"0":{"path":"/home/esther/cacophony/data/CT1MBGMRSV1.06.sf2","num_banks":2,"bank_index":0,"bank":0,"num_presets":128,"preset":0,"preset_index":0,"preset_name":"Piano 1"}},"gain":127},"paths_state":{"soundfonts":{"directory":{"path":"/home/esther/Documents/cacophony/soundfonts","is_file":false,"stem":"soundfonts"},"filename":null},"saves":{"directory":{"path":"/home/esther/Documents/cacophony/saves","is_file":false,"stem":"saves"},"filename":"uh"},"exports":{"directory":{"path":"/home/esther/Documents/cacophony/exports","is_file":false,"stem":"exports"},"filename":null},"midis":{"directory":{"path":"/home/esther/Documents/cacophony","is_file":false,"stem":"cacophony"},"filename":null}},"exporter":{"framerate":44100,"metadata":{"title":"My Music","artist":null,"album":null,"track_number":null,"genre":null,"comment":null},"copyright":false,"mp3_bit_rate":{"index":12,"length":16},"mp3_quality":{"index":9,"length":10},"multi_file":false,"multi_file_suffix":{"values":["ChannelAndPreset","Preset","Channel"],"index":{"index":0,"length":3}},"ogg_quality":{"index":9,"length":10},"export_type":{"values":["Wav","Mid","MP3","Ogg","Flac"],"index":{"index":0,"length":5}},"mid_settings":{"values":["Title","Artist","Copyright"],"index":{"index":0,"length":3}},"wav_settings":{"values":["Framerate","MultiFile","MultiFileSuffix"],"index":{"index":0,"length":3}},"mp3_settings":{"values":["Framerate","Mp3Quality","Mp3BitRate","Title","Artist","Copyright","Album","TrackNumber","Genre","Comment","MultiFile","MultiFileSuffix"],"index":{"index":0,"length":12}},"ogg_settings":{"values":["Framerate","OggQuality","Title","Artist","Copyright","Album","TrackNumber","Genre","Comment","MultiFile","MultiFileSuffix"],"index":{"index":0,"length":11}},"flac_settings":{"values":["Framerate","Title","Artist","Copyright","Album","TrackNumber","Genre","Comment","MultiFile","MultiFileSuffix"],"index":{"index":0,"length":10}}},"version":"0.2.4"}

test_files/child_paths/test_1.cac

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"state":{"music":{"midi_tracks":[{"channel":0,"gain":127,"notes":[[60,127,0,192],[62,127,192,384],[64,127,384,576]],"mute":false,"solo":false}],"selected":0},"view":{"dt":[0,25536],"dn":[75,45],"mode":{"values":["Normal","Quick","Precise"],"index":{"index":0,"length":3}},"single_track":true,"zoom_levels":[404,462,528,604,691,790,903,1033,1181,1350,1543,1764,2016,2304,2634,3011,3442,3934,4497,5140,5875,6715,7675,8772,10026,11459,13097,14968,17107,19551,22344,25536,29184,33353,38117,43562,49785,56897,65025,74314,84930,97062,110928,126774,144884,165581,189235,216268,247163,282472,322825,368942,421648,481883,550723,629397,719310,822068,939506,1073721,1227109,1402410,1602754,1831718],"zoom_index":{"index":31,"length":64},"zoom_increments":{"Quick":4,"Normal":2,"Precise":1},"initial_zoom_index":31},"time":{"cursor":576,"playback":0,"bpm":120,"mode":{"values":["Normal","Quick","Precise"],"index":{"index":0,"length":3}}},"input":{"armed":true,"alphanumeric_input":false,"volume":{"index":127,"length":128},"use_volume":true,"beat":192},"panels":["Music","Tracks","PianoRoll"],"focus":{"index":2,"length":3},"music_panel_field":{"values":["Name","BPM","Gain"],"index":{"index":0,"length":3}},"piano_roll_mode":"Time","edit_mode":{"values":["Normal","Quick","Precise"],"index":{"index":0,"length":3}},"select_mode":{"Single":null}},"synth_state":{"programs":{"0":{"path":"/home/esther/cacophony/data/CT1MBGMRSV1.06.sf2","num_banks":2,"bank_index":0,"bank":0,"num_presets":128,"preset":0,"preset_index":0,"preset_name":"Piano 1"}},"gain":127},"paths_state":{"soundfonts":{"directory":{"path":"/home/esther/Documents/cacophony/soundfonts","is_file":false,"stem":"soundfonts"},"filename":null},"saves":{"directory":{"path":"/home/esther/Documents/cacophony/saves","is_file":false,"stem":"saves"},"filename":"uh"},"exports":{"directory":{"path":"/home/esther/Documents/cacophony/exports","is_file":false,"stem":"exports"},"filename":null},"midis":{"directory":{"path":"/home/esther/Documents/cacophony","is_file":false,"stem":"cacophony"},"filename":null}},"exporter":{"framerate":44100,"metadata":{"title":"My Music","artist":null,"album":null,"track_number":null,"genre":null,"comment":null},"copyright":false,"mp3_bit_rate":{"index":12,"length":16},"mp3_quality":{"index":9,"length":10},"multi_file":false,"multi_file_suffix":{"values":["ChannelAndPreset","Preset","Channel"],"index":{"index":0,"length":3}},"ogg_quality":{"index":9,"length":10},"export_type":{"values":["Wav","Mid","MP3","Ogg","Flac"],"index":{"index":0,"length":5}},"mid_settings":{"values":["Title","Artist","Copyright"],"index":{"index":0,"length":3}},"wav_settings":{"values":["Framerate","MultiFile","MultiFileSuffix"],"index":{"index":0,"length":3}},"mp3_settings":{"values":["Framerate","Mp3Quality","Mp3BitRate","Title","Artist","Copyright","Album","TrackNumber","Genre","Comment","MultiFile","MultiFileSuffix"],"index":{"index":0,"length":12}},"ogg_settings":{"values":["Framerate","OggQuality","Title","Artist","Copyright","Album","TrackNumber","Genre","Comment","MultiFile","MultiFileSuffix"],"index":{"index":0,"length":11}},"flac_settings":{"values":["Framerate","Title","Artist","Copyright","Album","TrackNumber","Genre","Comment","MultiFile","MultiFileSuffix"],"index":{"index":0,"length":10}}},"version":"0.2.4"}

test_files/child_paths/test_2.CAC

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"state":{"music":{"midi_tracks":[{"channel":0,"gain":127,"notes":[[60,127,0,192],[62,127,192,384],[64,127,384,576]],"mute":false,"solo":false}],"selected":0},"view":{"dt":[0,25536],"dn":[75,45],"mode":{"values":["Normal","Quick","Precise"],"index":{"index":0,"length":3}},"single_track":true,"zoom_levels":[404,462,528,604,691,790,903,1033,1181,1350,1543,1764,2016,2304,2634,3011,3442,3934,4497,5140,5875,6715,7675,8772,10026,11459,13097,14968,17107,19551,22344,25536,29184,33353,38117,43562,49785,56897,65025,74314,84930,97062,110928,126774,144884,165581,189235,216268,247163,282472,322825,368942,421648,481883,550723,629397,719310,822068,939506,1073721,1227109,1402410,1602754,1831718],"zoom_index":{"index":31,"length":64},"zoom_increments":{"Quick":4,"Normal":2,"Precise":1},"initial_zoom_index":31},"time":{"cursor":576,"playback":0,"bpm":120,"mode":{"values":["Normal","Quick","Precise"],"index":{"index":0,"length":3}}},"input":{"armed":true,"alphanumeric_input":false,"volume":{"index":127,"length":128},"use_volume":true,"beat":192},"panels":["Music","Tracks","PianoRoll"],"focus":{"index":2,"length":3},"music_panel_field":{"values":["Name","BPM","Gain"],"index":{"index":0,"length":3}},"piano_roll_mode":"Time","edit_mode":{"values":["Normal","Quick","Precise"],"index":{"index":0,"length":3}},"select_mode":{"Single":null}},"synth_state":{"programs":{"0":{"path":"/home/esther/cacophony/data/CT1MBGMRSV1.06.sf2","num_banks":2,"bank_index":0,"bank":0,"num_presets":128,"preset":0,"preset_index":0,"preset_name":"Piano 1"}},"gain":127},"paths_state":{"soundfonts":{"directory":{"path":"/home/esther/Documents/cacophony/soundfonts","is_file":false,"stem":"soundfonts"},"filename":null},"saves":{"directory":{"path":"/home/esther/Documents/cacophony/saves","is_file":false,"stem":"saves"},"filename":"uh"},"exports":{"directory":{"path":"/home/esther/Documents/cacophony/exports","is_file":false,"stem":"exports"},"filename":null},"midis":{"directory":{"path":"/home/esther/Documents/cacophony","is_file":false,"stem":"cacophony"},"filename":null}},"exporter":{"framerate":44100,"metadata":{"title":"My Music","artist":null,"album":null,"track_number":null,"genre":null,"comment":null},"copyright":false,"mp3_bit_rate":{"index":12,"length":16},"mp3_quality":{"index":9,"length":10},"multi_file":false,"multi_file_suffix":{"values":["ChannelAndPreset","Preset","Channel"],"index":{"index":0,"length":3}},"ogg_quality":{"index":9,"length":10},"export_type":{"values":["Wav","Mid","MP3","Ogg","Flac"],"index":{"index":0,"length":5}},"mid_settings":{"values":["Title","Artist","Copyright"],"index":{"index":0,"length":3}},"wav_settings":{"values":["Framerate","MultiFile","MultiFileSuffix"],"index":{"index":0,"length":3}},"mp3_settings":{"values":["Framerate","Mp3Quality","Mp3BitRate","Title","Artist","Copyright","Album","TrackNumber","Genre","Comment","MultiFile","MultiFileSuffix"],"index":{"index":0,"length":12}},"ogg_settings":{"values":["Framerate","OggQuality","Title","Artist","Copyright","Album","TrackNumber","Genre","Comment","MultiFile","MultiFileSuffix"],"index":{"index":0,"length":11}},"flac_settings":{"values":["Framerate","Title","Artist","Copyright","Album","TrackNumber","Genre","Comment","MultiFile","MultiFileSuffix"],"index":{"index":0,"length":10}}},"version":"0.2.4"}

0 commit comments

Comments
 (0)