From 68ed836d9b347a9ceac3e415027183bae042be5d Mon Sep 17 00:00:00 2001 From: JP Appel Date: Tue, 8 Oct 2024 15:13:39 -0400 Subject: Add ansi filter writer --- util/util_test.go | 50 ++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 42 insertions(+), 8 deletions(-) (limited to 'util/util_test.go') 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") } -- cgit v1.2.3