Skip to content

Commit

Permalink
Test the interaction of inferred and default type parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
mgeisler committed May 1, 2021
1 parent 813701a commit 0219f0c
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions tests/traits.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
use textwrap::{NoHyphenation, Options};

/// Cleaned up type name.
fn type_name<T: ?Sized>(_val: &T) -> String {
std::any::type_name::<T>()
.replace("alloc::boxed::Box", "Box")
.replace("textwrap::splitting", "textwrap")
}

#[test]
fn static_hyphensplitter() {
// Inferring the full type.
let options = Options::new(10);
assert_eq!(
type_name(&options),
"textwrap::Options<textwrap::HyphenSplitter>"
);

// Explicitly making both parameters inferred.
let options: Options<'_, _> = Options::new(10);
assert_eq!(
type_name(&options),
"textwrap::Options<textwrap::HyphenSplitter>"
);
}

#[test]
fn box_static_nohyphenation() {
// Inferred static type.
let options = Options::new(10).splitter(Box::new(NoHyphenation));
assert_eq!(
type_name(&options),
"textwrap::Options<Box<textwrap::NoHyphenation>>"
);
}

#[test]
fn box_dyn_wordsplitter() {
// Inferred dynamic type due to default type parameter.
let options: Options = Options::new(10).splitter(Box::new(NoHyphenation));
assert_eq!(
type_name(&options),
"textwrap::Options<Box<dyn textwrap::WordSplitter>>"
);
}

0 comments on commit 0219f0c

Please sign in to comment.