Skip to content

Commit 234db27

Browse files
committed
0.1.2: fix path/lit str syntax, made B as Into<Option<Bytes>> instead
1 parent 1a1ced1 commit 234db27

File tree

7 files changed

+48
-31
lines changed

7 files changed

+48
-31
lines changed

Cargo.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ resolver = "2"
2424
members = ["crates/*"]
2525

2626
[workspace.package]
27-
version = "0.1.1"
27+
version = "0.1.2"
2828
repository = "https://github.com/charted-dev/testkit"
2929
license = "MIT"
3030
edition = "2021"
31-
rust-version = "1.76"
31+
rust-version = "1.78"
3232
authors = ["Noel Towa <[email protected]>", "Noelware, LLC. <[email protected]>"]

crates/macros/src/attr.rs

+6-14
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,11 @@
2020
// SOFTWARE.
2121

2222
use proc_macro2::Span;
23-
24-
use syn::ExprCall;
25-
#[allow(unused_imports)]
2623
use syn::{
2724
bracketed,
2825
parse::{Parse, ParseStream},
2926
punctuated::Punctuated,
30-
spanned::Spanned as _,
31-
Expr, ExprLit, ExprPath, Ident, Lit, Path, PathSegment, Result, Token,
27+
Expr, ExprCall, ExprLit, ExprPath, Ident, Lit, Path, PathSegment, Result, Token,
3228
};
3329

3430
macro_rules! err {
@@ -190,16 +186,12 @@ fn router_path() -> Path {
190186
}
191187

192188
fn parse_literal_or_path(input: ParseStream) -> Result<Path> {
193-
let expr = input.parse::<Expr>()?;
194-
if let Expr::Lit(ExprLit { lit: Lit::Str(s), .. }) = expr {
195-
Ok(PathSegment::from(Ident::new(&s.value(), s.span())).into())
196-
} else if let Expr::Path(ExprPath { path, .. }) = expr {
197-
Ok(path)
189+
let ahead = input.fork();
190+
if let Ok(Lit::Str(s)) = ahead.parse() {
191+
let ident = Ident::new(&s.value(), s.span());
192+
Ok(PathSegment::from(ident).into())
198193
} else {
199-
return Err(err!(
200-
expr.span(),
201-
"expected a literal string or valid path to a function for a teardown function"
202-
));
194+
input.parse()
203195
}
204196
}
205197

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1-
error: expected a literal string or valid path to a function for a teardown function
1+
error: expected identifier
22
--> ./tests/ui/invalid_setup.rs:22:40
33
|
44
22 | #[charted_testkit_macros::test(setup = 123)]
55
| ^^^
66

7-
error: expected a literal string or valid path to a function for a teardown function
7+
error: expected identifier
88
--> ./tests/ui/invalid_setup.rs:27:40
99
|
1010
27 | #[charted_testkit_macros::test(setup = [123])]
11-
| ^^^^^
11+
| ^
1212

13-
error: expected a literal string or valid path to a function for a teardown function
13+
error: expected identifier
1414
--> ./tests/ui/invalid_setup.rs:32:40
1515
|
1616
32 | #[charted_testkit_macros::test(setup = [true])]
17-
| ^^^^^^
17+
| ^
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1-
error: expected a literal string or valid path to a function for a teardown function
1+
error: expected identifier
22
--> ./tests/ui/invalid_teardown.rs:22:43
33
|
44
22 | #[charted_testkit_macros::test(teardown = 123)]
55
| ^^^
66

7-
error: expected a literal string or valid path to a function for a teardown function
7+
error: expected identifier
88
--> ./tests/ui/invalid_teardown.rs:27:43
99
|
1010
27 | #[charted_testkit_macros::test(teardown = [123])]
11-
| ^^^^^
11+
| ^
1212

13-
error: expected a literal string or valid path to a function for a teardown function
13+
error: expected identifier
1414
--> ./tests/ui/invalid_teardown.rs:32:43
1515
|
1616
32 | #[charted_testkit_macros::test(teardown = [true])]
17-
| ^^^^^^
17+
| ^

crates/testkit/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ default = ["macros"]
4040

4141
[dependencies]
4242
axum = "0.7.5"
43-
charted-testkit-macros = { version = "=0.1.1", path = "../macros", optional = true }
43+
charted-testkit-macros = { version = "=0.1.2", path = "../macros", optional = true }
4444
http-body-util = "0.1.2"
4545
hyper = { version = "1.4.1", features = ["client", "server", "http1"] }
4646
hyper-util = { version = "0.1.7", features = [

crates/testkit/src/lib.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -167,16 +167,16 @@ impl TestContext {
167167
/// assert_eq!(charted_testkit::consume_body!(res), Bytes::from_static(b"Hello, world!"));
168168
/// # }
169169
/// ```
170-
pub fn request<U: AsRef<str> + 'static, B: Into<Bytes>, F: Fn(&mut Request<Full<Bytes>>)>(
170+
pub fn request<U: AsRef<str> + 'static, B: Into<Option<Bytes>>, F: Fn(&mut Request<Full<Bytes>>)>(
171171
&self,
172172
uri: U,
173173
method: Method,
174-
body: Option<B>,
174+
body: B,
175175
build: F,
176176
) -> ResponseFuture {
177177
let addr = self.server_addr().expect("failed to get socket address");
178178

179-
let mut req = Request::<Full<Bytes>>::new(Full::new(body.map(Into::into).unwrap_or_default()));
179+
let mut req = Request::<Full<Bytes>>::new(Full::new(body.into().unwrap_or_default()));
180180
*req.method_mut() = method;
181181
*req.uri_mut() = format!("http://{addr}{}", uri.as_ref())
182182
.parse()
@@ -268,6 +268,7 @@ impl TestContext {
268268
// Private APIs used by macros; do not use!
269269
#[doc(hidden)]
270270
pub mod __private {
271+
pub use axum::http::header;
271272
pub use http_body_util::BodyExt;
272273
}
273274

@@ -295,7 +296,7 @@ mod tests {
295296
ctx.serve(router()).await;
296297

297298
let res = ctx
298-
.request("/", Method::GET, None::<axum::body::Bytes>, |_| {})
299+
.request("/", Method::GET, None, |_| {})
299300
.await
300301
.expect("unable to send request");
301302

crates/testkit/src/macros.rs

+24
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,22 @@ macro_rules! assert_successful {
3636
};
3737
}
3838

39+
/// Checks whenever if a [`Response`][axum::http::response::Response] failed.
40+
///
41+
/// ## Example
42+
/// ```rust
43+
/// # use axum::http::{response::Response, StatusCode};
44+
/// #
45+
/// let res = Response::builder().status(StatusCode::NOT_FOUND).body(()).expect("response to be avaliable");
46+
/// charted_testkit::assert_failure!(res);
47+
/// ```
48+
#[macro_export]
49+
macro_rules! assert_failure {
50+
($res:expr) => {
51+
assert!(!($res).status().is_success());
52+
};
53+
}
54+
3955
/// Macro to easily assert if a given [response][axum::http::response::Response]'s status code
4056
/// is the same as one you provide.
4157
///
@@ -95,6 +111,10 @@ macro_rules! consume_body {
95111
/// ```
96112
#[macro_export]
97113
macro_rules! assert_has_header {
114+
($res:expr, $header:ident) => {
115+
$crate::assert_has_header!($res, $crate::__private::header::$header);
116+
};
117+
98118
($res:expr, $header:expr) => {
99119
assert!($res.headers().get($header).is_some());
100120
};
@@ -111,6 +131,10 @@ macro_rules! assert_has_header {
111131
/// ```
112132
#[macro_export]
113133
macro_rules! assert_doesnt_have_header {
134+
($res:expr, $header:ident) => {
135+
$crate::assert_has_header!($res, $crate::__private::header::$header);
136+
};
137+
114138
($res:expr, $header:expr) => {
115139
assert!($res.headers().get($header).is_none());
116140
};

0 commit comments

Comments
 (0)