More tests

This commit is contained in:
Adrian Wozniak 2019-12-22 23:29:40 +01:00
parent eed6b72bb0
commit f06cb0fc1d
4 changed files with 183 additions and 10 deletions

View File

@ -39,7 +39,7 @@ impl Widget for SaveButton {
} }
fn set_dest(&mut self, rect: &Rect) { fn set_dest(&mut self, rect: &Rect) {
self.inner.dest = rect.clone(); self.dest = rect.clone();
} }
fn source(&self) -> &Rect { fn source(&self) -> &Rect {
@ -47,7 +47,7 @@ impl Widget for SaveButton {
} }
fn set_source(&mut self, rect: &Rect) { fn set_source(&mut self, rect: &Rect) {
self.inner.source = rect.clone(); self.source = rect.clone();
} }
fn on_left_click(&mut self, _point: &Point, _context: &UpdateContext) -> UR { fn on_left_click(&mut self, _point: &Point, _context: &UpdateContext) -> UR {
@ -74,3 +74,48 @@ impl SaveButton {
} }
} }
} }
#[cfg(test)]
mod test {
use super::*;
use crate::app::UpdateResult;
use crate::tests::support;
#[test]
fn must_return_save_on_left_click() {
let config = support::build_config();
let mut widget = SaveButton::new(config);
assert_eq!(
widget.on_left_click(&Point::new(0, 0), &UpdateContext::Nothing),
UpdateResult::SaveCurrentFile
);
}
#[test]
fn must_use_inner() {
let config = support::build_config();
let mut widget = SaveButton::new(config);
assert_eq!(
widget.dest(),
&Rect::new(0, 0, ICON_DEST_WIDTH, ICON_DEST_HEIGHT)
);
widget.set_dest(&Rect::new(1, 2, 3, 4));
assert_eq!(widget.dest(), &Rect::new(1, 2, 3, 4));
assert_eq!(
widget.source(),
&Rect::new(0, 0, ICON_SRC_WIDTH, ICON_SRC_HEIGHT)
);
widget.set_source(&Rect::new(5, 6, 7, 8));
assert_eq!(widget.source(), &Rect::new(5, 6, 7, 8));
}
#[test]
fn must_have_padding() {
let config = support::build_config();
let widget = SaveButton::new(config);
assert_eq!(widget.padding_width(), ICON_DEST_WIDTH);
assert_eq!(widget.padding_height(), ICON_DEST_HEIGHT);
}
}

View File

@ -40,7 +40,7 @@ impl Widget for SettingsButton {
} }
fn set_dest(&mut self, rect: &Rect) { fn set_dest(&mut self, rect: &Rect) {
self.inner.dest = rect.clone(); self.dest = rect.clone();
} }
fn source(&self) -> &Rect { fn source(&self) -> &Rect {
@ -48,7 +48,7 @@ impl Widget for SettingsButton {
} }
fn set_source(&mut self, rect: &Rect) { fn set_source(&mut self, rect: &Rect) {
self.inner.source = rect.clone(); self.source = rect.clone();
} }
fn on_left_click(&mut self, _point: &Point, _context: &UpdateContext) -> UR { fn on_left_click(&mut self, _point: &Point, _context: &UpdateContext) -> UR {
@ -75,3 +75,48 @@ impl SettingsButton {
} }
} }
} }
#[cfg(test)]
mod test {
use super::*;
use crate::app::UpdateResult;
use crate::tests::support;
#[test]
fn must_return_open_settings_on_left_click() {
let config = support::build_config();
let mut widget = SettingsButton::new(config);
assert_eq!(
widget.on_left_click(&Point::new(0, 0), &UpdateContext::Nothing),
UpdateResult::OpenSettings
);
}
#[test]
fn must_use_inner() {
let config = support::build_config();
let mut widget = SettingsButton::new(config);
assert_eq!(
widget.dest(),
&Rect::new(0, 0, ICON_DEST_WIDTH, ICON_DEST_HEIGHT)
);
widget.set_dest(&Rect::new(1, 2, 3, 4));
assert_eq!(widget.dest(), &Rect::new(1, 2, 3, 4));
assert_eq!(
widget.source(),
&Rect::new(0, 0, ICON_SRC_WIDTH, ICON_SRC_HEIGHT)
);
widget.set_source(&Rect::new(5, 6, 7, 8));
assert_eq!(widget.source(), &Rect::new(5, 6, 7, 8));
}
#[test]
fn must_have_padding() {
let config = support::build_config();
let widget = SettingsButton::new(config);
assert_eq!(widget.padding_width(), ICON_DEST_WIDTH);
assert_eq!(widget.padding_height(), ICON_DEST_HEIGHT);
}
}

View File

@ -31,12 +31,12 @@ impl Icon {
#[inline] #[inline]
pub fn height(&self) -> u32 { pub fn height(&self) -> u32 {
self.inner.dest.height() self.dest.height()
} }
#[inline] #[inline]
pub fn width(&self) -> u32 { pub fn width(&self) -> u32 {
self.inner.dest.width() self.dest.width()
} }
pub fn set_texture_path(&mut self, path: String) { pub fn set_texture_path(&mut self, path: String) {
@ -50,18 +50,57 @@ impl Widget for Icon {
} }
fn dest(&self) -> &Rect { fn dest(&self) -> &Rect {
&self.inner.dest &self.dest
} }
fn set_dest(&mut self, rect: &Rect) { fn set_dest(&mut self, rect: &Rect) {
self.inner.dest = rect.clone(); self.dest = rect.clone();
} }
fn source(&self) -> &Rect { fn source(&self) -> &Rect {
&self.inner.source &self.source
} }
fn set_source(&mut self, rect: &Rect) { fn set_source(&mut self, rect: &Rect) {
self.inner.source = rect.clone(); self.source = rect.clone();
}
}
#[cfg(test)]
mod test {
use super::*;
use crate::app::UpdateResult;
use crate::tests::support;
use crate::ui::UpdateContext;
use sdl2::rect::Point;
#[test]
fn must_return_noop_on_left_click() {
let config = support::build_config();
let dest = Rect::new(0, 10, 20, 30);
let src = Rect::new(40, 50, 60, 70);
let path = "/foo/bar.png".to_owned();
let mut widget = Icon::new(config, path, src, dest.clone());
assert_eq!(
widget.on_left_click(&Point::new(0, 0), &UpdateContext::Nothing),
UpdateResult::NoOp
);
}
#[test]
fn must_use_inner() {
let config = support::build_config();
let dest = Rect::new(0, 10, 20, 30);
let src = Rect::new(40, 50, 60, 70);
let path = "/foo/bar.png".to_owned();
let mut widget = Icon::new(config, path, src, dest.clone());
assert_eq!(widget.dest(), &dest);
widget.set_dest(&Rect::new(1, 2, 3, 4));
assert_eq!(widget.dest(), &Rect::new(1, 2, 3, 4));
assert_eq!(widget.source(), &src);
widget.set_source(&Rect::new(5, 6, 7, 8));
assert_eq!(widget.source(), &Rect::new(5, 6, 7, 8));
} }
} }

View File

@ -142,3 +142,47 @@ impl ConfigHolder for Label {
&self.config &self.config
} }
} }
#[cfg(test)]
mod test {
use super::*;
use crate::app::UpdateResult;
use crate::tests::support;
use crate::ui::{UpdateContext, Widget};
use sdl2::rect::Point;
#[test]
fn must_return_noop_on_left_click() {
let config = support::build_config();
let name = "Hello world".to_owned();
let mut widget = Label::new(name, config);
assert_eq!(
widget.on_left_click(&Point::new(0, 0), &UpdateContext::Nothing),
UpdateResult::NoOp
);
}
#[test]
fn must_use_inner() {
let config = support::build_config();
let mut renderer = support::SimpleRendererMock::new(config.clone());
let name = "Hello world".to_owned();
let mut widget = Label::new(name.clone(), config);
let dest = Rect::new(0, 0, DEST_WIDTH, DEST_HEIGHT);
let src = Rect::new(0, 0, SRC_WIDTH, SRC_HEIGHT);
assert_eq!(widget.dest(), &dest);
widget.set_dest(&Rect::new(1, 2, 3, 4));
assert_eq!(widget.dest(), &Rect::new(1, 2, 3, 4));
assert_eq!(widget.source(), &src);
widget.set_source(&Rect::new(5, 6, 7, 8));
assert_eq!(widget.source(), &Rect::new(5, 6, 7, 8));
assert_eq!(widget.name_width(), widget.dest().width());
assert_eq!(widget.name(), name);
widget.prepare_ui(&mut renderer);
}
}