//! Whitespace handling tests (Section 4.5).

use crate::{parse, Entry};

fn kv(line: u32, key: &str, value: &str) -> Entry {
    Entry::Kv { line, key: key.to_owned(), value: value.to_owned() }
}

/// Leading whitespace (spaces and tabs) is stripped from every line.
#[test]
fn leading_spaces_stripped() {
    let input = b"   KEY=value\n";
    let entries = parse(input).unwrap();
    assert_eq!(entries, vec![kv(1, "KEY", "value")]);
}

#[test]
fn leading_tabs_stripped() {
    let input = b"\t\tKEY=value\n";
    let entries = parse(input).unwrap();
    assert_eq!(entries, vec![kv(1, "KEY", "value")]);
}

#[test]
fn mixed_leading_whitespace_stripped() {
    let input = b" \t KEY=value\n";
    let entries = parse(input).unwrap();
    assert_eq!(entries, vec![kv(1, "KEY", "value")]);
}

/// Whitespace between key and `=` is stripped from the key side.
#[test]
fn key_operator_whitespace_stripped() {
    let input = b"KEY   =value\n";
    let entries = parse(input).unwrap();
    assert_eq!(entries, vec![kv(1, "KEY", "value")]);
}

/// Whitespace after `=` is part of the value (never stripped).
#[test]
fn value_leading_whitespace_preserved() {
    let input = b"KEY=   value\n";
    let entries = parse(input).unwrap();
    assert_eq!(entries, vec![kv(1, "KEY", "   value")]);
}

/// Trailing whitespace in values is preserved.
#[test]
fn value_trailing_whitespace_preserved() {
    let input = b"KEY=value   \n";
    let entries = parse(input).unwrap();
    assert_eq!(entries, vec![kv(1, "KEY", "value   ")]);
}

/// Whitespace-only lines are treated as blank lines.
#[test]
fn whitespace_only_line_is_blank() {
    use crate::ParserConfig;
    use crate::parse_with_config;
    let config = ParserConfig { emit_blanks: true, ..ParserConfig::default() };
    let input = b"KEY=v\n   \nKEY2=v2\n";
    let entries = parse_with_config(input, &config).unwrap();
    assert_eq!(entries, vec![
        kv(1, "KEY", "v"),
        Entry::Blank { line: 2 },
        kv(3, "KEY2", "v2"),
    ]);
}

/// Combined: leading whitespace + key-operator whitespace + value whitespace preserved.
#[test]
fn combined_whitespace_rules() {
    // "  KEY  =  value  " → key="KEY", value="  value  "
    let input = b"  KEY  =  value  \n";
    let entries = parse(input).unwrap();
    assert_eq!(entries, vec![kv(1, "KEY", "  value  ")]);
}
