aboutsummaryrefslogtreecommitdiff
path: root/azalea-chat/tests/integration_test.rs
blob: 08f59d48b6e9fc818c704c5558537d144555138e (plain)
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
use azalea_chat::{
    FormattedText,
    style::{Ansi, ChatFormatting, TextColor},
};
use serde::Deserialize;
use serde_json::Value;

#[test]
fn basic_ansi_test() {
    let j: Value = serde_json::from_str(
        r#"{
    "text": "hello",
    "color": "red",
    "bold": true
}"#,
    )
    .unwrap();
    let component = FormattedText::deserialize(&j).unwrap();
    assert_eq!(
        component.to_ansi(),
        "\u{1b}[1m\u{1b}[38;2;255;85;85mhello\u{1b}[m"
    );
}

#[test]
fn complex_ansi_test() {
    let j: Value = serde_json::from_str(
        r##"[
    {
        "text": "hello",
        "color": "red",
        "bold": true,
        "italic": true,
        "underlined": true,
        "adsfsf": "this should be ignored",
        "extra": [
            {"text": " ", "underlined": false},
            {"text": "world", "bold": false, "strikethrough": true, "color": "#abcdef"}
        ]
    },
    {
        "text": " asdf",
        "italic": false,
        "obfuscated": true,
        "strikethrough": false
    },
    {
        "text": "!",
        "bold": true
    }
]"##,
    )
    .unwrap();
    let component = FormattedText::deserialize(&j).unwrap();

    assert_eq!(
        component.to_ansi(),
        format!(
            "{bold}{italic}{underlined}{red}hello{reset}{bold}{italic}{red} {reset}{italic}{underlined}{strikethrough}{abcdef}world{reset}{bold}{underlined}{obfuscated}{red} asdf{reset}{bold}{italic}{underlined}{red}!{reset}",
            bold = Ansi::BOLD,
            italic = Ansi::ITALIC,
            underlined = Ansi::UNDERLINED,
            red = Ansi::rgb(ChatFormatting::Red.color().unwrap()),
            reset = Ansi::RESET,
            strikethrough = Ansi::STRIKETHROUGH,
            obfuscated = Ansi::OBFUSCATED,
            abcdef = Ansi::rgb(TextColor::parse("#abcdef").unwrap().value),
        )
    );
}

#[test]
fn component_from_string() {
    let j: Value = serde_json::from_str("\"foo\"").unwrap();
    let component = FormattedText::deserialize(&j).unwrap();
    assert_eq!(component.to_ansi(), "\u{1b}[38;2;255;255;255mfoo\u{1b}[m");
}