refactor: use reflect.TypeFor instead of reflect.TypeOf and improve related tests (#2101)

* refactor: using reflect.TypeFor

Signed-off-by: suranmiao <solsui@outlook.com>

* refactor: remove unused reflect.TypeFor calls and improve test assertions

* refactor: simplify TestSetup by removing reflect.TypeFor

* test: enhance test assertions and improve parallel execution in TestSetup

---------

Signed-off-by: suranmiao <solsui@outlook.com>
Co-authored-by: Adrian Gallagher <adrian.gallagher@thrasher.io>
This commit is contained in:
suranmiao
2025-12-10 07:54:54 +08:00
committed by GitHub
parent 8260aad9fd
commit 78382afb14
10 changed files with 75 additions and 123 deletions

View File

@@ -2,7 +2,6 @@ package gct
import (
"os"
"reflect"
"testing"
"time"
@@ -186,11 +185,7 @@ func TestExchangeOrderSubmit(t *testing.T) {
func TestAllModuleNames(t *testing.T) {
t.Parallel()
x := AllModuleNames()
xType := reflect.TypeOf(x).Kind()
if xType != reflect.Slice {
t.Errorf("AllModuleNames() should return slice instead received: %v", x)
}
require.NotEmpty(t, AllModuleNames(), "AllModuleNames must not return an empty slice")
}
func TestExchangeDepositAddress(t *testing.T) {

View File

@@ -1,18 +1,14 @@
package loader
import (
"reflect"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestGetModuleMap(t *testing.T) {
x := GetModuleMap()
xType := reflect.TypeOf(x).String()
if xType != "*tengo.ModuleMap" {
t.Fatalf("GetModuleMap() should return pointer to ModuleMap instead received: %v", x)
}
if x.Len() == 0 {
t.Fatal("expected GetModuleMap() to contain module results instead received 0 value")
}
require.NotNil(t, x, "GetModuleMap must not return nil")
assert.NotZero(t, x.Len(), "GetModuleMap should return a map with entries")
}

View File

@@ -3,7 +3,6 @@ package indicators
import (
"math/rand"
"os"
"reflect"
"testing"
"time"
@@ -394,42 +393,29 @@ func TestOBV(t *testing.T) {
}
func TestToFloat64(t *testing.T) {
value := 54.0
v, err := toFloat64(value)
if err != nil {
t.Fatal(err)
}
if reflect.TypeOf(v).Kind() != reflect.Float64 {
t.Fatalf("expected toFloat to return kind float64 received: %v", reflect.TypeOf(v).Kind())
}
v, err = toFloat64(int(value))
if err != nil {
t.Fatal(err)
}
if reflect.TypeOf(v).Kind() != reflect.Float64 {
t.Fatalf("expected toFloat to return kind float64 received: %v", reflect.TypeOf(v).Kind())
}
v, err = toFloat64(int32(value))
if err != nil {
t.Fatal(err)
}
if reflect.TypeOf(v).Kind() != reflect.Float64 {
t.Fatalf("expected toFloat to return kind float64 received: %v", reflect.TypeOf(v).Kind())
}
v, err = toFloat64(int64(value))
if err != nil {
t.Fatal(err)
}
if reflect.TypeOf(v).Kind() != reflect.Float64 {
t.Fatalf("expected toFloat to return kind float64 received: %v", reflect.TypeOf(v).Kind())
}
_, err = toFloat64("54")
if err == nil {
t.Fatalf("attempting to convert a string should fail but test passed")
t.Parallel()
for _, tc := range []struct {
name string
input any
expected float64
expectErr bool
}{
{"float64", 45.67, 45.67, false},
{"int", int(45), 45.0, false},
{"int32", int32(45), 45.0, false},
{"int64", int64(45), 45.0, false},
{"string", "45.67", 0, true},
} {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
result, err := toFloat64(tc.input)
if tc.expectErr {
assert.Error(t, err)
return
}
require.NoError(t, err)
assert.Equal(t, tc.expected, result)
})
}
}

View File

@@ -1,17 +1,11 @@
package ta
import (
"reflect"
"testing"
"github.com/stretchr/testify/require"
)
func TestGetModuleMap(t *testing.T) {
x := AllModuleNames()
xType := reflect.TypeOf(x).Kind()
if xType != reflect.Slice {
t.Fatalf("AllModuleNames() should return slice instead received: %v", x)
}
if len(x) != 9 {
t.Fatalf("unexpected results received expected 9 received: %v", len(x))
}
require.Len(t, AllModuleNames(), 9, "AllModuleNames must return 9 modules")
}

View File

@@ -4,7 +4,6 @@ import (
"errors"
"os"
"path/filepath"
"reflect"
"testing"
"time"
@@ -33,16 +32,9 @@ func TestNewVM(t *testing.T) {
manager := GctScriptManager{
config: configHelper(true, true, maxTestVirtualMachines),
}
x := manager.New()
if x != nil {
t.Error("Should not create a VM when manager not started")
}
require.Nil(t, manager.New(), "New must not create a VM when manager not started")
manager.started = 1
x = manager.New()
xType := reflect.TypeOf(x).String()
if xType != "*vm.VM" {
t.Fatalf("vm.New should return pointer to VM instead received: %v", x)
}
require.NotNil(t, manager.New(), "New must create a VM when manager is started")
}
func TestVMLoad(t *testing.T) {

View File

@@ -5,7 +5,6 @@ import (
"log"
"os"
"path/filepath"
"reflect"
"testing"
objects "github.com/d5/tengo/v2"
@@ -72,11 +71,8 @@ func TestMain(m *testing.M) {
}
func TestSetup(t *testing.T) {
x := Setup()
xType := reflect.TypeOf(x).String()
if xType != "*gct.Wrapper" {
t.Fatalf("SetupCommunicationManager() should return pointer to Wrapper instead received: %v", x)
}
t.Parallel()
require.NotNil(t, Setup(), "Setup must not return nil")
}
var (