1
0
Fork 0
mirror of https://github.com/kyverno/kyverno.git synced 2025-03-09 17:37:12 +00:00
kyverno/cmd/cli/kubectl-kyverno/output/table/table_test.go
shuting fb9c66f455
feat(perf): add new linter prealloc to enforce slice declarations best practice (#10250)
* feat(perf): add new linter prealloc to enforce slice declarations best practice

Signed-off-by: ShutingZhao <shuting@nirmata.com>

* fix(linter): prealloac slices

Signed-off-by: ShutingZhao <shuting@nirmata.com>

---------

Signed-off-by: ShutingZhao <shuting@nirmata.com>
2024-05-20 14:46:35 +05:30

198 lines
3.7 KiB
Go

package table
import (
"reflect"
"testing"
"github.com/stretchr/testify/assert"
)
func TestTable_Add(t *testing.T) {
tests := []struct {
name string
RawRows []Row
rows []Row
want int
}{{
name: "nil",
RawRows: nil,
rows: nil,
want: 0,
}, {
name: "empty",
RawRows: nil,
rows: []Row{},
want: 0,
}, {
name: "two",
RawRows: nil,
rows: []Row{{}, {}},
want: 2,
}, {
name: "two + two",
RawRows: []Row{{}, {}},
rows: []Row{{}, {}},
want: 4,
}}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tr := &Table{
RawRows: tt.RawRows,
}
tr.Add(tt.rows...)
assert.Equal(t, len(tr.RawRows), tt.want)
})
}
}
func TestTable_AddFailed(t *testing.T) {
tests := []struct {
name string
RawRows []Row
rows []Row
want int
}{{
name: "nil",
RawRows: nil,
rows: nil,
want: 0,
}, {
name: "empty",
RawRows: nil,
rows: []Row{},
want: 0,
}, {
name: "one",
RawRows: nil,
rows: []Row{{}, {RowCompact: RowCompact{IsFailure: true}}},
want: 1,
}, {
name: "two + one",
RawRows: []Row{{}, {}},
rows: []Row{{RowCompact: RowCompact{IsFailure: true}}, {}},
want: 3,
}}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tr := &Table{
RawRows: tt.RawRows,
}
tr.AddFailed(tt.rows...)
assert.Equal(t, len(tr.RawRows), tt.want)
})
}
}
func TestTable_Rows(t *testing.T) {
var nilRows []Row
tests := []struct {
name string
RawRows []Row
detailed bool
want interface{}
}{{
name: "nil",
want: []RowCompact{},
}, {
name: "nil - detailed",
detailed: true,
want: nilRows,
}, {
name: "compact",
RawRows: []Row{{
RowCompact: RowCompact{
ID: 1,
Policy: "policy1",
Rule: "rule1",
Resource: "resource1",
Result: "result1",
Reason: "reason1",
},
Message: "message1",
}, {
RowCompact: RowCompact{
IsFailure: true,
ID: 2,
Policy: "policy2",
Rule: "rule2",
Resource: "resource2",
Result: "result2",
Reason: "reason2",
},
Message: "message2",
}},
want: []RowCompact{{
ID: 1,
Policy: "policy1",
Rule: "rule1",
Resource: "resource1",
Result: "result1",
Reason: "reason1",
}, {
IsFailure: true,
ID: 2,
Policy: "policy2",
Rule: "rule2",
Resource: "resource2",
Result: "result2",
Reason: "reason2",
}},
}, {
name: "detailed",
detailed: true,
RawRows: []Row{{
RowCompact: RowCompact{
ID: 1,
Policy: "policy1",
Rule: "rule1",
Resource: "resource1",
Result: "result1",
Reason: "reason1",
},
Message: "message1",
}, {
RowCompact: RowCompact{
IsFailure: true,
ID: 2,
Policy: "policy2",
Rule: "rule2",
Resource: "resource2",
Result: "result2",
Reason: "reason2",
},
Message: "message2",
}},
want: []Row{{
RowCompact: RowCompact{
ID: 1,
Policy: "policy1",
Rule: "rule1",
Resource: "resource1",
Result: "result1",
Reason: "reason1",
},
Message: "message1",
}, {
RowCompact: RowCompact{
IsFailure: true,
ID: 2,
Policy: "policy2",
Rule: "rule2",
Resource: "resource2",
Result: "result2",
Reason: "reason2",
},
Message: "message2",
}},
}}
for i, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tr := &Table{
RawRows: tt.RawRows,
}
if got := tr.Rows(tt.detailed); !reflect.DeepEqual(got, tt.want) {
t.Errorf("test=%v, Table.Rows() = %v, want %v", i, got, tt.want)
}
})
}
}