-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: add test for getPrimitiveValue util
- Loading branch information
Dat Nguyen
committed
Dec 15, 2024
1 parent
0f275e9
commit fab3464
Showing
4 changed files
with
137 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
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,132 @@ | ||
package mold | ||
|
||
import ( | ||
"math" | ||
"reflect" | ||
"testing" | ||
|
||
. "github.com/go-playground/assert/v2" | ||
) | ||
|
||
func TestGetPrimitiveValue(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
typ reflect.Kind | ||
value string | ||
expected reflect.Value | ||
expectError bool | ||
}{ | ||
{ | ||
name: "string", | ||
typ: reflect.String, | ||
value: "test", | ||
expected: reflect.ValueOf("test"), | ||
}, | ||
{ | ||
name: "int", | ||
typ: reflect.Int, | ||
value: "123", | ||
expected: reflect.ValueOf(123), | ||
}, | ||
{ | ||
name: "int8", | ||
typ: reflect.Int8, | ||
value: "123", | ||
expected: reflect.ValueOf(int8(123)), | ||
}, | ||
{ | ||
name: "int16", | ||
typ: reflect.Int16, | ||
value: "123", | ||
expected: reflect.ValueOf(int16(123)), | ||
}, | ||
{ | ||
name: "bool", | ||
typ: reflect.Bool, | ||
value: "true", | ||
expected: reflect.ValueOf(true), | ||
}, | ||
{ | ||
name: "error", | ||
typ: reflect.Int, | ||
value: "abc", | ||
expectError: true, | ||
}, | ||
{ | ||
name: "unsupported type", | ||
typ: reflect.Struct, | ||
value: "abc", | ||
expectError: true, | ||
}, | ||
{ | ||
name: "uint", | ||
typ: reflect.Uint, | ||
value: "123", | ||
expected: reflect.ValueOf(uint(123)), | ||
}, | ||
{ | ||
name: "uint8", | ||
typ: reflect.Uint8, | ||
value: "123", | ||
expected: reflect.ValueOf(uint8(123)), | ||
}, | ||
{ | ||
name: "uint16", | ||
typ: reflect.Uint16, | ||
value: "123", | ||
expected: reflect.ValueOf(uint16(123)), | ||
}, | ||
{ | ||
name: "uint32", | ||
typ: reflect.Uint32, | ||
value: "123", | ||
expected: reflect.ValueOf(uint32(123)), | ||
}, | ||
{ | ||
name: "uint64", | ||
typ: reflect.Uint64, | ||
value: "123", | ||
expected: reflect.ValueOf(uint64(123)), | ||
}, | ||
{ | ||
name: "float32", | ||
typ: reflect.Float32, | ||
value: "123.45", | ||
expected: reflect.ValueOf(float32(123.45)), | ||
}, | ||
{ | ||
name: "float64", | ||
typ: reflect.Float64, | ||
value: "123.45", | ||
expected: reflect.ValueOf(float64(123.45)), | ||
}, | ||
} | ||
|
||
for _, tc := range tests { | ||
t.Run(tc.name, func(t *testing.T) { | ||
actual, err := GetPrimitiveValue(tc.typ, tc.value) | ||
if tc.expectError { | ||
NotEqual(t, nil, err) | ||
} else { | ||
Equal(t, nil, err) | ||
switch tc.typ { | ||
case reflect.String: | ||
Equal(t, tc.expected.String(), actual.String()) | ||
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: | ||
Equal(t, tc.expected.Int(), actual.Int()) | ||
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: | ||
Equal(t, tc.expected.Uint(), actual.Uint()) | ||
case reflect.Float32, reflect.Float64: | ||
// could not assert equal float because of precision issues | ||
// so we just check in 4 decimal places | ||
decimalMask := math.Pow(10, 4) | ||
Equal(t, math.Round(tc.expected.Float()*decimalMask), math.Round(actual.Float()*decimalMask)) | ||
case reflect.Bool: | ||
Equal(t, tc.expected.Bool(), actual.Bool()) | ||
default: | ||
Equal(t, tc.expected.Interface(), actual.Interface()) | ||
} | ||
} | ||
}) | ||
} | ||
} |