|
| 1 | +package tests |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "reflect" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/ovechkin-dm/go-dyno/pkg/dyno" |
| 9 | +) |
| 10 | + |
| 11 | +type Foo interface { |
| 12 | + Bar() string |
| 13 | + Baz() int |
| 14 | + Arr(arr [30]int, i int) int |
| 15 | + privateMethod() int |
| 16 | + MultiArg(i int, j int, a [2]int, f [2]float64) int |
| 17 | +} |
| 18 | + |
| 19 | +func TestToString(t *testing.T) { |
| 20 | + proxy, err := dyno.Dynamic[Foo](func(m reflect.Method, values []reflect.Value) []reflect.Value { |
| 21 | + return []reflect.Value{reflect.ValueOf("Hello")} |
| 22 | + }) |
| 23 | + if err != nil { |
| 24 | + t.Fatal(err) |
| 25 | + } |
| 26 | + s := fmt.Sprintf("%v", proxy) |
| 27 | + if s != "DynamicProxy" { |
| 28 | + t.Fatalf("Expected 'DynamicProxy', got '%s'", s) |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +func TestSingleMethod(t *testing.T) { |
| 33 | + proxy, err := dyno.Dynamic[Foo](func(m reflect.Method, values []reflect.Value) []reflect.Value { |
| 34 | + return []reflect.Value{reflect.ValueOf("Hello")} |
| 35 | + }) |
| 36 | + if err != nil { |
| 37 | + t.Fatal(err) |
| 38 | + } |
| 39 | + s := proxy.Bar() |
| 40 | + if s != "Hello" { |
| 41 | + t.Fatalf("Expected 'Hello', got '%s'", s) |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +func TestMultipleMethods(t *testing.T) { |
| 46 | + proxy, err := dyno.Dynamic[Foo](func(m reflect.Method, values []reflect.Value) []reflect.Value { |
| 47 | + switch m.Name { |
| 48 | + case "Bar": |
| 49 | + return []reflect.Value{reflect.ValueOf("Hello")} |
| 50 | + default: |
| 51 | + return []reflect.Value{reflect.ValueOf(42)} |
| 52 | + } |
| 53 | + }) |
| 54 | + if err != nil { |
| 55 | + t.Fatal(err) |
| 56 | + } |
| 57 | + s := proxy.Bar() |
| 58 | + if s != "Hello" { |
| 59 | + t.Fatalf("Expected 'Hello', got '%s'", s) |
| 60 | + } |
| 61 | + i := proxy.Baz() |
| 62 | + if i != 42 { |
| 63 | + t.Fatalf("Expected 42, got %d", i) |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +func TestStackAlignment(t *testing.T) { |
| 68 | + var arr [30]int |
| 69 | + for i := 0; i < 30; i++ { |
| 70 | + arr[i] = i |
| 71 | + } |
| 72 | + proxy, err := dyno.Dynamic[Foo](func(m reflect.Method, values []reflect.Value) []reflect.Value { |
| 73 | + sum := 0 |
| 74 | + for i := 0; i < len(values[0].Interface().([30]int)); i++ { |
| 75 | + sum += values[0].Interface().([30]int)[i] |
| 76 | + } |
| 77 | + return []reflect.Value{reflect.ValueOf(sum + int(values[1].Int()))} |
| 78 | + }) |
| 79 | + if err != nil { |
| 80 | + t.Fatal(err) |
| 81 | + } |
| 82 | + result := proxy.Arr(arr, 12) |
| 83 | + if result != 447 { |
| 84 | + t.Fatalf("Expected 447, got %d", result) |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +func TestPrivateIface(t *testing.T) { |
| 89 | + p, err := dyno.Dynamic[Foo](func(m reflect.Method, values []reflect.Value) []reflect.Value { |
| 90 | + return []reflect.Value{reflect.ValueOf(10)} |
| 91 | + }) |
| 92 | + if err != nil { |
| 93 | + t.FailNow() |
| 94 | + } |
| 95 | + result := p.privateMethod() |
| 96 | + if result != 10 { |
| 97 | + t.FailNow() |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +func TestStackAlign(t *testing.T) { |
| 102 | + var args []any |
| 103 | + p, err := dyno.Dynamic[Foo](func(m reflect.Method, values []reflect.Value) []reflect.Value { |
| 104 | + for i := range values { |
| 105 | + args = append(args, values[i].Interface()) |
| 106 | + } |
| 107 | + return []reflect.Value{reflect.ValueOf(values[0].Interface().(int) + 1)} |
| 108 | + }) |
| 109 | + if err != nil { |
| 110 | + t.FailNow() |
| 111 | + } |
| 112 | + result := p.MultiArg(10, 20, [2]int{123, 124}, [2]float64{123.1, 124.2}) |
| 113 | + if args[0] != 10 || args[1] != 20 || args[2] != [2]int{123, 124} || args[3] != [2]float64{123.1, 124.2} { |
| 114 | + fmt.Println("Got wrong args: ", args) |
| 115 | + t.FailNow() |
| 116 | + } |
| 117 | + if result != 11 { |
| 118 | + fmt.Println("Got wrong result: ", result) |
| 119 | + t.FailNow() |
| 120 | + } |
| 121 | +} |
0 commit comments