wails/v2/internal/binding/generate_test.go
atterpac 9ebc000582 tests
2026-01-28 10:40:27 -07:00

184 lines
3.2 KiB
Go

package binding
import (
"testing"
"github.com/leaanthony/slicer"
"github.com/stretchr/testify/assert"
"github.com/wailsapp/wails/v2/internal/logger"
)
type BindForTest struct {
}
func (b *BindForTest) GetA() A {
return A{}
}
type A struct {
B B `json:"B"`
}
type B struct {
Name string `json:"name"`
}
func TestNestedStruct(t *testing.T) {
bind := &BindForTest{}
testBindings := NewBindings(logger.New(nil), []interface{}{bind}, []interface{}{}, false, []interface{}{})
namesStrSlicer := testBindings.getAllStructNames()
names := []string{}
namesStrSlicer.Each(func(s string) {
names = append(names, s)
})
assert.Contains(t, names, "binding.A")
assert.Contains(t, names, "binding.B")
}
func Test_goTypeToJSDocType(t *testing.T) {
tests := []struct {
name string
input string
want string
}{
{
name: "string",
input: "string",
want: "string",
},
{
name: "error",
input: "error",
want: "Error",
},
{
name: "int",
input: "int",
want: "number",
},
{
name: "int32",
input: "int32",
want: "number",
},
{
name: "uint",
input: "uint",
want: "number",
},
{
name: "uint32",
input: "uint32",
want: "number",
},
{
name: "float32",
input: "float32",
want: "number",
},
{
name: "float64",
input: "float64",
want: "number",
},
{
name: "bool",
input: "bool",
want: "boolean",
},
{
name: "interface{}",
input: "interface{}",
want: "any",
},
{
name: "[]byte",
input: "[]byte",
want: "string",
},
{
name: "[]int",
input: "[]int",
want: "Array<number>",
},
{
name: "[]bool",
input: "[]bool",
want: "Array<boolean>",
},
{
name: "anything else",
input: "foo",
want: "any",
},
{
name: "map",
input: "map[string]float64",
want: "Record<string, number>",
},
{
name: "map",
input: "map[string]map[string]float64",
want: "Record<string, Record<string, number>>",
},
{
name: "types",
input: "main.SomeType",
want: "main.SomeType",
},
{
name: "primitive_generic",
input: "main.ListData[string]",
want: "main.ListData_string_",
},
{
name: "stdlib_generic",
input: "main.ListData[*net/http.Request]",
want: "main.ListData_net_http_Request_",
},
}
b := &Bindings{}
var importNamespaces slicer.StringSlicer
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := b.goTypeToJSDocType(tt.input, &importNamespaces); got != tt.want {
t.Errorf("goTypeToJSDocType() = %v, want %v", got, tt.want)
}
})
}
}
func Test_goTypeToJSDocType_nullable(t *testing.T) {
tests := []struct {
name string
input string
want string
}{
{
name: "[]int nullable",
input: "[]int",
want: "Array<number> | null",
},
{
name: "[]bool nullable",
input: "[]bool",
want: "Array<boolean> | null",
},
{
name: "[]byte still string",
input: "[]byte",
want: "string",
},
}
b := &Bindings{useNullableSlices: true}
var importNamespaces slicer.StringSlicer
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := b.goTypeToJSDocType(tt.input, &importNamespaces); got != tt.want {
t.Errorf("goTypeToJSDocType() = %v, want %v", got, tt.want)
}
})
}
}