use crate::languages::runner::truncate;
#[test]
fn short_input_is_returned_unchanged() {
assert_eq!(truncate("hello", 10), "hello");
}
#[test]
fn input_exactly_at_the_limit_is_unchanged() {
assert_eq!(truncate("hello", 5), "hello");
}
#[test]
fn long_input_is_actually_truncated() {
assert_eq!(truncate("abcdefghij", 3), "abc");
}
#[test]
fn empty_input_is_empty() {
assert_eq!(truncate("", 5), "");
}
#[test]
fn zero_limit_yields_empty_string() {
assert_eq!(truncate("abc", 0), "");
}
#[test]
fn a_cut_landing_mid_character_backs_off_to_a_boundary() {
assert_eq!(truncate("héllo", 2), "h");
}
#[test]
fn backing_off_walks_one_byte_at_a_time() {
assert_eq!(truncate("ab¢de", 3), "ab");
}
#[test]
fn a_string_that_is_entirely_one_multibyte_character_truncates_to_empty() {
assert_eq!(truncate("é", 1), "");
}
#[test]
fn never_returns_more_bytes_than_the_limit() {
for limit in 0..12 {
for input in ["", "abc", "héllo", "ab¢de", "日本語text", "abcdefghij"] {
let out = truncate(input, limit);
assert!(
out.len() <= limit,
"truncate({input:?}, {limit}) returned {out:?}, longer than the limit"
);
assert!(input.starts_with(&out), "output must be a prefix of input");
}
}
}