use std::io;

use aloggrep::multiline::MultilineMerger;

fn ok_lines(lines: &[&str]) -> Vec<io::Result<String>> {
    lines.iter().map(|s| Ok(s.to_string())).collect()
}

#[test]
fn test_no_continuation() {
    let input = ok_lines(&[
        "04-02 12:34:56.789  1234  5678 E Tag     : msg1",
        "04-02 12:34:57.000  1234  5678 W Tag     : msg2",
    ]);
    let merged: Vec<String> = MultilineMerger::new(input.into_iter())
        .map(|r| r.unwrap())
        .collect();
    assert_eq!(merged.len(), 2);
    assert!(!merged[0].contains('\n'));
}

#[test]
fn test_merge_stack_trace() {
    let input = ok_lines(&[
        "04-02 12:34:56.789  1234  5678 E AndroidRuntime: FATAL EXCEPTION: main",
        "    at com.app.Foo.bar(Foo.java:12)",
        "    at com.app.Baz.qux(Baz.java:34)",
        "04-02 12:34:57.000  1234  5678 W Tag     : next entry",
    ]);
    let merged: Vec<String> = MultilineMerger::new(input.into_iter())
        .map(|r| r.unwrap())
        .collect();
    assert_eq!(merged.len(), 2);
    assert!(merged[0].contains("FATAL EXCEPTION"));
    assert!(merged[0].contains("at com.app.Foo.bar"));
    assert!(merged[0].contains("at com.app.Baz.qux"));
    assert_eq!(merged[0].lines().count(), 3);
}

#[test]
fn test_orphan_continuation() {
    let input = ok_lines(&[
        "    orphan line with no preceding entry",
        "04-02 12:34:56.789  1234  5678 E Tag     : msg",
    ]);
    let merged: Vec<String> = MultilineMerger::new(input.into_iter())
        .map(|r| r.unwrap())
        .collect();
    assert_eq!(merged.len(), 2);
    assert_eq!(merged[0], "    orphan line with no preceding entry");
}

#[test]
fn test_trailing_continuation() {
    let input = ok_lines(&[
        "04-02 12:34:56.789  1234  5678 E Tag     : first",
        "  continuation 1",
        "  continuation 2",
    ]);
    let merged: Vec<String> = MultilineMerger::new(input.into_iter())
        .map(|r| r.unwrap())
        .collect();
    assert_eq!(merged.len(), 1);
    assert_eq!(merged[0].lines().count(), 3);
}
