diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..4536db2 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2021 Timofey Kovalev + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. \ No newline at end of file diff --git a/README.md b/README.md index af3c1b2..3eb9c15 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,19 @@ # omg.testingTools ---- +This tool can be useful for writing a tests. If you want change private field in struct from imported libraries than it can help you. +```go +package main + +import ( + "strings" + + "github.com/dedalqq/omg.testingtools" +) + +func TestSomeCase(t *testing.T) { + buffer := strings.Builder{} + + testingtools.SetPrivateValue(&buffer, "buf", []byte("data in strings buffer")) +} +``` \ No newline at end of file diff --git a/setPrivateValue.go b/setPrivateValue.go index f73f17a..9d037f6 100644 --- a/setPrivateValue.go +++ b/setPrivateValue.go @@ -6,6 +6,8 @@ import ( "unsafe" ) +// SetPrivateValue sets new a value in to the struct. Use it if you need to get access to private +// field of the struct from imported package. important: It function must be using only for the tests. func SetPrivateValue(obj interface{}, fieldName string, value interface{}) { reflectValue := reflect.ValueOf(obj) @@ -22,23 +24,17 @@ func SetPrivateValue(obj interface{}, fieldName string, value interface{}) { newReflectValueType := reflect.TypeOf(value) - var offset uintptr + if fieldType, ok := reflectType.FieldByName(fieldName); ok { + currentFieldType := fieldType.Type.String() - for i := 0; i < reflectValue.NumField(); i++ { - if reflectType.Field(i).Name == fieldName { - currentFieldType := reflectType.Field(i).Type.String() - - if newReflectValueType.String() != currentFieldType { - panic(fmt.Sprintf("incorrect type: must be %s", currentFieldType)) - } - - fieldPtr := unsafe.Pointer(reflectValue.UnsafeAddr() + offset) - reflect.NewAt(newReflectValueType, fieldPtr).Elem().Set(reflect.ValueOf(value)) - - return + if newReflectValueType.String() != currentFieldType { + panic(fmt.Sprintf("incorrect type: must be %s", currentFieldType)) } - offset += reflectValue.Field(i).Type().Size() + fieldPtr := unsafe.Pointer(reflectValue.UnsafeAddr() + fieldType.Offset) + reflect.NewAt(newReflectValueType, fieldPtr).Elem().Set(reflect.ValueOf(value)) + + return } panic(fmt.Sprintf("Field [%s] not found in struct", fieldName)) diff --git a/setPrivateValue_test.go b/setPrivateValue_test.go index d2078f5..172e06e 100644 --- a/setPrivateValue_test.go +++ b/setPrivateValue_test.go @@ -12,7 +12,7 @@ func TestSetPrivateValue(t *testing.T) { SetPrivateValue(&qq, "c", []int{1, 2, 3, 4}) SetPrivateValue(&qq, "b", "qwe") - if fmt.Sprint(qq) != "{123 qwe [1 2 3 4]}" { + if fmt.Sprint(qq) != "{false 123 qwe [1 2 3 4]}" { t.Fail() } } diff --git a/testmodule/omg.go b/testmodule/omg.go index be4af55..1460b99 100644 --- a/testmodule/omg.go +++ b/testmodule/omg.go @@ -1,13 +1,14 @@ package testmodule type TestStruct struct { - a int - b string - c []int + a bool + b int + c string + d []int } func NewQQ(a int, b string, c []int) TestStruct { return TestStruct{ - a, b, c, + false, a, b, c, } }