Skip to content

Commit

Permalink
fixt test
Browse files Browse the repository at this point in the history
  • Loading branch information
dedalqq committed Oct 14, 2021
1 parent 8fa436e commit e9b9945
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 20 deletions.
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -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.
17 changes: 16 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -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"))
}
```
24 changes: 10 additions & 14 deletions setPrivateValue.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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))
Expand Down
2 changes: 1 addition & 1 deletion setPrivateValue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}
9 changes: 5 additions & 4 deletions testmodule/omg.go
Original file line number Diff line number Diff line change
@@ -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,
}
}

0 comments on commit e9b9945

Please sign in to comment.