2019-02-19 11:47:25 +00:00
|
|
|
package vm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestStateFromString(t *testing.T) {
|
|
|
|
var (
|
|
|
|
s State
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
|
|
|
s, err = StateFromString("HALT")
|
|
|
|
assert.NoError(t, err)
|
2020-07-27 14:57:53 +00:00
|
|
|
assert.Equal(t, HaltState, s)
|
2019-02-19 11:47:25 +00:00
|
|
|
|
|
|
|
s, err = StateFromString("BREAK")
|
|
|
|
assert.NoError(t, err)
|
2020-07-27 14:57:53 +00:00
|
|
|
assert.Equal(t, BreakState, s)
|
2019-02-19 11:47:25 +00:00
|
|
|
|
|
|
|
s, err = StateFromString("FAULT")
|
|
|
|
assert.NoError(t, err)
|
2020-07-27 14:57:53 +00:00
|
|
|
assert.Equal(t, FaultState, s)
|
2019-02-19 11:47:25 +00:00
|
|
|
|
|
|
|
s, err = StateFromString("NONE")
|
|
|
|
assert.NoError(t, err)
|
2020-07-27 14:57:53 +00:00
|
|
|
assert.Equal(t, NoneState, s)
|
2019-02-19 11:47:25 +00:00
|
|
|
|
|
|
|
s, err = StateFromString("HALT, BREAK")
|
|
|
|
assert.NoError(t, err)
|
2020-07-27 14:57:53 +00:00
|
|
|
assert.Equal(t, HaltState|BreakState, s)
|
2019-02-19 11:47:25 +00:00
|
|
|
|
|
|
|
s, err = StateFromString("FAULT, BREAK")
|
|
|
|
assert.NoError(t, err)
|
2020-07-27 14:57:53 +00:00
|
|
|
assert.Equal(t, FaultState|BreakState, s)
|
2019-02-19 11:47:25 +00:00
|
|
|
|
2019-02-19 13:22:33 +00:00
|
|
|
_, err = StateFromString("HALT, KEK")
|
2019-02-19 11:47:25 +00:00
|
|
|
assert.Error(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestState_HasFlag(t *testing.T) {
|
2020-07-27 14:57:53 +00:00
|
|
|
assert.True(t, HaltState.HasFlag(HaltState))
|
|
|
|
assert.True(t, BreakState.HasFlag(BreakState))
|
|
|
|
assert.True(t, FaultState.HasFlag(FaultState))
|
|
|
|
assert.True(t, (HaltState | BreakState).HasFlag(HaltState))
|
|
|
|
assert.True(t, (HaltState | BreakState).HasFlag(BreakState))
|
|
|
|
|
|
|
|
assert.False(t, HaltState.HasFlag(BreakState))
|
|
|
|
assert.False(t, NoneState.HasFlag(HaltState))
|
|
|
|
assert.False(t, (FaultState | BreakState).HasFlag(HaltState))
|
2019-02-19 11:47:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestState_MarshalJSON(t *testing.T) {
|
|
|
|
var (
|
|
|
|
data []byte
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
2020-07-27 14:57:53 +00:00
|
|
|
data, err = json.Marshal(HaltState | BreakState)
|
2019-02-19 11:47:25 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, data, []byte(`"HALT, BREAK"`))
|
|
|
|
|
2020-07-27 14:57:53 +00:00
|
|
|
data, err = json.Marshal(FaultState)
|
2019-02-19 11:47:25 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, data, []byte(`"FAULT"`))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestState_UnmarshalJSON(t *testing.T) {
|
|
|
|
var (
|
|
|
|
s State
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
|
|
|
err = json.Unmarshal([]byte(`"HALT, BREAK"`), &s)
|
|
|
|
assert.NoError(t, err)
|
2020-07-27 14:57:53 +00:00
|
|
|
assert.Equal(t, HaltState|BreakState, s)
|
2019-02-19 11:47:25 +00:00
|
|
|
|
|
|
|
err = json.Unmarshal([]byte(`"FAULT, BREAK"`), &s)
|
|
|
|
assert.NoError(t, err)
|
2020-07-27 14:57:53 +00:00
|
|
|
assert.Equal(t, FaultState|BreakState, s)
|
2019-02-19 11:47:25 +00:00
|
|
|
|
|
|
|
err = json.Unmarshal([]byte(`"NONE"`), &s)
|
|
|
|
assert.NoError(t, err)
|
2020-07-27 14:57:53 +00:00
|
|
|
assert.Equal(t, NoneState, s)
|
2019-02-19 11:47:25 +00:00
|
|
|
}
|