Skip to content

Commit

Permalink
change naming
Browse files Browse the repository at this point in the history
  • Loading branch information
mfdeveloper508 authored and alovak committed Oct 5, 2023
1 parent e1507b3 commit ec6abff
Show file tree
Hide file tree
Showing 4 changed files with 151 additions and 149 deletions.
66 changes: 33 additions & 33 deletions field/binary_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,73 +144,73 @@ func TestBinaryNil(t *testing.T) {

func TestBinaryFieldUnmarshal(t *testing.T) {
testValue := []byte{0x12, 0x34, 0x56}
str := NewBinaryValue(testValue)
binaryField := NewBinaryValue(testValue)

val1 := &Binary{}
err := str.Unmarshal(val1)
vBinary := &Binary{}
err := binaryField.Unmarshal(vBinary)
require.NoError(t, err)
require.Equal(t, testValue, val1.Value())
require.Equal(t, testValue, vBinary.Value())

var val2 string
err = str.Unmarshal(&val2)
var s string
err = binaryField.Unmarshal(&s)
require.NoError(t, err)
require.Equal(t, "123456", val2)
require.Equal(t, "123456", s)

var val3 []byte
err = str.Unmarshal(&val3)
var b []byte
err = binaryField.Unmarshal(&b)
require.NoError(t, err)
require.Equal(t, testValue, val3)
require.Equal(t, testValue, b)

val4 := reflect.ValueOf(&val2).Elem()
err = str.Unmarshal(val4)
refStrValue := reflect.ValueOf(&s).Elem()
err = binaryField.Unmarshal(refStrValue)
require.NoError(t, err)
require.Equal(t, "123456", val4.String())
require.Equal(t, "123456", refStrValue.String())

val5 := reflect.ValueOf(&val3).Elem()
err = str.Unmarshal(val5)
refBytesValue := reflect.ValueOf(&b).Elem()
err = binaryField.Unmarshal(refBytesValue)
require.NoError(t, err)
require.Equal(t, testValue, val5.Bytes())
require.Equal(t, testValue, refBytesValue.Bytes())

val6 := reflect.ValueOf(val2)
err = str.Unmarshal(val6)
refStr := reflect.ValueOf(s)
err = binaryField.Unmarshal(refStr)
require.Error(t, err)
require.Equal(t, "cannot set reflect.Value of type string", err.Error())

val7 := reflect.ValueOf(&val2)
err = str.Unmarshal(val7)
refStrPointer := reflect.ValueOf(&s)
err = binaryField.Unmarshal(refStrPointer)
require.Error(t, err)
require.Equal(t, "cannot set reflect.Value of type ptr", err.Error())

err = str.Unmarshal(nil)
err = binaryField.Unmarshal(nil)
require.Error(t, err)
require.Equal(t, "unsupported type: expected *Binary, *string, *[]byte, or reflect.Value, got <nil>", err.Error())
}

func TestBinaryFieldMarshal(t *testing.T) {
testValue := []byte{0x12, 0x34, 0x56}
str := NewBinaryValue(nil)
binaryField := NewBinaryValue(nil)

vstring := "123456"
err := str.Marshal(vstring)
inputStr := "123456"
err := binaryField.Marshal(inputStr)
require.NoError(t, err)
require.Equal(t, testValue, str.Value())
require.Equal(t, testValue, binaryField.Value())

err = str.Marshal(&vstring)
err = binaryField.Marshal(&inputStr)
require.NoError(t, err)
require.Equal(t, testValue, str.Value())
require.Equal(t, testValue, binaryField.Value())

err = str.Marshal(testValue)
err = binaryField.Marshal(testValue)
require.NoError(t, err)
require.Equal(t, testValue, str.Value())
require.Equal(t, testValue, binaryField.Value())

err = str.Marshal(&testValue)
err = binaryField.Marshal(&testValue)
require.NoError(t, err)
require.Equal(t, testValue, str.Value())
require.Equal(t, testValue, binaryField.Value())

err = str.Marshal(nil)
err = binaryField.Marshal(nil)
require.NoError(t, err)

err = str.Marshal(123456)
err = binaryField.Marshal(123456)
require.Error(t, err)
require.Equal(t, "data does not match required *Binary or (string, *string, []byte, *[]byte) type", err.Error())
}
76 changes: 38 additions & 38 deletions field/hex_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,83 +146,83 @@ func TestHexPack(t *testing.T) {

func TestHexFieldUnmarshal(t *testing.T) {
testValue := []byte{0x12, 0x34, 0x56}
str := NewHexValue("123456")
hexField := NewHexValue("123456")

val1 := &Hex{}
err := str.Unmarshal(val1)
vHex := &Hex{}
err := hexField.Unmarshal(vHex)
require.NoError(t, err)
require.Equal(t, "123456", val1.Value())
buf, _ := val1.Bytes()
require.Equal(t, "123456", vHex.Value())
buf, _ := vHex.Bytes()
require.Equal(t, testValue, buf)

var val2 string
err = str.Unmarshal(&val2)
var s string
err = hexField.Unmarshal(&s)
require.NoError(t, err)
require.Equal(t, "123456", val2)
require.Equal(t, "123456", s)

var val3 []byte
err = str.Unmarshal(&val3)
var b []byte
err = hexField.Unmarshal(&b)
require.NoError(t, err)
require.Equal(t, testValue, val3)
require.Equal(t, testValue, b)

val4 := reflect.ValueOf(&val2).Elem()
err = str.Unmarshal(val4)
refStrValue := reflect.ValueOf(&s).Elem()
err = hexField.Unmarshal(refStrValue)
require.NoError(t, err)
require.Equal(t, "123456", val4.String())
require.Equal(t, "123456", refStrValue.String())

val5 := reflect.ValueOf(&val3).Elem()
err = str.Unmarshal(val5)
refBytesValue := reflect.ValueOf(&b).Elem()
err = hexField.Unmarshal(refBytesValue)
require.NoError(t, err)
require.Equal(t, testValue, val5.Bytes())
require.Equal(t, testValue, refBytesValue.Bytes())

val6 := reflect.ValueOf(val2)
err = str.Unmarshal(val6)
refStr := reflect.ValueOf(s)
err = hexField.Unmarshal(refStr)
require.Error(t, err)
require.Equal(t, "cannot set reflect.Value of type string", err.Error())

val7 := reflect.ValueOf(&val2)
err = str.Unmarshal(val7)
refStrPointer := reflect.ValueOf(&s)
err = hexField.Unmarshal(refStrPointer)
require.Error(t, err)
require.Equal(t, "cannot set reflect.Value of type ptr", err.Error())

err = str.Unmarshal(nil)
err = hexField.Unmarshal(nil)
require.Error(t, err)
require.Equal(t, "unsupported type: expected *Hex, *string, *[]byte, or reflect.Value, got <nil>", err.Error())
}

func TestHexFieldMarshal(t *testing.T) {
testValue := []byte{0x12, 0x34, 0x56}
str := NewHexValue("")
hexField := NewHexValue("")

vstring := "123456"
err := str.Marshal(vstring)
inputStr := "123456"
err := hexField.Marshal(inputStr)
require.NoError(t, err)
require.Equal(t, "123456", str.Value())
buf, _ := str.Bytes()
require.Equal(t, "123456", hexField.Value())
buf, _ := hexField.Bytes()
require.Equal(t, testValue, buf)

err = str.Marshal(&vstring)
err = hexField.Marshal(&inputStr)
require.NoError(t, err)
require.Equal(t, "123456", str.Value())
buf, _ = str.Bytes()
require.Equal(t, "123456", hexField.Value())
buf, _ = hexField.Bytes()
require.Equal(t, testValue, buf)

err = str.Marshal(testValue)
err = hexField.Marshal(testValue)
require.NoError(t, err)
require.Equal(t, "123456", str.Value())
buf, _ = str.Bytes()
require.Equal(t, "123456", hexField.Value())
buf, _ = hexField.Bytes()
require.Equal(t, testValue, buf)

err = str.Marshal(&testValue)
err = hexField.Marshal(&testValue)
require.NoError(t, err)
require.Equal(t, "123456", str.Value())
buf, _ = str.Bytes()
require.Equal(t, "123456", hexField.Value())
buf, _ = hexField.Bytes()
require.Equal(t, testValue, buf)

err = str.Marshal(nil)
err = hexField.Marshal(nil)
require.NoError(t, err)

err = str.Marshal(123456)
err = hexField.Marshal(123456)
require.Error(t, err)
require.Equal(t, "data does not match required *Hex or (string, *string, []byte, *[]byte) type", err.Error())
}
79 changes: 40 additions & 39 deletions field/numeric_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,75 +89,76 @@ func TestNumericPack(t *testing.T) {
}

func TestNumericFieldUnmarshal(t *testing.T) {
str := NewNumericValue(123456)
numericField := NewNumericValue(123456)

val1 := &Numeric{}
err := str.Unmarshal(val1)
vNumeric := &Numeric{}
err := numericField.Unmarshal(vNumeric)
require.NoError(t, err)
require.Equal(t, 123456, val1.Value())
require.Equal(t, 123456, vNumeric.Value())

var val2 string
err = str.Unmarshal(&val2)
var s string
err = numericField.Unmarshal(&s)
require.NoError(t, err)
require.Equal(t, "123456", val2)
require.Equal(t, "123456", s)

var val3 int
err = str.Unmarshal(&val3)
var b int
err = numericField.Unmarshal(&b)
require.NoError(t, err)
require.Equal(t, 123456, val3)
require.Equal(t, 123456, b)

val4 := reflect.ValueOf(&val2).Elem()
err = str.Unmarshal(val4)
refStrValue := reflect.ValueOf(&s).Elem()
err = numericField.Unmarshal(refStrValue)
require.NoError(t, err)
require.Equal(t, "123456", val4.String())
require.Equal(t, "123456", refStrValue.String())

val5 := reflect.ValueOf(&val3).Elem()
err = str.Unmarshal(val5)
refIntValue := reflect.ValueOf(&b).Elem()
err = numericField.Unmarshal(refIntValue)
require.NoError(t, err)
require.Equal(t, 123456, int(val5.Int()))
require.Equal(t, 123456, int(refIntValue.Int()))

val6 := reflect.ValueOf(val2)
err = str.Unmarshal(val6)
refStr := reflect.ValueOf(s)
err = numericField.Unmarshal(refStr)
require.Error(t, err)
require.Equal(t, "cannot set reflect.Value of type string", err.Error())

val7 := reflect.ValueOf(&val2)
err = str.Unmarshal(val7)
refStrPointer := reflect.ValueOf(&s)
err = numericField.Unmarshal(refStrPointer)
require.Error(t, err)
require.Equal(t, "cannot set reflect.Value of type ptr", err.Error())

err = str.Unmarshal(nil)
err = numericField.Unmarshal(nil)
require.Error(t, err)
require.Equal(t, "unsupported type: expected *Numeric, *int, or reflect.Value, got <nil>", err.Error())
}

func TestNumericFieldMarshal(t *testing.T) {
str := NewNumericValue(0)
vNumeric := NewNumericValue(123456)
str.Marshal(vNumeric)
require.Equal(t, 123456, vNumeric.Value())
numericField := NewNumericValue(0)

str.Marshal(&vNumeric)
require.Equal(t, 123456, vNumeric.Value())
inputNumeric := NewNumericValue(123456)
numericField.Marshal(inputNumeric)
require.Equal(t, 123456, numericField.Value())

vstring := "123456"
str.Marshal(vstring)
require.Equal(t, 123456, vNumeric.Value())
numericField.Marshal(&inputNumeric)
require.Equal(t, 123456, numericField.Value())

str.Marshal(&vstring)
require.Equal(t, 123456, vNumeric.Value())
inputStr := "123456"
numericField.Marshal(inputStr)
require.Equal(t, 123456, numericField.Value())

vint := 123456
str.Marshal(vint)
require.Equal(t, 123456, vNumeric.Value())
numericField.Marshal(&inputStr)
require.Equal(t, 123456, numericField.Value())

str.Marshal(&vint)
require.Equal(t, 123456, vNumeric.Value())
inputInt := 123456
numericField.Marshal(inputInt)
require.Equal(t, 123456, numericField.Value())

numericField.Marshal(&inputInt)
require.Equal(t, 123456, numericField.Value())

err := str.Marshal(nil)
err := numericField.Marshal(nil)
require.NoError(t, err)

err = str.Marshal([]byte("123456"))
err = numericField.Marshal([]byte("123456"))
require.Error(t, err)
require.Equal(t, "data does not match require *Numeric or (int, *int, string, *string) type", err.Error())
}
Expand Down
Loading

0 comments on commit ec6abff

Please sign in to comment.