X-Git-Url: https://ruderich.org/simon/gitweb/?a=blobdiff_plain;f=frontend%2Fterm_test.go;fp=frontend%2Fterm_test.go;h=7569a9934cc1a60a10ba99619e4eee669e7b86a1;hb=ecbcb0132728cc18016819a214378b642d92278e;hp=0000000000000000000000000000000000000000;hpb=b0f49e5d47786984e24731b200d3d3d7d6add263;p=safcm%2Fsafcm.git diff --git a/frontend/term_test.go b/frontend/term_test.go new file mode 100644 index 0000000..7569a99 --- /dev/null +++ b/frontend/term_test.go @@ -0,0 +1,90 @@ +// Copyright (C) 2021 Simon Ruderich +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + +package frontend + +import ( + "testing" + + "ruderich.org/simon/safcm/testutil" +) + +func TestEscapeControlCharacters(t *testing.T) { + tests := []struct { + name string + isTTY bool + x string + exp string + }{ + { + "UTF-8", + false, + "Hello, 世界", + "Hello, 世界", + }, + { + "UTF-8 (TTY)", + true, + "Hello, 世界", + "Hello, 世界", + }, + + { + "color", + false, + ColorString(true, ColorRed, "red"), + `\x1B[31mred\x1B[0m`, + }, + { + "color (TTY)", + true, + ColorString(true, ColorRed, "red"), + "\x1B[35m\\x1B\x1B[0m[31mred\x1B[35m\\x1B\x1B[0m[0m", + }, + + { + "\\r\\n", + false, + "\r\n", + "\\r\n", + }, + { + "\\r\\n (TTY)", + true, + "\r\n", + "\x1B[35m\\r\x1B[0m\n", + }, + + { + "binary", + false, + "\xD6\x24\xA4\x45\xA2\x68\xD3\x0E\xD4\xC7\xC3\x1F", + "\xD6$\xA4E\xA2h\xD3\\x0E\xD4\xC7\xC3\\x1F", + }, + { + "binary (TTY)", + true, + "\xD6\x24\xA4\x45\xA2\x68\xD3\x0E\xD4\xC7\xC3\x1F", + "\xD6$\xA4E\xA2h\xD3\x1B[35m\\x0E\x1B[0m\xD4\xC7\xc3\x1B[35m\\x1F\x1B[0m", + }, + } + + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + res := EscapeControlCharacters(tc.isTTY, tc.x) + testutil.AssertEqual(t, "res", res, tc.exp) + }) + } +}