//! Error-path tests.
//!
//! Selectors that parse but are rejected, or that fail to tokenize. Each case
//! asserts the exact message string the constructor returns.

use selderee::{DecisionTree, DecisionTreeOptions};

fn err_message(selector: &str) -> String {
    let input = vec![(selector.to_string(), "value".to_string())];
    DecisionTree::<String>::new(input, DecisionTreeOptions::default())
        .err()
        .expect("expected an error")
        .to_string()
}

#[test]
fn descendant_combinator() {
    // "combinator" + space + the space combinator char yields two trailing spaces.
    assert_eq!(
        err_message("foo bar"),
        "Unsupported selector kind: combinator  "
    );
}

#[test]
fn general_sibling_combinator() {
    assert_eq!(
        err_message("foo ~ bar"),
        "Unsupported selector kind: combinator ~"
    );
}

#[test]
fn column_combinator() {
    assert_eq!(
        err_message("foo || bar"),
        "Unsupported selector kind: combinator ||"
    );
}

#[test]
fn namespaced_tag() {
    assert_eq!(
        err_message("svg|circle"),
        "Unsupported namespace prefix: svg|"
    );
}

#[test]
fn pseudo_root() {
    assert_eq!(err_message(":root"), "Unsupported pseudo-class: :root");
}

#[test]
fn pseudo_only_of_type() {
    assert_eq!(
        err_message(":only-of-type"),
        "Unsupported pseudo-class: :only-of-type"
    );
}

#[test]
fn functional_nth_child_partial_tokenization() {
    assert_eq!(
        err_message(":nth-child(2n+1)"),
        "The input \":nth-child(2n+1)\" was only partially tokenized, stopped at offset 11!\n:nth-child(2n+1)\n           ^"
    );
}

#[test]
fn functional_is() {
    assert_eq!(
        err_message(":is(.foo)"),
        "Unsupported selector kind: fpc:is"
    );
}

#[test]
fn functional_where() {
    assert_eq!(
        err_message(":where(.foo)"),
        "Unsupported selector kind: fpc:where"
    );
}

#[test]
fn functional_not() {
    assert_eq!(
        err_message(":not(.foo)"),
        "Unsupported selector kind: fpc:not"
    );
}

#[test]
fn empty_selector() {
    assert_eq!(err_message(""), "No match for \"\" input!");
}

#[test]
fn whitespace_only_selector() {
    assert_eq!(err_message("   "), "No match for \"   \" input!");
}

#[test]
fn selector_list_comma_partial_parse() {
    // parse1 stops at the comma, so a selector list is a partial parse.
    assert_eq!(
        err_message("a, b"),
        "The input \"a, b\" was only partially parsed, stopped at offset 1!\na, b\n ^"
    );
}
