mirror of
https://github.com/kubeshark/kubeshark.git
synced 2025-08-01 00:35:31 +00:00
tests refactor (#216)
This commit is contained in:
parent
1d6ca9d392
commit
a427534605
@ -18,9 +18,7 @@ func TestEntryAddedCount(t *testing.T) {
|
|||||||
tests := []int{1, 5, 10, 100, 500, 1000}
|
tests := []int{1, 5, 10, 100, 500, 1000}
|
||||||
|
|
||||||
for _, entriesCount := range tests {
|
for _, entriesCount := range tests {
|
||||||
t.Run(fmt.Sprintf("EntriesCount%v", entriesCount), func(t *testing.T) {
|
t.Run(fmt.Sprintf("%d", entriesCount), func(t *testing.T) {
|
||||||
t.Cleanup(providers.ResetGeneralStats)
|
|
||||||
|
|
||||||
for i := 0; i < entriesCount; i++ {
|
for i := 0; i < entriesCount; i++ {
|
||||||
providers.EntryAdded()
|
providers.EntryAdded()
|
||||||
}
|
}
|
||||||
@ -30,6 +28,8 @@ func TestEntryAddedCount(t *testing.T) {
|
|||||||
if entriesStats.EntriesCount != entriesCount {
|
if entriesStats.EntriesCount != entriesCount {
|
||||||
t.Errorf("unexpected result - expected: %v, actual: %v", entriesCount, entriesStats.EntriesCount)
|
t.Errorf("unexpected result - expected: %v, actual: %v", entriesCount, entriesStats.EntriesCount)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
t.Cleanup(providers.ResetGeneralStats)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package config
|
package config
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"reflect"
|
"reflect"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
@ -22,186 +23,226 @@ type SectionMock struct {
|
|||||||
Test string `yaml:"test"`
|
Test string `yaml:"test"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type FieldSetValues struct {
|
||||||
|
SetValues []string
|
||||||
|
FieldName string
|
||||||
|
FieldValue interface{}
|
||||||
|
}
|
||||||
|
|
||||||
func TestMergeSetFlagNoSeparator(t *testing.T) {
|
func TestMergeSetFlagNoSeparator(t *testing.T) {
|
||||||
tests := [][]string{{""}, {"t"}, {"", "t"}, {"t", "test", "test:true"}, {"test", "test:true", "testing!", "true"}}
|
tests := []struct {
|
||||||
|
Name string
|
||||||
|
SetValues []string
|
||||||
|
}{
|
||||||
|
{Name: "empty value", SetValues: []string{""}},
|
||||||
|
{Name: "single char", SetValues: []string{"t"}},
|
||||||
|
{Name: "combine empty value and single char", SetValues: []string{"", "t"}},
|
||||||
|
{Name: "two values without separator", SetValues: []string{"test", "test:true"}},
|
||||||
|
{Name: "four values without separator", SetValues: []string{"test", "test:true", "testing!", "true"}},
|
||||||
|
}
|
||||||
|
|
||||||
for _, setValues := range tests {
|
for _, test := range tests {
|
||||||
configMock := ConfigMock{}
|
t.Run(test.Name, func(t *testing.T) {
|
||||||
configMockElemValue := reflect.ValueOf(&configMock).Elem()
|
configMock := ConfigMock{}
|
||||||
|
configMockElemValue := reflect.ValueOf(&configMock).Elem()
|
||||||
|
|
||||||
err := mergeSetFlag(configMockElemValue, setValues)
|
err := mergeSetFlag(configMockElemValue, test.SetValues)
|
||||||
|
|
||||||
if err == nil {
|
if err == nil {
|
||||||
t.Errorf("unexpected unhandled error - setValues: %v", setValues)
|
t.Errorf("unexpected unhandled error - SetValues: %v", test.SetValues)
|
||||||
continue
|
return
|
||||||
}
|
|
||||||
|
|
||||||
for i := 0; i < configMockElemValue.NumField(); i++ {
|
|
||||||
currentField := configMockElemValue.Type().Field(i)
|
|
||||||
currentFieldByName := configMockElemValue.FieldByName(currentField.Name)
|
|
||||||
|
|
||||||
if !currentFieldByName.IsZero() {
|
|
||||||
t.Errorf("unexpected value with not default value - setValues: %v", setValues)
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
for i := 0; i < configMockElemValue.NumField(); i++ {
|
||||||
|
currentField := configMockElemValue.Type().Field(i)
|
||||||
|
currentFieldByName := configMockElemValue.FieldByName(currentField.Name)
|
||||||
|
|
||||||
|
if !currentFieldByName.IsZero() {
|
||||||
|
t.Errorf("unexpected value with not default value - SetValues: %v", test.SetValues)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestMergeSetFlagInvalidFlagName(t *testing.T) {
|
func TestMergeSetFlagInvalidFlagName(t *testing.T) {
|
||||||
tests := [][]string{{"invalid_flag=true"}, {"section.invalid_flag=test"}, {"section=test"}, {"=true"}, {"invalid_flag=true", "config.invalid_flag=test", "section=test", "=true"}}
|
tests := []struct {
|
||||||
|
Name string
|
||||||
|
SetValues []string
|
||||||
|
}{
|
||||||
|
{Name: "invalid flag name", SetValues: []string{"invalid_flag=true"}},
|
||||||
|
{Name: "invalid flag name inside section struct", SetValues: []string{"section.invalid_flag=test"}},
|
||||||
|
{Name: "flag name is a struct", SetValues: []string{"section=test"}},
|
||||||
|
{Name: "empty flag name", SetValues: []string{"=true"}},
|
||||||
|
{Name: "four tests combined", SetValues: []string{"invalid_flag=true", "config.invalid_flag=test", "section=test", "=true"}},
|
||||||
|
}
|
||||||
|
|
||||||
for _, setValues := range tests {
|
for _, test := range tests {
|
||||||
configMock := ConfigMock{}
|
t.Run(test.Name, func(t *testing.T) {
|
||||||
configMockElemValue := reflect.ValueOf(&configMock).Elem()
|
configMock := ConfigMock{}
|
||||||
|
configMockElemValue := reflect.ValueOf(&configMock).Elem()
|
||||||
|
|
||||||
err := mergeSetFlag(configMockElemValue, setValues)
|
err := mergeSetFlag(configMockElemValue, test.SetValues)
|
||||||
|
|
||||||
if err == nil {
|
if err == nil {
|
||||||
t.Errorf("unexpected unhandled error - setValues: %v", setValues)
|
t.Errorf("unexpected unhandled error - SetValues: %v", test.SetValues)
|
||||||
continue
|
return
|
||||||
}
|
|
||||||
|
|
||||||
for i := 0; i < configMockElemValue.NumField(); i++ {
|
|
||||||
currentField := configMockElemValue.Type().Field(i)
|
|
||||||
currentFieldByName := configMockElemValue.FieldByName(currentField.Name)
|
|
||||||
|
|
||||||
if !currentFieldByName.IsZero() {
|
|
||||||
t.Errorf("unexpected case - setValues: %v", setValues)
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
for i := 0; i < configMockElemValue.NumField(); i++ {
|
||||||
|
currentField := configMockElemValue.Type().Field(i)
|
||||||
|
currentFieldByName := configMockElemValue.FieldByName(currentField.Name)
|
||||||
|
|
||||||
|
if !currentFieldByName.IsZero() {
|
||||||
|
t.Errorf("unexpected case - SetValues: %v", test.SetValues)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestMergeSetFlagInvalidFlagValue(t *testing.T) {
|
func TestMergeSetFlagInvalidFlagValue(t *testing.T) {
|
||||||
tests := [][]string{{"int-field=true"}, {"bool-field:5"}, {"uint-field=-1"}, {"int-slice-field=true"}, {"bool-slice-field=5"}, {"uint-slice-field=-1"}, {"int-field=6", "int-field=66"}}
|
tests := []struct {
|
||||||
|
Name string
|
||||||
|
SetValues []string
|
||||||
|
}{
|
||||||
|
{Name: "bool value to int field", SetValues: []string{"int-field=true"}},
|
||||||
|
{Name: "int value to bool field", SetValues: []string{"bool-field:5"}},
|
||||||
|
{Name: "int value to uint field", SetValues: []string{"uint-field=-1"}},
|
||||||
|
{Name: "bool value to int slice field", SetValues: []string{"int-slice-field=true"}},
|
||||||
|
{Name: "int value to bool slice field", SetValues: []string{"bool-slice-field=5"}},
|
||||||
|
{Name: "int value to uint slice field", SetValues: []string{"uint-slice-field=-1"}},
|
||||||
|
{Name: "int slice value to int field", SetValues: []string{"int-field=6", "int-field=66"}},
|
||||||
|
}
|
||||||
|
|
||||||
for _, setValues := range tests {
|
for _, test := range tests {
|
||||||
configMock := ConfigMock{}
|
t.Run(test.Name, func(t *testing.T) {
|
||||||
configMockElemValue := reflect.ValueOf(&configMock).Elem()
|
configMock := ConfigMock{}
|
||||||
|
configMockElemValue := reflect.ValueOf(&configMock).Elem()
|
||||||
|
|
||||||
err := mergeSetFlag(configMockElemValue, setValues)
|
err := mergeSetFlag(configMockElemValue, test.SetValues)
|
||||||
|
|
||||||
if err == nil {
|
if err == nil {
|
||||||
t.Errorf("unexpected unhandled error - setValues: %v", setValues)
|
t.Errorf("unexpected unhandled error - SetValues: %v", test.SetValues)
|
||||||
continue
|
return
|
||||||
}
|
|
||||||
|
|
||||||
for i := 0; i < configMockElemValue.NumField(); i++ {
|
|
||||||
currentField := configMockElemValue.Type().Field(i)
|
|
||||||
currentFieldByName := configMockElemValue.FieldByName(currentField.Name)
|
|
||||||
|
|
||||||
if !currentFieldByName.IsZero() {
|
|
||||||
t.Errorf("unexpected case - setValues: %v", setValues)
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
for i := 0; i < configMockElemValue.NumField(); i++ {
|
||||||
|
currentField := configMockElemValue.Type().Field(i)
|
||||||
|
currentFieldByName := configMockElemValue.FieldByName(currentField.Name)
|
||||||
|
|
||||||
|
if !currentFieldByName.IsZero() {
|
||||||
|
t.Errorf("unexpected case - SetValues: %v", test.SetValues)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestMergeSetFlagNotSliceValues(t *testing.T) {
|
func TestMergeSetFlagNotSliceValues(t *testing.T) {
|
||||||
tests := [][]struct {
|
tests := []struct {
|
||||||
SetValue string
|
Name string
|
||||||
FieldName string
|
FieldsSetValues []FieldSetValues
|
||||||
FieldValue interface{}
|
|
||||||
}{
|
}{
|
||||||
{{SetValue: "string-field=test", FieldName: "StringField", FieldValue: "test"}},
|
{Name: "string field", FieldsSetValues: []FieldSetValues{{SetValues: []string{"string-field=test"}, FieldName: "StringField", FieldValue: "test"}}},
|
||||||
{{SetValue: "int-field=6", FieldName: "IntField", FieldValue: 6}},
|
{Name: "int field", FieldsSetValues: []FieldSetValues{{SetValues: []string{"int-field=6"}, FieldName: "IntField", FieldValue: 6}}},
|
||||||
{{SetValue: "bool-field=true", FieldName: "BoolField", FieldValue: true}},
|
{Name: "bool field", FieldsSetValues: []FieldSetValues{{SetValues: []string{"bool-field=true"}, FieldName: "BoolField", FieldValue: true}}},
|
||||||
{{SetValue: "uint-field=6", FieldName: "UintField", FieldValue: uint(6)}},
|
{Name: "uint field", FieldsSetValues: []FieldSetValues{{SetValues: []string{"uint-field=6"}, FieldName: "UintField", FieldValue: uint(6)}}},
|
||||||
{
|
{Name: "four fields combined", FieldsSetValues: []FieldSetValues {
|
||||||
{SetValue: "string-field=test", FieldName: "StringField", FieldValue: "test"},
|
{SetValues: []string{"string-field=test"}, FieldName: "StringField", FieldValue: "test"},
|
||||||
{SetValue: "int-field=6", FieldName: "IntField", FieldValue: 6},
|
{SetValues: []string{"int-field=6"}, FieldName: "IntField", FieldValue: 6},
|
||||||
{SetValue: "bool-field=true", FieldName: "BoolField", FieldValue: true},
|
{SetValues: []string{"bool-field=true"}, FieldName: "BoolField", FieldValue: true},
|
||||||
{SetValue: "uint-field=6", FieldName: "UintField", FieldValue: uint(6)},
|
{SetValues: []string{"uint-field=6"}, FieldName: "UintField", FieldValue: uint(6)},
|
||||||
},
|
}},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, test := range tests {
|
for _, test := range tests {
|
||||||
configMock := ConfigMock{}
|
t.Run(test.Name, func(t *testing.T) {
|
||||||
configMockElemValue := reflect.ValueOf(&configMock).Elem()
|
configMock := ConfigMock{}
|
||||||
|
configMockElemValue := reflect.ValueOf(&configMock).Elem()
|
||||||
|
|
||||||
var setValues []string
|
var setValues []string
|
||||||
for _, setValueInfo := range test {
|
for _, fieldSetValues := range test.FieldsSetValues {
|
||||||
setValues = append(setValues, setValueInfo.SetValue)
|
setValues = append(setValues, fieldSetValues.SetValues...)
|
||||||
}
|
|
||||||
|
|
||||||
err := mergeSetFlag(configMockElemValue, setValues)
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
t.Errorf("unexpected error result - err: %v", err)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, setValueInfo := range test {
|
|
||||||
fieldValue := configMockElemValue.FieldByName(setValueInfo.FieldName).Interface()
|
|
||||||
if fieldValue != setValueInfo.FieldValue {
|
|
||||||
t.Errorf("unexpected result - expected: %v, actual: %v", setValueInfo.FieldValue, fieldValue)
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
err := mergeSetFlag(configMockElemValue, setValues)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("unexpected error result - err: %v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, fieldSetValues := range test.FieldsSetValues {
|
||||||
|
fieldValue := configMockElemValue.FieldByName(fieldSetValues.FieldName).Interface()
|
||||||
|
if fieldValue != fieldSetValues.FieldValue {
|
||||||
|
t.Errorf("unexpected result - expected: %v, actual: %v", fieldSetValues.FieldValue, fieldValue)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestMergeSetFlagSliceValues(t *testing.T) {
|
func TestMergeSetFlagSliceValues(t *testing.T) {
|
||||||
tests := [][]struct {
|
tests := []struct {
|
||||||
SetValues []string
|
Name string
|
||||||
FieldName string
|
FieldsSetValues []FieldSetValues
|
||||||
FieldValue interface{}
|
|
||||||
}{
|
}{
|
||||||
{{SetValues: []string{"string-slice-field=test"}, FieldName: "StringSliceField", FieldValue: []string{"test"}}},
|
{Name: "string slice field single value", FieldsSetValues: []FieldSetValues{{SetValues: []string{"string-slice-field=test"}, FieldName: "StringSliceField", FieldValue: []string{"test"}}}},
|
||||||
{{SetValues: []string{"int-slice-field=6"}, FieldName: "IntSliceField", FieldValue: []int{6}}},
|
{Name: "int slice field single value", FieldsSetValues: []FieldSetValues{{SetValues: []string{"int-slice-field=6"}, FieldName: "IntSliceField", FieldValue: []int{6}}}},
|
||||||
{{SetValues: []string{"bool-slice-field=true"}, FieldName: "BoolSliceField", FieldValue: []bool{true}}},
|
{Name: "bool slice field single value", FieldsSetValues: []FieldSetValues{{SetValues: []string{"bool-slice-field=true"}, FieldName: "BoolSliceField", FieldValue: []bool{true}}}},
|
||||||
{{SetValues: []string{"uint-slice-field=6"}, FieldName: "UintSliceField", FieldValue: []uint{uint(6)}}},
|
{Name: "uint slice field single value", FieldsSetValues: []FieldSetValues{{SetValues: []string{"uint-slice-field=6"}, FieldName: "UintSliceField", FieldValue: []uint{uint(6)}}}},
|
||||||
{
|
{Name: "four single value fields combined", FieldsSetValues: []FieldSetValues{
|
||||||
{SetValues: []string{"string-slice-field=test"}, FieldName: "StringSliceField", FieldValue: []string{"test"}},
|
{SetValues: []string{"string-slice-field=test"}, FieldName: "StringSliceField", FieldValue: []string{"test"}},
|
||||||
{SetValues: []string{"int-slice-field=6"}, FieldName: "IntSliceField", FieldValue: []int{6}},
|
{SetValues: []string{"int-slice-field=6"}, FieldName: "IntSliceField", FieldValue: []int{6}},
|
||||||
{SetValues: []string{"bool-slice-field=true"}, FieldName: "BoolSliceField", FieldValue: []bool{true}},
|
{SetValues: []string{"bool-slice-field=true"}, FieldName: "BoolSliceField", FieldValue: []bool{true}},
|
||||||
{SetValues: []string{"uint-slice-field=6"}, FieldName: "UintSliceField", FieldValue: []uint{uint(6)}},
|
{SetValues: []string{"uint-slice-field=6"}, FieldName: "UintSliceField", FieldValue: []uint{uint(6)}},
|
||||||
},
|
}},
|
||||||
{{SetValues: []string{"string-slice-field=test", "string-slice-field=test2"}, FieldName: "StringSliceField", FieldValue: []string{"test", "test2"}}},
|
{Name: "string slice field two values", FieldsSetValues: []FieldSetValues{{SetValues: []string{"string-slice-field=test", "string-slice-field=test2"}, FieldName: "StringSliceField", FieldValue: []string{"test", "test2"}}}},
|
||||||
{{SetValues: []string{"int-slice-field=6", "int-slice-field=66"}, FieldName: "IntSliceField", FieldValue: []int{6, 66}}},
|
{Name: "int slice field two values", FieldsSetValues: []FieldSetValues{{SetValues: []string{"int-slice-field=6", "int-slice-field=66"}, FieldName: "IntSliceField", FieldValue: []int{6, 66}}}},
|
||||||
{{SetValues: []string{"bool-slice-field=true", "bool-slice-field=false"}, FieldName: "BoolSliceField", FieldValue: []bool{true, false}}},
|
{Name: "bool slice field two values", FieldsSetValues: []FieldSetValues{{SetValues: []string{"bool-slice-field=true", "bool-slice-field=false"}, FieldName: "BoolSliceField", FieldValue: []bool{true, false}}}},
|
||||||
{{SetValues: []string{"uint-slice-field=6", "uint-slice-field=66"}, FieldName: "UintSliceField", FieldValue: []uint{uint(6), uint(66)}}},
|
{Name: "uint slice field two values", FieldsSetValues: []FieldSetValues{{SetValues: []string{"uint-slice-field=6", "uint-slice-field=66"}, FieldName: "UintSliceField", FieldValue: []uint{uint(6), uint(66)}}}},
|
||||||
{
|
{Name: "four two values fields combined", FieldsSetValues: []FieldSetValues{
|
||||||
{SetValues: []string{"string-slice-field=test", "string-slice-field=test2"}, FieldName: "StringSliceField", FieldValue: []string{"test", "test2"}},
|
{SetValues: []string{"string-slice-field=test", "string-slice-field=test2"}, FieldName: "StringSliceField", FieldValue: []string{"test", "test2"}},
|
||||||
{SetValues: []string{"int-slice-field=6", "int-slice-field=66"}, FieldName: "IntSliceField", FieldValue: []int{6, 66}},
|
{SetValues: []string{"int-slice-field=6", "int-slice-field=66"}, FieldName: "IntSliceField", FieldValue: []int{6, 66}},
|
||||||
{SetValues: []string{"bool-slice-field=true", "bool-slice-field=false"}, FieldName: "BoolSliceField", FieldValue: []bool{true, false}},
|
{SetValues: []string{"bool-slice-field=true", "bool-slice-field=false"}, FieldName: "BoolSliceField", FieldValue: []bool{true, false}},
|
||||||
{SetValues: []string{"uint-slice-field=6", "uint-slice-field=66"}, FieldName: "UintSliceField", FieldValue: []uint{uint(6), uint(66)}},
|
{SetValues: []string{"uint-slice-field=6", "uint-slice-field=66"}, FieldName: "UintSliceField", FieldValue: []uint{uint(6), uint(66)}},
|
||||||
},
|
}},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, test := range tests {
|
for _, test := range tests {
|
||||||
configMock := ConfigMock{}
|
t.Run(test.Name, func(t *testing.T) {
|
||||||
configMockElemValue := reflect.ValueOf(&configMock).Elem()
|
configMock := ConfigMock{}
|
||||||
|
configMockElemValue := reflect.ValueOf(&configMock).Elem()
|
||||||
|
|
||||||
var setValues []string
|
var setValues []string
|
||||||
for _, setValueInfo := range test {
|
for _, fieldSetValues := range test.FieldsSetValues {
|
||||||
for _, setValue := range setValueInfo.SetValues {
|
setValues = append(setValues, fieldSetValues.SetValues...)
|
||||||
setValues = append(setValues, setValue)
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
err := mergeSetFlag(configMockElemValue, setValues)
|
err := mergeSetFlag(configMockElemValue, setValues)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("unexpected error result - err: %v", err)
|
t.Errorf("unexpected error result - err: %v", err)
|
||||||
continue
|
return
|
||||||
}
|
|
||||||
|
|
||||||
for _, setValueInfo := range test {
|
|
||||||
fieldValue := configMockElemValue.FieldByName(setValueInfo.FieldName).Interface()
|
|
||||||
if !reflect.DeepEqual(fieldValue, setValueInfo.FieldValue) {
|
|
||||||
t.Errorf("unexpected result - expected: %v, actual: %v", setValueInfo.FieldValue, fieldValue)
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
for _, fieldSetValues := range test.FieldsSetValues {
|
||||||
|
fieldValue := configMockElemValue.FieldByName(fieldSetValues.FieldName).Interface()
|
||||||
|
if !reflect.DeepEqual(fieldValue, fieldSetValues.FieldValue) {
|
||||||
|
t.Errorf("unexpected result - expected: %v, actual: %v", fieldSetValues.FieldValue, fieldValue)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestMergeSetFlagMixValues(t *testing.T) {
|
func TestMergeSetFlagMixValues(t *testing.T) {
|
||||||
tests := [][]struct {
|
tests := []struct {
|
||||||
SetValues []string
|
Name string
|
||||||
FieldName string
|
FieldsSetValues []FieldSetValues
|
||||||
FieldValue interface{}
|
|
||||||
}{
|
}{
|
||||||
{
|
{Name: "single value all fields", FieldsSetValues: []FieldSetValues{
|
||||||
{SetValues: []string{"string-slice-field=test"}, FieldName: "StringSliceField", FieldValue: []string{"test"}},
|
{SetValues: []string{"string-slice-field=test"}, FieldName: "StringSliceField", FieldValue: []string{"test"}},
|
||||||
{SetValues: []string{"int-slice-field=6"}, FieldName: "IntSliceField", FieldValue: []int{6}},
|
{SetValues: []string{"int-slice-field=6"}, FieldName: "IntSliceField", FieldValue: []int{6}},
|
||||||
{SetValues: []string{"bool-slice-field=true"}, FieldName: "BoolSliceField", FieldValue: []bool{true}},
|
{SetValues: []string{"bool-slice-field=true"}, FieldName: "BoolSliceField", FieldValue: []bool{true}},
|
||||||
@ -210,8 +251,8 @@ func TestMergeSetFlagMixValues(t *testing.T) {
|
|||||||
{SetValues: []string{"int-field=6"}, FieldName: "IntField", FieldValue: 6},
|
{SetValues: []string{"int-field=6"}, FieldName: "IntField", FieldValue: 6},
|
||||||
{SetValues: []string{"bool-field=true"}, FieldName: "BoolField", FieldValue: true},
|
{SetValues: []string{"bool-field=true"}, FieldName: "BoolField", FieldValue: true},
|
||||||
{SetValues: []string{"uint-field=6"}, FieldName: "UintField", FieldValue: uint(6)},
|
{SetValues: []string{"uint-field=6"}, FieldName: "UintField", FieldValue: uint(6)},
|
||||||
},
|
}},
|
||||||
{
|
{Name: "two values slice fields and single value fields", FieldsSetValues: []FieldSetValues{
|
||||||
{SetValues: []string{"string-slice-field=test", "string-slice-field=test2"}, FieldName: "StringSliceField", FieldValue: []string{"test", "test2"}},
|
{SetValues: []string{"string-slice-field=test", "string-slice-field=test2"}, FieldName: "StringSliceField", FieldValue: []string{"test", "test2"}},
|
||||||
{SetValues: []string{"int-slice-field=6", "int-slice-field=66"}, FieldName: "IntSliceField", FieldValue: []int{6, 66}},
|
{SetValues: []string{"int-slice-field=6", "int-slice-field=66"}, FieldName: "IntSliceField", FieldValue: []int{6, 66}},
|
||||||
{SetValues: []string{"bool-slice-field=true", "bool-slice-field=false"}, FieldName: "BoolSliceField", FieldValue: []bool{true, false}},
|
{SetValues: []string{"bool-slice-field=true", "bool-slice-field=false"}, FieldName: "BoolSliceField", FieldValue: []bool{true, false}},
|
||||||
@ -220,33 +261,33 @@ func TestMergeSetFlagMixValues(t *testing.T) {
|
|||||||
{SetValues: []string{"int-field=6"}, FieldName: "IntField", FieldValue: 6},
|
{SetValues: []string{"int-field=6"}, FieldName: "IntField", FieldValue: 6},
|
||||||
{SetValues: []string{"bool-field=true"}, FieldName: "BoolField", FieldValue: true},
|
{SetValues: []string{"bool-field=true"}, FieldName: "BoolField", FieldValue: true},
|
||||||
{SetValues: []string{"uint-field=6"}, FieldName: "UintField", FieldValue: uint(6)},
|
{SetValues: []string{"uint-field=6"}, FieldName: "UintField", FieldValue: uint(6)},
|
||||||
},
|
}},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, test := range tests {
|
for _, test := range tests {
|
||||||
configMock := ConfigMock{}
|
t.Run(test.Name, func(t *testing.T) {
|
||||||
configMockElemValue := reflect.ValueOf(&configMock).Elem()
|
configMock := ConfigMock{}
|
||||||
|
configMockElemValue := reflect.ValueOf(&configMock).Elem()
|
||||||
|
|
||||||
var setValues []string
|
var setValues []string
|
||||||
for _, setValueInfo := range test {
|
for _, fieldSetValues := range test.FieldsSetValues {
|
||||||
for _, setValue := range setValueInfo.SetValues {
|
setValues = append(setValues, fieldSetValues.SetValues...)
|
||||||
setValues = append(setValues, setValue)
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
err := mergeSetFlag(configMockElemValue, setValues)
|
err := mergeSetFlag(configMockElemValue, setValues)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("unexpected error result - err: %v", err)
|
t.Errorf("unexpected error result - err: %v", err)
|
||||||
continue
|
return
|
||||||
}
|
|
||||||
|
|
||||||
for _, setValueInfo := range test {
|
|
||||||
fieldValue := configMockElemValue.FieldByName(setValueInfo.FieldName).Interface()
|
|
||||||
if !reflect.DeepEqual(fieldValue, setValueInfo.FieldValue) {
|
|
||||||
t.Errorf("unexpected result - expected: %v, actual: %v", setValueInfo.FieldValue, fieldValue)
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
for _, fieldSetValues := range test.FieldsSetValues {
|
||||||
|
fieldValue := configMockElemValue.FieldByName(fieldSetValues.FieldName).Interface()
|
||||||
|
if !reflect.DeepEqual(fieldValue, fieldSetValues.FieldValue) {
|
||||||
|
t.Errorf("unexpected result - expected: %v, actual: %v", fieldSetValues.FieldValue, fieldValue)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -283,16 +324,18 @@ func TestGetParsedValueValidValue(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for _, test := range tests {
|
for _, test := range tests {
|
||||||
parsedValue, err := getParsedValue(test.Kind, test.StringValue)
|
t.Run(fmt.Sprintf("%v %v", test.Kind, test.StringValue), func(t *testing.T) {
|
||||||
|
parsedValue, err := getParsedValue(test.Kind, test.StringValue)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("unexpected error result - err: %v", err)
|
t.Errorf("unexpected error result - err: %v", err)
|
||||||
continue
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if parsedValue.Interface() != test.ActualValue {
|
if parsedValue.Interface() != test.ActualValue {
|
||||||
t.Errorf("unexpected result - expected: %v, actual: %v", test.ActualValue, parsedValue)
|
t.Errorf("unexpected result - expected: %v, actual: %v", test.ActualValue, parsedValue)
|
||||||
}
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -326,15 +369,17 @@ func TestGetParsedValueInvalidValue(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for _, test := range tests {
|
for _, test := range tests {
|
||||||
parsedValue, err := getParsedValue(test.Kind, test.StringValue)
|
t.Run(fmt.Sprintf("%v %v", test.Kind, test.StringValue), func(t *testing.T) {
|
||||||
|
parsedValue, err := getParsedValue(test.Kind, test.StringValue)
|
||||||
|
|
||||||
if err == nil {
|
if err == nil {
|
||||||
t.Errorf("unexpected unhandled error - stringValue: %v, Kind: %v", test.StringValue, test.Kind)
|
t.Errorf("unexpected unhandled error - stringValue: %v, Kind: %v", test.StringValue, test.Kind)
|
||||||
continue
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if parsedValue != reflect.ValueOf(nil) {
|
if parsedValue != reflect.ValueOf(nil) {
|
||||||
t.Errorf("unexpected parsed value - parsedValue: %v", parsedValue)
|
t.Errorf("unexpected parsed value - parsedValue: %v", parsedValue)
|
||||||
}
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -15,9 +15,11 @@ func TestConfigWriteIgnoresReadonlyFields(t *testing.T) {
|
|||||||
|
|
||||||
configWithDefaults, _ := config.GetConfigWithDefaults()
|
configWithDefaults, _ := config.GetConfigWithDefaults()
|
||||||
for _, readonlyField := range readonlyFields {
|
for _, readonlyField := range readonlyFields {
|
||||||
if strings.Contains(configWithDefaults, readonlyField) {
|
t.Run(readonlyField, func(t *testing.T) {
|
||||||
t.Errorf("unexpected result - readonly field: %v, config: %v", readonlyField, configWithDefaults)
|
if strings.Contains(configWithDefaults, readonlyField) {
|
||||||
}
|
t.Errorf("unexpected result - readonly field: %v, config: %v", readonlyField, configWithDefaults)
|
||||||
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -7,76 +7,84 @@ import (
|
|||||||
|
|
||||||
func TestContainsExists(t *testing.T) {
|
func TestContainsExists(t *testing.T) {
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
slice []string
|
Slice []string
|
||||||
containsValue string
|
ContainsValue string
|
||||||
expected bool
|
Expected bool
|
||||||
}{
|
}{
|
||||||
{slice: []string{"apple", "orange", "banana", "grapes"}, containsValue: "apple", expected: true},
|
{Slice: []string{"apple", "orange", "banana", "grapes"}, ContainsValue: "apple", Expected: true},
|
||||||
{slice: []string{"apple", "orange", "banana", "grapes"}, containsValue: "orange", expected: true},
|
{Slice: []string{"apple", "orange", "banana", "grapes"}, ContainsValue: "orange", Expected: true},
|
||||||
{slice: []string{"apple", "orange", "banana", "grapes"}, containsValue: "banana", expected: true},
|
{Slice: []string{"apple", "orange", "banana", "grapes"}, ContainsValue: "banana", Expected: true},
|
||||||
{slice: []string{"apple", "orange", "banana", "grapes"}, containsValue: "grapes", expected: true},
|
{Slice: []string{"apple", "orange", "banana", "grapes"}, ContainsValue: "grapes", Expected: true},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, test := range tests {
|
for _, test := range tests {
|
||||||
actual := mizu.Contains(test.slice, test.containsValue)
|
t.Run(test.ContainsValue, func(t *testing.T) {
|
||||||
if actual != test.expected {
|
actual := mizu.Contains(test.Slice, test.ContainsValue)
|
||||||
t.Errorf("unexpected result - expected: %v, actual: %v", test.expected, actual)
|
if actual != test.Expected {
|
||||||
}
|
t.Errorf("unexpected result - Expected: %v, actual: %v", test.Expected, actual)
|
||||||
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestContainsNotExists(t *testing.T) {
|
func TestContainsNotExists(t *testing.T) {
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
slice []string
|
Slice []string
|
||||||
containsValue string
|
ContainsValue string
|
||||||
expected bool
|
Expected bool
|
||||||
}{
|
}{
|
||||||
{slice: []string{"apple", "orange", "banana", "grapes"}, containsValue: "cat", expected: false},
|
{Slice: []string{"apple", "orange", "banana", "grapes"}, ContainsValue: "cat", Expected: false},
|
||||||
{slice: []string{"apple", "orange", "banana", "grapes"}, containsValue: "dog", expected: false},
|
{Slice: []string{"apple", "orange", "banana", "grapes"}, ContainsValue: "dog", Expected: false},
|
||||||
{slice: []string{"apple", "orange", "banana", "grapes"}, containsValue: "apples", expected: false},
|
{Slice: []string{"apple", "orange", "banana", "grapes"}, ContainsValue: "apples", Expected: false},
|
||||||
{slice: []string{"apple", "orange", "banana", "grapes"}, containsValue: "rapes", expected: false},
|
{Slice: []string{"apple", "orange", "banana", "grapes"}, ContainsValue: "rapes", Expected: false},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, test := range tests {
|
for _, test := range tests {
|
||||||
actual := mizu.Contains(test.slice, test.containsValue)
|
t.Run(test.ContainsValue, func(t *testing.T) {
|
||||||
if actual != test.expected {
|
actual := mizu.Contains(test.Slice, test.ContainsValue)
|
||||||
t.Errorf("unexpected result - expected: %v, actual: %v", test.expected, actual)
|
if actual != test.Expected {
|
||||||
}
|
t.Errorf("unexpected result - Expected: %v, actual: %v", test.Expected, actual)
|
||||||
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestContainsEmptySlice(t *testing.T) {
|
func TestContainsEmptySlice(t *testing.T) {
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
slice []string
|
Slice []string
|
||||||
containsValue string
|
ContainsValue string
|
||||||
expected bool
|
Expected bool
|
||||||
}{
|
}{
|
||||||
{slice: []string{}, containsValue: "cat", expected: false},
|
{Slice: []string{}, ContainsValue: "cat", Expected: false},
|
||||||
{slice: []string{}, containsValue: "dog", expected: false},
|
{Slice: []string{}, ContainsValue: "dog", Expected: false},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, test := range tests {
|
for _, test := range tests {
|
||||||
actual := mizu.Contains(test.slice, test.containsValue)
|
t.Run(test.ContainsValue, func(t *testing.T) {
|
||||||
if actual != test.expected {
|
actual := mizu.Contains(test.Slice, test.ContainsValue)
|
||||||
t.Errorf("unexpected result - expected: %v, actual: %v", test.expected, actual)
|
if actual != test.Expected {
|
||||||
}
|
t.Errorf("unexpected result - Expected: %v, actual: %v", test.Expected, actual)
|
||||||
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestContainsNilSlice(t *testing.T) {
|
func TestContainsNilSlice(t *testing.T) {
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
slice []string
|
Slice []string
|
||||||
containsValue string
|
ContainsValue string
|
||||||
expected bool
|
Expected bool
|
||||||
}{
|
}{
|
||||||
{slice: nil, containsValue: "cat", expected: false},
|
{Slice: nil, ContainsValue: "cat", Expected: false},
|
||||||
{slice: nil, containsValue: "dog", expected: false},
|
{Slice: nil, ContainsValue: "dog", Expected: false},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, test := range tests {
|
for _, test := range tests {
|
||||||
actual := mizu.Contains(test.slice, test.containsValue)
|
t.Run(test.ContainsValue, func(t *testing.T) {
|
||||||
if actual != test.expected {
|
actual := mizu.Contains(test.Slice, test.ContainsValue)
|
||||||
t.Errorf("unexpected result - expected: %v, actual: %v", test.expected, actual)
|
if actual != test.Expected {
|
||||||
}
|
t.Errorf("unexpected result - Expected: %v, actual: %v", test.Expected, actual)
|
||||||
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user