Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

created a simpler syntax for macros for test fixtures #1061

Closed
wants to merge 5 commits into from
Closed
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions geo-types/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,18 @@ macro_rules! coord {
///
/// # Examples
///
/// Creating a [`LineString`] with WKT-like syntax:
///
/// ```
/// use geo_types::line_string;
/// let ls = line_string![76.9454 43.2497, 76.9636 43.2308, 77.0591 43.1575, 77.1108 43.1131];
///
/// assert_eq!(ls[1], geo_types::coord! {
/// x: 76.9636,
/// y: 43.2308
/// });
/// ```
///
/// Creating a [`LineString`], supplying x/y values:
///
/// ```
Expand Down Expand Up @@ -124,6 +136,9 @@ macro_rules! coord {
#[macro_export]
macro_rules! line_string {
() => { $crate::LineString::new(vec![]) };
($($x:literal $y:literal),+) => {
line_string![$((x: $x, y: $y)),+]
};
(
$(( $($tag:tt : $val:expr),* $(,)? )),*
$(,)?
Expand Down Expand Up @@ -166,6 +181,19 @@ macro_rules! line_string {
///
/// # Examples
///
/// Creating a [`Polygon'] with WKT-like syntax:
///
/// ```
/// use geo_types::polygon;
///
/// let simple_poly = polygon!(0.0 0.0, 30.0 0.0, 30.0 30.0, 0.0 30.0, 0.0 0.0);
/// assert_eq!(simple_poly.exterior()[1], geo_types::coord!{ x: 30.0, y: 0.0 });
/// assert_eq!(simple_poly.interiors().len(), 0);
///
/// let poly_with_hole = polygon!([0.0 0.0, 30.0 0.0, 30.0 30.0, 0.0 30.0, 0.0 0.0], [10.0 10.0, 20.0 10.0, 20.0 20.0, 10.0 20.0, 10.0 10.0]);
/// assert_eq!(poly_with_hole.interiors()[0][1], geo_types::coord!{ x: 20.0, y: 10.0 });
/// ```
///
/// Creating a [`Polygon`] without interior rings, supplying x/y values:
///
/// ```
Expand Down Expand Up @@ -217,6 +245,19 @@ macro_rules! line_string {
#[macro_export]
macro_rules! polygon {
() => { $crate::Polygon::new($crate::line_string![], vec![]) };
($($x:literal $y:literal),+) => {
polygon!($($crate::coord! { x: $x, y: $y }),+)
};
([$($xi:literal $yi:literal),+ $(,)?], $([$($xo:literal $yo:literal),+ $(,)?]),*) => {
polygon!(
exterior: [$($crate::coord!(x: $xi, y: $yi)),+],
interiors: [
$([
$($crate::coord!(x: $xo, y: $yo),)+
]),*
]
)
};
(
exterior: [
$(( $($exterior_tag:tt : $exterior_val:expr),* $(,)? )),*
Expand Down Expand Up @@ -318,6 +359,10 @@ mod test {

#[test]
fn test_line() {
let ls = line_string![12.345 34.567, 98.765 43.21];
assert_eq!(ls[0], coord! { x: 12.345, y: 34.567 });
assert_eq!(ls[1], coord! { x: 98.765, y: 43.21 });

let ls = line_string![(x: -1.2f32, y: 3.4f32)];
assert_eq!(ls[0], coord! { x: -1.2, y: 3.4 });

Expand Down Expand Up @@ -376,5 +421,14 @@ mod test {
);
assert_eq!(p.exterior()[0], coord! { x: 1, y: 2 });
assert_eq!(p.interiors()[0][0], coord! { x: 3, y: 4 });

let simple_poly = polygon!(0.0 0.0, 30.0 0.0, 30.0 30.0, 0.0 30.0, 0.0 0.0);
assert_eq!(simple_poly.exterior()[1], coord! { x: 30.0, y: 0.0 });

let poly_with_hole = polygon!([0.0 0.0, 30.0 0.0, 30.0 30.0, 0.0 30.0, 0.0 0.0], [10.0 10.0, 20.0 10.0, 20.0 20.0, 10.0 20.0, 10.0 10.0]);
assert_eq!(
poly_with_hole.interiors()[0][1],
coord! { x: 20.0, y: 10.0 }
);
}
}