github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/cli/printer/printer_test.go (about) 1 /* 2 Copyright (C) 2022-2023 ApeCloud Co., Ltd 3 4 This file is part of KubeBlocks project 5 6 This program is free software: you can redistribute it and/or modify 7 it under the terms of the GNU Affero General Public License as published by 8 the Free Software Foundation, either version 3 of the License, or 9 (at your option) any later version. 10 11 This program is distributed in the hope that it will be useful 12 but WITHOUT ANY WARRANTY; without even the implied warranty of 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 GNU Affero General Public License for more details. 15 16 You should have received a copy of the GNU Affero General Public License 17 along with this program. If not, see <http://www.gnu.org/licenses/>. 18 */ 19 20 package printer 21 22 import ( 23 "fmt" 24 "os" 25 "testing" 26 27 "github.com/stretchr/testify/assert" 28 29 clitesting "github.com/1aal/kubeblocks/pkg/cli/testing" 30 ) 31 32 var ( 33 header = []string{"NAME", "NAMESPACE", "CLUSTER-DEFINITION", "VERSION", "TERMINATION-POLICY", "CREATED-TIME"} 34 ) 35 36 func TestPrintTable(t *testing.T) { 37 printer := NewTablePrinter(os.Stdout) 38 headerRow := make([]interface{}, len(header)) 39 for i, h := range header { 40 headerRow[i] = h 41 } 42 printer.SetHeader(headerRow...) 43 for _, r := range [][]string{ 44 {"brier63", "default", "apecloud-mysql", "ac-mysql-8.0.30", "Delete", "Feb 20,2023 16:39 UTC+0800"}, 45 {"cedar51", "default", "apecloud-mysql", "ac-mysql-8.0.30", "Delete", "Feb 20,2023 16:39 UTC+0800"}, 46 } { 47 row := make([]interface{}, len(r)) 48 for i, rr := range r { 49 row[i] = rr 50 } 51 printer.AddRow(row...) 52 } 53 printer.Print() 54 } 55 56 func TestPrintPairStringToLine(t *testing.T) { 57 doPrintPairStringToLineAssert(nil, t) 58 spaceCount := 0 59 doPrintPairStringToLineAssert(&spaceCount, t) 60 spaceCount = 3 61 doPrintPairStringToLineAssert(&spaceCount, t) 62 } 63 64 func doPrintPairStringToLineAssert(spaceCount *int, t *testing.T) { 65 done := clitesting.Capture() 66 key, value := "key", "value" 67 var expectSpaceCount int 68 if spaceCount == nil { 69 PrintPairStringToLine(key, value) 70 expectSpaceCount = 2 71 } else { 72 PrintPairStringToLine(key, value, *spaceCount) 73 expectSpaceCount = *spaceCount 74 } 75 76 capturedOutput, err := done() 77 if err != nil { 78 t.Error("capture stdout failed:" + err.Error()) 79 } 80 var spaceStr string 81 for i := 0; i < expectSpaceCount; i++ { 82 spaceStr += " " 83 } 84 assert.Equal(t, fmt.Sprintf("%s%-20s%s", spaceStr, key+":", value+"\n"), capturedOutput) 85 } 86 87 func TestPrintLineWithTabSeparator(t *testing.T) { 88 done := clitesting.Capture() 89 key, value := "key", "value" 90 PrintLineWithTabSeparator(NewPair(key, value)) 91 checkOutPut(t, done, fmt.Sprintf("%s: %s\t\n", key, value)) 92 } 93 94 func TestPrintTitle(t *testing.T) { 95 done := clitesting.Capture() 96 line := "Title" 97 PrintTitle(line) 98 checkOutPut(t, done, fmt.Sprintf("\n%s:\n", line)) 99 } 100 101 func TestPrintLine(t *testing.T) { 102 done := clitesting.Capture() 103 line := "test line" 104 PrintLine(line) 105 checkOutPut(t, done, "test line\n") 106 } 107 108 func checkOutPut(t *testing.T, captureFunc func() (string, error), expect string) { 109 capturedOutput, err := captureFunc() 110 if err != nil { 111 t.Error("capture stdout failed:" + err.Error()) 112 } 113 assert.Equal(t, expect, capturedOutput) 114 } 115 116 func TestSort(t *testing.T) { 117 printer := NewTablePrinter(os.Stdout) 118 headerRow := make([]interface{}, len(header)) 119 for i, h := range header { 120 headerRow[i] = h 121 } 122 printer.SetHeader(headerRow...) 123 printer.SortBy(1) 124 for _, r := range [][]string{ 125 {"cedar51", "default", "apecloud-mysql", "ac-mysql-8.0.30", "Delete", "Feb 20,2023 16:39 UTC+0800"}, 126 {"brier63", "default", "apecloud-mysql", "ac-mysql-8.0.30", "Delete", "Feb 20,2023 16:39 UTC+0800"}, 127 {"alpha19", "default", "apecloud-mysql", "ac-mysql-8.0.30", "Delete", "Feb 20,2023 16:39 UTC+0800"}, 128 } { 129 row := make([]interface{}, len(r)) 130 for i, rr := range r { 131 row[i] = rr 132 } 133 printer.AddRow(row...) 134 } 135 printer.Print() 136 }