1package ui
2
3import (
4 "testing"
5
6 "git.sr.ht/~rockorager/vaxis"
7)
8
9func assertIRCString(t *testing.T, input string, expected StyledString) {
10 actual := IRCString(input)
11 if actual.string != expected.string {
12 t.Errorf("%q: expected string %q, got %q", input, expected.string, actual.string)
13 }
14 if len(actual.styles) != len(expected.styles) {
15 t.Errorf("%q: expected %d styles, got %d", input, len(expected.styles), len(actual.styles))
16 return
17 }
18 for i := range actual.styles {
19 if actual.styles[i] != expected.styles[i] {
20 t.Errorf("%q: style #%d expected to be %+v, got %+v", input, i, expected.styles[i], actual.styles[i])
21 }
22 }
23}
24
25func TestIRCString(t *testing.T) {
26 assertIRCString(t, "", StyledString{
27 string: "",
28 styles: nil,
29 })
30
31 assertIRCString(t, "hello", StyledString{
32 string: "hello",
33 styles: nil,
34 })
35 assertIRCString(t, "\x02hello", StyledString{
36 string: "hello",
37 styles: []rangedStyle{
38 {Start: 0, Style: vaxis.Style{Attribute: vaxis.AttrBold}},
39 },
40 })
41 assertIRCString(t, "\x035hello", StyledString{
42 string: "hello",
43 styles: []rangedStyle{
44 {Start: 0, Style: vaxis.Style{Foreground: vaxis.IndexColor(1)}},
45 },
46 })
47 assertIRCString(t, "\x0305hello", StyledString{
48 string: "hello",
49 styles: []rangedStyle{
50 {Start: 0, Style: vaxis.Style{Foreground: vaxis.IndexColor(1)}},
51 },
52 })
53 assertIRCString(t, "\x0305,0hello", StyledString{
54 string: "hello",
55 styles: []rangedStyle{
56 {Start: 0, Style: vaxis.Style{Foreground: vaxis.IndexColor(1), Background: vaxis.IndexColor(15)}},
57 },
58 })
59 assertIRCString(t, "\x035,00hello", StyledString{
60 string: "hello",
61 styles: []rangedStyle{
62 {Start: 0, Style: vaxis.Style{Foreground: vaxis.IndexColor(1), Background: vaxis.IndexColor(15)}},
63 },
64 })
65 assertIRCString(t, "\x0305,00hello", StyledString{
66 string: "hello",
67 styles: []rangedStyle{
68 {Start: 0, Style: vaxis.Style{Foreground: vaxis.IndexColor(1), Background: vaxis.IndexColor(15)}},
69 },
70 })
71
72 assertIRCString(t, "\x035,hello", StyledString{
73 string: ",hello",
74 styles: []rangedStyle{
75 {Start: 0, Style: vaxis.Style{Foreground: vaxis.IndexColor(1)}},
76 },
77 })
78 assertIRCString(t, "\x0305,hello", StyledString{
79 string: ",hello",
80 styles: []rangedStyle{
81 {Start: 0, Style: vaxis.Style{Foreground: vaxis.IndexColor(1)}},
82 },
83 })
84 assertIRCString(t, "\x03050hello", StyledString{
85 string: "0hello",
86 styles: []rangedStyle{
87 {Start: 0, Style: vaxis.Style{Foreground: vaxis.IndexColor(1)}},
88 },
89 })
90 assertIRCString(t, "\x0305,000hello", StyledString{
91 string: "0hello",
92 styles: []rangedStyle{
93 {Start: 0, Style: vaxis.Style{Foreground: vaxis.IndexColor(1), Background: vaxis.IndexColor(15)}},
94 },
95 })
96}