5
0
mirror of https://github.com/wailsapp/wails.git synced 2025-05-02 02:49:29 +08:00

Add tests for bound structs returned by function calls.

This commit is contained in:
Lea Anthony 2023-02-26 19:18:47 +11:00
parent f9ffe915f2
commit 33855ff01d
4 changed files with 265 additions and 0 deletions

View File

@ -1305,6 +1305,157 @@ func TestParseDirectory(t *testing.T) {
},
},
},
{
name: "should find a bound service returned from a function call",
dir: "testdata/function_single",
wantErr: false,
wantBoundMethods: map[string]map[string][]*BoundMethod{
"main": {
"GreetService": {
{
Name: "Greet",
DocComment: "Greet someone\n",
Inputs: []*Parameter{
{
Name: "name",
Type: &ParameterType{
Name: "string",
},
},
},
Outputs: []*Parameter{
{
Name: "",
Type: &ParameterType{
Name: "string",
},
},
},
},
},
},
},
},
{
name: "should find a bound service returned from a function call in another package",
dir: "testdata/function_from_imported_package",
wantErr: false,
wantBoundMethods: map[string]map[string][]*BoundMethod{
"main": {
"GreetService": {
{
Name: "Greet",
DocComment: "Greet does XYZ\n",
Inputs: []*Parameter{
{
Name: "name",
Type: &ParameterType{
Name: "string",
},
},
},
Outputs: []*Parameter{
{
Name: "",
Type: &ParameterType{
Name: "string",
},
},
},
},
{
Name: "NewPerson",
DocComment: "NewPerson creates a new person\n",
Inputs: []*Parameter{
{
Name: "name",
Type: &ParameterType{
Name: "string",
},
},
},
Outputs: []*Parameter{
{
Name: "",
Type: &ParameterType{
Name: "Person",
IsPointer: true,
IsStruct: true,
Package: "main",
},
},
},
},
},
},
"github.com/wailsapp/wails/v3/internal/parser/testdata/variable_single_from_other_function/services": {
"OtherService": {
{
Name: "Yay",
Outputs: []*Parameter{
{
Type: &ParameterType{
Name: "Address",
IsStruct: true,
IsPointer: true,
Package: "github.com/wailsapp/wails/v3/internal/parser/testdata/variable_single_from_other_function/services",
},
},
},
},
},
},
},
wantModels: map[string]map[string]*StructDef{
"main": {
"Person": {
Name: "Person",
Fields: []*Field{
{
Name: "Name",
Type: &ParameterType{
Name: "string",
},
},
{
Name: "Address",
Type: &ParameterType{
Name: "Address",
IsStruct: true,
IsPointer: true,
Package: "github.com/wailsapp/wails/v3/internal/parser/testdata/variable_single_from_other_function/services",
},
},
},
},
},
"github.com/wailsapp/wails/v3/internal/parser/testdata/variable_single_from_other_function/services": {
"Address": {
Name: "Address",
Fields: []*Field{
{
Name: "Street",
Type: &ParameterType{
Name: "string",
},
},
{
Name: "State",
Type: &ParameterType{
Name: "string",
},
},
{
Name: "Country",
Type: &ParameterType{
Name: "string",
},
},
},
},
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {

View File

@ -0,0 +1,49 @@
package main
import (
_ "embed"
"github.com/wailsapp/wails/v3/internal/parser/testdata/variable_single_from_other_function/services"
"log"
"github.com/wailsapp/wails/v3/pkg/application"
)
// GreetService is great
type GreetService struct {
SomeVariable int
lowerCase string
target *Person
}
type Person struct {
Name string
Address *services.Address
}
// Greet does XYZ
func (*GreetService) Greet(name string) string {
return "Hello " + name
}
// NewPerson creates a new person
func (*GreetService) NewPerson(name string) *Person {
return &Person{Name: name}
}
func main() {
app := application.New(application.Options{
Bind: []interface{}{
&GreetService{},
services.NewOtherService(),
},
})
app.NewWebviewWindow()
err := app.Run()
if err != nil {
log.Fatal(err)
}
}

View File

@ -0,0 +1,26 @@
package services
// OtherService is a struct
// that does things
type OtherService struct {
t int
}
type Address struct {
Street string
State string
Country string
}
// Yay does this and that
func (o *OtherService) Yay() *Address {
return &Address{
Street: "123 Pitt Street",
State: "New South Wales",
Country: "Australia",
}
}
func NewOtherService() *OtherService {
return &OtherService{}
}

View File

@ -0,0 +1,39 @@
package main
import (
_ "embed"
"github.com/wailsapp/wails/v3/pkg/application"
"log"
)
// GreetService is great
type GreetService struct {
SomeVariable int
lowerCase string
}
// Greet someone
func (*GreetService) Greet(name string) string {
return "Hello " + name
}
func NewGreetService() *GreetService {
return &GreetService{}
}
func main() {
app := application.New(application.Options{
Bind: []interface{}{
NewGreetService(),
},
})
app.NewWebviewWindow()
err := app.Run()
if err != nil {
log.Fatal(err)
}
}