/
fsm-spec.coffee
73 lines (56 loc) · 2.28 KB
/
fsm-spec.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
describe 'My pet dragon called Burt', () ->
burt = {}
sounds = {}
beforeEach ->
# Build a finite state machine for our pet dragon Burt
burt = new FSM.Machine()
# Define some callbacks that burt might make for different states
sounds =
growl: () ->
console.log('Grrr')
roar: () ->
console.log('Roar')
snore: () ->
console.log('Zzzz')
# By default any action will inrage burt
burt.setDefaultTransition 'enraged', () ->
sounds.roar()
spyOn(sounds, 'roar')
# If Burt is sleeping then any action will wake him (he's always
# grumpy when he wakes up).
burt.addTransitionAny 'sleeping', 'grumpy', () ->
sounds.growl()
spyOn(sounds, 'growl')
# Various things will sooth Burt when he's gumpy
burt.addTransition('stroke', 'grumpy', 'content')
burt.addTransition('feed', 'grumpy', 'content')
# The only way to get Burt back to sleep is by singing to him
burt.addTransition 'sing-to', 'content', 'sleeping', () ->
sounds.snore()#
spyOn(sounds, 'snore')
# Burt is sleeping
burt.setInitialState('sleeping')
it 'should growl when he wakes (as he will be grumpy)', () ->
burt.process('call')
expect(burt.getCurrentState()).toBe 'grumpy'
expect(sounds.growl).toHaveBeenCalled()
it 'should enrage if you kick him when awake', () ->
burt.process('call')
burt.process('kick')
expect(burt.getCurrentState()).toBe 'enraged'
expect(sounds.roar).toHaveBeenCalled()
it 'should be soothed if you stroke him', () ->
burt.process('call')
burt.process('stroke')
expect(burt.getCurrentState()).toBe 'content'
it 'should go to sleep if you feed and then sing to him', () ->
burt.process('call')
burt.process('feed')
burt.process('sing-to')
expect(burt.getCurrentState()).toBe 'sleeping'
expect(sounds.snore).toHaveBeenCalled()
it 'should go back to sleep when reset (even if enraged)', () ->
burt.process('call')
burt.process('prod')
burt.reset()
expect(burt.getCurrentState()).toBe 'sleeping'