machine/transition_test.go

56 lines
1.2 KiB
Go
Raw Normal View History

2024-04-04 17:33:22 +00:00
// Copyright 2024 Matthew Rich <matthewrich.conf@gmail.com>. All rights reserved.
2024-04-04 20:08:50 +00:00
2024-04-04 17:33:22 +00:00
package machine
import (
2024-04-04 20:06:45 +00:00
"github.com/stretchr/testify/assert"
2024-04-04 20:08:50 +00:00
"log"
"testing"
2024-04-04 17:33:22 +00:00
)
func setupTransition() Transitioner {
2024-04-04 20:08:50 +00:00
t := NewTransition("open", "closed", "open")
if t == nil {
log.Fatal("Failed creating new transition")
}
return t
2024-04-04 17:33:22 +00:00
}
func setupSubscriber() Subscriber {
2024-04-04 20:08:50 +00:00
c := make(EventChannel, 2)
return &c
2024-04-04 17:33:22 +00:00
}
func TestNewTransition(t *testing.T) {
2024-04-04 20:08:50 +00:00
s := NewTransition("connect", "disconnected", "connected")
if s == nil {
t.Errorf("Failed creating new transition")
}
2024-04-04 17:33:22 +00:00
}
func TestTransitionExecution(t *testing.T) {
2024-04-04 20:08:50 +00:00
s := setupTransition()
m := setupModel("closed")
assert.Nil(t, s.Run(m))
state := m.InspectState()
if state != "open" {
t.Errorf("Failed to transition state: %s", state)
}
2024-04-04 17:33:22 +00:00
}
func TestTransitionSubscribe(t *testing.T) {
2024-04-04 20:08:50 +00:00
c := setupSubscriber()
s := setupTransition()
s.Subscribe(c)
m := setupModel("closed")
assert.Nil(t, s.Run(m))
exitEvent := <-*c.(*EventChannel)
enterEvent := <-*c.(*EventChannel)
if exitEvent.on != EXITSTATEEVENT {
t.Errorf("Invalid exit event")
}
if enterEvent.on != ENTERSTATEEVENT {
t.Errorf("Invalid enter event")
}
2024-04-04 17:33:22 +00:00
}