blob: 2d01c1aeea285fa708f6a674de1abed1973aeccd [file] [log] [blame]
Matteo Scandolo11006992019-08-28 11:29:46 -07001/*
2 * Copyright 2018-present Open Networking Foundation
3
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7
8 * http://www.apache.org/licenses/LICENSE-2.0
9
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package devices
18
19import (
20 "github.com/looplab/fsm"
21 "gotest.tools/assert"
22 "testing"
23)
24
25var (
26 originalNewFSM func(initial string, events []fsm.EventDesc, callbacks map[string]fsm.Callback) *fsm.FSM
27)
28
29func setUp(t *testing.T) {
30 originalNewFSM = newFSM
31}
32
33
34func tearDown() {
35 newFSM = originalNewFSM
36}
37
38func Test_Helpers(t *testing.T) {
39
40 // feedback values for the mock
41 called := 0
42 args := struct {
43 initial string
44 events []fsm.EventDesc
45 callbacks map[string]fsm.Callback
46 }{}
47
48 // creating the mock function
49 mockFSM := func(initial string, events []fsm.EventDesc, callbacks map[string]fsm.Callback) *fsm.FSM {
50 called++
51 args.initial = initial
52 args.events = events
53 args.callbacks = callbacks
54 return fsm.NewFSM(initial, events, callbacks)
55 }
56 newFSM = mockFSM
57
58 // params for the method under test
59 cb_called := 0
60 cb := func(e *fsm.Event) {
61 cb_called++
62 return
63 }
64
65 // calling the method under test
66 sm := getOperStateFSM(cb)
67
68 // verify
69 assert.Equal(t, called, 1, "Expected fsm.NewFSM to have been called once, instead it was called %d", called)
70 assert.Equal(t, args.initial, "down")
71
72 assert.Equal(t, args.events[0].Name, "enable")
73 assert.Equal(t, args.events[0].Src[0], "down")
74 assert.Equal(t, args.events[0].Dst, "up")
75
76 assert.Equal(t, args.events[1].Name, "disable")
77 assert.Equal(t, args.events[1].Src[0], "up")
78 assert.Equal(t, args.events[1].Dst, "down")
79
80 // this is to test that the callback is called when the state change
81 sm.Event("enable")
82 assert.Equal(t, cb_called, 1)
83
84}