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