Skip to content

Commit

Permalink
some lame tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rusq committed Oct 17, 2024
1 parent bd78225 commit 47d9cfc
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 3 deletions.
8 changes: 5 additions & 3 deletions cmd/slackdump/internal/ui/cfgui/update_bool.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ func (m boolUpdateModel) Init() tea.Cmd {
return cmdSetValue("", !*m.v)
}

// cmdSetValue returns a command that sets a value to v.
// cmdSetValue returns a command that sets a value to v, key is implementation
// specific, may not be used by the caller.
func cmdSetValue[T any](key string, v T) func() tea.Msg {
return func() tea.Msg {
return wmSetValue[T]{key: "", v: v}
return wmSetValue[T]{key: key, v: v}
}
}

Expand All @@ -35,5 +36,6 @@ func (m boolUpdateModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
}

func (m boolUpdateModel) View() string {
return ""
// View is not being used, but it's here for tests.
return checkbox(*m.v)
}
94 changes: 94 additions & 0 deletions cmd/slackdump/internal/ui/cfgui/update_bool_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package cfgui

import (
"reflect"
"testing"

tea "github.com/charmbracelet/bubbletea"
)

func ptr[T any](v T) *T { return &v }

func Test_boolUpdateModel_Update(t *testing.T) {
type fields struct {
v *bool
}
type args struct {
msg tea.Msg
}
tests := []struct {
name string
fields fields
args args
want boolUpdateModel
want1 tea.Cmd
}{
{
name: "set value message",
fields: fields{
v: ptr(false),
},
args: args{
msg: cmdSetValue("", true)(),
},
want: boolUpdateModel{v: ptr(true)},
want1: cmdClose,
},
{
name: "unknown message",
fields: fields{
v: ptr(false),
},
args: args{
msg: tea.Key{},
},
want: boolUpdateModel{v: ptr(false)},
want1: nil,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
m := boolUpdateModel{
v: tt.fields.v,
}
got, got1 := m.Update(tt.args.msg)
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("boolUpdateModel.Update() got = %v, want %v", got.View(), tt.want.View())
}
if ((tt.want1 == nil) && (got1 != nil)) || ((tt.want1 != nil) && (got1 == nil)) {
t.Fatalf("boolUpdateModel.Update() got1 = %v, want %v", got1, tt.want1)
} else if tt.want1 != nil && got1 != nil && !reflect.DeepEqual(got1(), tt.want1()) {
t.Errorf("boolUpdateModel.Update() got1 = %v, want %v", got1, tt.want1)
}
})
}
}

func Test_boolUpdateModel_Init(t *testing.T) {
type fields struct {
v *bool
}
tests := []struct {
name string
fields fields
want tea.Cmd
}{
{
name: "init",
fields: fields{
v: ptr(false),
},
want: cmdSetValue("", true),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
m := boolUpdateModel{
v: tt.fields.v,
}
if got := m.Init(); !reflect.DeepEqual(got(), tt.want()) {
t.Errorf("boolUpdateModel.Init() = %v, want %v", got, tt.want)
}
})
}
}

0 comments on commit 47d9cfc

Please sign in to comment.