Skip to content

Commit

Permalink
more unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
axenteoctavian committed Jan 22, 2024
1 parent d612da4 commit 883b421
Show file tree
Hide file tree
Showing 6 changed files with 198 additions and 23 deletions.
52 changes: 52 additions & 0 deletions common/reflectcommon/export_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package reflectcommon

import "reflect"

func FitsWithinSignedIntegerRange(value reflect.Value, targetType reflect.Type) bool {
min, err := getMinInt(targetType)
if err != nil {
return false
}
max, err := getMaxInt(targetType)
if err != nil {
return false
}

switch value.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return value.Int() >= min && value.Int() <= max
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return value.Uint() <= uint64(max)
}

return false
}

func FitsWithinUnsignedIntegerRange(value reflect.Value, targetType reflect.Type) bool {
max, err := getMaxUint(targetType)
if err != nil {
return false
}

switch value.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return value.Int() >= 0 && uint64(value.Int()) <= max
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return value.Uint() <= max
}

return false
}

func FitsWithinFloatRange(value reflect.Value, targetType reflect.Type) bool {
min, err := getMinFloat(targetType)
if err != nil {
return false
}
max, err := getMaxFloat(targetType)
if err != nil {
return false
}

return value.Float() >= min && value.Float() <= max
}
32 changes: 19 additions & 13 deletions common/reflectcommon/structFieldsUpdate.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ func trySetTheNewValue(value *reflect.Value, newValue interface{}) error {

return trySetStructValue(value, structVal)
default:
return fmt.Errorf("unsupported type <%s> when trying to set the value <%s>", valueKind, newValue)
return fmt.Errorf("unsupported type <%s> when trying to set the value '%v' of type <%s>", valueKind, newValue, reflect.TypeOf(newValue))
}
return nil
}
Expand Down Expand Up @@ -314,44 +314,50 @@ func fitsWithinFloatRange(value reflect.Value, targetType reflect.Type) bool {

func getMinInt(targetType reflect.Type) (int64, error) {
switch targetType.Kind() {
case reflect.Int, reflect.Int64:
case reflect.Int:
return math.MinInt, nil
case reflect.Int64:
return math.MinInt64, nil
case reflect.Int8:
return int64(math.MinInt8), nil
return math.MinInt8, nil
case reflect.Int16:
return int64(math.MinInt16), nil
return math.MinInt16, nil
case reflect.Int32:
return int64(math.MinInt32), nil
return math.MinInt32, nil
default:
return 0, fmt.Errorf("target type is not integer")
}
}

func getMaxInt(targetType reflect.Type) (int64, error) {
switch targetType.Kind() {
case reflect.Int, reflect.Int64:
case reflect.Int:
return math.MaxInt, nil
case reflect.Int64:
return math.MaxInt64, nil
case reflect.Int8:
return int64(math.MaxInt8), nil
return math.MaxInt8, nil
case reflect.Int16:
return int64(math.MaxInt16), nil
return math.MaxInt16, nil
case reflect.Int32:
return int64(math.MaxInt32), nil
return math.MaxInt32, nil
default:
return 0, fmt.Errorf("target type is not integer")

Check warning on line 345 in common/reflectcommon/structFieldsUpdate.go

View check run for this annotation

Codecov / codecov/patch

common/reflectcommon/structFieldsUpdate.go#L344-L345

Added lines #L344 - L345 were not covered by tests
}
}

func getMaxUint(targetType reflect.Type) (uint64, error) {
switch targetType.Kind() {
case reflect.Uint, reflect.Uint64:
case reflect.Uint:
return math.MaxUint, nil
case reflect.Uint64:
return math.MaxUint64, nil
case reflect.Uint8:
return uint64(math.MaxUint8), nil
return math.MaxUint8, nil
case reflect.Uint16:
return uint64(math.MaxUint16), nil
return math.MaxUint16, nil
case reflect.Uint32:
return uint64(math.MaxUint32), nil
return math.MaxUint32, nil
default:
return 0, fmt.Errorf("taget type is not unsigned integer")
}
Expand Down
116 changes: 110 additions & 6 deletions common/reflectcommon/structFieldsUpdate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package reflectcommon

import (
"fmt"
"reflect"
"testing"

"github.com/multiversx/mx-chain-core-go/core"
Expand Down Expand Up @@ -436,6 +437,77 @@ func TestAdaptStructureValueBasedOnPath(t *testing.T) {
require.Equal(t, err.Error(), "unable to cast value '1' of type <int> to type <string>")
})

t.Run("should error for unsupported type", func(t *testing.T) {
t.Parallel()

testConfig, err := loadTestConfig("../../testscommon/toml/config.toml")
require.NoError(t, err)

expectedNewValue := make(map[string]int)
expectedNewValue["first"] = 1
expectedNewValue["second"] = 2

path := "TestMap.Value"

err = AdaptStructureValueBasedOnPath(testConfig, path, expectedNewValue)
require.Equal(t, err.Error(), "unsupported type <map> when trying to set the value 'map[first:1 second:2]' of type <map[string]int>")
})

t.Run("should error fit signed for target type not int", func(t *testing.T) {
t.Parallel()

newValue := 10
reflectNewValue := reflect.ValueOf(newValue)
targetType := reflect.TypeOf("string")

res := FitsWithinSignedIntegerRange(reflectNewValue, targetType)
require.False(t, res)
})

t.Run("should error fit signed for value not int and target type int", func(t *testing.T) {
t.Parallel()

newValue := "value"
reflectNewValue := reflect.ValueOf(newValue)
targetType := reflect.TypeOf(10)

res := FitsWithinSignedIntegerRange(reflectNewValue, targetType)
require.False(t, res)
})

t.Run("should error fit unsigned for target type not uint", func(t *testing.T) {
t.Parallel()

newValue := uint(10)
reflectNewValue := reflect.ValueOf(newValue)
targetType := reflect.TypeOf("string")

res := FitsWithinUnsignedIntegerRange(reflectNewValue, targetType)
require.False(t, res)
})

t.Run("should error fit unsigned for value not uint and target type uint", func(t *testing.T) {
t.Parallel()

newValue := "value"
reflectNewValue := reflect.ValueOf(newValue)
targetType := reflect.TypeOf(uint(10))

res := FitsWithinUnsignedIntegerRange(reflectNewValue, targetType)
require.False(t, res)
})

t.Run("should error fit float for target type not float", func(t *testing.T) {
t.Parallel()

newValue := float32(10)
reflectNewValue := reflect.ValueOf(newValue)
targetType := reflect.TypeOf("string")

res := FitsWithinFloatRange(reflectNewValue, targetType)
require.False(t, res)
})

t.Run("should work and override int8 value", func(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -962,6 +1034,21 @@ func TestAdaptStructureValueBasedOnPath(t *testing.T) {
require.Equal(t, err.Error(), "field <Nr> not found or cannot be set")
})

t.Run("should error with different types", func(t *testing.T) {
t.Parallel()

testConfig, err := loadTestConfig("../../testscommon/toml/config.toml")
require.NoError(t, err)

overrideConfig, err := loadOverrideConfig("../../testscommon/toml/overwrite.toml")
require.NoError(t, err)

path := "TestConfigStruct.ConfigStruct.Description"

err = AdaptStructureValueBasedOnPath(testConfig, path, overrideConfig.OverridableConfigTomlValues[33].Value)
require.Equal(t, err.Error(), "unable to cast value '11' of type <string> to type <uint32>")
})

t.Run("should work and override nested struct", func(t *testing.T) {
t.Parallel()

Expand All @@ -973,7 +1060,7 @@ func TestAdaptStructureValueBasedOnPath(t *testing.T) {

path := "TestConfigNestedStruct.ConfigNestedStruct"

err = AdaptStructureValueBasedOnPath(testConfig, path, overrideConfig.OverridableConfigTomlValues[33].Value)
err = AdaptStructureValueBasedOnPath(testConfig, path, overrideConfig.OverridableConfigTomlValues[34].Value)
require.NoError(t, err)
require.Equal(t, testConfig.TestConfigNestedStruct.ConfigNestedStruct.Text, "Overwritten text")
require.Equal(t, testConfig.TestConfigNestedStruct.ConfigNestedStruct.Message.Public, false)
Expand All @@ -991,7 +1078,7 @@ func TestAdaptStructureValueBasedOnPath(t *testing.T) {

path := "TestConfigNestedStruct.ConfigNestedStruct"

err = AdaptStructureValueBasedOnPath(testConfig, path, overrideConfig.OverridableConfigTomlValues[33].Value)
err = AdaptStructureValueBasedOnPath(testConfig, path, overrideConfig.OverridableConfigTomlValues[34].Value)
require.NoError(t, err)
require.Equal(t, testConfig.TestConfigNestedStruct.ConfigNestedStruct.Text, "Overwritten text")
require.Equal(t, testConfig.TestConfigNestedStruct.ConfigNestedStruct.Message.Public, false)
Expand All @@ -1009,7 +1096,7 @@ func TestAdaptStructureValueBasedOnPath(t *testing.T) {

path := "TestConfigNestedStruct.ConfigNestedStruct.Message.MessageDescription"

err = AdaptStructureValueBasedOnPath(testConfig, path, overrideConfig.OverridableConfigTomlValues[34].Value)
err = AdaptStructureValueBasedOnPath(testConfig, path, overrideConfig.OverridableConfigTomlValues[35].Value)
require.NoError(t, err)
require.Equal(t, testConfig.TestConfigNestedStruct.ConfigNestedStruct.Message.MessageDescription[0].Text, "Overwritten Text1")
require.Equal(t, testConfig.TestConfigNestedStruct.ConfigNestedStruct.Message.MessageDescription[1].Text, "Overwritten Text2")
Expand Down Expand Up @@ -1049,15 +1136,32 @@ func TestAdaptStructureValueBasedOnPath(t *testing.T) {

path := "TestConfigNestedStruct.ConfigNestedStruct.Message.MessageDescription"

var newValue = []toml.MessageDescriptionInts{
{Value: 10},
{Value: 20},
var newValue = []toml.MessageDescriptionOtherName{
{Value: "10"},
{Value: "20"},
}

err = AdaptStructureValueBasedOnPath(testConfig, path, newValue)
require.Equal(t, err.Error(), "field <Value> not found or cannot be set")
})

t.Run("should error on slice when override different struct types", func(t *testing.T) {
t.Parallel()

testConfig, err := loadTestConfig("../../testscommon/toml/config.toml")
require.NoError(t, err)

path := "TestConfigNestedStruct.ConfigNestedStruct.Message.MessageDescription"

var newValue = []toml.MessageDescriptionOtherType{
{Text: 10},
{Text: 20},
}

err = AdaptStructureValueBasedOnPath(testConfig, path, newValue)
require.Equal(t, err.Error(), "unable to cast value '10' of type <int> to type <string>")
})

t.Run("should work on slice and override struct", func(t *testing.T) {
t.Parallel()

Expand Down
13 changes: 11 additions & 2 deletions testscommon/toml/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ type Config struct {
TestConfigF64
TestConfigStruct
TestConfigNestedStruct
TestMap
}

type TestConfigI8 struct {
Expand Down Expand Up @@ -126,6 +127,14 @@ type MessageDescription struct {
Text string
}

type MessageDescriptionInts struct {
Value int
type MessageDescriptionOtherType struct {
Text int
}

type MessageDescriptionOtherName struct {
Value string
}

type TestMap struct {
Value map[string]int
}
3 changes: 3 additions & 0 deletions testscommon/toml/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,6 @@
[TestConfigNestedStruct.ConfigNestedStruct]
Text = "Config Nested Struct"
Mesage = { Public = true, MessageDescription = [{ Text = "Text1" }, { Text = "Text2"}] }

[TestMap]
Value = { "key" = 0 }
5 changes: 3 additions & 2 deletions testscommon/toml/overwrite.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ OverridableConfigTomlValues = [
{ File = "config.toml", Path = "TestConfigF32.Float32", Value = -3.4e+40 },
{ File = "config.toml", Path = "TestConfigF64.Float64", Value = 1.7e+308 },
{ File = "config.toml", Path = "TestConfigF64.Float64", Value = -1.7e+308 },
{ File = "config.toml", Path = "TestConfigStruct.ConfigStruct", Value = { Number = 11 } },
{ File = "config.toml", Path = "TestConfigStruct.ConfigStruct", Value = { Nr = 222 } },
{ File = "config.toml", Path = "TestConfigStruct.ConfigStruct.Description", Value = { Number = 11 } },
{ File = "config.toml", Path = "TestConfigStruct.ConfigStruct.Description", Value = { Nr = 222 } },
{ File = "config.toml", Path = "TestConfigStruct.ConfigStruct.Description", Value = { Number = "11" } },
{ File = "config.toml", Path = "TestConfigNestedStruct.ConfigNestedStruct", Value = { Text = "Overwritten text", Message = { Public = false, MessageDescription = [{ Text = "Overwritten Text1" }] } } },
{ File = "config.toml", Path = "TestConfigNestedStruct.ConfigNestedStruct.Message.MessageDescription", Value = [{ Text = "Overwritten Text1" }, { Text = "Overwritten Text2" }] },
]

0 comments on commit 883b421

Please sign in to comment.