5
0
mirror of https://github.com/wailsapp/wails.git synced 2025-05-02 17:22:01 +08:00
wails/v2/internal/binding/binding_test/binding_test.go
Oleg Gulevskyy ca8a1fab36
Ability to define prefix / suffix for TS module (#2101)
* add tspostfix and tsprefix flags + organise under struct

* postifx -> suffix

* tsPrefix options on bindings struct

* pass prefix and suffix to the executable

* add support for CLI flags for generating module

* method to set TSpref/suff to bindings

* use passed ts prefix for typescriptify

* add brief Readme udpate to include new flags

* create reusable common flags

* use common flags instead of hardcoded text

* support tsprefix & suffix for dev

* add tsPrefix & tsSuffix for build cmd

* take pref & suff in account when gen d.ts

* export colorsful log functions into utils for reuse

* detect and warn the user about usage of reserved keyword

* fmt

* add TrimSpace on fn input

* refactor utils -> logutils

* add bindings -> ts_generation options to wailsjson parse

* use wailsjson for ts generation

* update warning message + extract to func

* remove suff/pref info from readme

* update json schema

* add tests for prefix and suffix case

* rename suffix method

* Update v2/internal/typescriptify/typescriptify.go

Co-authored-by: Lea Anthony <lea.anthony@gmail.com>

* Update website/static/schemas/config.v2.json

Co-authored-by: Lea Anthony <lea.anthony@gmail.com>

* Update website/static/schemas/config.v2.json

Co-authored-by: Lea Anthony <lea.anthony@gmail.com>

* update changelog

* Minor tweaks

Co-authored-by: Lea Anthony <lea.anthony@gmail.com>
2022-11-24 21:33:58 +11:00

65 lines
1.4 KiB
Go

package binding_test
import (
"reflect"
"strings"
"testing"
"github.com/stretchr/testify/require"
"github.com/wailsapp/wails/v2/internal/binding"
"github.com/wailsapp/wails/v2/internal/logger"
)
type BindingTest struct {
name string
structs []interface{}
exemptions []interface{}
want string
shouldError bool
TsGenerationOptionsTest
}
type TsGenerationOptionsTest struct {
TsPrefix string
TsSuffix string
}
func TestBindings_GenerateModels(t *testing.T) {
tests := []BindingTest{
EscapedNameTest,
ImportedStructTest,
ImportedSliceTest,
ImportedMapTest,
NestedFieldTest,
NonStringMapKeyTest,
SingleFieldTest,
MultistructTest,
EmptyStructTest,
GeneratedJsEntityTest,
}
testLogger := &logger.Logger{}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
b := binding.NewBindings(testLogger, tt.structs, tt.exemptions, false)
for _, s := range tt.structs {
err := b.Add(s)
require.NoError(t, err)
}
b.SetTsPrefix(tt.TsPrefix)
// TODO - rename this to SetTsSuffix
b.SetTsSuffix(tt.TsSuffix)
got, err := b.GenerateModels()
if (err != nil) != tt.shouldError {
t.Errorf("GenerateModels() error = %v, shouldError %v", err, tt.shouldError)
return
}
if !reflect.DeepEqual(strings.Fields(string(got)), strings.Fields(tt.want)) {
t.Errorf("GenerateModels() got = %v, want %v", string(got), tt.want)
}
})
}
}