Skip to content

Commit

Permalink
Fix Delta Text not Corresponding Correctly
Browse files Browse the repository at this point in the history
Fixes #75
  • Loading branch information
hallcristobal authored and CryZe committed Sep 28, 2017
1 parent 76b8597 commit 17be26d
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 13 deletions.
20 changes: 7 additions & 13 deletions src/component/delta.rs → src/component/delta/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ use std::borrow::Cow;
use settings::{Color, Field, Gradient, SemanticColor, SettingsDescription, Value};
use super::DEFAULT_INFO_TEXT_GRADIENT;

#[cfg(test)]
mod tests;

#[derive(Default, Clone)]
pub struct Component {
settings: Settings,
Expand Down Expand Up @@ -74,25 +77,16 @@ impl Component {
}

pub fn name(&self) -> Cow<str> {
self.text(
self.settings
.comparison_override
.as_ref()
.map(String::as_ref),
)
}

fn text(&self, comparison: Option<&str>) -> Cow<str> {
if let Some(comparison) = comparison {
format!("Delta ({})", comparison::shorten(comparison)).into()
if let Some(ref comparison) = self.settings.comparison_override {
format!("Delta ({})", comparison).into()
} else {
"Delta".into()
}
}

pub fn state(&self, timer: &Timer, layout_settings: &GeneralLayoutSettings) -> State {
let comparison = comparison::resolve(&self.settings.comparison_override, timer);
let text = self.text(comparison);
let text = comparison.unwrap_or_else(|| timer.current_comparison());
let comparison = comparison::or_current(comparison, timer);

let (delta, use_live_delta) = delta::calculate(timer, comparison);
Expand Down Expand Up @@ -121,7 +115,7 @@ impl Component {
State {
background: self.settings.background,
label_color: self.settings.label_color,
text: text.into_owned(),
text: text.to_string(),
time: Delta::custom(self.settings.drop_decimals, self.settings.accuracy)
.format(delta)
.to_string(),
Expand Down
56 changes: 56 additions & 0 deletions src/component/delta/tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
use {GeneralLayoutSettings, Run, Segment, Timer};
use super::Component;

#[test]
fn comparison_text() {
let mut run = Run::new();
run.push_segment(Segment::new("Ok"));

let timer = Timer::new(run).unwrap();
let mut delta_comp = Component::new();
let settings = GeneralLayoutSettings::default();

// No Override
delta_comp.settings_mut().comparison_override = None;
assert_eq!(delta_comp.name(), "Delta"); // Displayed in Layout Editor
assert_eq!(
delta_comp.state(&timer, &settings).text,
timer.current_comparison()
); // Displayed in Layout

// Good Override
let mut comp = "Personal Best";
delta_comp.settings_mut().comparison_override = Some(format!("{}", comp));
assert_eq!(delta_comp.name(), format!("Delta ({})", comp));
assert_eq!(
delta_comp.state(&timer, &settings).text,
format!("{}", comp)
);

// Good Override
comp = "Best Segments";
delta_comp.settings_mut().comparison_override = Some(format!("{}", comp));
assert_eq!(delta_comp.name(), format!("Delta ({})", comp));
assert_eq!(
delta_comp.state(&timer, &settings).text,
format!("{}", comp)
);

// Good Override
comp = "None";
delta_comp.settings_mut().comparison_override = Some(format!("{}", comp));
assert_eq!(delta_comp.name(), format!("Delta ({})", comp));
assert_eq!(
delta_comp.state(&timer, &settings).text,
format!("{}", comp)
);

// Bad Override
comp = "Fake Comparison";
delta_comp.settings_mut().comparison_override = Some(format!("{}", comp));
assert_eq!(delta_comp.name(), format!("Delta ({})", comp));
assert_eq!(
delta_comp.state(&timer, &settings).text,
timer.current_comparison()
);
}

0 comments on commit 17be26d

Please sign in to comment.