Skip to content

Commit

Permalink
tests: add tests for states.go
Browse files Browse the repository at this point in the history
Added tests for: StateGroup, State
  • Loading branch information
vitaliy-ukiru committed Jun 4, 2024
1 parent 6fad4a4 commit 742466e
Showing 1 changed file with 202 additions and 0 deletions.
202 changes: 202 additions & 0 deletions states_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,202 @@
package fsm

import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/vitaliy-ukiru/fsm-telebot/v2/internal/container"
)

func TestStateGroup_MatchState(t *testing.T) {

tests := []struct {
name string
sg StateGroup
arg State
want bool
}{
{
name: "success",
sg: NewStateGroup("test", "state1", "state2"),
arg: "test:state1",
want: true,
},
{
name: "not contains",
sg: NewStateGroup("test", "state1"),
arg: "state1", // state must don't match, because it not have prefix
want: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equalf(t, tt.want, tt.sg.MatchState(tt.arg), "MatchState(%v)", tt.arg)
})
}
}

func TestStateGroup_New(t *testing.T) {
type fields struct {
prefix string
group *container.LinkedHashSet[State]
}
type args struct {
name string
}
tests := []struct {
name string
fields fields
args args
want State
}{
{
name: "add prefix",
fields: fields{
prefix: "test",
group: container.NewLinkedHashSet[State](),
},
args: args{
name: "add",
},
want: "test:add",
},
{
name: "don't add prefix",
fields: fields{
prefix: "test",
group: container.NewLinkedHashSet[State](),
},
args: args{
name: "test:add",
},
want: "test:add",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
sg := StateGroup{
prefix: tt.fields.prefix,
group: tt.fields.group,
}
assert.Equalf(t, tt.want, sg.New(tt.args.name), "New(%v)", tt.args.name)
})
}
}

func TestStateGroup_Next(t *testing.T) {
tests := []struct {
name string
sg StateGroup
arg State
want State
}{
{
name: "empty group",
sg: NewStateGroup("test"),
arg: "test:my_state",
want: DefaultState,
},
{
name: "normal behavior",
sg: NewStateGroup("test", "a", "b", "c"),
arg: "test:b",
want: "test:c",
},
{
name: "unknown state",
sg: NewStateGroup("test", "a", "b"),
arg: "test:c",
want: DefaultState,
},
{
name: "last state",
sg: NewStateGroup("test", "a", "b"),
arg: "test:b",
want: DefaultState,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equalf(t, tt.want, tt.sg.Next(tt.arg), "Next(%v)", tt.arg)
})
}
}

func TestStateGroup_Prev(t *testing.T) {

tests := []struct {
name string
sg StateGroup
arg State
want State
}{
{
name: "empty group",
sg: NewStateGroup("test"),
arg: "test:my_state",
want: DefaultState,
},
{
name: "normal behavior",
sg: NewStateGroup("test", "a", "b", "c"),
arg: "test:b",
want: "test:a",
},
{
name: "unknown state",
sg: NewStateGroup("test", "a", "b"),
arg: "test:c",
want: DefaultState,
},
{
name: "first state",
sg: NewStateGroup("test", "a", "b"),
arg: "test:a",
want: DefaultState,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equalf(t, tt.want, tt.sg.Prev(tt.arg), "Prev(%v)", tt.arg)
})
}
}

func TestState_MatchState(t *testing.T) {
tests := []struct {
name string
state State
other State
want bool
}{
{
name: "same state",
state: "a",
other: "a",
want: true,
},
{
name: "different state",
state: "a",
other: "b",
want: false,
},
{
name: "state is Any",
state: AnyState,
other: "test",
want: true,
},
{
name: "other is Any",
state: "test",
other: AnyState,
want: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equalf(t, tt.want, tt.state.MatchState(tt.other), "MatchState(%v)", tt.other)
})
}
}

0 comments on commit 742466e

Please sign in to comment.