File: dedent_test.go

package info (click to toggle)
golang-github-muesli-reflow 0.3.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bookworm-backports, forky, sid, trixie
  • size: 180 kB
  • sloc: makefile: 2
file content (64 lines) | stat: -rw-r--r-- 1,500 bytes parent folder | download
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package dedent

import (
	"testing"
)

func TestDedent(t *testing.T) {
	tt := []struct {
		Input    string
		Expected string
	}{
		{
			Input:    "      --help      Show help for command\n      --version   Show version\n",
			Expected: "--help      Show help for command\n--version   Show version\n",
		},
		{
			Input:    "      --help              Show help for command\n  -C, --config string   Specify the config file to use\n",
			Expected: "    --help              Show help for command\n-C, --config string   Specify the config file to use\n",
		},
		{
			Input:    "  line 1\n\n  line 2\n line 3",
			Expected: " line 1\n\n line 2\nline 3",
		},
		{
			Input:    "  line 1\n  line 2\n  line 3\n\n",
			Expected: "line 1\nline 2\nline 3\n\n",
		},
		{
			Input:    " \tline 1\n\t\tline 2\n\t line 3\n\n",
			Expected: "line 1\nline 2\nline 3\n\n",
		},
		{
			Input:    "\t\tline 1\n\n\t\tline 2\n\tline 3",
			Expected: "\tline 1\n\n\tline 2\nline 3",
		},
		{
			Input:    "\n\n\n\n\n\n",
			Expected: "\n\n\n\n\n\n",
		},
		{
			Input:    "",
			Expected: "",
		},
	}

	for i, tc := range tt {
		s := String(tc.Input)
		if s != tc.Expected {
			t.Errorf("Test %d, expected:\n\n`%s`\n\nActual Output:\n\n`%s`", i, tc.Expected, s)
		}
	}
}

// go test -bench=BenchmarkDedent -benchmem -count=4
func BenchmarkDedent(b *testing.B) {
	b.RunParallel(func(pb *testing.PB) {
		input := "  line 1\n\n  line 2\n line 3"
		b.ReportAllocs()
		b.ResetTimer()
		for pb.Next() {
			String(input)
		}
	})
}