neoneo-go/pkg/compiler/codegen_test.go

93 lines
1.4 KiB
Go
Raw Normal View History

2019-12-19 10:30:57 +00:00
package compiler
import (
"go/token"
"go/types"
2019-12-19 10:30:57 +00:00
"testing"
"github.com/nspcc-dev/neo-go/pkg/vm/opcode"
2019-12-19 10:30:57 +00:00
"github.com/stretchr/testify/assert"
)
func TestConvertToken(t *testing.T) {
type testCase struct {
name string
token token.Token
opcode opcode.Opcode
typ types.Type
2019-12-19 10:30:57 +00:00
}
testCases := []testCase{
2020-08-23 10:12:10 +00:00
{"ADD (number)",
2019-12-19 10:30:57 +00:00
token.ADD,
opcode.ADD,
types.Typ[types.Int],
},
2020-08-23 10:12:10 +00:00
{"ADD (string)",
token.ADD,
opcode.CAT,
types.Typ[types.String],
2019-12-19 10:30:57 +00:00
},
{"SUB",
token.SUB,
opcode.SUB,
nil,
2019-12-19 10:30:57 +00:00
},
{"MUL",
token.MUL,
opcode.MUL,
nil,
2019-12-19 10:30:57 +00:00
},
{"QUO",
token.QUO,
opcode.DIV,
nil,
2019-12-19 10:30:57 +00:00
},
{"REM",
token.REM,
opcode.MOD,
nil,
2019-12-19 10:30:57 +00:00
},
2020-08-23 10:12:10 +00:00
{"ADD_ASSIGN (number)",
2019-12-19 10:30:57 +00:00
token.ADD_ASSIGN,
opcode.ADD,
2020-08-23 10:12:10 +00:00
types.Typ[types.Int],
},
{"ADD_ASSIGN (string)",
token.ADD_ASSIGN,
opcode.CAT,
types.Typ[types.String],
2019-12-19 10:30:57 +00:00
},
{"SUB_ASSIGN",
token.SUB_ASSIGN,
opcode.SUB,
nil,
2019-12-19 10:30:57 +00:00
},
{"MUL_ASSIGN",
token.MUL_ASSIGN,
opcode.MUL,
nil,
2019-12-19 10:30:57 +00:00
},
{"QUO_ASSIGN",
token.QUO_ASSIGN,
opcode.DIV,
nil,
2019-12-19 10:30:57 +00:00
},
{"REM_ASSIGN",
token.REM_ASSIGN,
opcode.MOD,
nil,
2019-12-19 10:30:57 +00:00
},
}
for _, tcase := range testCases {
t.Run(tcase.name, func(t *testing.T) { eval(t, tcase.token, tcase.opcode, tcase.typ) })
2019-12-19 10:30:57 +00:00
}
}
func eval(t *testing.T, token token.Token, opcode opcode.Opcode, typ types.Type) {
op, err := convertToken(token, typ)
assert.NoError(t, err)
assert.Equal(t, opcode, op)
2019-12-19 10:30:57 +00:00
}