-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed bug in flagutil package and added tests (#15046)
Signed-off-by: VaibhavMalik4187 <[email protected]>
- Loading branch information
1 parent
4d0c066
commit 079cdda
Showing
5 changed files
with
212 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/* | ||
Copyright 2024 The Vitess Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package flagutil | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestStringEnum(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
initialValue string | ||
choices []string | ||
caseInsensitive bool | ||
secondValue string | ||
expectedErr string | ||
}{ | ||
{ | ||
name: "valid set call", | ||
initialValue: "mango", | ||
choices: []string{"apple", "mango", "kiwi"}, | ||
caseInsensitive: true, | ||
secondValue: "kiwi", | ||
}, | ||
{ | ||
name: "invalid set call", | ||
initialValue: "apple", | ||
choices: []string{"apple", "mango", "kiwi"}, | ||
caseInsensitive: false, | ||
secondValue: "banana", | ||
expectedErr: "invalid choice for enum (valid choices: [apple kiwi mango])", | ||
}, | ||
{ | ||
name: "invalid set call case insensitive", | ||
initialValue: "apple", | ||
choices: []string{"apple", "kiwi"}, | ||
caseInsensitive: true, | ||
secondValue: "banana", | ||
expectedErr: "invalid choice for enum (valid choices: [apple kiwi] [case insensitive])", | ||
}, | ||
} | ||
for _, tt := range tests { | ||
tt := tt | ||
t.Run(tt.name, func(t *testing.T) { | ||
var enum *StringEnum | ||
if tt.caseInsensitive { | ||
enum = NewCaseInsensitiveStringEnum(tt.name, tt.initialValue, tt.choices) | ||
} else { | ||
enum = NewStringEnum(tt.name, tt.initialValue, tt.choices) | ||
} | ||
|
||
require.Equal(t, "string", enum.Type()) | ||
err := enum.Set(tt.secondValue) | ||
if tt.expectedErr == "" { | ||
require.NoError(t, err) | ||
require.Equal(t, tt.secondValue, enum.String()) | ||
} else { | ||
require.ErrorContains(t, err, tt.expectedErr) | ||
require.Equal(t, tt.initialValue, enum.String()) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* | ||
Copyright 2024 The Vitess Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package flagutil | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestNewOptionalFloat64(t *testing.T) { | ||
fl := NewOptionalFloat64(4.187) | ||
require.NotEmpty(t, fl) | ||
require.Equal(t, false, fl.IsSet()) | ||
|
||
require.Equal(t, "4.187", fl.String()) | ||
require.Equal(t, "float64", fl.Type()) | ||
|
||
err := fl.Set("invalid value") | ||
require.ErrorContains(t, err, "parse error") | ||
|
||
err = fl.Set("7.77") | ||
require.NoError(t, err) | ||
require.Equal(t, 7.77, fl.Get()) | ||
require.Equal(t, true, fl.IsSet()) | ||
|
||
err = fl.Set("1e1000") | ||
require.ErrorContains(t, err, "value out of range") | ||
} | ||
|
||
func TestNewOptionalString(t *testing.T) { | ||
optStr := NewOptionalString("4.187") | ||
require.NotEmpty(t, optStr) | ||
require.Equal(t, false, optStr.IsSet()) | ||
|
||
require.Equal(t, "4.187", optStr.String()) | ||
require.Equal(t, "string", optStr.Type()) | ||
|
||
err := optStr.Set("value") | ||
require.NoError(t, err) | ||
|
||
require.Equal(t, "value", optStr.Get()) | ||
require.Equal(t, true, optStr.IsSet()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/* | ||
Copyright 2019 The Vitess Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package flagutil | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestStringSetFlag(t *testing.T) { | ||
strSetFlag := StringSetFlag{} | ||
set := strSetFlag.ToSet() | ||
require.Empty(t, set) | ||
|
||
set = set.Insert("mango", "apple", "mango") | ||
strSetFlag.set = set | ||
|
||
require.Equal(t, "StringSetFlag", strSetFlag.Type()) | ||
require.Equal(t, "apple, mango", strSetFlag.String()) | ||
|
||
err := strSetFlag.Set("guvava") | ||
require.NoError(t, err) | ||
require.Equal(t, "apple, guvava, mango", strSetFlag.String()) | ||
|
||
require.NotEmpty(t, strSetFlag.ToSet()) | ||
} | ||
|
||
func TestStringSetFlagWithEmptySet(t *testing.T) { | ||
strSetFlag := StringSetFlag{} | ||
require.Equal(t, "", strSetFlag.String()) | ||
|
||
err := strSetFlag.Set("tmp") | ||
require.NoError(t, err) | ||
require.Empty(t, strSetFlag.ToSet()) | ||
|
||
err = strSetFlag.Set("guvava") | ||
require.NoError(t, err) | ||
require.Equal(t, "guvava", strSetFlag.String()) | ||
} |