shad-go/utf8/spacecollapse/collapse_test.go

42 lines
922 B
Go
Raw Normal View History

package spacecollapse
2020-02-24 19:54:48 +00:00
import (
"fmt"
2021-02-18 10:37:40 +00:00
"strings"
2020-02-24 19:54:48 +00:00
"testing"
"github.com/stretchr/testify/require"
)
func TestCollapseSpaces(t *testing.T) {
for i, tc := range []struct {
input string
output string
}{
{input: "", output: ""},
{input: "x", output: "x"},
{input: "Hello, World!", output: "Hello, World!"},
{input: "Привет,\tМир!", output: "Привет, Мир!"},
{input: "\r\n", output: " "},
{input: "\n\n", output: " "},
{input: "\t*", output: " *"},
{input: " \t \t ", output: " "},
{input: " \tx\t ", output: " x "},
2021-02-18 10:37:40 +00:00
{input: "\xff\x00\xff\x00", output: "\xff\x00\xff\x00"},
2020-02-24 19:54:48 +00:00
} {
t.Run(fmt.Sprintf("#%v: %v", i, tc.input), func(t *testing.T) {
require.Equal(t, tc.output, CollapseSpaces(tc.input))
})
}
}
2021-02-18 10:37:40 +00:00
func BenchmarkCollapse(b *testing.B) {
input := strings.Repeat("🙂 🙂", 100)
b.ReportAllocs()
for i := 0; i < b.N; i++ {
_ = CollapseSpaces(input)
}
}