Skip to content

Commit

Permalink
Increase test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
staticintlucas committed May 15, 2024
1 parent 52e6c43 commit eb5ae65
Show file tree
Hide file tree
Showing 12 changed files with 1,158 additions and 109 deletions.
66 changes: 55 additions & 11 deletions keyset-drawing/src/imp/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ fn step_path(rect: RoundRect<Dot>) -> Path<Dot> {
#[cfg(test)]
mod tests {
use isclose::assert_is_close;
use key::Key;

use super::*;

Expand All @@ -208,7 +209,7 @@ mod tests {
let options = Options::default();

// Regular 1u key
let key = key::Key::example();
let key = Key::example();
let path = top(&key, &options);
let bounds = path.data.bounds;

Expand All @@ -218,9 +219,30 @@ mod tests {
let top_rect = options.profile.top_with_size(Size::new(1.0, 1.0));
assert_is_close!(bounds, top_rect.rect());

// None
let key = {
let mut key = Key::example();
key.shape = key::Shape::None(Size::splat(1.0));
key
};
let path = top(&key, &options);
let bounds = path.data.bounds;
assert_is_close!(bounds, Rect::zero());

// Homing
let key = {
let mut key = Key::example();
key.shape = key::Shape::Homing(None);
key
};
let path = top(&key, &options);
let bounds = path.data.bounds;
let top_rect = options.profile.top_with_size(Size::splat(1.0));
assert_is_close!(bounds, top_rect.rect());

// Stepped caps
let key = {
let mut key = key::Key::example();
let mut key = Key::example();
key.shape = key::Shape::SteppedCaps;
key
};
Expand All @@ -231,7 +253,7 @@ mod tests {

// ISO enter
let key = {
let mut key = key::Key::example();
let mut key = Key::example();
key.shape = key::Shape::IsoVertical;
key
};
Expand All @@ -243,9 +265,10 @@ mod tests {

#[test]
fn test_bottom() {
let key = key::Key::example();
let options = Options::default();

// Regular 1u key
let key = Key::example();
let path = bottom(&key, &options);
let bounds = path.data.bounds;

Expand All @@ -255,9 +278,30 @@ mod tests {
let bottom_rect = options.profile.bottom_with_size(Size::new(1.0, 1.0));
assert_is_close!(bounds, bottom_rect.rect());

// None
let key = {
let mut key = Key::example();
key.shape = key::Shape::None(Size::splat(1.0));
key
};
let path = bottom(&key, &options);
let bounds = path.data.bounds;
assert_is_close!(bounds, Rect::zero());

// Homing
let key = {
let mut key = Key::example();
key.shape = key::Shape::Homing(None);
key
};
let path = bottom(&key, &options);
let bounds = path.data.bounds;
let bottom_rect = options.profile.bottom_with_size(Size::splat(1.0));
assert_is_close!(bounds, bottom_rect.rect());

// Stepped caps
let key = {
let mut key = key::Key::example();
let mut key = Key::example();
key.shape = key::Shape::SteppedCaps;
key
};
Expand All @@ -268,7 +312,7 @@ mod tests {

// ISO enter
let key = {
let mut key = key::Key::example();
let mut key = Key::example();
key.shape = key::Shape::IsoVertical;
key
};
Expand All @@ -284,7 +328,7 @@ mod tests {

// Scoop
let scoop = {
let mut key = key::Key::example();
let mut key = Key::example();
key.shape = key::Shape::Homing(Some(key::Homing::Scoop));
key
};
Expand All @@ -294,7 +338,7 @@ mod tests {

// Bar
let bar = {
let mut key = key::Key::example();
let mut key = Key::example();
key.shape = key::Shape::Homing(Some(key::Homing::Bar));
key
};
Expand All @@ -319,7 +363,7 @@ mod tests {

// Bump
let bump = {
let mut key = key::Key::example();
let mut key = Key::example();
key.shape = key::Shape::Homing(Some(key::Homing::Bump));
key
};
Expand All @@ -343,7 +387,7 @@ mod tests {
assert_is_close!(bounds, expected);

// Non-homing key
let none = key::Key::example();
let none = Key::example();

let path = homing(&none, &options);
assert!(path.is_none()); // No additional feature to draw
Expand All @@ -352,7 +396,7 @@ mod tests {
#[test]
fn test_step() {
let key = {
let mut key = key::Key::example();
let mut key = Key::example();
key.shape = key::Shape::SteppedCaps;
key
};
Expand Down
2 changes: 2 additions & 0 deletions keyset-drawing/src/svg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ fn draw_path(path: &KeyPath) -> SvgPath {
PathSegment::CubicBezier(c1, c2, d) => {
fmt_num!("c{} {} {} {} {} {}", c1.x, c1.y, c2.x, c2.y, d.x, d.y)
}
// GRCOV_EXCL_START - no quads in example
PathSegment::QuadraticBezier(c1, d) => fmt_num!("q{} {} {} {}", c1.x, c1.y, d.x, d.y),
// GRCOV_EXCL_STOP
PathSegment::Close => "z".into(),
})
.collect();
Expand Down
54 changes: 52 additions & 2 deletions keyset-geom/src/circle.rs
Original file line number Diff line number Diff line change
@@ -1,28 +1,46 @@
use std::borrow::Borrow;
use std::fmt;

use isclose::IsClose;

use crate::{Length, Point};

/// A circle
#[derive(Debug, PartialEq)]
pub struct Circle<U> {
/// Center point
pub center: Point<U>,
/// Radius size
pub radius: Length<U>,
}

// Impl here rather than derive so we don't require U: Clone everywhere
// Impl here rather than derive so we don't require U: Clone
impl<U> Clone for Circle<U> {
#[inline]
fn clone(&self) -> Self {
*self
}
}

// Impl here rather than derive so we don't require U: Copy
impl<U> Copy for Circle<U> {}

// Impl here rather than derive so we don't require U: PartialEq
impl<U> PartialEq for Circle<U> {
fn eq(&self, other: &Self) -> bool {
self.center.eq(&other.center) && self.radius.eq(&other.radius)
}
}

// Impl here rather than derive so we don't require U: Debug
impl<U> fmt::Debug for Circle<U> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Circle")
.field("center", &self.center)
.field("radius", &self.radius)
.finish()
}
}

impl<U> Circle<U> {
/// Create a new circle with the given center and radius.
#[inline]
Expand Down Expand Up @@ -63,6 +81,38 @@ mod tests {

use super::*;

#[test]
fn circle_clone() {
struct NonCloneable;
let circle = Circle::<NonCloneable> {
center: Point::new(1.0, 2.0),
radius: Length::new(1.0),
};

#[allow(clippy::clone_on_copy)] // We want to test clone, not copy
let circle2 = circle.clone();

assert_is_close!(circle, circle2);
}

#[test]
fn circle_partial_eq() {
struct NonPartialEq;
let circle = Circle::<NonPartialEq>::new(Point::new(1.0, 2.0), Length::new(1.0));
let circle2 = circle;

assert_eq!(circle, circle2);
}

#[test]
fn circle_debug() {
struct NonDebug;
let circle = Circle::<NonDebug>::new(Point::new(1.0, 2.0), Length::new(1.0));
let dbg = format!("{circle:?}");

assert_eq!(dbg, "Circle { center: (1.0, 2.0), radius: 1.0 }");
}

#[test]
fn circle_new() {
let circle = Circle::<()>::new(Point::new(1.0, 2.0), Length::new(0.5));
Expand Down
3 changes: 1 addition & 2 deletions keyset-geom/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ pub use path::{Path, PathBuilder, PathSegment, ToPath};
pub use round_rect::RoundRect;
pub use traits::*;
pub use unit::{
Dot, Inch, Mm, ToTransform, Unit, DOT_PER_INCH, DOT_PER_MM, DOT_PER_UNIT, INCH_PER_UNIT,
MM_PER_UNIT,
Dot, Inch, Mm, Unit, DOT_PER_INCH, DOT_PER_MM, DOT_PER_UNIT, INCH_PER_UNIT, MM_PER_UNIT,
};

/// An angle in radians
Expand Down
Loading

0 comments on commit eb5ae65

Please sign in to comment.