aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/util/util_test.go
blob: 1c4ae39766bc3976ad848c1d86084f6472c19c1b (plain) (blame)
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
package util_test;

import (
	"io"
	"nonsense-time/util"
	"strings"
	"testing"
)

type stringReadCloser struct {
	*strings.Reader
}

func newStringReadCloser(s string) stringReadCloser {
	rc := stringReadCloser{}
	rc.Reader = strings.NewReader(s)

	return rc
}

func (stringReadCloser) Close() error {
	return nil
}

func testAnsiStripper(t *testing.T, input string, expected string) {
	reader := newStringReadCloser(input)
	cleanReader := util.NewAnsiStripper(reader)
	defer cleanReader.Close()

	buf := new(strings.Builder)
	n, err := io.Copy(buf, cleanReader)
	if err != nil {
		t.Fatal("Error while copying cleaned text to output", err)
	}

	result := buf.String()

	if n != int64(len(expected)) {
		t.Errorf("Expected to write %d characters but wrote %d\n", 3, n)
	}

	if result != expected {
		t.Errorf("Expected string `%s` but wrote `%s`", "abc", result)
	}
}

func TestStripAnsiColors(t *testing.T) {
	testAnsiStripper(t, "a\x1b[31mbc", "abc")
	testAnsiStripper(t, "[\x1b[32minfo\x1b[39m]", "[info]")
}