-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #412 from woshilapin/procmacro-tests
Add tests for transit_model_procmacro
- Loading branch information
Showing
7 changed files
with
139 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
12
transit_model_procmacro/tests/03-non-supported-argument.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() {} |
7 changes: 7 additions & 0 deletions
7
transit_model_procmacro/tests/03-non-supported-argument.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} |