From f4800b59c446e25a960cb58275025d03ce26fd90 Mon Sep 17 00:00:00 2001 From: Jean SIMARD Date: Fri, 11 Oct 2019 16:46:58 +0200 Subject: [PATCH] Add tests for transit_model_procmacro --- transit_model_procmacro/Cargo.toml | 12 +++ .../tests/01-get-corresponding.rs | 82 +++++++++++++++++++ .../tests/02-invalid-weight.rs | 12 +++ .../tests/02-invalid-weight.stderr | 7 ++ .../tests/03-non-supported-argument.rs | 12 +++ .../tests/03-non-supported-argument.stderr | 7 ++ transit_model_procmacro/tests/tests.rs | 7 ++ 7 files changed, 139 insertions(+) create mode 100644 transit_model_procmacro/tests/01-get-corresponding.rs create mode 100644 transit_model_procmacro/tests/02-invalid-weight.rs create mode 100644 transit_model_procmacro/tests/02-invalid-weight.stderr create mode 100644 transit_model_procmacro/tests/03-non-supported-argument.rs create mode 100644 transit_model_procmacro/tests/03-non-supported-argument.stderr create mode 100644 transit_model_procmacro/tests/tests.rs diff --git a/transit_model_procmacro/Cargo.toml b/transit_model_procmacro/Cargo.toml index f1da04be5..e44157bd9 100644 --- a/transit_model_procmacro/Cargo.toml +++ b/transit_model_procmacro/Cargo.toml @@ -5,6 +5,7 @@ authors = ["Guillaume Pinot "] license = "AGPL-3.0-only" description = "A private procmacro crate for the transit_model crate" edition = "2018" +autotests = false [lib] proc-macro = true @@ -12,3 +13,14 @@ proc-macro = true [dependencies] syn = "0.11.11" quote = "0.3.15" + +[dev-dependencies] +pretty_assertions = "0.6" +trybuild = "1" +transit_model = { path = "../" } +transit_model_collection = { path = "../collection" } +transit_model_relations = { path = "../relations" } + +[[test]] +name = "tests" +path = "tests/tests.rs" diff --git a/transit_model_procmacro/tests/01-get-corresponding.rs b/transit_model_procmacro/tests/01-get-corresponding.rs new file mode 100644 index 000000000..a07598182 --- /dev/null +++ b/transit_model_procmacro/tests/01-get-corresponding.rs @@ -0,0 +1,82 @@ +use transit_model::objects::*; +use transit_model_collection::*; +use transit_model_procmacro::*; +use transit_model_relations::*; + +#[derive(GetCorresponding)] +pub struct Model { + lines_to_routes: OneToMany, + routes_to_vehicle_journeys: OneToMany, +} + +fn main() { + let line = Line { + id: String::from("line_id"), + name: String::from("Line name"), + ..Default::default() + }; + let route1 = Route { + id: String::from("route_id_1"), + name: String::from("Route Name 1"), + line_id: String::from("line_id"), + ..Default::default() + }; + let route2 = Route { + id: String::from("route_id_2"), + name: String::from("Route Name 2"), + line_id: String::from("line_id"), + ..Default::default() + }; + let vehicle_journey_1 = VehicleJourney { + id: String::from("vehicle_journey_id_1"), + route_id: String::from("route_id_1"), + ..Default::default() + }; + let vehicle_journey_2 = VehicleJourney { + id: String::from("vehicle_journey_id_2"), + route_id: String::from("route_id_1"), + ..Default::default() + }; + let vehicle_journey_3 = VehicleJourney { + id: String::from("vehicle_journey_id_3"), + route_id: String::from("route_id_2"), + ..Default::default() + }; + let vehicle_journey_4 = VehicleJourney { + id: String::from("vehicle_journey_id_4"), + route_id: String::from("route_id_2"), + ..Default::default() + }; + let lines = CollectionWithId::from(line); + let routes = CollectionWithId::new(vec![route1, route2]).unwrap(); + let vehicle_journeys = CollectionWithId::new(vec![ + vehicle_journey_1, + vehicle_journey_2, + vehicle_journey_3, + vehicle_journey_4, + ]) + .unwrap(); + let model = Model { + lines_to_routes: OneToMany::new(&lines, &routes, "lines_to_routes").unwrap(), + routes_to_vehicle_journeys: OneToMany::new( + &routes, + &vehicle_journeys, + "routes_to_vehicle_journeys", + ) + .unwrap(), + }; + + let line_idx = lines.get_idx("line_id").unwrap(); + let vehicle_journey_indexes = model.get_corresponding_from_idx(line_idx); + let vehicle_journey_1_idx = vehicle_journeys.get_idx("vehicle_journey_id_1").unwrap(); + assert!(vehicle_journey_indexes.contains(&vehicle_journey_1_idx)); + let vehicle_journey_2_idx = vehicle_journeys.get_idx("vehicle_journey_id_2").unwrap(); + assert!(vehicle_journey_indexes.contains(&vehicle_journey_2_idx)); + let vehicle_journey_3_idx = vehicle_journeys.get_idx("vehicle_journey_id_3").unwrap(); + assert!(vehicle_journey_indexes.contains(&vehicle_journey_3_idx)); + let vehicle_journey_4_idx = vehicle_journeys.get_idx("vehicle_journey_id_4").unwrap(); + assert!(vehicle_journey_indexes.contains(&vehicle_journey_4_idx)); + + let line_indexes = model.get_corresponding_from_idx(vehicle_journey_1_idx); + assert!(line_indexes.contains(&line_idx)); +} diff --git a/transit_model_procmacro/tests/02-invalid-weight.rs b/transit_model_procmacro/tests/02-invalid-weight.rs new file mode 100644 index 000000000..179d8fce4 --- /dev/null +++ b/transit_model_procmacro/tests/02-invalid-weight.rs @@ -0,0 +1,12 @@ +use transit_model::objects::*; +use transit_model_collection::*; +use transit_model_procmacro::*; +use transit_model_relations::*; + +#[derive(GetCorresponding)] +pub struct Model { + #[get_corresponding(weight = "abc")] + lines_to_routes: OneToMany, +} + +fn main() {} diff --git a/transit_model_procmacro/tests/02-invalid-weight.stderr b/transit_model_procmacro/tests/02-invalid-weight.stderr new file mode 100644 index 000000000..c6ec52f35 --- /dev/null +++ b/transit_model_procmacro/tests/02-invalid-weight.stderr @@ -0,0 +1,7 @@ +error: proc-macro derive panicked + --> $DIR/02-invalid-weight.rs:6:10 + | +6 | #[derive(GetCorresponding)] + | ^^^^^^^^^^^^^^^^ + | + = help: message: `weight` attribute must be convertible to f64: ParseFloatError { kind: Invalid } diff --git a/transit_model_procmacro/tests/03-non-supported-argument.rs b/transit_model_procmacro/tests/03-non-supported-argument.rs new file mode 100644 index 000000000..cdb990548 --- /dev/null +++ b/transit_model_procmacro/tests/03-non-supported-argument.rs @@ -0,0 +1,12 @@ +use transit_model::objects::*; +use transit_model_collection::*; +use transit_model_procmacro::*; +use transit_model_relations::*; + +#[derive(GetCorresponding)] +pub struct Model { + #[get_corresponding(nonsupportedargument)] + lines_to_routes: OneToMany, +} + +fn main() {} diff --git a/transit_model_procmacro/tests/03-non-supported-argument.stderr b/transit_model_procmacro/tests/03-non-supported-argument.stderr new file mode 100644 index 000000000..506188cd3 --- /dev/null +++ b/transit_model_procmacro/tests/03-non-supported-argument.stderr @@ -0,0 +1,7 @@ +error: proc-macro derive panicked + --> $DIR/03-non-supported-argument.rs:6:10 + | +6 | #[derive(GetCorresponding)] + | ^^^^^^^^^^^^^^^^ + | + = help: message: Only `key = "value"` attributes supported. diff --git a/transit_model_procmacro/tests/tests.rs b/transit_model_procmacro/tests/tests.rs new file mode 100644 index 000000000..fd02bc1e2 --- /dev/null +++ b/transit_model_procmacro/tests/tests.rs @@ -0,0 +1,7 @@ +#[test] +fn compile_error() { + let t = trybuild::TestCases::new(); + t.pass("tests/01-get-corresponding.rs"); + t.compile_fail("tests/02-invalid-weight.rs"); + t.compile_fail("tests/03-non-supported-argument.rs"); +}