Skip to content

Commit

Permalink
Move crate tests to separate file
Browse files Browse the repository at this point in the history
  • Loading branch information
slowli committed Sep 25, 2023
1 parent 9bdf64d commit 0955873
Show file tree
Hide file tree
Showing 3 changed files with 186 additions and 186 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ authors = ["Alex Ostrovski <ostrovski.alex@gmail.com>"]
license = "MIT OR Apache-2.0"
keywords = ["format", "compile-time", "const"]
categories = ["value-formatting", "development-tools", "no-std"]
description = "Compile-time formatting and derived functionality (e.g., panics / assertions)."
description = "Compile-time formatting and derived functionality (e.g., panics / assertions)"
repository = "https://github.com/slowli/compile-fmt"

[dev-dependencies]
Expand Down
187 changes: 2 additions & 185 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ extern crate std;
mod argument;
mod format;
mod macros;
#[cfg(test)]
mod tests;
mod utils;

#[doc(hidden)]
Expand Down Expand Up @@ -224,188 +226,3 @@ impl<const CAP: usize> CompileArgs<CAP> {

#[cfg(doctest)]
doc_comment::doctest!("../README.md");

#[cfg(test)]
mod 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"));
}
}
183 changes: 183 additions & 0 deletions src/tests.rs
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"));
}

0 comments on commit 0955873

Please sign in to comment.