pub use expression_format_impl::ex_eprint;
pub use expression_format_impl::ex_eprintln;
pub use expression_format_impl::ex_format;
pub use expression_format_impl::ex_print;
pub use expression_format_impl::ex_println;
pub mod short {
pub use expression_format_impl::ex_eprint as exep;
pub use expression_format_impl::ex_format as exf;
pub use expression_format_impl::ex_print as exp;
pub use expression_format_impl::ex_println as expl;
pub use expression_format_impl::ex_eprintln as exepl;
}
#[cfg(test)]
mod tests {
use crate::short::exf;
#[test]
fn test_single_argument() {
let r = "ipsum";
assert_eq!(exf!("lorem {r} dolor"), "lorem ipsum dolor");
}
#[test]
fn test_raw_string() {
assert_eq!(exf!(r#"lorem {r"{ipsum}"}"#), "lorem {ipsum}");
}
#[test]
fn test_raw_string_with_depth() {
assert_eq!(exf!(r###"lorem {r##"{ipsum"#}"##}"###), r##"lorem {ipsum"#}"##);
}
#[test]
fn test_no_argument() {
assert_eq!(exf!("lorem ipsum"), "lorem ipsum");
}
#[test]
fn test_multiple_arguments() {
let args = ["lorem ", "ipsum ", "dolor"];
assert_eq!(exf!("{args[0]}{args[1]}{args[2]}"), "lorem ipsum dolor");
}
#[test]
fn test_string() {
assert_eq!(exf!(r#"lorem {"ip\"sum"}"#), "lorem ip\"sum");
}
#[test]
fn test_char() {
assert_eq!(exf!(r#"lorem {'\''}ipsum'"#), "lorem 'ipsum'");
}
#[test]
fn test_line_comment() {
assert_eq!(
exf!(r#"lorem { {
// line comment
"ipsum"
}}"#),
"lorem ipsum"
);
}
#[test]
fn test_struct_in_expression() {
#[derive(Debug)]
struct Point {
x: i32,
y: i32,
}
assert_eq!(
exf!("value of point = {:?Point {x: 1 + 2, y: 3 * 4}}"),
"value of point = Point { x: 3, y: 12 }"
);
}
#[test]
fn test_escape_brackets() {
let arg = "lorem";
assert_eq!(exf!("{{{arg}}} {{{{ipsum}}}}"), "{lorem} {{ipsum}}");
}
#[test]
fn test_block_comment() {
assert_eq!(exf!(
r#"lorem {/*/*inside comment*/still inside comment*/"ipsum"}"#),
"lorem ipsum");
}
#[test]
fn test_function_with_lifetime() {
assert_eq!(
exf!(
r#"{
{
fn foo() -> &'static str {
"lorem"
}
foo()
}
} ipsum"#
),
"lorem ipsum"
);
}
#[test]
fn test_number_format_width() {
assert_eq!(
exf!("{:04 42}"),
"0042"
);
}
#[test]
fn test_text_format_width() {
assert_eq!(
exf!(r#"Hello {:5 "x"}!"#),
"Hello x !"
);
}
#[test]
fn test_text_format_width_left_aligned() {
assert_eq!(
exf!(r#"Hello {:<5 "x"}!"#),
"Hello x !"
);
}
#[test]
fn test_text_format_width_left_aligned_with_dash() {
assert_eq!(
exf!(r#"Hello {:-<5 "x"}!"#),
"Hello x----!"
);
}
#[test]
fn test_text_format_width_center_aligned() {
assert_eq!(
exf!(r#"Hello {:^5 "x"}!"#),
"Hello x !"
);
}
#[test]
fn test_text_format_width_right_aligned() {
assert_eq!(
exf!(r#"Hello {:>5 "x"}!"#),
"Hello x!"
);
}
#[test]
fn test_sign() {
assert_eq!(exf!("Hello {:+ 5}!"), "Hello +5!");
}
#[test]
fn test_sharp_x() {
assert_eq!(exf!("{:#x 27}!"), "0x1b!");
}
#[test]
fn test_sharp_x_with_width() {
assert_eq!(exf!("{:#010x 27}!"), "0x0000001b!");
}
#[test]
fn test_precission() {
assert_eq!(exf!("{:.5 12.3}"), "12.30000");
}
#[test]
fn test_format_alignment_with_char() {
assert_eq!(exf!(r#"{:'>10 "test"}"#), "''''''test");
}
#[test]
fn test_format_alignment_with_quotes() {
assert_eq!(exf!(r#"{:"<10 "test"}"#), r#"test"""""""#);
}
#[test]
fn test_format_alignment_with_space() {
assert_eq!(exf!(r#"{: <10 "test"}"#), r#"test "#);
}
}