istio.io/istio@v0.0.0-20240520182934-d79c90f27776/istioctl/pkg/writer/table/writer_test.go (about) 1 // Copyright Istio Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package table 16 17 import ( 18 "bytes" 19 "testing" 20 21 "github.com/fatih/color" 22 ) 23 24 type testObject struct { 25 name string 26 namespace string 27 version string 28 } 29 30 var newTestObject = func(name, namespace, version string) testObject { 31 return testObject{ 32 name: name, 33 namespace: namespace, 34 version: version, 35 } 36 } 37 38 func TestWriter(t *testing.T) { 39 got := &bytes.Buffer{} 40 w := NewStyleWriter(got) 41 w.AddHeader("NAME", "NAMESPACE", "VERSION") 42 w.SetAddRowFunc(func(obj interface{}) Row { 43 o := obj.(testObject) 44 return Row{ 45 Cells: []Cell{ 46 NewCell(o.name), 47 NewCell(o.namespace, color.FgGreen), 48 NewCell(o.version), 49 }, 50 } 51 }) 52 w.AddRow(newTestObject("foo", "bar", "1.0")) 53 w.AddRow(newTestObject("baz", "qux", "2.0")) 54 w.AddRow(newTestObject("qux", "quux", "3")) 55 w.Flush() 56 expected := "NAME NAMESPACE VERSION\n" + 57 "foo \x1b[32mbar\x1b[0m 1.0\n" + 58 "baz \x1b[32mqux\x1b[0m 2.0\n" + 59 "qux \x1b[32mquux\x1b[0m 3\n" 60 if got.String() != expected { 61 t.Errorf("got %q, want %q", got.String(), expected) 62 } 63 }