humfmt 0.5.2

Ergonomic human-readable formatting toolkit for Rust
Documentation
use humfmt::{list, list_with, locale::CustomLocale, ListOptions};

#[test]
fn formats_empty_lists() {
    assert_eq!(list::<&str>(&[]).to_string(), "");
}

#[test]
fn formats_single_item_lists() {
    assert_eq!(list(&["red"]).to_string(), "red");
}

#[test]
fn formats_two_item_lists() {
    assert_eq!(list(&["red", "green"]).to_string(), "red and green");
}

#[test]
fn formats_three_item_lists_with_english_serial_comma() {
    assert_eq!(
        list(&["red", "green", "blue"]).to_string(),
        "red, green, and blue"
    );
}

#[test]
fn can_disable_serial_comma() {
    let out = list_with(
        &["red", "green", "blue"],
        ListOptions::new().no_serial_comma(),
    );

    assert_eq!(out.to_string(), "red, green and blue");
}

#[test]
fn supports_non_string_display_items() {
    assert_eq!(list(&[1, 2, 3]).to_string(), "1, 2, and 3");
}

#[cfg(feature = "russian")]
#[test]
fn uses_russian_conjunction_without_serial_comma() {
    let out = list_with(
        &["яблоки", "груши", "сливы"],
        ListOptions::new().locale(humfmt::locale::Russian),
    );

    assert_eq!(out.to_string(), "яблоки, груши и сливы");
}

#[cfg(feature = "polish")]
#[test]
fn uses_polish_conjunction_without_serial_comma() {
    let out = list_with(
        &["jabłka", "gruszki", "śliwki"],
        ListOptions::new().locale(humfmt::locale::Polish),
    );

    assert_eq!(out.to_string(), "jabłka, gruszki i śliwki");
}

#[test]
fn supports_custom_conjunction_and_serial_comma_style() {
    let locale = CustomLocale::english().and_word("plus").serial_comma(false);
    let out = list_with(&["red", "green", "blue"], ListOptions::new().locale(locale));

    assert_eq!(out.to_string(), "red, green plus blue");
}

#[test]
fn supports_custom_list_separator() {
    let locale = CustomLocale::english()
        .list_separator(" | ")
        .and_word("&")
        .serial_comma(false);
    let out = list_with(&["red", "green", "blue"], ListOptions::new().locale(locale));

    assert_eq!(out.to_string(), "red | green & blue");
}

#[test]
fn serial_comma_is_ignored_for_non_comma_separators() {
    // Serial comma is a comma-specific stylistic rule. If the user overrides the
    // list separator away from commas, injecting a literal comma becomes surprising.
    let locale = CustomLocale::english()
        .list_separator(" | ")
        .and_word("&")
        .serial_comma(true);

    let out = list_with(
        &["red", "green", "blue"],
        ListOptions::new().locale(locale).serial_comma_enabled(true),
    );

    assert_eq!(out.to_string(), "red | green & blue");
}

#[test]
fn supports_explicit_serial_comma_boolean_setter() {
    let with = list_with(
        &["red", "green", "blue"],
        ListOptions::new().serial_comma_enabled(true),
    );
    let without = list_with(
        &["red", "green", "blue"],
        ListOptions::new().serial_comma_enabled(false),
    );

    assert_eq!(with.to_string(), "red, green, and blue");
    assert_eq!(without.to_string(), "red, green and blue");
}

#[test]
fn supports_conjunction_override_without_custom_locale() {
    let out = list_with(
        &["red", "green", "blue"],
        ListOptions::new().conjunction("plus").no_serial_comma(),
    );

    assert_eq!(out.to_string(), "red, green plus blue");
}