Skip to content

Commit

Permalink
multiflag: add simple testcases
Browse files Browse the repository at this point in the history
Signed-off-by: Mustafa Abdelrahman <[email protected]>
  • Loading branch information
MustafaSaber committed Nov 25, 2024
1 parent f6f9efb commit 8760d94
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 28 deletions.
9 changes: 0 additions & 9 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"github.com/zalando/skipper/filters/openpolicyagent"
"github.com/zalando/skipper/net"
"github.com/zalando/skipper/proxy"
"gopkg.in/yaml.v2"

"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
Expand Down Expand Up @@ -482,11 +481,3 @@ func TestDeprecatedFlags(t *testing.T) {
}
}
}

func TestMultiFlagYamlErr(t *testing.T) {
m := &multiFlag{}
err := yaml.Unmarshal([]byte(`foo=bar`), m)
if err == nil {
t.Error("Failed to get error on wrong yaml input")
}
}
41 changes: 22 additions & 19 deletions config/mapflags_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ package config
import (
"testing"

"github.com/google/go-cmp/cmp"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gopkg.in/yaml.v2"
)

Expand Down Expand Up @@ -83,7 +84,7 @@ func Test_mapFlags_Set_nil(t *testing.T) {
}

func Test_mapFlags_UnmarshalYAML(t *testing.T) {
tests := []struct {
for _, tt := range []struct {
name string
yml string
wantErr bool
Expand All @@ -102,30 +103,32 @@ key3: k3=10s`,
"key3": "k3=10s",
},
},
} {
t.Run(tt.name+"_valid", func(t *testing.T) {
mf := &mapFlags{}

err := yaml.Unmarshal([]byte(tt.yml), mf)
require.NoError(t, err)
assert.Equal(t, tt.want, mf.values)
})
}

for _, tt := range []struct {
name string
yml string
}{
{
name: "test mapFlags with yaml error",
yml: `---
foo=bar`,
wantErr: true,
want: nil,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
} {
t.Run(tt.name+"_invalid", func(t *testing.T) {
mf := &mapFlags{}

if err := yaml.Unmarshal([]byte(tt.yml), mf); (err != nil) != tt.wantErr {
t.Errorf("mapFlags.UnmarshalYAML() error = %v, wantErr %v", err, tt.wantErr)
}
if !tt.wantErr {
if len(mf.values) != len(tt.want) {
t.Errorf("Failed to have mapFlags created: %d != %d", len(mf.values), len(tt.want))
}

if cmp.Diff(mf.values, tt.want) != "" {
t.Errorf("mapFlags.UnmarshalYAML() got %v, want %v", mf.values, tt.want)
}
}
err := yaml.Unmarshal([]byte(tt.yml), mf)
assert.Error(t, err)
assert.Nil(t, mf.values)
})
}
}
56 changes: 56 additions & 0 deletions config/multiflag_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package config

import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gopkg.in/yaml.v2"
)

func TestMultiFlagSet(t *testing.T) {
for _, tc := range []struct {
name string
args string
values string
wantErr bool
}{
{
name: "single value",
args: "foo=bar",
values: "foo=bar",
wantErr: false,
},
{
name: "multiple values",
args: "foo=bar foo=baz foo=qux bar=baz",
values: "foo=bar foo=baz foo=qux bar=baz",
wantErr: false,
},
} {
t.Run(tc.name, func(t *testing.T) {
multiFlag := &multiFlag{}
err := multiFlag.Set(tc.args)
require.Condition(t, func() bool {
if (err != nil) && tc.wantErr == false {
t.Logf("set error: %v", err)
return false
} else if (err == nil) && tc.wantErr == true {
t.Logf("expected error, got nil")
return false
} else if err != nil {
return false
}
return true
}, "error: %v, wantErr: %v", err, tc.wantErr)

assert.Equal(t, tc.values, multiFlag.String())
})
}
}

func TestMultiFlagYamlErr(t *testing.T) {
m := &multiFlag{}
err := yaml.Unmarshal([]byte(`-foo=bar`), m)
require.Error(t, err, "Failed to get error on wrong yaml input")
}

0 comments on commit 8760d94

Please sign in to comment.