extern crate html2md;

use html2md::parse_html;
use std::fs::File;
use std::io::prelude::*;

use indoc::indoc;

#[test]
#[ignore]
fn test_marcfs() {
    let mut html = String::new();
    let mut html_file = File::open("tests/input/marcfs-readme.html").unwrap();
    html_file
        .read_to_string(&mut html)
        .expect("File must be readable");
    let result = parse_html(&html);
    println!("{}", result);
}

#[test]
#[ignore]
fn test_cheatsheet() {
    let mut html = String::new();
    let mut md = String::new();
    let mut html_file = File::open("tests/input/markdown-cheatsheet.html").unwrap();
    let mut md_file = File::open("tests/input/markdown-cheatsheet.md").unwrap();
    html_file
        .read_to_string(&mut html)
        .expect("File must be readable");
    md_file
        .read_to_string(&mut md)
        .expect("File must be readable");
    let md_parsed = parse_html(&html);
    println!("{}", md_parsed);
    //assert_eq!(md, md_parsed);
}

/// newlines after list shouldn't be converted into text of the last list element
#[test]
fn test_list_newlines() {
    let mut html = String::new();
    if let Ok(mut html_file) = File::open("tests/input/dybr-bug-with-list-newlines.html") {
        html_file
            .read_to_string(&mut html)
            .expect("File must be readable");
        let result = parse_html(&html);
        assert!((result).contains(".\n\nxxx xxxx"));
        assert!((result).contains("xx x.\n\nxxxxx:"));
    }
}

#[test]
fn test_lists_from_text() {
    let mut html = String::new();
    if let Ok(mut html_file) = File::open("tests/input/dybr-bug-with-lists-from-text.html") {
        html_file
            .read_to_string(&mut html)
            .expect("File must be readable");
        let result = parse_html(&html);
        assert!((result).contains("\\- x xxxx xxxxx xx xxxxxxxxxx"));
        assert!((result).contains("\\- x xxxx xxxxxxxx xxxxxxxxx xxxxxx xxx x xxxxxxxx xxxx"));
        assert!((result).contains("\\- xxxx xxxxxxxx"));
    }
}

#[test]
fn test_strong_inside_link() {
    let mut html = String::new();
    if let Ok(mut html_file) = File::open("tests/input/dybr-bug-with-strong-inside-link.html") {
        html_file
            .read_to_string(&mut html)
            .expect("File must be readable");
        let result = parse_html(&html);
        assert!((result).contains("[**Just God**](http://fanfics.me/ficXXXXXXX)"));
    }
}

#[test]
fn test_tables_with_newlines() {
    let mut html = String::new();
    if let Ok(mut html_file) = File::open("tests/input/dybr-bug-with-tables-masked.html") {
        html_file
            .read_to_string(&mut html)
            .expect("File must be readable");
        let result = parse_html(&html);

        // all lines starting with | should end with | as well
        let invalid_table_lines: Vec<&str> = result
            .lines()
            .filter(|line| line.starts_with("|"))
            .filter(|line| !line.ends_with("|"))
            .collect();

        assert!((invalid_table_lines).is_empty());
    }
}

#[test]
fn test_tables2() {
    let mut html = String::new();
    if let Ok(mut html_file) = File::open("tests/input/dybr-bug-with-tables-2-masked.html") {
        html_file
            .read_to_string(&mut html)
            .expect("File must be readable");
        let table_with_vertical_header = parse_html(&html);

        assert!((table_with_vertical_header).contains(indoc! {"
            |Current Conditions:|Open all year. No reservations. No services.|
            |-------------------|--------------------------------------------|
            |   Reservations:   |              No reservations.              |
            |       Fees        |                  No fee.                   |
            |      Water:       |                 No water.                  |"
        }));
    }
}
