forked from TrueCloudLab/neoneo-go
opcode: implement FromString()
This commit is contained in:
parent
e53055a560
commit
1100f629df
2 changed files with 30 additions and 0 deletions
20
pkg/vm/opcode/from_string.go
Normal file
20
pkg/vm/opcode/from_string.go
Normal file
|
@ -0,0 +1,20 @@
|
|||
package opcode
|
||||
|
||||
import "errors"
|
||||
|
||||
var stringToOpcode = make(map[string]Opcode)
|
||||
|
||||
func init() {
|
||||
for i := 0; i < 255; i++ {
|
||||
op := Opcode(i)
|
||||
stringToOpcode[op.String()] = op
|
||||
}
|
||||
}
|
||||
|
||||
// FromString converts string representation to and opcode itself.
|
||||
func FromString(s string) (Opcode, error) {
|
||||
if op, ok := stringToOpcode[s]; ok {
|
||||
return op, nil
|
||||
}
|
||||
return 0, errors.New("invalid opcode")
|
||||
}
|
|
@ -4,6 +4,7 @@ import (
|
|||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
// Nothing more to test here, really.
|
||||
|
@ -18,3 +19,12 @@ func TestStringer(t *testing.T) {
|
|||
assert.Equal(t, s, o.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestFromString(t *testing.T) {
|
||||
_, err := FromString("abcdef")
|
||||
require.Error(t, err)
|
||||
|
||||
op, err := FromString(MUL.String())
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, MUL, op)
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue