~poldi1405/go-ansi

ref: 62ed27221ee273d9d6568cbaa296eb5f86c4a45b go-ansi/strip_test.go -rw-r--r-- 1.4 KiB
62ed2722 — Moritz Poldrack fixed minor typo in documentation 4 months ago
                                                                                
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
package ansi

import (
	"fmt"
	"regexp"
	"testing"
)

func BenchmarkStripping(b *testing.B) {
	DetectionPattern := regexp.MustCompile(`(?m)(((\x1b\[[;\d]*[A-Za-z])|\x1b]8;;|\x1b\\.*?\x1b|]8;;\x1b\\)*)`)
	str := fmt.Sprint(Black("black text"), "some kept text", ReverseVideo("inverted text"), Green("green text"), Bold("bold text"))

	b.Run("regex", func(b *testing.B) {
		for i := 0; i < b.N; i++ {
			DetectionPattern.ReplaceAllString(str, "")
		}
	})

	b.Run("split", func(b *testing.B) {
		for i := 0; i < b.N; i++ {
			StripString(str)
		}
	})
}

func TestStrip(t *testing.T) {
	str := fmt.Sprint(Black("black text"), "some kept text", ReverseVideo("inverted text"), Green("green text"), Bold("bold text"))
	if StripString(str) != "black textsome kept textinverted textgreen textbold text" {
		t.Errorf("strip not successful!\ngot:      %s\n%v\nexpected: black textsome kept textinverted textgreen textbold text\n%v", StripString(str), []byte(StripString(str)), []byte("black textsome kept textinverted textgreen textbold text"))
	}

	str = fmt.Sprintf("Link to my homepage: %s", LinkString("https://moritz.sh", "Linktext"))
	if StripString(str) != "Link to my homepage: https://moritz.sh" {
		t.Errorf("strip not successful!\ngot:      %s\n%v\nexpected: Link to my homepage: https://moritz.sh\n%v", StripString(str), []byte(StripString(str)), []byte("Link to my homepage: https://moritz.sh"))
	}
}