aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/util/util_test.go
diff options
context:
space:
mode:
Diffstat (limited to 'util/util_test.go')
-rw-r--r--util/util_test.go50
1 files changed, 42 insertions, 8 deletions
diff --git a/util/util_test.go b/util/util_test.go
index 1c4ae39..fd9b7b8 100644
--- a/util/util_test.go
+++ b/util/util_test.go
@@ -1,4 +1,4 @@
-package util_test;
+package util_test
import (
"io"
@@ -22,9 +22,9 @@ func (stringReadCloser) Close() error {
return nil
}
-func testAnsiStripper(t *testing.T, input string, expected string) {
+func testAnsiFilterReadCloser(t *testing.T, input string, expected string) {
reader := newStringReadCloser(input)
- cleanReader := util.NewAnsiStripper(reader)
+ cleanReader := util.NewAnsiFilterReadCloser(reader)
defer cleanReader.Close()
buf := new(strings.Builder)
@@ -36,15 +36,49 @@ func testAnsiStripper(t *testing.T, input string, expected string) {
result := buf.String()
if n != int64(len(expected)) {
- t.Errorf("Expected to write %d characters but wrote %d\n", 3, n)
+ t.Errorf("Expected to read %d bytes but read %d\n", len(expected), n)
}
if result != expected {
- t.Errorf("Expected string `%s` but wrote `%s`", "abc", result)
+ t.Errorf("Expected string `%s` but read `%s`\n", "abc", result)
}
}
-func TestStripAnsiColors(t *testing.T) {
- testAnsiStripper(t, "a\x1b[31mbc", "abc")
- testAnsiStripper(t, "[\x1b[32minfo\x1b[39m]", "[info]")
+func testAnsiFilterWriter(t *testing.T, input string, expected string) {
+ writer := new(strings.Builder)
+ w := util.NewAnsiFilterWriter(writer)
+
+ n, err := w.Write([]byte(input))
+ if err != nil {
+ t.Fatal("Error while writting input", err)
+ }
+
+ if n != len(expected) {
+ t.Errorf("Expected to write %d bytes but read %d\n", len(expected), n)
+ }
+
+ result := writer.String()
+ if result != expected {
+ t.Errorf("Expected to write `%s` but wrote `%s`\n", expected, result)
+ }
+}
+
+func TestAnsiFilterColorsReadCloser(t *testing.T) {
+ testAnsiFilterReadCloser(t, "a\x1b[31mbc", "abc")
+ testAnsiFilterReadCloser(t, "[\x1b[32minfo\x1b[39m]", "[info]")
+ testAnsiFilterReadCloser(t,
+ "FoundryVTT | 2024-10-06 20:11:14 | [\x1b[32minfo\x1b[39m] Running on Node.js - Version 20.17.0\n"+
+ "FoundryVTT | 2024-10-06 20:11:14 | [\x1b[32minfo\x1b[39m] Foundry Virtual Tabletop - Version 12 Build 331\n",
+ "FoundryVTT | 2024-10-06 20:11:14 | [info] Running on Node.js - Version 20.17.0\n"+
+ "FoundryVTT | 2024-10-06 20:11:14 | [info] Foundry Virtual Tabletop - Version 12 Build 331\n")
+}
+
+func TestAnsiFilterColorsWriter(t *testing.T) {
+ testAnsiFilterWriter(t, "a\x1b[31mbc", "abc")
+ testAnsiFilterWriter(t, "[\x1b[32minfo\x1b[39m]", "[info]")
+ testAnsiFilterWriter(t,
+ "FoundryVTT | 2024-10-06 20:11:14 | [\x1b[32minfo\x1b[39m] Running on Node.js - Version 20.17.0\n"+
+ "FoundryVTT | 2024-10-06 20:11:14 | [\x1b[32minfo\x1b[39m] Foundry Virtual Tabletop - Version 12 Build 331\n",
+ "FoundryVTT | 2024-10-06 20:11:14 | [info] Running on Node.js - Version 20.17.0\n"+
+ "FoundryVTT | 2024-10-06 20:11:14 | [info] Foundry Virtual Tabletop - Version 12 Build 331\n")
}