-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhtml2xterm_test.go
100 lines (89 loc) · 2.32 KB
/
html2xterm_test.go
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
97
98
99
100
package html2xterm
import (
"io/ioutil"
"strings"
"testing"
)
func loadFile(t *testing.T, filename string) string {
b, err := ioutil.ReadFile(filename)
if err != nil {
t.Fatal(err)
}
return string(b)
}
func TestConvert(t *testing.T) {
cases := []struct {
html string
outputType string
expected string
}{
{
html: loadFile(t, `testdata/001_input_div_span.html`),
outputType: "string",
expected: loadFile(t, `testdata/001_output_string.txt`),
},
{
html: loadFile(t, `testdata/002_input_font_br.html`),
outputType: "string",
expected: loadFile(t, `testdata/002_output_string.txt`),
},
{
html: loadFile(t, `testdata/003_input_html_span_br.html`),
outputType: "ansi",
expected: loadFile(t, `testdata/003_output_ansi.ans`),
},
{
html: loadFile(t, `testdata/003_input_html_span_br.html`),
outputType: "xtermjs",
expected: loadFile(t, `testdata/003_output_xterm.js`),
},
}
for _, c := range cases {
output, err := Convert(c.html)
if err != nil {
t.Fatal(err)
}
var actual string
switch c.outputType {
case "string":
actual = output.String()
case "ansi":
actual = output.ANSI()
case "xtermjs":
actual = output.JS()
default:
t.Errorf("unknown output type '%s'", c.outputType)
continue
}
assertEqual(t, c.expected, actual)
}
}
func assertEqual(t *testing.T, expected, actual string) {
if expected == actual {
return
}
t.Errorf("\nwant:\n---\n%s\n---\ngot:\n---\n%s\n---", expected, actual)
expectedLines := strings.Split(expected, "\n")
actualLines := strings.Split(actual, "\n")
for i := 0; i < len(expectedLines) && i < len(actualLines); i++ {
if expectedLines[i] != actualLines[i] {
if len(expectedLines[i]) > 200 {
found := false
for j := 0; j < len(expectedLines[i]) && j < len(actualLines[i]); j++ {
if expectedLines[i][j] != actualLines[i][j] {
t.Errorf("first difference at line %d pos %d: want: '%c' %v got: '%c' %v", i, j,
expectedLines[i][j], expectedLines[i][j], actualLines[i][j], actualLines[i][j])
found = true
break
}
}
if found {
break
}
}
t.Errorf("first difference:\n\nwant: '%s'\n %v\ngot: '%s'\n %v\n",
expectedLines[i], []byte(expectedLines[i]), actualLines[i], []byte(actualLines[i]))
break
}
}
}