go.charczuk.com@v0.0.0-20240327042549-bc490516bd1a/sdk/stringutil/table_test.go (about) 1 /* 2 3 Copyright (c) 2023 - Present. Will Charczuk. All rights reserved. 4 Use of this source code is governed by a MIT license that can be found in the LICENSE file at the root of the repository. 5 6 */ 7 8 package stringutil 9 10 import ( 11 "bytes" 12 "testing" 13 14 . "go.charczuk.com/sdk/assert" 15 ) 16 17 func Test_Table(t *testing.T) { 18 buffer := new(bytes.Buffer) 19 20 err := Table(buffer, []string{"foo", "bar"}, [][]string{{"1", "2"}, {"3", "4"}}) 21 ItsEqual(t, nil, err) 22 ItsEqual(t, "┌───┬───┐\n│foo│bar│\n├───┼───┤\n│1 │2 │\n│3 │4 │\n└───┴───┘\n", buffer.String()) 23 } 24 25 func Test_Table_empty(t *testing.T) { 26 buffer := new(bytes.Buffer) 27 28 err := Table(buffer, nil, [][]string{{"1", "2"}, {"3", "4"}}) 29 ItsEqual(t, nil, err) 30 ItsEqual(t, "", buffer.String()) 31 } 32 33 func Test_Table_write_err(t *testing.T) { 34 failAfter := &failAfter{MaxBytes: 32} 35 36 err := Table(failAfter, []string{"foo", "bar", "baz"}, [][]string{ 37 {"1", "2", "3"}, 38 {"4", "5", "6"}, 39 {"7", "8", "9"}, 40 }) 41 ItsEqual(t, "did fail", err.Error()) 42 } 43 44 func Test_TableForSlice(t *testing.T) { 45 objects := []struct { 46 ID int 47 Name string 48 }{ 49 {1, "Foo"}, 50 {2, "Bar"}, 51 {3, "Baz"}, 52 } 53 54 output := new(bytes.Buffer) 55 ItsEqual(t, nil, TableForSlice(output, objects)) 56 ItsEqual(t, 57 "┌──┬────┐\n│ID│Name│\n├──┼────┤\n│1 │Foo │\n│2 │Bar │\n│3 │Baz │\n└──┴────┘\n", 58 output.String(), 59 ) 60 } 61 62 func Test_TableForSlice_unicode(t *testing.T) { 63 objects := []struct { 64 ID string 65 Count int 66 }{ 67 {"モ foo", 1}, 68 {"ふ bar", 1}, 69 {"ス baz", 3}, 70 } 71 72 output := new(bytes.Buffer) 73 ItsEqual(t, nil, TableForSlice(output, objects)) 74 ItsEqual(t, 75 "┌──────┬─────┐\n│ID │Count│\n├──────┼─────┤\n│モ foo│1 │\n│ふ bar│1 │\n│ス baz│3 │\n└──────┴─────┘\n", 76 output.String(), 77 ) 78 }