mirror of
https://github.com/rwf2/Rocket.git
synced 2025-01-18 23:49:09 +00:00
Fixed unchecked unwrap. Added codegen tests.
This commit is contained in:
parent
f259593727
commit
5915c69f39
9
codegen_tests/Cargo.toml
Normal file
9
codegen_tests/Cargo.toml
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
[package]
|
||||||
|
name = "codegen_tests"
|
||||||
|
version = "0.1.0"
|
||||||
|
authors = ["Sergio Benitez <sb@sergio.bz>"]
|
||||||
|
|
||||||
|
[dev-dependencies]
|
||||||
|
compiletest-rs = "*"
|
||||||
|
rocket = "../lib"
|
||||||
|
rocket_macros = "../macros"
|
3
codegen_tests/src/main.rs
Normal file
3
codegen_tests/src/main.rs
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
fn main() {
|
||||||
|
println!("Hello, world!");
|
||||||
|
}
|
@ -10,3 +10,6 @@ plugin = true
|
|||||||
rocket = { path = "../lib/" }
|
rocket = { path = "../lib/" }
|
||||||
log = "*"
|
log = "*"
|
||||||
env_logger = "*"
|
env_logger = "*"
|
||||||
|
|
||||||
|
[dev-dependencies]
|
||||||
|
compiletest_rs = "*"
|
||||||
|
@ -78,6 +78,11 @@ impl RouteGenerateExt for RouteParams {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let arg = arg.unwrap();
|
let arg = arg.unwrap();
|
||||||
|
if arg.ident().is_none() {
|
||||||
|
ecx.span_err(arg.pat.span, "argument names must be identifiers");
|
||||||
|
return None;
|
||||||
|
};
|
||||||
|
|
||||||
let name = arg.ident().unwrap().prepend(PARAM_PREFIX);
|
let name = arg.ident().unwrap().prepend(PARAM_PREFIX);
|
||||||
let ty = strip_ty_lifetimes(arg.ty.clone());
|
let ty = strip_ty_lifetimes(arg.ty.clone());
|
||||||
Some(quote_stmt!(ecx,
|
Some(quote_stmt!(ecx,
|
||||||
@ -147,12 +152,14 @@ impl RouteGenerateExt for RouteParams {
|
|||||||
|
|
||||||
// A from_request parameter is one that isn't declared, form, or query.
|
// A from_request parameter is one that isn't declared, form, or query.
|
||||||
let from_request = |a: &&Arg| {
|
let from_request = |a: &&Arg| {
|
||||||
!declared_set.contains(&*a.name().unwrap())
|
a.name().map_or(false, |name| {
|
||||||
&& self.form_param.as_ref().map_or(true, |p| {
|
!declared_set.contains(name)
|
||||||
!a.named(&p.value().name)
|
&& self.form_param.as_ref().map_or(true, |p| {
|
||||||
}) && self.query_param.as_ref().map_or(true, |p| {
|
!a.named(&p.value().name)
|
||||||
!a.named(&p.node.name)
|
}) && self.query_param.as_ref().map_or(true, |p| {
|
||||||
})
|
!a.named(&p.node.name)
|
||||||
|
})
|
||||||
|
})
|
||||||
};
|
};
|
||||||
|
|
||||||
// Generate the code for `form_request` parameters.
|
// Generate the code for `form_request` parameters.
|
||||||
@ -173,9 +180,10 @@ impl RouteGenerateExt for RouteParams {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn generate_fn_arguments(&self, ecx: &ExtCtxt) -> Vec<TokenTree> {
|
fn generate_fn_arguments(&self, ecx: &ExtCtxt) -> Vec<TokenTree> {
|
||||||
let args = self.annotated_fn.decl().inputs.iter().map(|a| {
|
let args = self.annotated_fn.decl().inputs.iter()
|
||||||
a.ident().expect("function decl pat -> ident").prepend(PARAM_PREFIX)
|
.filter_map(|a| a.ident())
|
||||||
}).collect::<Vec<Ident>>();
|
.map(|ident| ident.prepend(PARAM_PREFIX))
|
||||||
|
.collect::<Vec<Ident>>();
|
||||||
|
|
||||||
sep_by_tok(ecx, &args, token::Comma)
|
sep_by_tok(ecx, &args, token::Comma)
|
||||||
}
|
}
|
||||||
|
7
macros/tests/compile-fail/ignored_params.rs
Normal file
7
macros/tests/compile-fail/ignored_params.rs
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
#![feature(plugin)]
|
||||||
|
#![plugin(rocket_macros)]
|
||||||
|
|
||||||
|
#[get("/<name>")] //~ ERROR 'name' is declared
|
||||||
|
fn get(_: &str) -> &'static str { "hi" } //~ ERROR isn't in the function
|
||||||
|
|
||||||
|
fn main() { }
|
17
macros/tests/compile-fail/phantom-declared-param.rs
Normal file
17
macros/tests/compile-fail/phantom-declared-param.rs
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
#![feature(plugin)]
|
||||||
|
#![plugin(rocket_macros)]
|
||||||
|
|
||||||
|
#[get("/<param>")] //~ ERROR declared
|
||||||
|
fn get() { } //~ ERROR isn't in the function signature
|
||||||
|
|
||||||
|
#[get("/<a>")] //~ ERROR declared
|
||||||
|
fn get2() { } //~ ERROR isn't in the function signature
|
||||||
|
|
||||||
|
#[get("/a/b/c/<a>/<b>")]
|
||||||
|
//~^ ERROR 'a' is declared
|
||||||
|
//~^^ ERROR 'b' is declared
|
||||||
|
fn get32() { }
|
||||||
|
//~^ ERROR isn't in the function signature
|
||||||
|
//~^^ ERROR isn't in the function signature
|
||||||
|
|
||||||
|
fn main() { }
|
11
macros/tests/run-pass/empty-fn.rs
Normal file
11
macros/tests/run-pass/empty-fn.rs
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
#![feature(plugin)]
|
||||||
|
#![plugin(rocket_macros)]
|
||||||
|
|
||||||
|
extern crate rocket;
|
||||||
|
|
||||||
|
#[get("")]
|
||||||
|
fn get() -> &'static str { "hi" }
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let _ = routes![get];
|
||||||
|
}
|
14
macros/tests/run-pass/issue-1-colliding-names.rs
Normal file
14
macros/tests/run-pass/issue-1-colliding-names.rs
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
#![feature(plugin)]
|
||||||
|
#![plugin(rocket_macros)]
|
||||||
|
|
||||||
|
extern crate rocket;
|
||||||
|
|
||||||
|
#[get("/<todo>")]
|
||||||
|
fn todo(todo: &str) -> &str {
|
||||||
|
todo
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let _ = routes![todo];
|
||||||
|
}
|
||||||
|
|
20
macros/tests/tests.rs
Normal file
20
macros/tests/tests.rs
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
extern crate compiletest_rs as compiletest;
|
||||||
|
|
||||||
|
use std::path::PathBuf;
|
||||||
|
|
||||||
|
fn run_mode(mode: &'static str) {
|
||||||
|
let mut config = compiletest::default_config();
|
||||||
|
let cfg_mode = mode.parse().ok().expect("Invalid mode");
|
||||||
|
|
||||||
|
config.mode = cfg_mode;
|
||||||
|
config.src_base = PathBuf::from(format!("tests/{}", mode));
|
||||||
|
config.target_rustcflags = Some("-L ../target/debug/ -L ../target/debug/deps/".to_owned());
|
||||||
|
|
||||||
|
compiletest::run_tests(&config);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn compile_test() {
|
||||||
|
run_mode("compile-fail");
|
||||||
|
run_mode("run-pass");
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user