Commit ae4c9d71 authored by Guoliang Wang's avatar Guoliang Wang

use subtest for table units (pkg/printers)

parent e24fd8ef
......@@ -47,20 +47,22 @@ func TestMassageJSONPath(t *testing.T) {
{input: "{{foo.bar}", expectErr: true},
}
for _, test := range tests {
t.Run(test.input, func(t *testing.T) {
output, err := printers.RelaxedJSONPathExpression(test.input)
if err != nil && !test.expectErr {
t.Errorf("unexpected error: %v", err)
continue
return
}
if test.expectErr {
if err == nil {
t.Error("unexpected non-error")
}
continue
return
}
if output != test.expectedOutput {
t.Errorf("input: %s, expected: %s, saw: %s", test.input, test.expectedOutput, output)
}
})
}
}
......@@ -113,16 +115,17 @@ func TestNewColumnPrinterFromSpec(t *testing.T) {
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
printer, err := printers.NewCustomColumnsPrinterFromSpec(test.spec, legacyscheme.Codecs.UniversalDecoder(), test.noHeaders)
if test.expectErr {
if err == nil {
t.Errorf("[%s] unexpected non-error", test.name)
}
continue
return
}
if !test.expectErr && err != nil {
t.Errorf("[%s] unexpected error: %v", test.name, err)
continue
return
}
if test.noHeaders {
buffer := &bytes.Buffer{}
......@@ -139,7 +142,7 @@ func TestNewColumnPrinterFromSpec(t *testing.T) {
} else if !reflect.DeepEqual(test.expectedColumns, printer.Columns) {
t.Errorf("[%s]\nexpected:\n%v\nsaw:\n%v\n", test.name, test.expectedColumns, printer.Columns)
}
})
}
}
......@@ -215,23 +218,24 @@ func TestNewColumnPrinterFromTemplate(t *testing.T) {
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
reader := bytes.NewBufferString(test.spec)
printer, err := printers.NewCustomColumnsPrinterFromTemplate(reader, legacyscheme.Codecs.UniversalDecoder())
if test.expectErr {
if err == nil {
t.Errorf("[%s] unexpected non-error", test.name)
}
continue
return
}
if !test.expectErr && err != nil {
t.Errorf("[%s] unexpected error: %v", test.name, err)
continue
return
}
if !reflect.DeepEqual(test.expectedColumns, printer.Columns) {
t.Errorf("[%s]\nexpected:\n%v\nsaw:\n%v\n", test.name, test.expectedColumns, printer.Columns)
}
})
}
}
......@@ -310,6 +314,7 @@ foo baz <none>
}
for _, test := range tests {
t.Run(test.expectedOutput, func(t *testing.T) {
printer := &printers.CustomColumnsPrinter{
Columns: test.columns,
Decoder: legacyscheme.Codecs.UniversalDecoder(),
......@@ -321,6 +326,7 @@ foo baz <none>
if buffer.String() != test.expectedOutput {
t.Errorf("\nexpected:\n'%s'\nsaw\n'%s'\n", test.expectedOutput, buffer.String())
}
})
}
}
......
......@@ -48,7 +48,8 @@ func testPrintNamespace(obj *api.Namespace, options PrintOptions) ([]metav1beta1
func TestPrintRowsForHandlerEntry(t *testing.T) {
printFunc := reflect.ValueOf(testPrintNamespace)
testCase := map[string]struct {
testCase := []struct {
name string
h *handlerEntry
opt PrintOptions
obj runtime.Object
......@@ -56,7 +57,8 @@ func TestPrintRowsForHandlerEntry(t *testing.T) {
expectOut string
expectErr string
}{
"no tablecolumndefinition and includeheader flase": {
{
name: "no tablecolumndefinition and includeheader flase",
h: &handlerEntry{
columnDefinitions: []metav1beta1.TableColumnDefinition{},
printRows: true,
......@@ -69,7 +71,8 @@ func TestPrintRowsForHandlerEntry(t *testing.T) {
includeHeader: false,
expectOut: "test\t\t<unknow>\n",
},
"no tablecolumndefinition and includeheader true": {
{
name: "no tablecolumndefinition and includeheader true",
h: &handlerEntry{
columnDefinitions: []metav1beta1.TableColumnDefinition{},
printRows: true,
......@@ -82,7 +85,8 @@ func TestPrintRowsForHandlerEntry(t *testing.T) {
includeHeader: true,
expectOut: "\ntest\t\t<unknow>\n",
},
"have tablecolumndefinition and includeheader true": {
{
name: "have tablecolumndefinition and includeheader true",
h: &handlerEntry{
columnDefinitions: testNamespaceColumnDefinitions,
printRows: true,
......@@ -95,7 +99,8 @@ func TestPrintRowsForHandlerEntry(t *testing.T) {
includeHeader: true,
expectOut: "NAME\tSTATUS\tAGE\ntest\t\t<unknow>\n",
},
"print namespace and withnamespace true, should not print header": {
{
name: "print namespace and withnamespace true, should not print header",
h: &handlerEntry{
columnDefinitions: testNamespaceColumnDefinitions,
printRows: true,
......@@ -112,16 +117,18 @@ func TestPrintRowsForHandlerEntry(t *testing.T) {
expectErr: "namespace is not namespaced",
},
}
for name, test := range testCase {
for _, test := range testCase {
t.Run(test.name, func(t *testing.T) {
buffer := &bytes.Buffer{}
err := printRowsForHandlerEntry(buffer, test.h, test.obj, test.opt, test.includeHeader)
if err != nil {
if err.Error() != test.expectErr {
t.Errorf("[%s]expect:\n %v\n but got:\n %v\n", name, test.expectErr, err)
t.Errorf("[%s]expect:\n %v\n but got:\n %v\n", test.name, test.expectErr, err)
}
}
if test.expectOut != buffer.String() {
t.Errorf("[%s]expect:\n %v\n but got:\n %v\n", name, test.expectOut, buffer.String())
t.Errorf("[%s]expect:\n %v\n but got:\n %v\n", test.name, test.expectOut, buffer.String())
}
})
}
}
......@@ -26,13 +26,15 @@ import (
)
func TestTemplate(t *testing.T) {
testCase := map[string]struct {
testCase := []struct {
name string
template string
obj runtime.Object
expectOut string
expectErr func(error) (string, bool)
}{
"support base64 decoding of secret data": {
{
name: "support base64 decoding of secret data",
template: "{{ .data.username | base64decode }}",
obj: &v1.Secret{
Data: map[string][]byte{
......@@ -41,7 +43,8 @@ func TestTemplate(t *testing.T) {
},
expectOut: "hunter",
},
"test error path for base64 decoding": {
{
name: "test error path for base64 decoding",
template: "{{ .data.username | base64decode }}",
obj: &badlyMarshaledSecret{},
expectErr: func(err error) (string, bool) {
......@@ -50,41 +53,43 @@ func TestTemplate(t *testing.T) {
},
},
}
for name, test := range testCase {
for _, test := range testCase {
t.Run(test.name, func(t *testing.T) {
buffer := &bytes.Buffer{}
p, err := NewGoTemplatePrinter([]byte(test.template))
if err != nil {
if test.expectErr == nil {
t.Errorf("[%s]expected success but got:\n %v\n", name, err)
continue
t.Errorf("[%s]expected success but got:\n %v\n", test.name, err)
return
}
if expected, ok := test.expectErr(err); !ok {
t.Errorf("[%s]expect:\n %v\n but got:\n %v\n", name, expected, err)
t.Errorf("[%s]expect:\n %v\n but got:\n %v\n", test.name, expected, err)
}
continue
return
}
err = p.PrintObj(test.obj, buffer)
if err != nil {
if test.expectErr == nil {
t.Errorf("[%s]expected success but got:\n %v\n", name, err)
continue
t.Errorf("[%s]expected success but got:\n %v\n", test.name, err)
return
}
if expected, ok := test.expectErr(err); !ok {
t.Errorf("[%s]expect:\n %v\n but got:\n %v\n", name, expected, err)
t.Errorf("[%s]expect:\n %v\n but got:\n %v\n", test.name, expected, err)
}
continue
return
}
if test.expectErr != nil {
t.Errorf("[%s]expect:\n error\n but got:\n no error\n", name)
continue
t.Errorf("[%s]expect:\n error\n but got:\n no error\n", test.name)
return
}
if test.expectOut != buffer.String() {
t.Errorf("[%s]expect:\n %v\n but got:\n %v\n", name, test.expectOut, buffer.String())
t.Errorf("[%s]expect:\n %v\n but got:\n %v\n", test.name, test.expectOut, buffer.String())
}
})
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment