File: error_handling.rs

package info (click to toggle)
rust-syntect-no-panic 6.0.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,080 kB
  • sloc: makefile: 2
file content (86 lines) | stat: -rw-r--r-- 2,350 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
use std::{
    error::Error,
    fmt::Display,
    io::{Error as IoError, ErrorKind},
};

use syntect_no_panic::{
    parsing::{ParseScopeError, ParseSyntaxError},
    LoadingError,
};

#[test]
fn loading_error_bad_path_display() {
    assert_display(LoadingError::BadPath, "Invalid path");
}

#[test]
fn loading_error_parse_syntax_display() {
    assert_display(
        LoadingError::ParseSyntax(
            ParseSyntaxError::MissingMandatoryKey("main"),
            String::from("file.sublime-syntax"),
        ),
        "file.sublime-syntax: Missing mandatory key in YAML file: main",
    );
}

#[test]
fn loading_error_io_source() {
    let io_error_source = IoError::new(ErrorKind::Other, "this is an error string");
    assert_display(
        LoadingError::Io(io_error_source).source().unwrap(),
        "this is an error string",
    );
}

#[test]
fn parse_syntax_error_missing_mandatory_key_display() {
    assert_display(
        ParseSyntaxError::MissingMandatoryKey("mandatory_key"),
        "Missing mandatory key in YAML file: mandatory_key",
    );
}

#[test]
fn parse_syntax_error_regex_compile_error_display() {
    assert_display(
        ParseSyntaxError::RegexCompileError("[a-Z]".to_owned(), LoadingError::BadPath.into()),
        "Error while compiling regex '[a-Z]': Invalid path",
    );
}

#[test]
fn parse_scope_error_display() {
    assert_display(
        ParseScopeError::TooLong,
        "Too long scope. Scopes can be at most 8 atoms long.",
    )
}

#[test]
fn parse_syntax_error_regex_compile_error_source() {
    let error = ParseSyntaxError::RegexCompileError(
        "[[[[[[[[[[[[[[[".to_owned(),
        LoadingError::BadPath.into(),
    );
    assert_display(error.source().unwrap(), "Invalid path");
}

#[test]
fn loading_error_parse_syntax_source() {
    let error = LoadingError::ParseSyntax(
        ParseSyntaxError::RegexCompileError("[a-Z]".to_owned(), LoadingError::BadPath.into()),
        String::from("any-file.sublime-syntax"),
    );
    assert_display(
        error.source().unwrap(),
        "Error while compiling regex '[a-Z]': Invalid path",
    )
}

/// Helper to assert that a given implementation of [Display] generates the
/// expected string.
fn assert_display(display: impl Display, expected_display: &str) {
    assert_eq!(format!("{}", display), String::from(expected_display));
}