From 4c2fa25f675906f9ee3aba5205c0c0eaf2ed8202 Mon Sep 17 00:00:00 2001 From: Egipti Pavel Date: Thu, 9 Mar 2023 20:36:07 +0300 Subject: [PATCH 1/7] Fix bug with nested unexported structs --- .../go-samples/simple/samples_go_ut_test.go | 102 ++---------------- utbot-go/go-samples/simple/supported_types.go | 5 + .../simple/supported_types_go_ut_test.go | 55 +++++----- .../kotlin/org/utbot/go/api/GoTypesApi.kt | 5 +- .../org/utbot/go/api/util/GoTypesApiUtil.kt | 20 ++-- .../org/utbot/go/framework/api/go/GoApi.kt | 8 ++ .../org/utbot/go/imports/GoImportsResolver.kt | 4 +- .../org/utbot/go/worker/GoCodeTemplates.kt | 12 ++- .../go/worker/GoWorkerCodeGenerationHelper.kt | 4 +- 9 files changed, 80 insertions(+), 135 deletions(-) diff --git a/utbot-go/go-samples/simple/samples_go_ut_test.go b/utbot-go/go-samples/simple/samples_go_ut_test.go index a6c69385c0..df8435cf74 100644 --- a/utbot-go/go-samples/simple/samples_go_ut_test.go +++ b/utbot-go/go-samples/simple/samples_go_ut_test.go @@ -46,69 +46,29 @@ func TestGenerateArrayOfIntegersByUtGoFuzzer(t *testing.T) { } func TestDistanceBetweenTwoPointsByUtGoFuzzer(t *testing.T) { - actualVal := DistanceBetweenTwoPoints(Point{x: 0.990722785714783, y: 0.990722785714783}, Point{x: 2.0, y: 2.0}) + actualVal := DistanceBetweenTwoPoints(Point{x: 0.730967787376657, y: 0.730967787376657}, Point{x: 2.0, y: 2.0}) - assert.Equal(t, 1.4273335246362906, actualVal) + assert.Equal(t, 1.794682566180269, actualVal) } func TestGetCoordinatesOfMiddleBetweenTwoPointsByUtGoFuzzer(t *testing.T) { - actualVal0, actualVal1 := GetCoordinatesOfMiddleBetweenTwoPoints(Point{x: 0.4872328470301428, y: 0.4872328470301428}, Point{x: 2.0, y: 2.0}) + actualVal0, actualVal1 := GetCoordinatesOfMiddleBetweenTwoPoints(Point{x: 0.24053641567148587, y: 0.24053641567148587}, Point{x: 2.0, y: 2.0}) assertMultiple := assert.New(t) - assertMultiple.Equal(1.2436164235150713, actualVal0) - assertMultiple.Equal(1.2436164235150713, actualVal1) + assertMultiple.Equal(1.1202682078357429, actualVal0) + assertMultiple.Equal(1.1202682078357429, actualVal1) } -func TestGetCoordinateSumOfPointsByUtGoFuzzer1(t *testing.T) { - actualVal0, actualVal1 := GetCoordinateSumOfPoints([]Point{{x: 0.7462414053223305, y: 0.7462414053223305}}) +func TestGetCoordinateSumOfPointsByUtGoFuzzer(t *testing.T) { + actualVal0, actualVal1 := GetCoordinateSumOfPoints([]Point{{x: 0.6374174253501083, y: 0.6374174253501083}}) assertMultiple := assert.New(t) - assertMultiple.Equal(0.7462414053223305, actualVal0) - assertMultiple.Equal(0.7462414053223305, actualVal1) -} - -func TestGetCoordinateSumOfPointsByUtGoFuzzer2(t *testing.T) { - actualVal0, actualVal1 := GetCoordinateSumOfPoints([]Point{{x: 0.7462414053223305, y: 0.0}, {x: 0.7462414053223305, y: 0.0}, {x: 0.7462414053223305, y: 2.8480945388892178e-306}, {x: 0.7462414053223305, y: 0.0}}) - - assertMultiple := assert.New(t) - assertMultiple.Equal(2.984965621289322, actualVal0) - assertMultiple.Equal(2.8480945388892178e-306, actualVal1) -} - -func TestGetCoordinateSumOfPointsByUtGoFuzzer3(t *testing.T) { - actualVal0, actualVal1 := GetCoordinateSumOfPoints([]Point{{x: 0.7462414053223305, y: 0.7462414053223305}, {x: 0.0, y: 0.7462414053223305}}) - - assertMultiple := assert.New(t) - assertMultiple.Equal(0.7462414053223305, actualVal0) - assertMultiple.Equal(1.492482810644661, actualVal1) -} - -func TestGetCoordinateSumOfPointsByUtGoFuzzer4(t *testing.T) { - actualVal0, actualVal1 := GetCoordinateSumOfPoints([]Point{{x: 0.0, y: 0.0}, {x: 0.0, y: 0.0}, {x: 0.0, y: 0.0}}) - - assertMultiple := assert.New(t) - assertMultiple.Equal(0.0, actualVal0) - assertMultiple.Equal(0.0, actualVal1) -} - -func TestGetCoordinateSumOfPointsByUtGoFuzzer5(t *testing.T) { - actualVal0, actualVal1 := GetCoordinateSumOfPoints([]Point{{x: 0.0, y: 0.7462414053223305}, {x: 0.0, y: 0.7462414053223305}, {x: 4.7783097267364807e-299, y: 0.7462414053223305}, {x: 0.0, y: 0.7462414053223305}, {x: 0.0, y: 0.7462414053223305}}) - - assertMultiple := assert.New(t) - assertMultiple.Equal(4.7783097267364807e-299, actualVal0) - assertMultiple.Equal(3.731207026611653, actualVal1) -} - -func TestGetCoordinateSumOfPointsByUtGoFuzzer6(t *testing.T) { - actualVal0, actualVal1 := GetCoordinateSumOfPoints([]Point{}) - - assertMultiple := assert.New(t) - assertMultiple.Equal(0.0, actualVal0) - assertMultiple.Equal(0.0, actualVal1) + assertMultiple.Equal(0.6374174253501083, actualVal0) + assertMultiple.Equal(0.6374174253501083, actualVal1) } func TestGetAreaOfCircleByUtGoFuzzer(t *testing.T) { - actualVal := GetAreaOfCircle(Circle{Center: Point{x: 0.7331520701949938, y: 0.7331520701949938}, Radius: 2.0}) + actualVal := GetAreaOfCircle(Circle{Center: Point{x: 0.5504370051176339, y: 0.5504370051176339}, Radius: 2.0}) assert.Equal(t, 12.566370614359172, actualVal) } @@ -126,48 +86,6 @@ func TestIsIdentityByUtGoFuzzer2(t *testing.T) { } func TestIsIdentityByUtGoFuzzer3(t *testing.T) { - actualVal := IsIdentity([3][3]int{{1, 0, 0}, {0, 0, 0}, {0, 0, 0}}) - - assert.Equal(t, false, actualVal) -} - -func TestIsIdentityByUtGoFuzzer4(t *testing.T) { - actualVal := IsIdentity([3][3]int{{1, 0, 1}, {1, 0, 1}, {0, 1, 1}}) - - assert.Equal(t, false, actualVal) -} - -func TestIsIdentityByUtGoFuzzer5(t *testing.T) { - actualVal := IsIdentity([3][3]int{{1, 0, 0}, {0, 1, 2048}, {0, 0, 0}}) - - assert.Equal(t, false, actualVal) -} - -func TestIsIdentityByUtGoFuzzer6(t *testing.T) { - actualVal := IsIdentity([3][3]int{{1, 0, 0}, {2199023255552, 1, 2048}, {0, 0, 3}}) - - assert.Equal(t, false, actualVal) -} - -func TestIsIdentityByUtGoFuzzer7(t *testing.T) { - actualVal := IsIdentity([3][3]int{{1, 0, 0}, {0, 1, 0}, {0, 0, 0}}) - - assert.Equal(t, false, actualVal) -} - -func TestIsIdentityByUtGoFuzzer8(t *testing.T) { - actualVal := IsIdentity([3][3]int{{1, 0, 0}, {0, 1, 0}, {0, 1, 0}}) - - assert.Equal(t, false, actualVal) -} - -func TestIsIdentityByUtGoFuzzer9(t *testing.T) { - actualVal := IsIdentity([3][3]int{{1, 0, 0}, {0, 1, 0}, {18014398509481984, 1, 0}}) - - assert.Equal(t, false, actualVal) -} - -func TestIsIdentityByUtGoFuzzer10(t *testing.T) { actualVal := IsIdentity([3][3]int{{1, 0, 0}, {0, 1, 0}, {0, 0, 1}}) assert.Equal(t, true, actualVal) diff --git a/utbot-go/go-samples/simple/supported_types.go b/utbot-go/go-samples/simple/supported_types.go index 3b308da3f1..fac4dde4d7 100644 --- a/utbot-go/go-samples/simple/supported_types.go +++ b/utbot-go/go-samples/simple/supported_types.go @@ -4,6 +4,7 @@ import ( "errors" "github.com/pmezard/go-difflib/difflib" dif "github.com/pmezard/go-difflib/difflib" + "go-samples/simple/nested" "math" ) @@ -214,3 +215,7 @@ func SliceOfSliceOfStructs(slice [][]Structure) [][]Structure { func SliceOfArrayOfInt(slice [][5]int) [][5]int { return slice } + +func ExportedStructWithEmbeddedUnexportedStruct(exportedStruct nested.ExportedStruct) nested.ExportedStruct { + return exportedStruct +} diff --git a/utbot-go/go-samples/simple/supported_types_go_ut_test.go b/utbot-go/go-samples/simple/supported_types_go_ut_test.go index c3b3f3eaf3..94ebfa7ad7 100644 --- a/utbot-go/go-samples/simple/supported_types_go_ut_test.go +++ b/utbot-go/go-samples/simple/supported_types_go_ut_test.go @@ -3,6 +3,7 @@ package simple import ( "github.com/pmezard/go-difflib/difflib" "github.com/stretchr/testify/assert" + "go-samples/simple/nested" "math" "testing" ) @@ -108,27 +109,27 @@ func TestUintPtrByUtGoFuzzer(t *testing.T) { } func TestFloat32ByUtGoFuzzer(t *testing.T) { - actualVal := Float32(0.6063452) + actualVal := Float32(0.73096776) - assert.Equal(t, float32(0.6063452), actualVal) + assert.Equal(t, float32(0.73096776), actualVal) } func TestFloat64ByUtGoFuzzer(t *testing.T) { - actualVal := Float64(0.6374174253501083) + actualVal := Float64(0.8314409887870612) - assert.Equal(t, 0.6374174253501083, actualVal) + assert.Equal(t, 0.8314409887870612, actualVal) } func TestComplex64ByUtGoFuzzer(t *testing.T) { - actualVal := Complex64(complex(0.550437, 0.550437)) + actualVal := Complex64(complex(0.6063452, 0.6063452)) - assert.Equal(t, complex(float32(0.550437), float32(0.550437)), actualVal) + assert.Equal(t, complex(float32(0.6063452), float32(0.6063452)), actualVal) } func TestComplex128ByUtGoFuzzer(t *testing.T) { - actualVal := Complex128(complex(0.11700660880722513, 0.11700660880722513)) + actualVal := Complex128(complex(0.6374174253501083, 0.6374174253501083)) - assert.Equal(t, complex(0.11700660880722513, 0.11700660880722513), actualVal) + assert.Equal(t, complex(0.6374174253501083, 0.6374174253501083), actualVal) } func TestByteByUtGoFuzzer(t *testing.T) { @@ -156,15 +157,15 @@ func TestBoolByUtGoFuzzer(t *testing.T) { } func TestStructByUtGoFuzzer(t *testing.T) { - actualVal := Struct(Structure{int: -1, int8: 1, int16: 32767, int32: -1, int64: -1, uint: 18446744073709551615, uint8: 0, uint16: 1, uint32: 0, uint64: 18446744073709551615, uintptr: 18446744073709551615, float32: 0.7815346, float64: 0.3332183994766498, complex64: complex(0.7815346, 0.7815346), complex128: complex(0.3332183994766498, 0.3332183994766498), byte: 0, rune: -1, string: "", bool: false}) + actualVal := Struct(Structure{int: -1, int8: 1, int16: 32767, int32: -1, int64: -1, uint: 18446744073709551615, uint8: 0, uint16: 1, uint32: 0, uint64: 18446744073709551615, uintptr: 18446744073709551615, float32: 0.550437, float64: 0.11700660880722513, complex64: complex(0.550437, 0.550437), complex128: complex(0.11700660880722513, 0.11700660880722513), byte: 0, rune: -1, string: "", bool: false}) - assert.Equal(t, Structure{int: -1, int8: 1, int16: 32767, int32: -1, int64: -1, uint: 18446744073709551615, uint8: 0, uint16: 1, uint32: 0, uint64: 18446744073709551615, uintptr: 18446744073709551615, float32: 0.7815346, float64: 0.3332183994766498, complex64: complex(float32(0.7815346), float32(0.7815346)), complex128: complex(0.3332183994766498, 0.3332183994766498), byte: 0, rune: -1, string: "", bool: false}, actualVal) + assert.Equal(t, Structure{int: -1, int8: 1, int16: 32767, int32: -1, int64: -1, uint: 18446744073709551615, uint8: 0, uint16: 1, uint32: 0, uint64: 18446744073709551615, uintptr: 18446744073709551615, float32: 0.550437, float64: 0.11700660880722513, complex64: complex(float32(0.550437), float32(0.550437)), complex128: complex(0.11700660880722513, 0.11700660880722513), byte: 0, rune: -1, string: "", bool: false}, actualVal) } func TestStructWithNanByUtGoFuzzer(t *testing.T) { - actualVal := StructWithNan(Structure{int: -1, int8: 1, int16: 32767, int32: -1, int64: -1, uint: 18446744073709551615, uint8: 0, uint16: 1, uint32: 0, uint64: 18446744073709551615, uintptr: 18446744073709551615, float32: 0.38518918, float64: 0.6130357680446138, complex64: complex(0.38518918, 0.38518918), complex128: complex(0.6130357680446138, 0.6130357680446138), byte: 0, rune: -1, string: "", bool: false}) + actualVal := StructWithNan(Structure{int: -1, int8: 1, int16: 32767, int32: -1, int64: -1, uint: 18446744073709551615, uint8: 0, uint16: 1, uint32: 0, uint64: 18446744073709551615, uintptr: 18446744073709551615, float32: 0.7815346, float64: 0.3332183994766498, complex64: complex(0.7815346, 0.7815346), complex128: complex(0.3332183994766498, 0.3332183994766498), byte: 0, rune: -1, string: "", bool: false}) - assert.NotEqual(t, Structure{int: -1, int8: 1, int16: 32767, int32: -1, int64: -1, uint: 18446744073709551615, uint8: 0, uint16: 1, uint32: 0, uint64: 18446744073709551615, uintptr: 18446744073709551615, float32: 0.38518918, float64: math.NaN(), complex64: complex(float32(0.38518918), float32(0.38518918)), complex128: complex(0.6130357680446138, 0.6130357680446138), byte: 0, rune: -1, string: "", bool: false}, actualVal) + assert.NotEqual(t, Structure{int: -1, int8: 1, int16: 32767, int32: -1, int64: -1, uint: 18446744073709551615, uint8: 0, uint16: 1, uint32: 0, uint64: 18446744073709551615, uintptr: 18446744073709551615, float32: 0.7815346, float64: math.NaN(), complex64: complex(float32(0.7815346), float32(0.7815346)), complex128: complex(0.3332183994766498, 0.3332183994766498), byte: 0, rune: -1, string: "", bool: false}, actualVal) } func TestArrayOfIntByUtGoFuzzer(t *testing.T) { @@ -186,15 +187,15 @@ func TestArrayOfStringByUtGoFuzzer(t *testing.T) { } func TestArrayOfStructsByUtGoFuzzer(t *testing.T) { - actualVal := ArrayOfStructs([10]Structure{{int: -1, int8: 0, int16: -1, int32: -1, int64: -9223372036854775808, uint: 1, uint8: 0, uint16: 65535, uint32: 1, uint64: 18446744073709551615, uintptr: 1, float32: 0.9828195, float64: 0.8791825178724801, complex64: complex(0.9828195, 0.9828195), complex128: complex(0.8791825178724801, 0.8791825178724801), byte: 255, rune: 2147483647, string: "", bool: false}, {}, {}, {}, {}, {}, {}, {}, {}, {}}) + actualVal := ArrayOfStructs([10]Structure{{int: -1, int8: 0, int16: -1, int32: -1, int64: -9223372036854775808, uint: 1, uint8: 0, uint16: 65535, uint32: 1, uint64: 18446744073709551615, uintptr: 1, float32: 0.38518918, float64: 0.6130357680446138, complex64: complex(0.38518918, 0.38518918), complex128: complex(0.6130357680446138, 0.6130357680446138), byte: 255, rune: 2147483647, string: "", bool: false}, {}, {}, {}, {}, {}, {}, {}, {}, {}}) - assert.Equal(t, [10]Structure{{int: -1, int8: 0, int16: -1, int32: -1, int64: -9223372036854775808, uint: 1, uint8: 0, uint16: 65535, uint32: 1, uint64: 18446744073709551615, uintptr: 1, float32: 0.9828195, float64: 0.8791825178724801, complex64: complex(float32(0.9828195), float32(0.9828195)), complex128: complex(0.8791825178724801, 0.8791825178724801), byte: 255, rune: 2147483647, string: "", bool: false}, {int: 0, int8: 0, int16: 0, int32: 0, int64: 0, uint: 0, uint8: 0, uint16: 0, uint32: 0, uint64: 0, uintptr: 0, float32: 0.0, float64: 0.0, complex64: complex(float32(0.0), float32(0.0)), complex128: complex(0.0, 0.0), byte: 0, rune: 0, string: "", bool: false}, {int: 0, int8: 0, int16: 0, int32: 0, int64: 0, uint: 0, uint8: 0, uint16: 0, uint32: 0, uint64: 0, uintptr: 0, float32: 0.0, float64: 0.0, complex64: complex(float32(0.0), float32(0.0)), complex128: complex(0.0, 0.0), byte: 0, rune: 0, string: "", bool: false}, {int: 0, int8: 0, int16: 0, int32: 0, int64: 0, uint: 0, uint8: 0, uint16: 0, uint32: 0, uint64: 0, uintptr: 0, float32: 0.0, float64: 0.0, complex64: complex(float32(0.0), float32(0.0)), complex128: complex(0.0, 0.0), byte: 0, rune: 0, string: "", bool: false}, {int: 0, int8: 0, int16: 0, int32: 0, int64: 0, uint: 0, uint8: 0, uint16: 0, uint32: 0, uint64: 0, uintptr: 0, float32: 0.0, float64: 0.0, complex64: complex(float32(0.0), float32(0.0)), complex128: complex(0.0, 0.0), byte: 0, rune: 0, string: "", bool: false}, {int: 0, int8: 0, int16: 0, int32: 0, int64: 0, uint: 0, uint8: 0, uint16: 0, uint32: 0, uint64: 0, uintptr: 0, float32: 0.0, float64: 0.0, complex64: complex(float32(0.0), float32(0.0)), complex128: complex(0.0, 0.0), byte: 0, rune: 0, string: "", bool: false}, {int: 0, int8: 0, int16: 0, int32: 0, int64: 0, uint: 0, uint8: 0, uint16: 0, uint32: 0, uint64: 0, uintptr: 0, float32: 0.0, float64: 0.0, complex64: complex(float32(0.0), float32(0.0)), complex128: complex(0.0, 0.0), byte: 0, rune: 0, string: "", bool: false}, {int: 0, int8: 0, int16: 0, int32: 0, int64: 0, uint: 0, uint8: 0, uint16: 0, uint32: 0, uint64: 0, uintptr: 0, float32: 0.0, float64: 0.0, complex64: complex(float32(0.0), float32(0.0)), complex128: complex(0.0, 0.0), byte: 0, rune: 0, string: "", bool: false}, {int: 0, int8: 0, int16: 0, int32: 0, int64: 0, uint: 0, uint8: 0, uint16: 0, uint32: 0, uint64: 0, uintptr: 0, float32: 0.0, float64: 0.0, complex64: complex(float32(0.0), float32(0.0)), complex128: complex(0.0, 0.0), byte: 0, rune: 0, string: "", bool: false}, {int: 0, int8: 0, int16: 0, int32: 0, int64: 0, uint: 0, uint8: 0, uint16: 0, uint32: 0, uint64: 0, uintptr: 0, float32: 0.0, float64: 0.0, complex64: complex(float32(0.0), float32(0.0)), complex128: complex(0.0, 0.0), byte: 0, rune: 0, string: "", bool: false}}, actualVal) + assert.Equal(t, [10]Structure{{int: -1, int8: 0, int16: -1, int32: -1, int64: -9223372036854775808, uint: 1, uint8: 0, uint16: 65535, uint32: 1, uint64: 18446744073709551615, uintptr: 1, float32: 0.38518918, float64: 0.6130357680446138, complex64: complex(float32(0.38518918), float32(0.38518918)), complex128: complex(0.6130357680446138, 0.6130357680446138), byte: 255, rune: 2147483647, string: "", bool: false}, {int: 0, int8: 0, int16: 0, int32: 0, int64: 0, uint: 0, uint8: 0, uint16: 0, uint32: 0, uint64: 0, uintptr: 0, float32: 0.0, float64: 0.0, complex64: complex(float32(0.0), float32(0.0)), complex128: complex(0.0, 0.0), byte: 0, rune: 0, string: "", bool: false}, {int: 0, int8: 0, int16: 0, int32: 0, int64: 0, uint: 0, uint8: 0, uint16: 0, uint32: 0, uint64: 0, uintptr: 0, float32: 0.0, float64: 0.0, complex64: complex(float32(0.0), float32(0.0)), complex128: complex(0.0, 0.0), byte: 0, rune: 0, string: "", bool: false}, {int: 0, int8: 0, int16: 0, int32: 0, int64: 0, uint: 0, uint8: 0, uint16: 0, uint32: 0, uint64: 0, uintptr: 0, float32: 0.0, float64: 0.0, complex64: complex(float32(0.0), float32(0.0)), complex128: complex(0.0, 0.0), byte: 0, rune: 0, string: "", bool: false}, {int: 0, int8: 0, int16: 0, int32: 0, int64: 0, uint: 0, uint8: 0, uint16: 0, uint32: 0, uint64: 0, uintptr: 0, float32: 0.0, float64: 0.0, complex64: complex(float32(0.0), float32(0.0)), complex128: complex(0.0, 0.0), byte: 0, rune: 0, string: "", bool: false}, {int: 0, int8: 0, int16: 0, int32: 0, int64: 0, uint: 0, uint8: 0, uint16: 0, uint32: 0, uint64: 0, uintptr: 0, float32: 0.0, float64: 0.0, complex64: complex(float32(0.0), float32(0.0)), complex128: complex(0.0, 0.0), byte: 0, rune: 0, string: "", bool: false}, {int: 0, int8: 0, int16: 0, int32: 0, int64: 0, uint: 0, uint8: 0, uint16: 0, uint32: 0, uint64: 0, uintptr: 0, float32: 0.0, float64: 0.0, complex64: complex(float32(0.0), float32(0.0)), complex128: complex(0.0, 0.0), byte: 0, rune: 0, string: "", bool: false}, {int: 0, int8: 0, int16: 0, int32: 0, int64: 0, uint: 0, uint8: 0, uint16: 0, uint32: 0, uint64: 0, uintptr: 0, float32: 0.0, float64: 0.0, complex64: complex(float32(0.0), float32(0.0)), complex128: complex(0.0, 0.0), byte: 0, rune: 0, string: "", bool: false}, {int: 0, int8: 0, int16: 0, int32: 0, int64: 0, uint: 0, uint8: 0, uint16: 0, uint32: 0, uint64: 0, uintptr: 0, float32: 0.0, float64: 0.0, complex64: complex(float32(0.0), float32(0.0)), complex128: complex(0.0, 0.0), byte: 0, rune: 0, string: "", bool: false}, {int: 0, int8: 0, int16: 0, int32: 0, int64: 0, uint: 0, uint8: 0, uint16: 0, uint32: 0, uint64: 0, uintptr: 0, float32: 0.0, float64: 0.0, complex64: complex(float32(0.0), float32(0.0)), complex128: complex(0.0, 0.0), byte: 0, rune: 0, string: "", bool: false}}, actualVal) } func TestArrayOfStructsWithNanByUtGoFuzzer(t *testing.T) { - actualVal := ArrayOfStructsWithNan([10]Structure{{int: 1, int8: 0, int16: -1, int32: -1, int64: -9223372036854775808, uint: 1, uint8: 0, uint16: 65535, uint32: 1, uint64: 18446744073709551615, uintptr: 1, float32: 0.94124913, float64: 0.17597680203548016, complex64: complex(0.94124913, 0.94124913), complex128: complex(0.17597680203548016, 0.17597680203548016), byte: 255, rune: 2147483647, string: "", bool: false}, {}, {}, {}, {}, {}, {}, {}, {}, {}}) + actualVal := ArrayOfStructsWithNan([10]Structure{{int: 1, int8: 0, int16: -1, int32: -1, int64: -9223372036854775808, uint: 1, uint8: 0, uint16: 65535, uint32: 1, uint64: 18446744073709551615, uintptr: 1, float32: 0.9828195, float64: 0.8791825178724801, complex64: complex(0.9828195, 0.9828195), complex128: complex(0.8791825178724801, 0.8791825178724801), byte: 255, rune: 2147483647, string: "", bool: false}, {}, {}, {}, {}, {}, {}, {}, {}, {}}) - assert.NotEqual(t, [10]Structure{{int: 1, int8: 0, int16: -1, int32: -1, int64: -9223372036854775808, uint: 1, uint8: 0, uint16: 65535, uint32: 1, uint64: 18446744073709551615, uintptr: 1, float32: 0.94124913, float64: math.NaN(), complex64: complex(float32(0.94124913), float32(0.94124913)), complex128: complex(0.17597680203548016, 0.17597680203548016), byte: 255, rune: 2147483647, string: "", bool: false}, {int: 0, int8: 0, int16: 0, int32: 0, int64: 0, uint: 0, uint8: 0, uint16: 0, uint32: 0, uint64: 0, uintptr: 0, float32: 0.0, float64: 0.0, complex64: complex(float32(0.0), float32(0.0)), complex128: complex(0.0, 0.0), byte: 0, rune: 0, string: "", bool: false}, {int: 0, int8: 0, int16: 0, int32: 0, int64: 0, uint: 0, uint8: 0, uint16: 0, uint32: 0, uint64: 0, uintptr: 0, float32: 0.0, float64: 0.0, complex64: complex(float32(0.0), float32(0.0)), complex128: complex(0.0, 0.0), byte: 0, rune: 0, string: "", bool: false}, {int: 0, int8: 0, int16: 0, int32: 0, int64: 0, uint: 0, uint8: 0, uint16: 0, uint32: 0, uint64: 0, uintptr: 0, float32: 0.0, float64: 0.0, complex64: complex(float32(0.0), float32(0.0)), complex128: complex(0.0, 0.0), byte: 0, rune: 0, string: "", bool: false}, {int: 0, int8: 0, int16: 0, int32: 0, int64: 0, uint: 0, uint8: 0, uint16: 0, uint32: 0, uint64: 0, uintptr: 0, float32: 0.0, float64: 0.0, complex64: complex(float32(0.0), float32(0.0)), complex128: complex(0.0, 0.0), byte: 0, rune: 0, string: "", bool: false}, {int: 0, int8: 0, int16: 0, int32: 0, int64: 0, uint: 0, uint8: 0, uint16: 0, uint32: 0, uint64: 0, uintptr: 0, float32: 0.0, float64: 0.0, complex64: complex(float32(0.0), float32(0.0)), complex128: complex(0.0, 0.0), byte: 0, rune: 0, string: "", bool: false}, {int: 0, int8: 0, int16: 0, int32: 0, int64: 0, uint: 0, uint8: 0, uint16: 0, uint32: 0, uint64: 0, uintptr: 0, float32: 0.0, float64: 0.0, complex64: complex(float32(0.0), float32(0.0)), complex128: complex(0.0, 0.0), byte: 0, rune: 0, string: "", bool: false}, {int: 0, int8: 0, int16: 0, int32: 0, int64: 0, uint: 0, uint8: 0, uint16: 0, uint32: 0, uint64: 0, uintptr: 0, float32: 0.0, float64: 0.0, complex64: complex(float32(0.0), float32(0.0)), complex128: complex(0.0, 0.0), byte: 0, rune: 0, string: "", bool: false}, {int: 0, int8: 0, int16: 0, int32: 0, int64: 0, uint: 0, uint8: 0, uint16: 0, uint32: 0, uint64: 0, uintptr: 0, float32: 0.0, float64: 0.0, complex64: complex(float32(0.0), float32(0.0)), complex128: complex(0.0, 0.0), byte: 0, rune: 0, string: "", bool: false}, {int: 0, int8: 0, int16: 0, int32: 0, int64: 0, uint: 0, uint8: 0, uint16: 0, uint32: 0, uint64: 0, uintptr: 0, float32: 0.0, float64: 0.0, complex64: complex(float32(0.0), float32(0.0)), complex128: complex(0.0, 0.0), byte: 0, rune: 0, string: "", bool: false}}, actualVal) + assert.NotEqual(t, [10]Structure{{int: 1, int8: 0, int16: -1, int32: -1, int64: -9223372036854775808, uint: 1, uint8: 0, uint16: 65535, uint32: 1, uint64: 18446744073709551615, uintptr: 1, float32: 0.9828195, float64: math.NaN(), complex64: complex(float32(0.9828195), float32(0.9828195)), complex128: complex(0.8791825178724801, 0.8791825178724801), byte: 255, rune: 2147483647, string: "", bool: false}, {int: 0, int8: 0, int16: 0, int32: 0, int64: 0, uint: 0, uint8: 0, uint16: 0, uint32: 0, uint64: 0, uintptr: 0, float32: 0.0, float64: 0.0, complex64: complex(float32(0.0), float32(0.0)), complex128: complex(0.0, 0.0), byte: 0, rune: 0, string: "", bool: false}, {int: 0, int8: 0, int16: 0, int32: 0, int64: 0, uint: 0, uint8: 0, uint16: 0, uint32: 0, uint64: 0, uintptr: 0, float32: 0.0, float64: 0.0, complex64: complex(float32(0.0), float32(0.0)), complex128: complex(0.0, 0.0), byte: 0, rune: 0, string: "", bool: false}, {int: 0, int8: 0, int16: 0, int32: 0, int64: 0, uint: 0, uint8: 0, uint16: 0, uint32: 0, uint64: 0, uintptr: 0, float32: 0.0, float64: 0.0, complex64: complex(float32(0.0), float32(0.0)), complex128: complex(0.0, 0.0), byte: 0, rune: 0, string: "", bool: false}, {int: 0, int8: 0, int16: 0, int32: 0, int64: 0, uint: 0, uint8: 0, uint16: 0, uint32: 0, uint64: 0, uintptr: 0, float32: 0.0, float64: 0.0, complex64: complex(float32(0.0), float32(0.0)), complex128: complex(0.0, 0.0), byte: 0, rune: 0, string: "", bool: false}, {int: 0, int8: 0, int16: 0, int32: 0, int64: 0, uint: 0, uint8: 0, uint16: 0, uint32: 0, uint64: 0, uintptr: 0, float32: 0.0, float64: 0.0, complex64: complex(float32(0.0), float32(0.0)), complex128: complex(0.0, 0.0), byte: 0, rune: 0, string: "", bool: false}, {int: 0, int8: 0, int16: 0, int32: 0, int64: 0, uint: 0, uint8: 0, uint16: 0, uint32: 0, uint64: 0, uintptr: 0, float32: 0.0, float64: 0.0, complex64: complex(float32(0.0), float32(0.0)), complex128: complex(0.0, 0.0), byte: 0, rune: 0, string: "", bool: false}, {int: 0, int8: 0, int16: 0, int32: 0, int64: 0, uint: 0, uint8: 0, uint16: 0, uint32: 0, uint64: 0, uintptr: 0, float32: 0.0, float64: 0.0, complex64: complex(float32(0.0), float32(0.0)), complex128: complex(0.0, 0.0), byte: 0, rune: 0, string: "", bool: false}, {int: 0, int8: 0, int16: 0, int32: 0, int64: 0, uint: 0, uint8: 0, uint16: 0, uint32: 0, uint64: 0, uintptr: 0, float32: 0.0, float64: 0.0, complex64: complex(float32(0.0), float32(0.0)), complex128: complex(0.0, 0.0), byte: 0, rune: 0, string: "", bool: false}, {int: 0, int8: 0, int16: 0, int32: 0, int64: 0, uint: 0, uint8: 0, uint16: 0, uint32: 0, uint64: 0, uintptr: 0, float32: 0.0, float64: 0.0, complex64: complex(float32(0.0), float32(0.0)), complex128: complex(0.0, 0.0), byte: 0, rune: 0, string: "", bool: false}}, actualVal) } func TestArrayOfArrayOfUintByUtGoFuzzer(t *testing.T) { @@ -228,9 +229,9 @@ func TestReturnErrorOrNilWithNonNilErrorByUtGoFuzzer2(t *testing.T) { } func TestExternalStructByUtGoFuzzer(t *testing.T) { - actualVal := ExternalStruct(difflib.Match{A: 9223372036854775807, B: -1, Size: -9223372036854775808}, Structure{int: -1, int8: 1, int16: -32768, int32: 2147483647, int64: 1, uint: 1, uint8: 1, uint16: 1, uint32: 1, uint64: 18446744073709551615, uintptr: 18446744073709551615, float32: 0.14660162, float64: 0.7051747444754559, complex64: complex(0.14660162, 0.14660162), complex128: complex(0.7051747444754559, 0.7051747444754559), byte: 1, rune: 0, string: "", bool: false}) + actualVal := ExternalStruct(difflib.Match{A: 9223372036854775807, B: -1, Size: -9223372036854775808}, Structure{int: -1, int8: 1, int16: -32768, int32: 2147483647, int64: 1, uint: 1, uint8: 1, uint16: 1, uint32: 1, uint64: 18446744073709551615, uintptr: 18446744073709551615, float32: 0.94124913, float64: 0.17597680203548016, complex64: complex(0.94124913, 0.94124913), complex128: complex(0.17597680203548016, 0.17597680203548016), byte: 1, rune: 0, string: "", bool: false}) - assert.Equal(t, Structure{int: -1, int8: 1, int16: -32768, int32: 2147483647, int64: 1, uint: 1, uint8: 1, uint16: 1, uint32: 1, uint64: 18446744073709551615, uintptr: 18446744073709551615, float32: 0.14660162, float64: 0.7051747444754559, complex64: complex(float32(0.14660162), float32(0.14660162)), complex128: complex(0.7051747444754559, 0.7051747444754559), byte: 1, rune: 0, string: "", bool: false}, actualVal) + assert.Equal(t, Structure{int: -1, int8: 1, int16: -32768, int32: 2147483647, int64: 1, uint: 1, uint8: 1, uint16: 1, uint32: 1, uint64: 18446744073709551615, uintptr: 18446744073709551615, float32: 0.94124913, float64: 0.17597680203548016, complex64: complex(float32(0.94124913), float32(0.94124913)), complex128: complex(0.17597680203548016, 0.17597680203548016), byte: 1, rune: 0, string: "", bool: false}, actualVal) } func TestExternalStructWithAliasByUtGoFuzzer(t *testing.T) { @@ -258,19 +259,15 @@ func TestSliceOfStringByUtGoFuzzer(t *testing.T) { } func TestSliceOfStructsByUtGoFuzzer(t *testing.T) { - actualVal := SliceOfStructs([]Structure{{int: -1, int8: 0, int16: -1, int32: -1, int64: -9223372036854775808, uint: 1, uint8: 0, uint16: 65535, uint32: 1, uint64: 18446744073709551615, uintptr: 1, float32: 0.0013866425, float64: 0.5467397571984656, complex64: complex(0.0013866425, 0.0013866425), complex128: complex(0.5467397571984656, 0.5467397571984656), byte: 255, rune: 2147483647, string: "", bool: false}}) + actualVal := SliceOfStructs([]Structure{{int: -1, int8: 0, int16: -1, int32: -1, int64: -9223372036854775808, uint: 1, uint8: 0, uint16: 65535, uint32: 1, uint64: 18446744073709551615, uintptr: 1, float32: 0.31293595, float64: 0.12889715087377673, complex64: complex(0.31293595, 0.31293595), complex128: complex(0.12889715087377673, 0.12889715087377673), byte: 255, rune: 2147483647, string: "", bool: false}}) - assert.Equal(t, []Structure{{int: -1, int8: 0, int16: -1, int32: -1, int64: -9223372036854775808, uint: 1, uint8: 0, uint16: 65535, uint32: 1, uint64: 18446744073709551615, uintptr: 1, float32: 0.0013866425, float64: 0.5467397571984656, complex64: complex(float32(0.0013866425), float32(0.0013866425)), complex128: complex(0.5467397571984656, 0.5467397571984656), byte: 255, rune: 2147483647, string: "", bool: false}}, actualVal) + assert.Equal(t, []Structure{{int: -1, int8: 0, int16: -1, int32: -1, int64: -9223372036854775808, uint: 1, uint8: 0, uint16: 65535, uint32: 1, uint64: 18446744073709551615, uintptr: 1, float32: 0.31293595, float64: 0.12889715087377673, complex64: complex(float32(0.31293595), float32(0.31293595)), complex128: complex(0.12889715087377673, 0.12889715087377673), byte: 255, rune: 2147483647, string: "", bool: false}}, actualVal) } func TestSliceOfStructsWithNanByUtGoFuzzer(t *testing.T) { - actualVal := SliceOfStructsWithNan([]Structure{{int: 1, int8: 0, int16: -1, int32: -1, int64: -9223372036854775808, uint: 1, uint8: 0, uint16: 65535, uint32: 1, uint64: 18446744073709551615, uintptr: 1, float32: 0.96448684, float64: 0.5629496738983792, complex64: complex(0.96448684, 0.96448684), complex128: complex(0.5629496738983792, 0.5629496738983792), byte: 255, rune: 2147483647, string: "", bool: false}}) + actualVal := SliceOfStructsWithNan([]Structure{{int: 1, int8: 0, int16: -1, int32: -1, int64: -9223372036854775808, uint: 1, uint8: 0, uint16: 65535, uint32: 1, uint64: 18446744073709551615, uintptr: 1, float32: 0.14660162, float64: 0.7051747444754559, complex64: complex(0.14660162, 0.14660162), complex128: complex(0.7051747444754559, 0.7051747444754559), byte: 255, rune: 2147483647, string: "", bool: false}}) - assert.NotEqual(t, []Structure{{int: 1, int8: 0, int16: -1, int32: -1, int64: -9223372036854775808, uint: 1, uint8: 0, uint16: 65535, uint32: 1, uint64: 18446744073709551615, uintptr: 1, float32: 0.96448684, float64: math.NaN(), complex64: complex(float32(0.96448684), float32(0.96448684)), complex128: complex(0.5629496738983792, 0.5629496738983792), byte: 255, rune: 2147483647, string: "", bool: false}}, actualVal) -} - -func TestSliceOfStructsWithNanPanicsByUtGoFuzzer(t *testing.T) { - assert.PanicsWithError(t, "runtime error: index out of range [0] with length 0", func() { SliceOfStructsWithNan([]Structure{}) }) + assert.NotEqual(t, []Structure{{int: 1, int8: 0, int16: -1, int32: -1, int64: -9223372036854775808, uint: 1, uint8: 0, uint16: 65535, uint32: 1, uint64: 18446744073709551615, uintptr: 1, float32: 0.14660162, float64: math.NaN(), complex64: complex(float32(0.14660162), float32(0.14660162)), complex128: complex(0.7051747444754559, 0.7051747444754559), byte: 255, rune: 2147483647, string: "", bool: false}}, actualVal) } func TestSliceOfSliceOfByteByUtGoFuzzer(t *testing.T) { @@ -290,3 +287,9 @@ func TestSliceOfArrayOfIntByUtGoFuzzer(t *testing.T) { assert.Equal(t, [][5]int{{0, 0, 0, 0, 0}}, actualVal) } + +func TestExportedStructWithEmbeddedUnexportedStructByUtGoFuzzer(t *testing.T) { + actualVal := ExportedStructWithEmbeddedUnexportedStruct(nested.ExportedStruct{}) + + assert.Equal(t, nested.ExportedStruct{}, actualVal) +} diff --git a/utbot-go/src/main/kotlin/org/utbot/go/api/GoTypesApi.kt b/utbot-go/src/main/kotlin/org/utbot/go/api/GoTypesApi.kt index f5fa7649e4..c3c8c54423 100644 --- a/utbot-go/src/main/kotlin/org/utbot/go/api/GoTypesApi.kt +++ b/utbot-go/src/main/kotlin/org/utbot/go/api/GoTypesApi.kt @@ -1,6 +1,7 @@ package org.utbot.go.api import org.utbot.go.framework.api.go.GoFieldId +import org.utbot.go.framework.api.go.GoNamedTypeId import org.utbot.go.framework.api.go.GoPackage import org.utbot.go.framework.api.go.GoTypeId @@ -31,7 +32,7 @@ class GoStructTypeId( implementsError: Boolean, override val sourcePackage: GoPackage, val fields: List, -) : GoTypeId(name, implementsError = implementsError) { +) : GoNamedTypeId(name, implementsError = implementsError) { val packageName: String = sourcePackage.packageName val packagePath: String = sourcePackage.packagePath override val canonicalName: String = "${sourcePackage.packageName}.$name" @@ -102,7 +103,7 @@ class GoInterfaceTypeId( name: String, implementsError: Boolean, override val sourcePackage: GoPackage, -) : GoTypeId(name, implementsError = implementsError) { +) : GoNamedTypeId(name, implementsError = implementsError) { val packageName: String = sourcePackage.packageName val packagePath: String = sourcePackage.packagePath override val canonicalName: String = if (packageName != "") { diff --git a/utbot-go/src/main/kotlin/org/utbot/go/api/util/GoTypesApiUtil.kt b/utbot-go/src/main/kotlin/org/utbot/go/api/util/GoTypesApiUtil.kt index d043d6144a..97532c58fa 100644 --- a/utbot-go/src/main/kotlin/org/utbot/go/api/util/GoTypesApiUtil.kt +++ b/utbot-go/src/main/kotlin/org/utbot/go/api/util/GoTypesApiUtil.kt @@ -1,6 +1,7 @@ package org.utbot.go.api.util import org.utbot.go.api.* +import org.utbot.go.framework.api.go.GoPackage import org.utbot.go.framework.api.go.GoTypeId import org.utbot.go.framework.api.go.GoUtModel import kotlin.reflect.KClass @@ -156,15 +157,20 @@ fun GoTypeId.goDefaultValueModel(): GoUtModel = when (this) { else -> GoUtNilModel(this) } -fun GoTypeId.getAllStructTypes(): Set = when (this) { - is GoStructTypeId -> fields.fold(setOf(this)) { acc: Set, field -> - acc + (field.declaringType).getAllStructTypes() +fun GoTypeId.getAllVisibleStructTypes(goPackage: GoPackage): Set = when (this) { + is GoStructTypeId -> if (this.sourcePackage == goPackage || this.exported()) { + fields.fold(setOf(this)) { acc: Set, field -> + acc + (field.declaringType).getAllVisibleStructTypes(goPackage) + } + } else { + emptySet() } - is GoArrayTypeId, is GoSliceTypeId -> elementTypeId!!.getAllStructTypes() + is GoArrayTypeId, is GoSliceTypeId -> elementTypeId!!.getAllVisibleStructTypes(goPackage) else -> emptySet() } -fun List.getAllStructTypes(): Set = this.fold(emptySet()) { acc, type -> - acc + type.getAllStructTypes() -} +fun List.getAllVisibleStructTypes(goPackage: GoPackage): Set = + this.fold(emptySet()) { acc, type -> + acc + type.getAllVisibleStructTypes(goPackage) + } diff --git a/utbot-go/src/main/kotlin/org/utbot/go/framework/api/go/GoApi.kt b/utbot-go/src/main/kotlin/org/utbot/go/framework/api/go/GoApi.kt index c2f0ed3ce2..36f52f9124 100644 --- a/utbot-go/src/main/kotlin/org/utbot/go/framework/api/go/GoApi.kt +++ b/utbot-go/src/main/kotlin/org/utbot/go/framework/api/go/GoApi.kt @@ -18,6 +18,14 @@ abstract class GoTypeId( override fun toString(): String = canonicalName } +abstract class GoNamedTypeId( + name: String, + elementTypeId: GoTypeId? = null, + implementsError: Boolean = false +) : GoTypeId(name, elementTypeId, implementsError) { + fun exported(): Boolean = name.first().isUpperCase() +} + /** * Parent class for all Go models. * diff --git a/utbot-go/src/main/kotlin/org/utbot/go/imports/GoImportsResolver.kt b/utbot-go/src/main/kotlin/org/utbot/go/imports/GoImportsResolver.kt index 9fff1c5edc..0fbb0353c6 100644 --- a/utbot-go/src/main/kotlin/org/utbot/go/imports/GoImportsResolver.kt +++ b/utbot-go/src/main/kotlin/org/utbot/go/imports/GoImportsResolver.kt @@ -1,6 +1,6 @@ package org.utbot.go.imports -import org.utbot.go.api.util.getAllStructTypes +import org.utbot.go.api.util.getAllVisibleStructTypes import org.utbot.go.framework.api.go.GoImport import org.utbot.go.framework.api.go.GoPackage import org.utbot.go.framework.api.go.GoTypeId @@ -12,7 +12,7 @@ object GoImportsResolver { sourcePackage: GoPackage, busyImports: Set = emptySet() ): Set { - val structTypes = types.getAllStructTypes() + val structTypes = types.getAllVisibleStructTypes(sourcePackage) val result = busyImports.toMutableSet() val busyAliases = busyImports.map { it.alias ?: it.goPackage.packageName }.toMutableSet() structTypes.map { it.sourcePackage }.distinct().filter { it != sourcePackage }.forEach { goPackage -> diff --git a/utbot-go/src/main/kotlin/org/utbot/go/worker/GoCodeTemplates.kt b/utbot-go/src/main/kotlin/org/utbot/go/worker/GoCodeTemplates.kt index 01cf8760f7..984b94602a 100644 --- a/utbot-go/src/main/kotlin/org/utbot/go/worker/GoCodeTemplates.kt +++ b/utbot-go/src/main/kotlin/org/utbot/go/worker/GoCodeTemplates.kt @@ -421,16 +421,20 @@ object GoCodeTemplates { }, nil case reflect.Struct: fields := reflect.VisibleFields(valueOfRes.Type()) - resultValues := make([]__FieldValue__, len(fields)) - for i, field := range fields { + resultValues := make([]__FieldValue__, 0, valueOfRes.NumField()) + for _, field := range fields { + if len(field.Index) != 1 { + continue + } + res, err := __convertReflectValueToRawValue__(valueOfRes.FieldByName(field.Name)) __checkErrorAndExit__(err) - resultValues[i] = __FieldValue__{ + resultValues = append(resultValues, __FieldValue__{ Name: field.Name, Value: res, IsExported: field.IsExported(), - } + }) } return __StructValue__{ Type: valueOfRes.Type().String(), diff --git a/utbot-go/src/main/kotlin/org/utbot/go/worker/GoWorkerCodeGenerationHelper.kt b/utbot-go/src/main/kotlin/org/utbot/go/worker/GoWorkerCodeGenerationHelper.kt index 1d4e7b6806..aca57a5a45 100644 --- a/utbot-go/src/main/kotlin/org/utbot/go/worker/GoWorkerCodeGenerationHelper.kt +++ b/utbot-go/src/main/kotlin/org/utbot/go/worker/GoWorkerCodeGenerationHelper.kt @@ -2,7 +2,7 @@ package org.utbot.go.worker import org.utbot.go.api.GoUtFile import org.utbot.go.api.GoUtFunction -import org.utbot.go.api.util.getAllStructTypes +import org.utbot.go.api.util.getAllVisibleStructTypes import org.utbot.go.framework.api.go.GoImport import org.utbot.go.framework.api.go.GoPackage import org.utbot.go.simplecodegeneration.GoFileCodeBuilder @@ -89,7 +89,7 @@ internal object GoWorkerCodeGenerationHelper { val workerTestFunctionCode = generateWorkerTestFunctionCode(functions, eachExecutionTimeoutMillis, port) val types = functions.flatMap { it.parameters }.map { it.type } - val structTypes = types.getAllStructTypes() + val structTypes = types.getAllVisibleStructTypes(destinationPackage) val aliases = imports.associate { it.goPackage to it.alias } fileCodeBuilder.addTopLevelElements( From d558dd9f3abcb355b81f60832998758d6f81fedd Mon Sep 17 00:00:00 2001 From: Egipti Pavel Date: Mon, 13 Mar 2023 11:15:42 +0300 Subject: [PATCH 2/7] Delete timeout for worker execution --- .../src/main/kotlin/org/utbot/go/logic/GoTestCasesGenerator.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/utbot-go/src/main/kotlin/org/utbot/go/logic/GoTestCasesGenerator.kt b/utbot-go/src/main/kotlin/org/utbot/go/logic/GoTestCasesGenerator.kt index 161bf838e7..009d38d5b4 100644 --- a/utbot-go/src/main/kotlin/org/utbot/go/logic/GoTestCasesGenerator.kt +++ b/utbot-go/src/main/kotlin/org/utbot/go/logic/GoTestCasesGenerator.kt @@ -63,7 +63,7 @@ object GoTestCasesGenerator { // starting worker process val testFunctionName = GoWorkerCodeGenerationHelper.workerTestFunctionName val command = listOf( - goExecutableAbsolutePath, "test", "-run", testFunctionName + goExecutableAbsolutePath, "test", "-run", testFunctionName, "-timeout", "0" ) val sourceFileDir = File(sourceFile.absoluteDirectoryPath) val processStartTime = System.currentTimeMillis() From 94b7a2d20a0fe4f69814a4b00ed0c8b74a4c10fe Mon Sep 17 00:00:00 2001 From: Egipti Pavel Date: Wed, 22 Mar 2023 18:57:00 +0300 Subject: [PATCH 3/7] Add logging when worker doesn't finish --- .../org/utbot/go/logic/GoTestCasesGenerator.kt | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/utbot-go/src/main/kotlin/org/utbot/go/logic/GoTestCasesGenerator.kt b/utbot-go/src/main/kotlin/org/utbot/go/logic/GoTestCasesGenerator.kt index 009d38d5b4..4e4952a310 100644 --- a/utbot-go/src/main/kotlin/org/utbot/go/logic/GoTestCasesGenerator.kt +++ b/utbot-go/src/main/kotlin/org/utbot/go/logic/GoTestCasesGenerator.kt @@ -112,7 +112,13 @@ object GoTestCasesGenerator { val processHasExited = process.waitFor(endOfWorkerExecutionTimeout, TimeUnit.MILLISECONDS) if (!processHasExited) { process.destroy() - throw TimeoutException("Timeout exceeded: Worker didn't finish") + val processOutput = InputStreamReader(process.inputStream).readText() + throw TimeoutException( + StringBuilder() + .append("Timeout exceeded: Worker didn't finish. Process output: ") + .appendLine() + .append(processOutput).toString() + ) } val exitCode = process.exitValue() if (exitCode != 0) { @@ -132,7 +138,13 @@ object GoTestCasesGenerator { val processHasExited = process.waitFor(endOfWorkerExecutionTimeout, TimeUnit.MILLISECONDS) if (!processHasExited) { process.destroy() - logger.error { "Timeout exceeded: Worker didn't finish" } + val processOutput = InputStreamReader(process.inputStream).readText() + logger.error { + StringBuilder() + .append("Timeout exceeded: Worker didn't finish. Process output: ") + .appendLine() + .append(processOutput).toString() + } } val exitCode = process.exitValue() if (exitCode != 0) { From 2dc4ce9a82ccfb05fcb030c9fdcae585ca1aa68c Mon Sep 17 00:00:00 2001 From: Egipti Pavel Date: Fri, 24 Mar 2023 11:59:54 +0300 Subject: [PATCH 4/7] Add sample file --- utbot-go/go-samples/simple/nested/nested.go | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 utbot-go/go-samples/simple/nested/nested.go diff --git a/utbot-go/go-samples/simple/nested/nested.go b/utbot-go/go-samples/simple/nested/nested.go new file mode 100644 index 0000000000..772d6cc2a6 --- /dev/null +++ b/utbot-go/go-samples/simple/nested/nested.go @@ -0,0 +1,9 @@ +package nested + +type unexportedStruct struct { + int +} + +type ExportedStruct struct { + unexportedStruct +} From eeed1f69bd2249ecd562c82ba43748d9b37b189b Mon Sep 17 00:00:00 2001 From: Egipti Pavel Date: Sun, 26 Mar 2023 21:19:13 +0300 Subject: [PATCH 5/7] Add message about failed parsing of source code analysis result --- .../utbot/go/gocodeanalyzer/AnalysisResults.kt | 4 +++- .../go/gocodeanalyzer/GoSourceCodeAnalyzer.kt | 8 ++++++++ .../go/generator/GoUtTestsDialogProcessor.kt | 16 +++++++++++++--- 3 files changed, 24 insertions(+), 4 deletions(-) diff --git a/utbot-go/src/main/kotlin/org/utbot/go/gocodeanalyzer/AnalysisResults.kt b/utbot-go/src/main/kotlin/org/utbot/go/gocodeanalyzer/AnalysisResults.kt index d3e60a9b48..dafd6308ee 100644 --- a/utbot-go/src/main/kotlin/org/utbot/go/gocodeanalyzer/AnalysisResults.kt +++ b/utbot-go/src/main/kotlin/org/utbot/go/gocodeanalyzer/AnalysisResults.kt @@ -115,4 +115,6 @@ internal data class AnalysisResult( val notFoundFunctionsNames: List ) -internal data class AnalysisResults(val results: List, val intSize: Int, val maxTraceLength: Int) \ No newline at end of file +internal data class AnalysisResults(val results: List, val intSize: Int, val maxTraceLength: Int) + +class GoSourceCodeAnalysisException(s: String) : Exception(s) \ No newline at end of file diff --git a/utbot-go/src/main/kotlin/org/utbot/go/gocodeanalyzer/GoSourceCodeAnalyzer.kt b/utbot-go/src/main/kotlin/org/utbot/go/gocodeanalyzer/GoSourceCodeAnalyzer.kt index 4ae8a5b0bd..df5e73f224 100644 --- a/utbot-go/src/main/kotlin/org/utbot/go/gocodeanalyzer/GoSourceCodeAnalyzer.kt +++ b/utbot-go/src/main/kotlin/org/utbot/go/gocodeanalyzer/GoSourceCodeAnalyzer.kt @@ -1,5 +1,6 @@ package org.utbot.go.gocodeanalyzer +import com.beust.klaxon.KlaxonException import org.utbot.common.FileUtil.extractDirectoryFromArchive import org.utbot.common.scanForResourcesContaining import org.utbot.go.api.GoPrimitiveTypeId @@ -114,6 +115,13 @@ object GoSourceCodeAnalyzer { analysisResult.notFoundFunctionsNames ) }, intSize, maxTraceLength) + } catch (e: KlaxonException) { + throw GoSourceCodeAnalysisException( + buildString { + appendLine("An error occurred while parsing the result of the source code analysis.") + appendLine("Please try running \"go mod tidy\" in the project's root directory or fix any errors in the code.") + } + ) } finally { // TODO correctly? analysisTargetsFile.delete() diff --git a/utbot-intellij-go/src/main/kotlin/org/utbot/intellij/plugin/language/go/generator/GoUtTestsDialogProcessor.kt b/utbot-intellij-go/src/main/kotlin/org/utbot/intellij/plugin/language/go/generator/GoUtTestsDialogProcessor.kt index fbca87c656..313ebcc613 100644 --- a/utbot-intellij-go/src/main/kotlin/org/utbot/intellij/plugin/language/go/generator/GoUtTestsDialogProcessor.kt +++ b/utbot-intellij-go/src/main/kotlin/org/utbot/intellij/plugin/language/go/generator/GoUtTestsDialogProcessor.kt @@ -12,10 +12,12 @@ import com.intellij.openapi.progress.ProgressManager import com.intellij.openapi.progress.Task import com.intellij.openapi.project.Project import com.intellij.openapi.ui.Messages +import org.utbot.go.gocodeanalyzer.GoSourceCodeAnalysisException import org.utbot.go.logic.GoUtTestsGenerationConfig import org.utbot.intellij.plugin.language.go.models.GenerateGoTestsModel import org.utbot.intellij.plugin.language.go.ui.GenerateGoTestsDialogWindow import org.utbot.intellij.plugin.language.go.ui.utils.resolveGoExecutablePath +import org.utbot.intellij.plugin.ui.utils.showErrorDialogLater object GoUtTestsDialogProcessor { @@ -88,9 +90,17 @@ object GoUtTestsDialogProcessor { model.allFunctionExecutionTimeoutMillis ) - IntellijGoUtTestsGenerationController(model, indicator).generateTests( - selectedFunctionsNamesBySourceFiles, testsGenerationConfig - ) { indicator.isCanceled } + try { + IntellijGoUtTestsGenerationController(model, indicator).generateTests( + selectedFunctionsNamesBySourceFiles, testsGenerationConfig + ) { indicator.isCanceled } + } catch (e: GoSourceCodeAnalysisException) { + showErrorDialogLater( + model.project, + e.message ?: "Please try running \"go mod tidy\" in the project's root directory or fix any errors in the code.", + title = "Unit tests generation is cancelled" + ) + } } }) } From 2fee27d6440aa754b2213fd54a13f6fe6ef44730 Mon Sep 17 00:00:00 2001 From: Egipti Pavel Date: Sun, 26 Mar 2023 21:59:17 +0300 Subject: [PATCH 6/7] Change StringBuilder on buildString --- .../utbot/go/logic/GoTestCasesGenerator.kt | 32 +++++++++---------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/utbot-go/src/main/kotlin/org/utbot/go/logic/GoTestCasesGenerator.kt b/utbot-go/src/main/kotlin/org/utbot/go/logic/GoTestCasesGenerator.kt index 4e4952a310..83a31b93b7 100644 --- a/utbot-go/src/main/kotlin/org/utbot/go/logic/GoTestCasesGenerator.kt +++ b/utbot-go/src/main/kotlin/org/utbot/go/logic/GoTestCasesGenerator.kt @@ -114,20 +114,20 @@ object GoTestCasesGenerator { process.destroy() val processOutput = InputStreamReader(process.inputStream).readText() throw TimeoutException( - StringBuilder() - .append("Timeout exceeded: Worker didn't finish. Process output: ") - .appendLine() - .append(processOutput).toString() + buildString { + appendLine("Timeout exceeded: Worker didn't finish. Process output: ") + appendLine(processOutput) + } ) } val exitCode = process.exitValue() if (exitCode != 0) { val processOutput = InputStreamReader(process.inputStream).readText() throw RuntimeException( - StringBuilder() - .append("Execution of functions from $sourceFile in child process failed with non-zero exit code = $exitCode: ") - .appendLine() - .append(processOutput).toString() + buildString { + appendLine("Execution of functions from $sourceFile in child process failed with non-zero exit code = $exitCode: ") + appendLine(processOutput) + } ) } } catch (e: TimeoutException) { @@ -140,20 +140,20 @@ object GoTestCasesGenerator { process.destroy() val processOutput = InputStreamReader(process.inputStream).readText() logger.error { - StringBuilder() - .append("Timeout exceeded: Worker didn't finish. Process output: ") - .appendLine() - .append(processOutput).toString() + buildString { + appendLine("Timeout exceeded: Worker didn't finish. Process output: ") + appendLine(processOutput) + } } } val exitCode = process.exitValue() if (exitCode != 0) { val processOutput = InputStreamReader(process.inputStream).readText() logger.error { - StringBuilder() - .append("Execution of functions from $sourceFile in child process failed with non-zero exit code = $exitCode: ") - .appendLine() - .append(processOutput).toString() + buildString { + appendLine("Execution of functions from $sourceFile in child process failed with non-zero exit code = $exitCode: ") + appendLine(processOutput) + } } } From 318f4fcf9b47241a556a6f368cb2e8312b8ea3dc Mon Sep 17 00:00:00 2001 From: Egipti Pavel Date: Sun, 26 Mar 2023 23:04:49 +0300 Subject: [PATCH 7/7] Add message about failed starting the worker --- .../go/gocodeanalyzer/AnalysisResults.kt | 2 +- .../go/gocodeanalyzer/GoSourceCodeAnalyzer.kt | 10 +++---- .../utbot/go/logic/GoTestCasesGenerator.kt | 4 +-- .../utbot/go/worker/RawExecutionResults.kt | 2 ++ .../go/generator/GoUtTestsDialogProcessor.kt | 27 ++++++++++++++++--- 5 files changed, 33 insertions(+), 12 deletions(-) diff --git a/utbot-go/src/main/kotlin/org/utbot/go/gocodeanalyzer/AnalysisResults.kt b/utbot-go/src/main/kotlin/org/utbot/go/gocodeanalyzer/AnalysisResults.kt index dafd6308ee..60a9e9f59e 100644 --- a/utbot-go/src/main/kotlin/org/utbot/go/gocodeanalyzer/AnalysisResults.kt +++ b/utbot-go/src/main/kotlin/org/utbot/go/gocodeanalyzer/AnalysisResults.kt @@ -117,4 +117,4 @@ internal data class AnalysisResult( internal data class AnalysisResults(val results: List, val intSize: Int, val maxTraceLength: Int) -class GoSourceCodeAnalysisException(s: String) : Exception(s) \ No newline at end of file +class GoParsingSourceCodeAnalysisResultException(s: String, t: Throwable) : Exception(s, t) \ No newline at end of file diff --git a/utbot-go/src/main/kotlin/org/utbot/go/gocodeanalyzer/GoSourceCodeAnalyzer.kt b/utbot-go/src/main/kotlin/org/utbot/go/gocodeanalyzer/GoSourceCodeAnalyzer.kt index df5e73f224..350ea7a4e3 100644 --- a/utbot-go/src/main/kotlin/org/utbot/go/gocodeanalyzer/GoSourceCodeAnalyzer.kt +++ b/utbot-go/src/main/kotlin/org/utbot/go/gocodeanalyzer/GoSourceCodeAnalyzer.kt @@ -115,12 +115,10 @@ object GoSourceCodeAnalyzer { analysisResult.notFoundFunctionsNames ) }, intSize, maxTraceLength) - } catch (e: KlaxonException) { - throw GoSourceCodeAnalysisException( - buildString { - appendLine("An error occurred while parsing the result of the source code analysis.") - appendLine("Please try running \"go mod tidy\" in the project's root directory or fix any errors in the code.") - } + } catch (exception: KlaxonException) { + throw GoParsingSourceCodeAnalysisResultException( + "An error occurred while parsing the result of the source code analysis.", + exception ) } finally { // TODO correctly? diff --git a/utbot-go/src/main/kotlin/org/utbot/go/logic/GoTestCasesGenerator.kt b/utbot-go/src/main/kotlin/org/utbot/go/logic/GoTestCasesGenerator.kt index 83a31b93b7..8a3e5f6530 100644 --- a/utbot-go/src/main/kotlin/org/utbot/go/logic/GoTestCasesGenerator.kt +++ b/utbot-go/src/main/kotlin/org/utbot/go/logic/GoTestCasesGenerator.kt @@ -12,6 +12,7 @@ import org.utbot.go.imports.GoImportsResolver import org.utbot.go.util.executeCommandByNewProcessOrFailWithoutWaiting import org.utbot.go.worker.GoWorker import org.utbot.go.worker.GoWorkerCodeGenerationHelper +import org.utbot.go.worker.GoWorkerFailedException import java.io.File import java.io.InputStreamReader import java.net.ServerSocket @@ -78,8 +79,7 @@ object GoTestCasesGenerator { } catch (e: SocketTimeoutException) { val processHasExited = process.waitFor(endOfWorkerExecutionTimeout, TimeUnit.MILLISECONDS) if (processHasExited) { - val processOutput = InputStreamReader(process.inputStream).readText() - throw TimeoutException("Timeout exceeded: Worker not connected. Process output: $processOutput") + throw GoWorkerFailedException("An error occurred while starting the worker.") } else { process.destroy() } diff --git a/utbot-go/src/main/kotlin/org/utbot/go/worker/RawExecutionResults.kt b/utbot-go/src/main/kotlin/org/utbot/go/worker/RawExecutionResults.kt index cb9e714e4e..78b72c3920 100644 --- a/utbot-go/src/main/kotlin/org/utbot/go/worker/RawExecutionResults.kt +++ b/utbot-go/src/main/kotlin/org/utbot/go/worker/RawExecutionResults.kt @@ -131,6 +131,8 @@ private object RawValuesCodes { const val COMPLEX_PARTS_DELIMITER = "@" } +class GoWorkerFailedException(s: String) : Exception(s) + fun convertRawExecutionResultToExecutionResult( rawExecutionResult: RawExecutionResult, functionResultTypes: List, diff --git a/utbot-intellij-go/src/main/kotlin/org/utbot/intellij/plugin/language/go/generator/GoUtTestsDialogProcessor.kt b/utbot-intellij-go/src/main/kotlin/org/utbot/intellij/plugin/language/go/generator/GoUtTestsDialogProcessor.kt index 313ebcc613..d7723a04be 100644 --- a/utbot-intellij-go/src/main/kotlin/org/utbot/intellij/plugin/language/go/generator/GoUtTestsDialogProcessor.kt +++ b/utbot-intellij-go/src/main/kotlin/org/utbot/intellij/plugin/language/go/generator/GoUtTestsDialogProcessor.kt @@ -12,8 +12,9 @@ import com.intellij.openapi.progress.ProgressManager import com.intellij.openapi.progress.Task import com.intellij.openapi.project.Project import com.intellij.openapi.ui.Messages -import org.utbot.go.gocodeanalyzer.GoSourceCodeAnalysisException +import org.utbot.go.gocodeanalyzer.GoParsingSourceCodeAnalysisResultException import org.utbot.go.logic.GoUtTestsGenerationConfig +import org.utbot.go.worker.GoWorkerFailedException import org.utbot.intellij.plugin.language.go.models.GenerateGoTestsModel import org.utbot.intellij.plugin.language.go.ui.GenerateGoTestsDialogWindow import org.utbot.intellij.plugin.language.go.ui.utils.resolveGoExecutablePath @@ -21,6 +22,9 @@ import org.utbot.intellij.plugin.ui.utils.showErrorDialogLater object GoUtTestsDialogProcessor { + private const val helpMessage: String = + "Please try running \"go mod tidy\" in one of the project directories or fix any errors in the code." + fun createDialogAndGenerateTests( project: Project, module: Module, @@ -77,6 +81,16 @@ object GoUtTestsDialogProcessor { ) } + private fun buildErrorMessage(exception: Exception): String = + if (exception.message == null) { + helpMessage + } else { + buildString { + appendLine(exception.message) + appendLine(helpMessage) + } + } + private fun createTests(model: GenerateGoTestsModel) { ProgressManager.getInstance().run(object : Task.Backgroundable(model.project, "Generate Go tests") { override fun run(indicator: ProgressIndicator) { @@ -94,10 +108,17 @@ object GoUtTestsDialogProcessor { IntellijGoUtTestsGenerationController(model, indicator).generateTests( selectedFunctionsNamesBySourceFiles, testsGenerationConfig ) { indicator.isCanceled } - } catch (e: GoSourceCodeAnalysisException) { + } catch (e: GoParsingSourceCodeAnalysisResultException) { + val errorMessage = buildErrorMessage(e) + showErrorDialogLater( + model.project, + errorMessage, + title = "Unit tests generation is cancelled" + ) + } catch (e: GoWorkerFailedException) { showErrorDialogLater( model.project, - e.message ?: "Please try running \"go mod tidy\" in the project's root directory or fix any errors in the code.", + helpMessage, title = "Unit tests generation is cancelled" ) }