| 1 |
// Copyright 2014 Oleku Konko All rights reserved. |
| 2 |
// Use of this source code is governed by a MIT |
| 3 |
// license that can be found in the LICENSE file. |
| 4 |
|
| 5 |
// This module is a Table Writer API for the Go Programming Language. |
| 6 |
// The protocols were written in pure Go and works on windows and unix systems |
| 7 |
|
| 8 |
package twwarp |
| 9 |
|
| 10 |
import ( |
| 11 |
"bytes" |
| 12 |
"fmt" |
| 13 |
"os" |
| 14 |
"reflect" |
| 15 |
"runtime" |
| 16 |
"strings" |
| 17 |
"testing" |
| 18 |
"time" |
| 19 |
|
| 20 |
"github.com/olekukonko/tablewriter/pkg/twwidth" |
| 21 |
"github.com/olekukonko/tablewriter/tw" |
| 22 |
) |
| 23 |
|
| 24 |
var ( |
| 25 |
text = "The quick brown fox jumps over the lazy dog." |
| 26 |
testDir = "./_data" |
| 27 |
) |
| 28 |
|
| 29 |
// checkEqual compares two values and fails the test if they are not equal |
| 30 |
func checkEqual(t *testing.T, got, want interface{}, msgs ...interface{}) { |
| 31 |
t.Helper() |
| 32 |
if !reflect.DeepEqual(got, want) { |
| 33 |
var buf bytes.Buffer |
| 34 |
buf.WriteString(fmt.Sprintf("got:\n[%v]\nwant:\n[%v]\n", got, want)) |
| 35 |
for _, v := range msgs { |
| 36 |
buf.WriteString(fmt.Sprint(v)) |
| 37 |
} |
| 38 |
t.Error(buf.String()) |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
func TestWrap(t *testing.T) { |
| 43 |
exp := []string{ |
| 44 |
"The", "quick", "brown", "fox", |
| 45 |
"jumps", "over", "the", "lazy", "dog.", |
| 46 |
} |
| 47 |
|
| 48 |
got, _ := WrapString(text, 6) |
| 49 |
checkEqual(t, len(got), len(exp)) |
| 50 |
} |
| 51 |
|
| 52 |
func TestWrapOneLine(t *testing.T) { |
| 53 |
exp := "The quick brown fox jumps over the lazy dog." |
| 54 |
words, _ := WrapString(text, 500) |
| 55 |
checkEqual(t, strings.Join(words, string(tw.Space)), exp) |
| 56 |
} |
| 57 |
|
| 58 |
func TestUnicode(t *testing.T) { |
| 59 |
input := "Česká řeřicha" |
| 60 |
var wordsUnicode []string |
| 61 |
if twwidth.IsEastAsian() { |
| 62 |
wordsUnicode, _ = WrapString(input, 14) |
| 63 |
} else { |
| 64 |
wordsUnicode, _ = WrapString(input, 13) |
| 65 |
} |
| 66 |
// input contains 13 (or 14 for CJK) runes, so it fits on one line. |
| 67 |
checkEqual(t, len(wordsUnicode), 1) |
| 68 |
} |
| 69 |
|
| 70 |
func TestDisplayWidth(t *testing.T) { |
| 71 |
input := "Česká řeřicha" |
| 72 |
want := 13 |
| 73 |
if twwidth.IsEastAsian() { |
| 74 |
want = 14 |
| 75 |
} |
| 76 |
if n := twwidth.Width(input); n != want { |
| 77 |
t.Errorf("Wants: %d Got: %d", want, n) |
| 78 |
} |
| 79 |
input = "\033[43;30m" + input + "\033[00m" |
| 80 |
checkEqual(t, twwidth.Width(input), want) |
| 81 |
|
| 82 |
input = "\033]8;;idea://open/?file=/path/somefile.php&line=12\033\\some URL\033]8;;\033\\" |
| 83 |
checkEqual(t, twwidth.Width(input), 8) |
| 84 |
} |
| 85 |
|
| 86 |
// WrapString was extremely memory greedy, it performed insane number of |
| 87 |
// allocations for what it was doing. See BenchmarkWrapString for details. |
| 88 |
func TestWrapStringAllocation(t *testing.T) { |
| 89 |
originalTextBytes, err := os.ReadFile(testDir + "/long-text.txt") |
| 90 |
if err != nil { |
| 91 |
t.Fatal(err) |
| 92 |
} |
| 93 |
originalText := string(originalTextBytes) |
| 94 |
|
| 95 |
wantWrappedBytes, err := os.ReadFile(testDir + "/long-text-wrapped.txt") |
| 96 |
if err != nil { |
| 97 |
t.Fatal(err) |
| 98 |
} |
| 99 |
wantWrappedText := string(wantWrappedBytes) |
| 100 |
|
| 101 |
var ms runtime.MemStats |
| 102 |
runtime.ReadMemStats(&ms) |
| 103 |
heapAllocBefore := int64(ms.HeapAlloc / 1024 / 1024) |
| 104 |
|
| 105 |
// When |
| 106 |
gotLines, gotLim := WrapString(originalText, 80) |
| 107 |
|
| 108 |
// Then |
| 109 |
wantLim := 80 |
| 110 |
if gotLim != wantLim { |
| 111 |
t.Errorf("Invalid limit: want=%d, got=%d", wantLim, gotLim) |
| 112 |
} |
| 113 |
|
| 114 |
gotWrappedText := strings.Join(gotLines, "\n") |
| 115 |
if gotWrappedText != wantWrappedText { |
| 116 |
t.Errorf("Invalid lines: want=\n%s\n got=\n%s", wantWrappedText, gotWrappedText) |
| 117 |
} |
| 118 |
|
| 119 |
runtime.ReadMemStats(&ms) |
| 120 |
heapAllocAfter := int64(ms.HeapAlloc / 1024 / 1024) |
| 121 |
heapAllocDelta := heapAllocAfter - heapAllocBefore |
| 122 |
if heapAllocDelta > 1 { |
| 123 |
t.Fatalf("heap allocation should not be greater than 1Mb, got=%dMb", heapAllocDelta) |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
// Before optimization: |
| 128 |
// BenchmarkWrapString-16 1 2490331031 ns/op 2535184104 B/op 50905550 allocs/op |
| 129 |
// After optimization: |
| 130 |
// BenchmarkWrapString-16 1652 658098 ns/op 230223 B/op 5176 allocs/op |
| 131 |
func BenchmarkWrapString(b *testing.B) { |
| 132 |
d, err := os.ReadFile(testDir + "/long-text.txt") |
| 133 |
s := string(d) |
| 134 |
b.SetBytes(int64(len(s))) |
| 135 |
b.ResetTimer() |
| 136 |
if err != nil { |
| 137 |
b.Fatal(err) |
| 138 |
} |
| 139 |
for i := 0; i < b.N; i++ { |
| 140 |
WrapString(s, 128) |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
func BenchmarkWrapStringWithSpaces(b *testing.B) { |
| 145 |
d, err := os.ReadFile(testDir + "/long-text.txt") |
| 146 |
s := string(d) |
| 147 |
b.SetBytes(int64(len(s))) |
| 148 |
b.ResetTimer() |
| 149 |
if err != nil { |
| 150 |
b.Fatal(err) |
| 151 |
} |
| 152 |
for i := 0; i < b.N; i++ { |
| 153 |
WrapStringWithSpaces(s, 128) |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
func TestSplitWords(t *testing.T) { |
| 158 |
for _, tt := range []struct { |
| 159 |
in string |
| 160 |
out []string |
| 161 |
}{{ |
| 162 |
in: "", |
| 163 |
out: []string{}, |
| 164 |
}, { |
| 165 |
in: "a", |
| 166 |
out: []string{"a"}, |
| 167 |
}, { |
| 168 |
in: "a b", |
| 169 |
out: []string{"a", "b"}, |
| 170 |
}, { |
| 171 |
in: " a b ", |
| 172 |
out: []string{"a", "b"}, |
| 173 |
}, { |
| 174 |
in: "\r\na\t\t \r\t b\r\n ", |
| 175 |
out: []string{"a", "b"}, |
| 176 |
}} { |
| 177 |
t.Run(tt.in, func(t *testing.T) { |
| 178 |
got := SplitWords(tt.in) |
| 179 |
if !reflect.DeepEqual(tt.out, got) { |
| 180 |
t.Errorf("want=%s, got=%s", tt.out, got) |
| 181 |
} |
| 182 |
}) |
| 183 |
} |
| 184 |
} |
| 185 |
|
| 186 |
func TestWrapString(t *testing.T) { |
| 187 |
want := []string{"ああああああああああああああああああああああああ", "あああああああ"} |
| 188 |
got, _ := WrapString("ああああああああああああああああああああああああ あああああああ", 55) |
| 189 |
checkEqual(t, got, want) |
| 190 |
} |
| 191 |
|
| 192 |
func TestWrapWords(t *testing.T) { |
| 193 |
for _, tc := range []struct { |
| 194 |
name string |
| 195 |
words []string |
| 196 |
spc int |
| 197 |
lim int |
| 198 |
want [][]string |
| 199 |
}{ |
| 200 |
// A last word wider than lim used to leave the break chain unterminated. |
| 201 |
{"single word over limit", []string{"hello"}, 1, 3, [][]string{{"hello"}}}, |
| 202 |
{"last word over limit", []string{"aaa", "bbbb"}, 1, 3, [][]string{{"aaa"}, {"bbbb"}}}, |
| 203 |
{"last of three over limit", []string{"a", "b", "cccc"}, 1, 3, [][]string{{"a", "b"}, {"cccc"}}}, |
| 204 |
{"every word over limit", []string{"aaaa", "bbbb"}, 1, 3, [][]string{{"aaaa"}, {"bbbb"}}}, |
| 205 |
{"wide runes over limit", []string{"ああ", "いいい"}, 1, 4, [][]string{{"ああ"}, {"いいい"}}}, |
| 206 |
{"zero limit", []string{"x"}, 1, 0, [][]string{{"x"}}}, |
| 207 |
|
| 208 |
// Unaffected shapes, kept so the fix is pinned to the last-word case. |
| 209 |
{"empty", nil, 1, 3, nil}, |
| 210 |
{"single word at limit", []string{"abc"}, 1, 3, [][]string{{"abc"}}}, |
| 211 |
{"single word under limit", []string{"ab"}, 1, 3, [][]string{{"ab"}}}, |
| 212 |
{"over-limit word not last", []string{"aaaa", "b"}, 1, 3, [][]string{{"aaaa"}, {"b"}}}, |
| 213 |
{"wraps at limit", []string{"the", "quick", "brown", "fox"}, 1, 10, [][]string{{"the", "quick"}, {"brown", "fox"}}}, |
| 214 |
{"one word per line", []string{"the", "quick", "brown", "fox"}, 1, 5, [][]string{{"the"}, {"quick"}, {"brown"}, {"fox"}}}, |
| 215 |
{"all on one line", []string{"a", "b", "c"}, 1, 100, [][]string{{"a", "b", "c"}}}, |
| 216 |
{"no space between words", []string{"aaa", "bbb"}, 0, 3, [][]string{{"aaa"}, {"bbb"}}}, |
| 217 |
} { |
| 218 |
t.Run(tc.name, func(t *testing.T) { |
| 219 |
done := make(chan [][]string, 1) |
| 220 |
go func() { done <- WrapWords(tc.words, tc.spc, tc.lim, defaultPenalty) }() |
| 221 |
select { |
| 222 |
case got := <-done: |
| 223 |
checkEqual(t, got, tc.want) |
| 224 |
case <-time.After(5 * time.Second): |
| 225 |
t.Fatalf("WrapWords(%q, %d, %d) did not return", tc.words, tc.spc, tc.lim) |
| 226 |
} |
| 227 |
}) |
| 228 |
} |
| 229 |
} |