-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
186 additions
and
186 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,183 @@ | ||
//! General-purpose tests. | ||
|
||
use std::{ | ||
panic, | ||
string::{String, ToString}, | ||
}; | ||
|
||
use super::*; | ||
|
||
const THRESHOLD: usize = 32; | ||
|
||
#[test] | ||
fn basics() { | ||
const TEST: CompileArgs<32> = | ||
compile_args!("expected ", 1_usize, " to be greater than ", THRESHOLD); | ||
assert_eq!(TEST.to_string(), "expected 1 to be greater than 32"); | ||
} | ||
|
||
#[test] | ||
fn using_chars() { | ||
const CHARS: CompileArgs<11> = compile_args!('H', 'i', 'ß', 'ℝ', '💣'); | ||
assert_eq!(CHARS.to_string(), "Hißℝ💣"); | ||
} | ||
|
||
#[test] | ||
fn using_dynamic_chars() { | ||
for char in ['i', 'ß', 'ℝ', '💣'] { | ||
let s = compile_args!("char: ", char => fmt::<char>(), "!"); | ||
assert_eq!(s.as_str(), std::format!("char: {char}!")); | ||
} | ||
} | ||
|
||
#[test] | ||
fn clipping_strings() { | ||
let arg = "dynamic"; | ||
let s = compile_args!("string: '", arg => clip(3, ""), '\''); | ||
assert_eq!(s.as_str(), "string: 'dyn'"); | ||
|
||
let arg = "Tℝ💣eßt"; | ||
let s = compile_args!("string: '", arg => clip(2, ""), '\''); | ||
assert_eq!(s.as_str(), "string: 'Tℝ'"); | ||
let s = compile_args!("string: '", arg => clip(3, ""), '\''); | ||
assert_eq!(s.as_str(), "string: 'Tℝ💣'"); | ||
let s = compile_args!("string: '", arg => clip(4, ""), '\''); | ||
assert_eq!(s.as_str(), "string: 'Tℝ💣e'"); | ||
let s = compile_args!("string: '", arg => clip(5, ""), '\''); | ||
assert_eq!(s.as_str(), "string: 'Tℝ💣eß'"); | ||
} | ||
|
||
#[test] | ||
fn clipping_strings_with_clip_chars() { | ||
let arg = "dynamic"; | ||
let s = compile_args!("string: '", arg => clip(3, "-"), '\''); | ||
assert_eq!(s.as_str(), "string: 'dyn-'"); | ||
let s = compile_args!("string: '", arg => clip(3, "[..]"), '\''); | ||
assert_eq!(s.as_str(), "string: 'dyn[..]'"); | ||
let s = compile_args!("string: '", arg => clip(3, "…"), '\''); | ||
assert_eq!(s.as_str(), "string: 'dyn…'"); | ||
|
||
let s = compile_args!("string: '", arg => clip(10, "-"), '\''); | ||
assert_eq!(s.as_str(), "string: 'dynamic'"); | ||
} | ||
|
||
#[test] | ||
fn padding() { | ||
let num = 42_u64; | ||
let s = compile_args!( | ||
"number: [", num => fmt::<u64>().pad_left(4, ' '), "]" | ||
); | ||
assert_eq!(s.as_str(), "number: [42 ]"); | ||
|
||
let s = compile_args!( | ||
"number: [", num => fmt::<u64>().pad_center(4, ' '), "]" | ||
); | ||
assert_eq!(s.as_str(), "number: [ 42 ]"); | ||
|
||
let s = compile_args!( | ||
"number: [", num => fmt::<u64>().pad_right(4, '0'), "]" | ||
); | ||
assert_eq!(s.as_str(), "number: [0042]"); | ||
|
||
let s = compile_args!( | ||
"number: [", num => fmt::<u64>().pad_right(4, 'ℝ'), "]" | ||
); | ||
assert_eq!(s.as_str(), "number: [ℝℝ42]"); | ||
let s = compile_args!( | ||
"number: [", num => fmt::<u64>().pad_right(4, '💣'), "]" | ||
); | ||
assert_eq!(s.as_str(), "number: [💣💣42]"); | ||
|
||
let s = compile_args!( | ||
"number: [", num * 10_000 => fmt::<u64>().pad_right(4, '0'), "]" | ||
); | ||
assert_eq!(s.as_str(), "number: [420000]"); | ||
} | ||
|
||
#[test] | ||
fn clipping_and_padding() { | ||
let arg = "test string"; | ||
let s = compile_args!( | ||
"string: [", arg => clip(4, "").pad_left(8, ' '), "]" | ||
); | ||
assert_eq!(s.as_str(), "string: [test ]"); | ||
|
||
let s = compile_args!( | ||
"string: [", arg => clip(4, "-").pad_right(8, ' '), "]" | ||
); | ||
assert_eq!(s.as_str(), "string: [ test-]"); | ||
|
||
let s = compile_args!( | ||
"string: [", arg => clip(4, "…").pad_center(8, ' '), "]" | ||
); | ||
assert_eq!(s.as_str(), "string: [ test… ]"); | ||
|
||
let s = compile_args!( | ||
"string: [", arg => clip(4, "…").pad_left(8, '💣'), "]" | ||
); | ||
assert_eq!(s.as_str(), "string: [test…💣💣💣]"); | ||
let s = compile_args!( | ||
"string: [", arg => clip(4, "…").pad_center(8, 'ß'), "]" | ||
); | ||
assert_eq!(s.as_str(), "string: [ßtest…ßß]"); | ||
|
||
let s = compile_args!( | ||
"string: [", arg => clip(4, "…").pad_left(4, ' '), "]" | ||
); | ||
assert_eq!(s.as_str(), "string: [test…]"); | ||
} | ||
|
||
#[test] | ||
fn ascii_strings() { | ||
let s: CompileArgs<11> = compile_args!("ASCII: ", Ascii::new("test")); | ||
assert_eq!(s.as_str(), "ASCII: test"); | ||
|
||
let s: CompileArgs<25> = compile_args!( | ||
"ASCII: ", Ascii::new("test") => clip_ascii(16, "..") | ||
); | ||
// ^ 25 = "ASCII: ".len() + 16 + "..".len() | ||
assert_eq!(s.as_str(), "ASCII: test"); | ||
|
||
let s: CompileArgs<10> = compile_args!( | ||
"ASCII: ", Ascii::new("test") => clip_ascii(2, "~") | ||
); | ||
assert_eq!(s.as_str(), "ASCII: te~"); | ||
} | ||
|
||
#[test] | ||
#[should_panic(expected = "expected 1 to be greater than 32")] | ||
fn assertion() { | ||
let value = 1; | ||
compile_assert!( | ||
value > THRESHOLD, | ||
"expected ", value => fmt::<usize>(), " to be greater than ", THRESHOLD | ||
); | ||
} | ||
|
||
#[cfg(panic = "unwind")] | ||
#[test] | ||
fn assertion_produces_exactly_expected_string() { | ||
let panic_result = panic::catch_unwind(|| { | ||
let value = 1; | ||
compile_assert!( | ||
value > THRESHOLD, | ||
"expected ", value => fmt::<usize>(), " to be greater than ", THRESHOLD | ||
); | ||
}); | ||
let panic_message = panic_result.unwrap_err(); | ||
let panic_message = panic_message.downcast_ref::<String>().unwrap(); | ||
assert_eq!(panic_message, "expected 1 to be greater than 32"); | ||
// ^ `const_panic` crate fails this test; it pads the panic message with '\0' chars | ||
} | ||
|
||
const fn unwrap_result(res: Result<(), &str>) { | ||
if let Err(err) = res { | ||
compile_panic!("Encountered an error: ", err => clip(64, "…")); | ||
} | ||
} | ||
|
||
#[test] | ||
#[should_panic(expected = "Encountered an error: operation not supported")] | ||
fn using_panic() { | ||
unwrap_result(Err("operation not supported")); | ||
} |