]> ruderich.org/simon Gitweb - safcm/safcm.git/blob - cmd/safcm/term_test.go
safcm: add tests for logEvent()
[safcm/safcm.git] / cmd / safcm / term_test.go
1 // Copyright (C) 2021  Simon Ruderich
2 //
3 // This program is free software: you can redistribute it and/or modify
4 // it under the terms of the GNU General Public License as published by
5 // the Free Software Foundation, either version 3 of the License, or
6 // (at your option) any later version.
7 //
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 // GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License
14 // along with this program.  If not, see <http://www.gnu.org/licenses/>.
15
16 package main
17
18 import (
19         "testing"
20
21         "ruderich.org/simon/safcm/testutil"
22 )
23
24 func TestEscapeControlCharacters(t *testing.T) {
25         tests := []struct {
26                 name  string
27                 isTTY bool
28                 x     string
29                 exp   string
30         }{
31                 {
32                         "UTF-8",
33                         false,
34                         "Hello, 世界",
35                         "Hello, 世界",
36                 },
37                 {
38                         "UTF-8 (TTY)",
39                         true,
40                         "Hello, 世界",
41                         "Hello, 世界",
42                 },
43
44                 {
45                         "color",
46                         false,
47                         ColorString(true, ColorRed, "red"),
48                         `\x1B[31mred\x1B[0m`,
49                 },
50                 {
51                         "color (TTY)",
52                         true,
53                         ColorString(true, ColorRed, "red"),
54                         "\x1B[35m\\x1B\x1B[0m[31mred\x1B[35m\\x1B\x1B[0m[0m",
55                 },
56
57                 {
58                         "\\r\\n",
59                         false,
60                         "\r\n",
61                         "\\r\n",
62                 },
63                 {
64                         "\\r\\n (TTY)",
65                         true,
66                         "\r\n",
67                         "\x1B[35m\\r\x1B[0m\n",
68                 },
69
70                 {
71                         "binary",
72                         false,
73                         "\xD6\x24\xA4\x45\xA2\x68\xD3\x0E\xD4\xC7\xC3\x1F",
74                         "\xD6$\xA4E\xA2h\xD3\\x0E\xD4\xC7\xC3\\x1F",
75                 },
76                 {
77                         "binary (TTY)",
78                         true,
79                         "\xD6\x24\xA4\x45\xA2\x68\xD3\x0E\xD4\xC7\xC3\x1F",
80                         "\xD6$\xA4E\xA2h\xD3\x1B[35m\\x0E\x1B[0m\xD4\xC7\xc3\x1B[35m\\x1F\x1B[0m",
81                 },
82         }
83
84         for _, tc := range tests {
85                 t.Run(tc.name, func(t *testing.T) {
86                         res := EscapeControlCharacters(tc.isTTY, tc.x)
87                         testutil.AssertEqual(t, "res", res, tc.exp)
88                 })
89         }
90 }