mirror of
https://github.com/nspcc-dev/neo-go.git
synced 2024-12-03 13:58:37 +00:00
2345858238
* refactored structs, the scope is not needed anymore + fix passing struct in func arguments. * implemented byte arrays and added runtime tests * Added sc examples in compiler README + added quick nested if test. * Updated README
81 lines
1.9 KiB
Go
81 lines
1.9 KiB
Go
package compiler
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/hex"
|
|
"fmt"
|
|
"os"
|
|
"strings"
|
|
"testing"
|
|
"text/tabwriter"
|
|
|
|
"github.com/CityOfZion/neo-go/pkg/vm"
|
|
"github.com/CityOfZion/neo-go/pkg/vm/compiler"
|
|
)
|
|
|
|
type testCase struct {
|
|
name string
|
|
src string
|
|
result string
|
|
}
|
|
|
|
func TestAllCases(t *testing.T) {
|
|
testCases := []testCase{}
|
|
|
|
// The Go language
|
|
testCases = append(testCases, assignTestCases...)
|
|
testCases = append(testCases, arrayTestCases...)
|
|
testCases = append(testCases, binaryExprTestCases...)
|
|
testCases = append(testCases, functionCallTestCases...)
|
|
testCases = append(testCases, boolTestCases...)
|
|
testCases = append(testCases, stringTestCases...)
|
|
testCases = append(testCases, structTestCases...)
|
|
testCases = append(testCases, ifStatementTestCases...)
|
|
testCases = append(testCases, customTypeTestCases...)
|
|
testCases = append(testCases, constantTestCases...)
|
|
testCases = append(testCases, importTestCases...)
|
|
|
|
// Blockchain specific
|
|
testCases = append(testCases, storageTestCases...)
|
|
testCases = append(testCases, runtimeTestCases...)
|
|
|
|
for _, tc := range testCases {
|
|
b, err := compiler.Compile(strings.NewReader(tc.src), &compiler.Options{})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
expectedResult, err := hex.DecodeString(tc.result)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
if bytes.Compare(b, expectedResult) != 0 {
|
|
fmt.Println(tc.src)
|
|
t.Log(hex.EncodeToString(b))
|
|
dumpOpCodeSideBySide(b, expectedResult)
|
|
t.Fatalf("compiling %s failed", tc.name)
|
|
}
|
|
}
|
|
}
|
|
|
|
func dumpOpCodeSideBySide(have, want []byte) {
|
|
w := tabwriter.NewWriter(os.Stdout, 0, 0, 4, ' ', 0)
|
|
fmt.Fprintln(w, "INDEX\tHAVE OPCODE\tDESC\tWANT OPCODE\tDESC\tDIFF")
|
|
|
|
var b byte
|
|
for i := 0; i < len(have); i++ {
|
|
if len(want) <= i {
|
|
b = 0x00
|
|
} else {
|
|
b = want[i]
|
|
}
|
|
diff := ""
|
|
if have[i] != b {
|
|
diff = "<<"
|
|
}
|
|
fmt.Fprintf(w, "%d\t0x%2x\t%s\t0x%2x\t%s\t%s\n",
|
|
i, have[i], vm.Opcode(have[i]), b, vm.Opcode(b), diff)
|
|
}
|
|
w.Flush()
|
|
}
|