From 3ddc133a0e561ffb05b5a1fc7ac72510ed4a127a Mon Sep 17 00:00:00 2001 From: Sergio Benitez Date: Wed, 28 Sep 2016 20:39:14 -0700 Subject: [PATCH] Add more codegen tests. --- codegen/src/decorators/derive_form.rs | 1 + codegen/src/parser/route.rs | 9 +++++---- .../tests/compile-fail/bad-attribute-form.rs | 13 ++++++++++++ .../tests/compile-fail/bad-attribute-param.rs | 12 +++++++++++ .../bad-value-types-in-attribute.rs | 20 +++++++++++++++++++ codegen/tests/compile-fail/decorate-enum.rs | 12 +++++++++++ codegen/tests/compile-fail/decorate-impl.rs | 12 +++++++++++ codegen/tests/compile-fail/decorate-struct.rs | 11 ++++++++++ codegen/tests/compile-fail/decorate-trait.rs | 11 ++++++++++ codegen/tests/compile-fail/ignored_params.rs | 6 ++++++ .../tests/compile-fail/route-bad-method.rs | 13 ++++++++++++ .../compile-fail/route-invalid-method.rs | 12 +++++++++++ .../compile-fail/unknown-attribute-param.rs | 12 +++++++++++ codegen/tests/run-pass/dynamic-paths.rs | 11 ++++++++++ 14 files changed, 151 insertions(+), 4 deletions(-) create mode 100644 codegen/tests/compile-fail/bad-attribute-form.rs create mode 100644 codegen/tests/compile-fail/bad-attribute-param.rs create mode 100644 codegen/tests/compile-fail/bad-value-types-in-attribute.rs create mode 100644 codegen/tests/compile-fail/decorate-enum.rs create mode 100644 codegen/tests/compile-fail/decorate-impl.rs create mode 100644 codegen/tests/compile-fail/decorate-struct.rs create mode 100644 codegen/tests/compile-fail/decorate-trait.rs create mode 100644 codegen/tests/compile-fail/route-bad-method.rs create mode 100644 codegen/tests/compile-fail/route-invalid-method.rs create mode 100644 codegen/tests/compile-fail/unknown-attribute-param.rs create mode 100644 codegen/tests/run-pass/dynamic-paths.rs diff --git a/codegen/src/decorators/derive_form.rs b/codegen/src/decorators/derive_form.rs index 9812c818..a41bf938 100644 --- a/codegen/src/decorators/derive_form.rs +++ b/codegen/src/decorators/derive_form.rs @@ -46,6 +46,7 @@ fn get_struct_lifetime(ecx: &mut ExtCtxt, item: &Annotatable, span: Span) } } +// TODO: Use proper logging to emit the error messages. pub fn from_form_derive(ecx: &mut ExtCtxt, span: Span, meta_item: &MetaItem, annotated: &Annotatable, push: &mut FnMut(Annotatable)) { let struct_lifetime = get_struct_lifetime(ecx, annotated, span); diff --git a/codegen/src/parser/route.rs b/codegen/src/parser/route.rs index 5d116128..7ca31c1a 100644 --- a/codegen/src/parser/route.rs +++ b/codegen/src/parser/route.rs @@ -164,7 +164,7 @@ fn parse_method(ecx: &ExtCtxt, meta_item: &NestedMetaItem) -> Spanned { return span(method, word.span()); } } else { - let msg = format!("{} is not a valid method.", word.name()); + let msg = format!("{} is not a valid HTTP method.", word.name()); ecx.span_err(word.span(), &msg); } } @@ -224,7 +224,8 @@ fn parse_form(ecx: &ExtCtxt, kv: &KVSpanned) -> Ident { } } - ecx.struct_span_err(kv.span, r#"expected `form = ""`"#) + let err_string = r#"`form` value must be a parameter, e.g: ""`"#; + ecx.struct_span_fatal(kv.span, err_string) .help(r#"form, if specified, must be a key-value pair where the key is `form` and the value is a string with a single parameter inside '<' '>'. e.g: form = """#) @@ -243,7 +244,7 @@ fn parse_rank(ecx: &ExtCtxt, kv: &KVSpanned) -> isize { ecx.span_err(kv.value.span, msg.as_str()); } } else { - ecx.struct_span_err(kv.span, r#"expected `rank = int`"#) + ecx.struct_span_err(kv.span, r#"`rank` value must be an int"#) .help(r#"the rank, if specified, must be a key-value pair where the key is `rank` and the value is an integer. e.g: rank = 1, or e.g: rank = 10"#) @@ -267,7 +268,7 @@ fn parse_format(ecx: &ExtCtxt, kv: &KVSpanned) -> ContentType { } } - ecx.struct_span_err(kv.span, r#"expected `format = "content/type"`"#) + ecx.struct_span_err(kv.span, r#"`format` must be a "content/type"`"#) .help(r#"format, if specified, must be a key-value pair where the key is `format` and the value is a string representing the content-type accepted. e.g: format = "application/json""#) diff --git a/codegen/tests/compile-fail/bad-attribute-form.rs b/codegen/tests/compile-fail/bad-attribute-form.rs new file mode 100644 index 00000000..2315262b --- /dev/null +++ b/codegen/tests/compile-fail/bad-attribute-form.rs @@ -0,0 +1,13 @@ +#![feature(plugin)] +#![plugin(rocket_codegen)] + +extern crate rocket; + +#[get] //~ ERROR incorrect use of attribute +//~^ ERROR malformed attribute +fn get() -> &'static str { "hi" } + +fn main() { + let _ = routes![get]; +} + diff --git a/codegen/tests/compile-fail/bad-attribute-param.rs b/codegen/tests/compile-fail/bad-attribute-param.rs new file mode 100644 index 00000000..b2a4b3e1 --- /dev/null +++ b/codegen/tests/compile-fail/bad-attribute-param.rs @@ -0,0 +1,12 @@ +#![feature(plugin)] +#![plugin(rocket_codegen)] + +extern crate rocket; + +#[get(path = "hello", 123)] //~ ERROR expected +fn get() -> &'static str { "hi" } + +fn main() { + let _ = routes![get]; +} + diff --git a/codegen/tests/compile-fail/bad-value-types-in-attribute.rs b/codegen/tests/compile-fail/bad-value-types-in-attribute.rs new file mode 100644 index 00000000..d3ae9464 --- /dev/null +++ b/codegen/tests/compile-fail/bad-value-types-in-attribute.rs @@ -0,0 +1,20 @@ +#![feature(plugin)] +#![plugin(rocket_codegen)] + +extern crate rocket; + +#[get(1)] //~ ERROR expected `path = string` +fn get0() -> &'static str { "hi" } + +#[get(path = 1)] //~ ERROR must be a string +fn get1() -> &'static str { "hi" } + +#[get(path = "h", rank = "2")] //~ ERROR must be an int +fn get2() -> &'static str { "hi" } + +#[get(path = "h", format = 100)] //~ ERROR must be a "content/type" +fn get3() -> &'static str { "hi" } + +fn main() { +} + diff --git a/codegen/tests/compile-fail/decorate-enum.rs b/codegen/tests/compile-fail/decorate-enum.rs new file mode 100644 index 00000000..0998e244 --- /dev/null +++ b/codegen/tests/compile-fail/decorate-enum.rs @@ -0,0 +1,12 @@ +#![feature(plugin)] +#![plugin(rocket_codegen)] + +extern crate rocket; + +#[get("")] //~ ERROR can only be used on functions +enum B { } //~ ERROR but was applied + +fn main() { + let _ = routes![get]; +} + diff --git a/codegen/tests/compile-fail/decorate-impl.rs b/codegen/tests/compile-fail/decorate-impl.rs new file mode 100644 index 00000000..7d0b88ca --- /dev/null +++ b/codegen/tests/compile-fail/decorate-impl.rs @@ -0,0 +1,12 @@ +#![feature(plugin)] +#![plugin(rocket_codegen)] + +extern crate rocket; + +#[get("")] //~ ERROR can only be used on functions +impl C for A { } //~ ERROR but was applied + +fn main() { + let _ = routes![get]; +} + diff --git a/codegen/tests/compile-fail/decorate-struct.rs b/codegen/tests/compile-fail/decorate-struct.rs new file mode 100644 index 00000000..066a13da --- /dev/null +++ b/codegen/tests/compile-fail/decorate-struct.rs @@ -0,0 +1,11 @@ +#![feature(plugin)] +#![plugin(rocket_codegen)] + +extern crate rocket; + +#[get("")] //~ ERROR can only be used on functions +struct A; //~ ERROR but was applied + +fn main() { + let _ = routes![get]; +} diff --git a/codegen/tests/compile-fail/decorate-trait.rs b/codegen/tests/compile-fail/decorate-trait.rs new file mode 100644 index 00000000..2757e14c --- /dev/null +++ b/codegen/tests/compile-fail/decorate-trait.rs @@ -0,0 +1,11 @@ +#![feature(plugin)] +#![plugin(rocket_codegen)] + +extern crate rocket; + +#[get("")] //~ ERROR can only be used on functions +trait C { } //~ ERROR but was applied + +fn main() { + let _ = routes![get]; +} diff --git a/codegen/tests/compile-fail/ignored_params.rs b/codegen/tests/compile-fail/ignored_params.rs index 0e3ee394..2bc0a749 100644 --- a/codegen/tests/compile-fail/ignored_params.rs +++ b/codegen/tests/compile-fail/ignored_params.rs @@ -4,4 +4,10 @@ #[get("/")] //~ ERROR 'name' is declared fn get(other: &str) -> &'static str { "hi" } //~ ERROR isn't in the function +#[get("/a?")] //~ ERROR 'r' is declared +fn get1() -> &'static str { "hi" } //~ ERROR isn't in the function + +#[get("/a", form = "")] //~ ERROR 'test' is declared +fn get2() -> &'static str { "hi" } //~ ERROR isn't in the function + fn main() { } diff --git a/codegen/tests/compile-fail/route-bad-method.rs b/codegen/tests/compile-fail/route-bad-method.rs new file mode 100644 index 00000000..00f126aa --- /dev/null +++ b/codegen/tests/compile-fail/route-bad-method.rs @@ -0,0 +1,13 @@ +#![feature(plugin)] +#![plugin(rocket_codegen)] + +extern crate rocket; + +#[route(FIX, "hello")] //~ ERROR FIX is not a valid HTTP method +//~^ ERROR valid HTTP method +fn get() -> &'static str { "hi" } + +fn main() { + let _ = routes![get]; +} + diff --git a/codegen/tests/compile-fail/route-invalid-method.rs b/codegen/tests/compile-fail/route-invalid-method.rs new file mode 100644 index 00000000..8d3b2201 --- /dev/null +++ b/codegen/tests/compile-fail/route-invalid-method.rs @@ -0,0 +1,12 @@ +#![feature(plugin)] +#![plugin(rocket_codegen)] + +extern crate rocket; + +#[route(CONNECT, "hello")] //~ ERROR valid HTTP method +fn get() -> &'static str { "hi" } + +fn main() { + let _ = routes![get]; +} + diff --git a/codegen/tests/compile-fail/unknown-attribute-param.rs b/codegen/tests/compile-fail/unknown-attribute-param.rs new file mode 100644 index 00000000..8b1f1932 --- /dev/null +++ b/codegen/tests/compile-fail/unknown-attribute-param.rs @@ -0,0 +1,12 @@ +#![feature(plugin)] +#![plugin(rocket_codegen)] + +extern crate rocket; + +#[get(path = "hello", unknown = 123)] //~ ERROR 'unknown' is not a known param +fn get() -> &'static str { "hi" } + +fn main() { + let _ = routes![get]; +} + diff --git a/codegen/tests/run-pass/dynamic-paths.rs b/codegen/tests/run-pass/dynamic-paths.rs new file mode 100644 index 00000000..92c331c6 --- /dev/null +++ b/codegen/tests/run-pass/dynamic-paths.rs @@ -0,0 +1,11 @@ +#![feature(plugin)] +#![plugin(rocket_codegen)] + +extern crate rocket; + +#[get("/test///")] +fn get(one: &str, two: usize, three: isize) -> &'static str { "hi" } + +fn main() { + let _ = routes![get]; +}