-
Notifications
You must be signed in to change notification settings - Fork 223
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(app): add test coverage to FindStructField (#1769)
Signed-off-by: Artur Troian <[email protected]>
- Loading branch information
Showing
2 changed files
with
73 additions
and
0 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,65 @@ | ||
package types | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestFindStructFieldEmptyField(t *testing.T) { | ||
type testType struct { | ||
Val string | ||
} | ||
testStruct := testType{} | ||
|
||
_, err := FindStructField[string](testStruct, "") | ||
require.EqualError(t, err, ErrEmptyFieldName.Error()) | ||
} | ||
|
||
func TestFindStructFieldObjectAsValue(t *testing.T) { | ||
type testType struct { | ||
Val string | ||
} | ||
testStruct := testType{} | ||
|
||
val, err := FindStructField[string](testStruct, "Val") | ||
require.NoError(t, err) | ||
require.Equal(t, "", val) | ||
} | ||
|
||
func TestFindStructFieldObjectAsPointer(t *testing.T) { | ||
type testType struct { | ||
Val string | ||
} | ||
testStruct := testType{ | ||
Val: "testVal", | ||
} | ||
|
||
val, err := FindStructField[string](&testStruct, "Val") | ||
require.NoError(t, err) | ||
require.Equal(t, "testVal", val) | ||
} | ||
|
||
func TestFindStructFieldUnknownField(t *testing.T) { | ||
type testType struct { | ||
Val string | ||
} | ||
testStruct := testType{ | ||
Val: "testVal", | ||
} | ||
|
||
_, err := FindStructField[string](&testStruct, "Vals") | ||
require.Error(t, err) | ||
} | ||
|
||
func TestFindStructFieldNonMatchingType(t *testing.T) { | ||
type testType struct { | ||
Val string | ||
} | ||
testStruct := testType{ | ||
Val: "testVal", | ||
} | ||
|
||
_, err := FindStructField[int](&testStruct, "Vals") | ||
require.Error(t, err) | ||
} |