package receipt import ( "reflect" "testing" ) func TestWrap(t *testing.T) { tests := []struct { name string text string first, width int want []string }{ { name: "fits", text: "hello world", first: 48, width: 48, want: []string{"hello world"}, }, { name: "breaks at word boundary", text: "the quick brown fox", first: 10, width: 10, want: []string{"the quick", "brown fox"}, }, { name: "never splits a word that fits on a line", text: "Thank you for shopping with us today", first: 12, width: 12, want: []string{"Thank you", "for shopping", "with us", "today"}, }, { name: "hard splits only words longer than a line", text: "see https://example.com/very/long ok", first: 12, width: 12, want: []string{"see", "https://exam", "ple.com/very", "/long ok"}, }, { name: "runs of spaces kept inside a line", text: "a b", first: 10, width: 10, want: []string{"a b"}, }, { name: "spaces at a break are dropped", text: "aaaa b", first: 5, width: 5, want: []string{"aaaa", "b"}, }, { name: "paragraphs wrap independently", text: "one two\n\nthree", first: 5, width: 5, want: []string{"one", "two", "", "three"}, }, { name: "indent kept on first line only", text: " indented text here", first: 10, width: 10, want: []string{" indented", "text here"}, }, { name: "inline continuation fits", text: " world", first: 6, width: 10, want: []string{" world"}, }, { name: "inline continuation breaks before the word", text: "world", first: 3, width: 10, want: []string{"", "world"}, }, { name: "inline continuation keeps filling then wraps", text: " big wide world", first: 8, width: 10, want: []string{" big", "wide world"}, }, { name: "trailing space kept for inline use", text: "Total: ", first: 48, width: 48, want: []string{"Total: "}, }, { name: "empty", text: "", first: 10, width: 10, want: []string{""}, }, { name: "unicode counted as runes", text: "Grüße an Übermorgen", first: 10, width: 10, want: []string{"Grüße an", "Übermorgen"}, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { got := wrap(tt.text, tt.first, tt.width) if !reflect.DeepEqual(got, tt.want) { t.Errorf("wrap(%q, %d, %d) = %q, want %q", tt.text, tt.first, tt.width, got, tt.want) } for i, line := range got { limit := tt.width if i == 0 { limit = tt.first } if n := len([]rune(line)); n > limit { t.Errorf("line %d %q has %d runes, limit %d", i, line, n, limit) } } }) } } func TestExpandTabs(t *testing.T) { tests := []struct { in string start int want string }{ {"a\tb", 0, "a b"}, {"abcdefgh\tx", 0, "abcdefgh x"}, {"\tx", 3, " x"}, {"a\n\tb", 5, "a\n b"}, {"none", 0, "none"}, } for _, tt := range tests { if got := expandTabs(tt.in, tt.start); got != tt.want { t.Errorf("expandTabs(%q, %d) = %q, want %q", tt.in, tt.start, got, tt.want) } } } func TestSanitize(t *testing.T) { if got := sanitize("a\x1b@b\r\n\tc\x00"); got != "a@b\n\tc" { t.Errorf("sanitize = %q", got) } }