-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented Opt-in crate-name support Fix #258
- Loading branch information
Showing
9 changed files
with
265 additions
and
21 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
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
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
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
75 changes: 75 additions & 0 deletions
75
rstest/tests/resources/rstest/convert_string_literal_other_name.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,75 @@ | ||
use other_name::*; | ||
use std::net::SocketAddr; | ||
|
||
#[rstest] | ||
#[case(true, "1.2.3.4:42")] | ||
#[case(true, r#"4.3.2.1:24"#)] | ||
#[case(false, "[2001:db8:85a3:8d3:1319:8a2e:370:7348]:443")] | ||
#[case(false, r#"[2aa1:db8:85a3:8af:1319:8a2e:375:4873]:344"#)] | ||
#[case(false, "this.is.not.a.socket.address")] | ||
#[case(false, r#"this.is.not.a.socket.address"#)] | ||
fn cases(#[case] expected: bool, #[case] addr: SocketAddr) { | ||
assert_eq!(expected, addr.is_ipv4()); | ||
} | ||
|
||
#[rstest] | ||
fn values( | ||
#[values( | ||
"1.2.3.4:42", | ||
r#"4.3.2.1:24"#, | ||
"this.is.not.a.socket.address", | ||
r#"this.is.not.a.socket.address"# | ||
)] | ||
addr: SocketAddr, | ||
) { | ||
assert!(addr.is_ipv4()) | ||
} | ||
|
||
#[rstest] | ||
#[case(b"12345")] | ||
fn not_convert_byte_array(#[case] cases: &[u8], #[values(b"abc")] values: &[u8]) { | ||
assert_eq!(5, cases.len()); | ||
assert_eq!(3, values.len()); | ||
} | ||
|
||
trait MyTrait { | ||
fn my_trait(&self) -> u32 { | ||
42 | ||
} | ||
} | ||
|
||
impl MyTrait for &str {} | ||
|
||
#[rstest] | ||
#[case("impl", "nothing")] | ||
fn not_convert_impl(#[case] that_impl: impl MyTrait, #[case] s: &str) { | ||
assert_eq!(42, that_impl.my_trait()); | ||
assert_eq!(42, s.my_trait()); | ||
} | ||
|
||
#[rstest] | ||
#[case("1.2.3.4", "1.2.3.4:42")] | ||
#[case("1.2.3.4".to_owned(), "1.2.3.4:42")] | ||
fn not_convert_generics<S: AsRef<str>>(#[case] ip: S, #[case] addr: SocketAddr) { | ||
assert_eq!(addr.ip().to_string(), ip.as_ref()); | ||
} | ||
|
||
struct MyType(String); | ||
struct E; | ||
impl core::str::FromStr for MyType { | ||
type Err = E; | ||
|
||
fn from_str(s: &str) -> Result<Self, Self::Err> { | ||
match s { | ||
"error" => Err(E), | ||
inner => Ok(MyType(inner.to_owned())), | ||
} | ||
} | ||
} | ||
|
||
#[rstest] | ||
#[case("hello", "hello")] | ||
#[case("doesn't mater", "error")] | ||
fn convert_without_debug(#[case] expected: &str, #[case] converted: MyType) { | ||
assert_eq!(expected, converted.0); | ||
} |
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,114 @@ | ||
use other_name::*; | ||
use std::time::Duration; | ||
|
||
fn ms(ms: u32) -> Duration { | ||
Duration::from_millis(ms.into()) | ||
} | ||
|
||
mod thread { | ||
use super::*; | ||
|
||
fn delayed_sum(a: u32, b: u32, delay: Duration) -> u32 { | ||
std::thread::sleep(delay); | ||
a + b | ||
} | ||
|
||
#[rstest] | ||
#[timeout(ms(80))] | ||
fn single_pass() { | ||
assert_eq!(4, delayed_sum(2, 2, ms(10))); | ||
} | ||
|
||
#[rstest] | ||
#[timeout(ms(100))] | ||
fn single_fail_value() { | ||
assert_eq!(5, delayed_sum(2, 2, ms(1))); | ||
} | ||
|
||
#[rstest] | ||
#[timeout(ms(1000))] | ||
#[should_panic = "user message"] | ||
fn fail_with_user_message() { | ||
panic!("user message"); | ||
} | ||
|
||
#[rstest] | ||
#[timeout(ms(10))] | ||
fn single_fail_timeout() { | ||
assert_eq!(4, delayed_sum(2, 2, ms(80))); | ||
} | ||
|
||
#[rstest] | ||
#[timeout(ms(80))] | ||
#[case(ms(10))] | ||
fn one_pass(#[case] delay: Duration) { | ||
assert_eq!(4, delayed_sum(2, 2, delay)); | ||
} | ||
|
||
#[rstest] | ||
#[timeout(ms(10))] | ||
#[case(ms(80))] | ||
fn one_fail_timeout(#[case] delay: Duration) { | ||
assert_eq!(4, delayed_sum(2, 2, delay)); | ||
} | ||
|
||
#[rstest] | ||
#[timeout(ms(100))] | ||
#[case(ms(1))] | ||
fn one_fail_value(#[case] delay: Duration) { | ||
assert_eq!(5, delayed_sum(2, 2, delay)); | ||
} | ||
|
||
#[rstest] | ||
#[case::pass(ms(1), 4)] | ||
#[case::fail_timeout(ms(80), 4)] | ||
#[case::fail_value(ms(1), 5)] | ||
#[timeout(ms(40))] | ||
fn group_same_timeout(#[case] delay: Duration, #[case] expected: u32) { | ||
assert_eq!(expected, delayed_sum(2, 2, delay)); | ||
} | ||
|
||
#[rstest] | ||
#[timeout(ms(100))] | ||
#[case::pass(ms(1), 4)] | ||
#[timeout(ms(30))] | ||
#[case::fail_timeout(ms(70), 4)] | ||
#[timeout(ms(100))] | ||
#[case::fail_value(ms(1), 5)] | ||
fn group_single_timeout(#[case] delay: Duration, #[case] expected: u32) { | ||
assert_eq!(expected, delayed_sum(2, 2, delay)); | ||
} | ||
|
||
#[rstest] | ||
#[case::pass(ms(1), 4)] | ||
#[timeout(ms(10))] | ||
#[case::fail_timeout(ms(60), 4)] | ||
#[case::fail_value(ms(1), 5)] | ||
#[timeout(ms(100))] | ||
fn group_one_timeout_override(#[case] delay: Duration, #[case] expected: u32) { | ||
assert_eq!(expected, delayed_sum(2, 2, delay)); | ||
} | ||
|
||
struct S {} | ||
|
||
#[rstest] | ||
#[case(S{})] | ||
fn compile_with_no_copy_arg(#[case] _s: S) { | ||
assert!(true); | ||
} | ||
|
||
#[fixture] | ||
fn no_copy() -> S { | ||
S {} | ||
} | ||
|
||
#[rstest] | ||
fn compile_with_no_copy_fixture(no_copy: S) { | ||
assert!(true); | ||
} | ||
|
||
#[rstest] | ||
fn default_timeout_failure() { | ||
assert_eq!(4, delayed_sum(2, 2, ms(1100))); | ||
} | ||
} |
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
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
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 |
---|---|---|
@@ -1,14 +1,21 @@ | ||
use quote::format_ident; | ||
use syn::parse_quote; | ||
|
||
pub fn crate_name() -> syn::Path { | ||
use proc_macro_crate::FoundCrate; | ||
cfg_if::cfg_if! { | ||
if #[cfg(feature = "crate-name")] { | ||
use proc_macro_crate::FoundCrate; | ||
use quote::format_ident; | ||
|
||
match proc_macro_crate::crate_name("rstest").expect("rstest is present in `Cargo.toml` qed") { | ||
FoundCrate::Itself => parse_quote! { rstest }, | ||
FoundCrate::Name(name) => { | ||
let myself = format_ident!("{name}"); | ||
parse_quote! { #myself } | ||
match proc_macro_crate::crate_name("rstest").expect("rstest is present in `Cargo.toml` qed") | ||
{ | ||
FoundCrate::Itself => parse_quote! { rstest }, | ||
FoundCrate::Name(name) => { | ||
let myself = format_ident!("{name}"); | ||
parse_quote! { #myself } | ||
} | ||
} | ||
} else { | ||
parse_quote! { rstest } | ||
} | ||
} | ||
} |