33
44//! The `#[testkit::test]` attribute.
55//!
6- //! A test is an `async fn` taking `&mut testkit::Context` and
7- //! returning `testkit::Result`. Its doc comment is the test's description and
8- //! is required. `id` is the required stable test ID: random, never changed,
9- //! and kept when the test is renamed or moved. A missing ID's compile error
10- //! suggests a fresh one. Requirements are named after target capabilities:
6+ //! A test is an `async fn` taking `&mut testkit::Context` and returning
7+ //! `testkit::Result`. Its doc comment is the test's description and is
8+ //! required:
119//!
1210//! ```ignore
1311//! /// An exposed loopback HTTP service is reachable and goes away after delete.
14- //! #[testkit::test(id = "t-4f8k2m0q", requires = [service_exposure]) ]
12+ //! #[testkit::test]
1513//! async fn expose_service(cx: &mut Context) -> testkit::Result { /* ... */ }
1614//! ```
1715//!
1816//! The attribute expands to a `#[tokio::test]` wrapper with the same name, so
19- //! the nextest test name is the function name. The original function is nested in
20- //! the wrapper and passed to `testkit::run`. Parsing lives in `testkit-spec`,
21- //! so tools that list tests share the macro's rules.
17+ //! the nextest test name is the function name. The original function is nested
18+ //! in the wrapper and passed to `testkit::run`.
2219
2320use proc_macro:: TokenStream ;
2421use proc_macro2:: TokenStream as TokenStream2 ;
2522use quote:: { format_ident, quote} ;
26- use syn:: { Ident , ItemFn , LitStr } ;
23+ use syn:: { Attribute , Expr , ExprLit , ItemFn , Lit , LitStr , Meta } ;
2724
2825#[ proc_macro_attribute]
2926pub fn test ( attr : TokenStream , item : TokenStream ) -> TokenStream {
@@ -34,16 +31,35 @@ pub fn test(attr: TokenStream, item: TokenStream) -> TokenStream {
3431
3532fn expand ( attr : TokenStream2 , item : TokenStream2 ) -> syn:: Result < TokenStream2 > {
3633 let mut body: ItemFn = syn:: parse2 ( item) ?;
37- let spec = testkit_spec:: parse ( attr, & body) ?;
34+ if !attr. is_empty ( ) {
35+ return Err ( syn:: Error :: new_spanned (
36+ attr,
37+ "`#[testkit::test]` takes no arguments" ,
38+ ) ) ;
39+ }
40+ if body. sig . asyncness . is_none ( ) {
41+ return Err ( syn:: Error :: new_spanned (
42+ body. sig . fn_token ,
43+ "testkit tests must be `async fn`" ,
44+ ) ) ;
45+ }
46+ if body. sig . inputs . len ( ) != 1 {
47+ return Err ( syn:: Error :: new_spanned (
48+ & body. sig . inputs ,
49+ "testkit tests take exactly one argument: `cx: &mut testkit::Context`" ,
50+ ) ) ;
51+ }
52+ let description = doc_comment ( & body. attrs ) ;
53+ if description. is_empty ( ) {
54+ return Err ( syn:: Error :: new (
55+ body. sig . ident . span ( ) ,
56+ "testkit tests need a doc comment describing the behavior they verify" ,
57+ ) ) ;
58+ }
3859
39- let name = spec . name ;
60+ let name = body . sig . ident . clone ( ) ;
4061 let name_str = LitStr :: new ( & name. to_string ( ) , name. span ( ) ) ;
41- let id = LitStr :: new ( & spec. id , name. span ( ) ) ;
42- let description = LitStr :: new ( & spec. description , name. span ( ) ) ;
43- let requires = spec. requires . iter ( ) . map ( |capability| {
44- let variant = Ident :: new ( & to_camel_case ( & capability. to_string ( ) ) , capability. span ( ) ) ;
45- quote ! ( :: testkit:: Capability :: #variant)
46- } ) ;
62+ let description = LitStr :: new ( & description, name. span ( ) ) ;
4763
4864 let vis = body. vis . clone ( ) ;
4965 let doc_attrs: Vec < _ > = body
@@ -63,10 +79,8 @@ fn expand(attr: TokenStream2, item: TokenStream2) -> syn::Result<TokenStream2> {
6379 #body
6480 :: testkit:: run(
6581 :: testkit:: TestInfo {
66- id: #id,
6782 name: #name_str,
6883 description: #description,
69- requires: & [ #( #requires) , * ] ,
7084 } ,
7185 body,
7286 )
@@ -75,16 +89,23 @@ fn expand(attr: TokenStream2, item: TokenStream2) -> syn::Result<TokenStream2> {
7589 } )
7690}
7791
78- fn to_camel_case ( snake : & str ) -> String {
79- snake
80- . split ( '_' )
81- . map ( |word| {
82- let mut chars = word. chars ( ) ;
83- chars. next ( ) . map_or_else ( String :: new, |first| {
84- first. to_uppercase ( ) . chain ( chars) . collect ( )
85- } )
92+ /// The doc comment, joined into one line. Empty if there is none.
93+ fn doc_comment ( attrs : & [ Attribute ] ) -> String {
94+ attrs
95+ . iter ( )
96+ . filter_map ( |attr| match & attr. meta {
97+ Meta :: NameValue ( meta) if meta. path . is_ident ( "doc" ) => match & meta. value {
98+ Expr :: Lit ( ExprLit {
99+ lit : Lit :: Str ( line) ,
100+ ..
101+ } ) => Some ( line. value ( ) . trim ( ) . to_string ( ) ) ,
102+ _ => None ,
103+ } ,
104+ _ => None ,
86105 } )
87- . collect ( )
106+ . filter ( |line| !line. is_empty ( ) )
107+ . collect :: < Vec < _ > > ( )
108+ . join ( " " )
88109}
89110
90111#[ cfg( test) ]
@@ -98,39 +119,53 @@ mod tests {
98119 #[ test]
99120 fn expands_to_wrapper_with_same_name ( ) {
100121 let out = expand_str (
101- "id = \" t-0123abcd \" " ,
102- "/// Does a thing .\n async fn does_a_thing(cx: &mut Context) -> testkit::Result { Ok(()) }" ,
122+ "" ,
123+ "/// First line \n /// second line .\n async fn does_a_thing(cx: &mut Context) -> testkit::Result { Ok(()) }" ,
103124 )
104125 . unwrap ( ) ;
105126 assert ! ( out. contains( "async fn does_a_thing ()" ) , "{out}" ) ;
106127 assert ! ( out. contains( "async fn body (cx : & mut Context)" ) , "{out}" ) ;
107- assert ! ( out. contains( "id : \" t-0123abcd\" " ) , "{out}" ) ;
108128 assert ! ( out. contains( "name : \" does_a_thing\" " ) , "{out}" ) ;
109- assert ! ( out. contains( "description : \" Does a thing.\" " ) , "{out}" ) ;
110- }
111-
112- #[ test]
113- fn maps_requirements_to_capabilities ( ) {
114- let out = expand_str (
115- "id = \" t-0123abcd\" , requires = [service_exposure, gpu]" ,
116- "/// Doc.\n async fn t(cx: &mut Context) -> testkit::Result { Ok(()) }" ,
117- )
118- . unwrap ( ) ;
119129 assert ! (
120- out. contains(
121- "requires : & [:: testkit :: Capability :: ServiceExposure , :: testkit :: Capability :: Gpu]"
122- ) ,
130+ out. contains( "description : \" First line second line.\" " ) ,
123131 "{out}"
124132 ) ;
125133 }
126134
127135 #[ test]
128- fn reports_spec_errors ( ) {
136+ fn rejects_missing_doc_comment ( ) {
129137 let err = expand_str (
130138 "" ,
131139 "async fn t(cx: &mut Context) -> testkit::Result { Ok(()) }" ,
132140 )
133141 . unwrap_err ( ) ;
134142 assert ! ( err. to_string( ) . contains( "doc comment" ) , "{err}" ) ;
135143 }
144+
145+ #[ test]
146+ fn rejects_sync_fn ( ) {
147+ let err = expand_str (
148+ "" ,
149+ "/// Doc.\n fn t(cx: &mut Context) -> testkit::Result { Ok(()) }" ,
150+ )
151+ . unwrap_err ( ) ;
152+ assert ! ( err. to_string( ) . contains( "async fn" ) , "{err}" ) ;
153+ }
154+
155+ #[ test]
156+ fn rejects_wrong_arity ( ) {
157+ let err =
158+ expand_str ( "" , "/// Doc.\n async fn t() -> testkit::Result { Ok(()) }" ) . unwrap_err ( ) ;
159+ assert ! ( err. to_string( ) . contains( "exactly one argument" ) , "{err}" ) ;
160+ }
161+
162+ #[ test]
163+ fn rejects_arguments ( ) {
164+ let err = expand_str (
165+ "id = \" t-0123abcd\" " ,
166+ "/// Doc.\n async fn t(cx: &mut Context) -> testkit::Result { Ok(()) }" ,
167+ )
168+ . unwrap_err ( ) ;
169+ assert ! ( err. to_string( ) . contains( "takes no arguments" ) , "{err}" ) ;
170+ }
136171}
0 commit comments