~taiite/senpai

ref: a532aae30446d7d76c74780bebaf0a40c775694e senpai/ui/style_test.go -rw-r--r-- 2.6 KiB
a532aae3Hubert Hirtz Fix history not being loaded on channel re-JOIN 8 months ago
                                                                                
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
87
88
89
90
91
92
93
94
95
96
package ui

import (
	"testing"

	"github.com/gdamore/tcell/v2"
)

func assertIRCString(t *testing.T, input string, expected StyledString) {
	actual := IRCString(input)
	if actual.string != expected.string {
		t.Errorf("%q: expected string %q, got %q", input, expected.string, actual.string)
	}
	if len(actual.styles) != len(expected.styles) {
		t.Errorf("%q: expected %d styles, got %d", input, len(expected.styles), len(actual.styles))
		return
	}
	for i := range actual.styles {
		if actual.styles[i] != expected.styles[i] {
			t.Errorf("%q: style #%d expected to be %+v, got %+v", input, i, expected.styles[i], actual.styles[i])
		}
	}
}

func TestIRCString(t *testing.T) {
	assertIRCString(t, "", StyledString{
		string: "",
		styles: nil,
	})

	assertIRCString(t, "hello", StyledString{
		string: "hello",
		styles: nil,
	})
	assertIRCString(t, "\x02hello", StyledString{
		string: "hello",
		styles: []rangedStyle{
			{Start: 0, Style: tcell.StyleDefault.Bold(true)},
		},
	})
	assertIRCString(t, "\x035hello", StyledString{
		string: "hello",
		styles: []rangedStyle{
			{Start: 0, Style: tcell.StyleDefault.Foreground(tcell.ColorBrown)},
		},
	})
	assertIRCString(t, "\x0305hello", StyledString{
		string: "hello",
		styles: []rangedStyle{
			{Start: 0, Style: tcell.StyleDefault.Foreground(tcell.ColorBrown)},
		},
	})
	assertIRCString(t, "\x0305,0hello", StyledString{
		string: "hello",
		styles: []rangedStyle{
			{Start: 0, Style: tcell.StyleDefault.Foreground(tcell.ColorBrown).Background(tcell.ColorWhite)},
		},
	})
	assertIRCString(t, "\x035,00hello", StyledString{
		string: "hello",
		styles: []rangedStyle{
			{Start: 0, Style: tcell.StyleDefault.Foreground(tcell.ColorBrown).Background(tcell.ColorWhite)},
		},
	})
	assertIRCString(t, "\x0305,00hello", StyledString{
		string: "hello",
		styles: []rangedStyle{
			{Start: 0, Style: tcell.StyleDefault.Foreground(tcell.ColorBrown).Background(tcell.ColorWhite)},
		},
	})

	assertIRCString(t, "\x035,hello", StyledString{
		string: ",hello",
		styles: []rangedStyle{
			{Start: 0, Style: tcell.StyleDefault.Foreground(tcell.ColorBrown)},
		},
	})
	assertIRCString(t, "\x0305,hello", StyledString{
		string: ",hello",
		styles: []rangedStyle{
			{Start: 0, Style: tcell.StyleDefault.Foreground(tcell.ColorBrown)},
		},
	})
	assertIRCString(t, "\x03050hello", StyledString{
		string: "0hello",
		styles: []rangedStyle{
			{Start: 0, Style: tcell.StyleDefault.Foreground(tcell.ColorBrown)},
		},
	})
	assertIRCString(t, "\x0305,000hello", StyledString{
		string: "0hello",
		styles: []rangedStyle{
			{Start: 0, Style: tcell.StyleDefault.Foreground(tcell.ColorBrown).Background(tcell.ColorWhite)},
		},
	})
}