Skip to content

Commit

Permalink
Merge pull request #412 from woshilapin/procmacro-tests
Browse files Browse the repository at this point in the history
Add tests for transit_model_procmacro
  • Loading branch information
ArnaudOggy authored Mar 3, 2020
2 parents f16bd08 + f4800b5 commit 759b64c
Show file tree
Hide file tree
Showing 7 changed files with 139 additions and 0 deletions.
12 changes: 12 additions & 0 deletions transit_model_procmacro/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,22 @@ authors = ["Guillaume Pinot <[email protected]>"]
license = "AGPL-3.0-only"
description = "A private procmacro crate for the transit_model crate"
edition = "2018"
autotests = false

[lib]
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"
82 changes: 82 additions & 0 deletions transit_model_procmacro/tests/01-get-corresponding.rs
Original file line number Diff line number Diff line change
@@ -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<Line, Route>,
routes_to_vehicle_journeys: OneToMany<Route, VehicleJourney>,
}

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));
}
12 changes: 12 additions & 0 deletions transit_model_procmacro/tests/02-invalid-weight.rs
Original file line number Diff line number Diff line change
@@ -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<Line, Route>,
}

fn main() {}
7 changes: 7 additions & 0 deletions transit_model_procmacro/tests/02-invalid-weight.stderr
Original file line number Diff line number Diff line change
@@ -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 }
12 changes: 12 additions & 0 deletions transit_model_procmacro/tests/03-non-supported-argument.rs
Original file line number Diff line number Diff line change
@@ -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<Line, Route>,
}

fn main() {}
Original file line number Diff line number Diff line change
@@ -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.
7 changes: 7 additions & 0 deletions transit_model_procmacro/tests/tests.rs
Original file line number Diff line number Diff line change
@@ -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");
}

0 comments on commit 759b64c

Please sign in to comment.