fader_master/config/tests/editor.rs

110 lines
3.2 KiB
Rust

use fader_config::{ProfileDocument, SelectorDraft, SelectorField, SelectorPattern, TargetKind};
const PROFILE: &str = include_str!("../../examples/profiles/desktop.toml");
#[test]
fn draft_creates_an_application_group_and_fader_as_valid_toml() {
let mut draft = ProfileDocument::parse(PROFILE).unwrap();
draft
.add_application(
"chat",
"Chat",
&[SelectorDraft {
field: SelectorField::ProcessBinary,
pattern: SelectorPattern::Exact("signal-desktop".to_owned()),
}],
)
.unwrap();
draft
.add_group("communications", "Communications", &["chat", "browser"])
.unwrap();
draft
.add_fader(
"communications-fader",
TargetKind::Group,
"communications",
true,
0.02,
)
.unwrap();
let profile = draft.preview().unwrap();
assert!(
profile
.applications()
.iter()
.any(|item| item.id() == "chat")
);
assert!(
profile
.groups()
.iter()
.any(|item| item.id() == "communications")
);
assert!(
profile
.faders()
.iter()
.any(|item| item.id() == "communications-fader")
);
ProfileDocument::parse(&draft.finish().unwrap()).unwrap();
}
#[test]
fn invalid_edits_and_referenced_deletes_roll_back_the_draft() {
let mut draft = ProfileDocument::parse(PROFILE).unwrap();
let original = draft.source();
let invalid = draft
.add_application(
"broken",
"Broken",
&[SelectorDraft {
field: SelectorField::ApplicationName,
pattern: SelectorPattern::Regex("(".to_owned()),
}],
)
.unwrap_err();
assert!(invalid.to_string().contains("regex"));
assert_eq!(draft.source(), original);
let referenced = draft.remove_application("browser").unwrap_err();
assert!(referenced.to_string().contains("unknown application"));
assert_eq!(draft.source(), original);
}
#[test]
fn existing_applications_groups_and_faders_can_be_edited_and_deleted() {
let mut draft = ProfileDocument::parse(PROFILE).unwrap();
draft
.set_application(
"browser",
"Web",
&[SelectorDraft {
field: SelectorField::ProcessBinary,
pattern: SelectorPattern::Exact("waterfox-bin".to_owned()),
}],
)
.unwrap();
draft.set_group("media", "Players", &["browser"]).unwrap();
draft
.set_fader(
"media-fader",
TargetKind::Application,
"browser",
false,
0.05,
)
.unwrap();
let profile = draft.preview().unwrap();
assert_eq!(profile.applications()[1].label(), "Web");
assert_eq!(profile.groups()[0].members(), &["browser"]);
assert_eq!(profile.faders()[0].target_kind(), TargetKind::Application);
assert!(!profile.faders()[0].pickup());
draft.remove_group("media").unwrap();
draft.remove_fader("media-fader").unwrap_err();
assert!(draft.source().contains("media-fader"));
}