1
use super::with_article;
2
use crate::ast::WasmType;
3
use crate::error::ParseErrorDetails;
4

            
5
#[test]
6
1
fn the_article_follows_the_first_letter_of_the_noun() {
7
1
    assert_eq!(with_article(WasmType::AnyRef), "an any");
8
1
    assert_eq!(with_article("index"), "an index");
9
1
    assert_eq!(with_article(WasmType::Ratio), "a ratio");
10
1
    assert_eq!(with_article("scalar"), "a scalar");
11
1
}
12

            
13
#[test]
14
1
fn an_undefined_labels_alias_names_the_function_that_ended() {
15
1
    let err = super::Error::undefined_symbol("M (LABELS 7)");
16
1
    assert!(
17
1
        err.to_string()
18
1
            .contains("M: a LABELS function does not outlive its form")
19
    );
20
1
    assert!(matches!(
21
1
        super::Error::undefined_symbol("M"),
22
        super::Error::UndefinedSymbol(_)
23
    ));
24
1
}
25

            
26
2
fn caret_column(input: &str, needle: &str) -> usize {
27
2
    let start = input.find(needle).unwrap_or_default();
28
2
    let details = ParseErrorDetails::new("x".to_string(), input.to_string(), start..start + 1);
29
2
    let rendered = details.render(false);
30
2
    let caret_row = rendered.lines().last().unwrap_or_default();
31
2
    caret_row.find('^').unwrap_or_default() - "  | ".len()
32
2
}
33

            
34
#[test]
35
1
fn the_caret_column_counts_characters_not_bytes() {
36
1
    assert_eq!(
37
1
        caret_column("(ok)\n(\u{0436}\u{0436}\u{0436} bad", "bad"),
38
1
        5
39
    );
40
1
}
41

            
42
#[test]
43
1
fn an_offset_inside_a_multibyte_character_renders_without_panicking() {
44
1
    let input = "(\u{0436}\u{0436} bad";
45
1
    for start in 0..=input.len() + 2 {
46
12
        let details = ParseErrorDetails::new("x".to_string(), input.to_string(), start..start + 1);
47
12
        assert!(details.render(false).contains('^'));
48
    }
49
1
}
50

            
51
#[test]
52
1
fn an_ascii_line_keeps_its_column() {
53
1
    assert_eq!(caret_column("(abc bad", "bad"), 5);
54
1
}