github.com/AlpineAIO/wails/v2@v2.0.0-beta.32.0.20240505041856-1047a8fa5fef/internal/binding/generate_test.go (about)

     1  package binding
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/AlpineAIO/wails/v2/internal/logger"
     7  	"github.com/leaanthony/slicer"
     8  	"github.com/stretchr/testify/assert"
     9  )
    10  
    11  type BindForTest struct {
    12  }
    13  
    14  func (b *BindForTest) GetA() A {
    15  	return A{}
    16  }
    17  
    18  type A struct {
    19  	B B `json:"B"`
    20  }
    21  
    22  type B struct {
    23  	Name string `json:"name"`
    24  }
    25  
    26  func TestNestedStruct(t *testing.T) {
    27  	bind := &BindForTest{}
    28  	testBindings := NewBindings(logger.New(nil), []interface{}{bind}, []interface{}{}, false, []interface{}{})
    29  
    30  	namesStrSlicer := testBindings.getAllStructNames()
    31  	names := []string{}
    32  	namesStrSlicer.Each(func(s string) {
    33  		names = append(names, s)
    34  	})
    35  	assert.Contains(t, names, "binding.A")
    36  	assert.Contains(t, names, "binding.B")
    37  }
    38  
    39  func Test_goTypeToJSDocType(t *testing.T) {
    40  
    41  	tests := []struct {
    42  		name  string
    43  		input string
    44  		want  string
    45  	}{
    46  		{
    47  			name:  "string",
    48  			input: "string",
    49  			want:  "string",
    50  		},
    51  		{
    52  			name:  "error",
    53  			input: "error",
    54  			want:  "Error",
    55  		},
    56  		{
    57  			name:  "int",
    58  			input: "int",
    59  			want:  "number",
    60  		},
    61  		{
    62  			name:  "int32",
    63  			input: "int32",
    64  			want:  "number",
    65  		},
    66  		{
    67  			name:  "uint",
    68  			input: "uint",
    69  			want:  "number",
    70  		},
    71  		{
    72  			name:  "uint32",
    73  			input: "uint32",
    74  			want:  "number",
    75  		},
    76  		{
    77  			name:  "float32",
    78  			input: "float32",
    79  			want:  "number",
    80  		},
    81  		{
    82  			name:  "float64",
    83  			input: "float64",
    84  			want:  "number",
    85  		},
    86  		{
    87  			name:  "bool",
    88  			input: "bool",
    89  			want:  "boolean",
    90  		},
    91  		{
    92  			name:  "interface{}",
    93  			input: "interface{}",
    94  			want:  "any",
    95  		},
    96  		{
    97  			name:  "[]byte",
    98  			input: "[]byte",
    99  			want:  "string",
   100  		},
   101  		{
   102  			name:  "[]int",
   103  			input: "[]int",
   104  			want:  "Array<number>",
   105  		},
   106  		{
   107  			name:  "[]bool",
   108  			input: "[]bool",
   109  			want:  "Array<boolean>",
   110  		},
   111  		{
   112  			name:  "anything else",
   113  			input: "foo",
   114  			want:  "any",
   115  		},
   116  		{
   117  			name:  "map",
   118  			input: "map[string]float64",
   119  			want:  "{[key: string]: number}",
   120  		},
   121  		{
   122  			name:  "map",
   123  			input: "map[string]map[string]float64",
   124  			want:  "{[key: string]: {[key: string]: number}}",
   125  		},
   126  		{
   127  			name:  "types",
   128  			input: "main.SomeType",
   129  			want:  "main.SomeType",
   130  		},
   131  	}
   132  	var importNamespaces slicer.StringSlicer
   133  	for _, tt := range tests {
   134  		t.Run(tt.name, func(t *testing.T) {
   135  			if got := goTypeToJSDocType(tt.input, &importNamespaces); got != tt.want {
   136  				t.Errorf("goTypeToJSDocType() = %v, want %v", got, tt.want)
   137  			}
   138  		})
   139  	}
   140  }