094c8474b7
These don't belong to VM as they compile some Go code and run it in a VM. One may call them integration tests, but I prefer to attribute them to compiler. Moving these tests into pkg/compiler also allows to properly count the compiler coverage they add: -ok github.com/CityOfZion/neo-go/pkg/compiler (cached) coverage: 69.7% of statements +ok github.com/CityOfZion/neo-go/pkg/compiler (cached) coverage: 84.2% of statements This change also fixes `contant` typo and removes fake packages exposed to the public by moving foo/bar/foobar into the testdata directory.
244 lines
2.8 KiB
Go
244 lines
2.8 KiB
Go
package compiler_test
|
|
|
|
import (
|
|
"math/big"
|
|
"testing"
|
|
|
|
"github.com/CityOfZion/neo-go/pkg/vm"
|
|
)
|
|
|
|
var structTestCases = []testCase{
|
|
{
|
|
"struct field assign",
|
|
`
|
|
package foo
|
|
func Main() int {
|
|
t := token {
|
|
x: 2,
|
|
y: 4,
|
|
}
|
|
|
|
age := t.x
|
|
return age
|
|
}
|
|
|
|
type token struct {
|
|
x int
|
|
y int
|
|
}
|
|
`,
|
|
big.NewInt(2),
|
|
},
|
|
{
|
|
"struct field return",
|
|
`
|
|
package foo
|
|
type token struct {
|
|
x int
|
|
y int
|
|
}
|
|
|
|
func Main() int {
|
|
t := token {
|
|
x: 2,
|
|
y: 4,
|
|
}
|
|
|
|
return t.x
|
|
}
|
|
`,
|
|
big.NewInt(2),
|
|
},
|
|
{
|
|
"struct field assign",
|
|
`
|
|
package foo
|
|
type token struct {
|
|
x int
|
|
y int
|
|
}
|
|
|
|
func Main() int {
|
|
t := token {
|
|
x: 2,
|
|
y: 4,
|
|
}
|
|
t.x = 10
|
|
return t.x
|
|
}
|
|
`,
|
|
big.NewInt(10),
|
|
},
|
|
{
|
|
"complex struct",
|
|
`
|
|
package foo
|
|
type token struct {
|
|
x int
|
|
y int
|
|
}
|
|
|
|
func Main() int {
|
|
x := 10
|
|
|
|
t := token {
|
|
x: 2,
|
|
y: 4,
|
|
}
|
|
|
|
y := x + t.x
|
|
|
|
return y
|
|
}
|
|
`,
|
|
big.NewInt(12),
|
|
},
|
|
{
|
|
"initialize same struct twice",
|
|
`
|
|
package foo
|
|
type token struct {
|
|
x int
|
|
y int
|
|
}
|
|
|
|
func Main() int {
|
|
t1 := token {
|
|
x: 2,
|
|
y: 4,
|
|
}
|
|
t2 := token {
|
|
x: 2,
|
|
y: 4,
|
|
}
|
|
return t1.x + t2.y
|
|
}
|
|
`,
|
|
big.NewInt(6),
|
|
},
|
|
{
|
|
"struct methods",
|
|
`
|
|
package foo
|
|
type token struct {
|
|
x int
|
|
}
|
|
|
|
func(t token) getInteger() int {
|
|
return t.x
|
|
}
|
|
|
|
func Main() int {
|
|
t := token {
|
|
x: 4,
|
|
}
|
|
someInt := t.getInteger()
|
|
return someInt
|
|
}
|
|
`,
|
|
big.NewInt(4),
|
|
},
|
|
{
|
|
"struct methods with arguments",
|
|
`
|
|
package foo
|
|
type token struct {
|
|
x int
|
|
}
|
|
|
|
// Also tests if x conflicts with t.x
|
|
func(t token) addIntegers(x int, y int) int {
|
|
return t.x + x + y
|
|
}
|
|
|
|
func Main() int {
|
|
t := token {
|
|
x: 4,
|
|
}
|
|
someInt := t.addIntegers(2, 4)
|
|
return someInt
|
|
}
|
|
`,
|
|
big.NewInt(10),
|
|
},
|
|
{
|
|
"initialize struct partially",
|
|
`
|
|
package foo
|
|
type token struct {
|
|
x int
|
|
y int
|
|
z string
|
|
b bool
|
|
}
|
|
|
|
func Main() int {
|
|
t := token {
|
|
x: 4,
|
|
}
|
|
return t.y
|
|
}
|
|
`,
|
|
[]byte{},
|
|
},
|
|
{
|
|
"test return struct from func",
|
|
`
|
|
package foo
|
|
type token struct {
|
|
x int
|
|
y int
|
|
z string
|
|
b bool
|
|
}
|
|
|
|
func newToken() token {
|
|
return token{
|
|
x: 1,
|
|
y: 2,
|
|
z: "hello",
|
|
b: false,
|
|
}
|
|
}
|
|
|
|
func Main() token {
|
|
return newToken()
|
|
}
|
|
`,
|
|
[]vm.StackItem{
|
|
vm.NewBigIntegerItem(1),
|
|
vm.NewBigIntegerItem(2),
|
|
vm.NewByteArrayItem([]byte("hello")),
|
|
vm.NewByteArrayItem([]byte{}),
|
|
},
|
|
},
|
|
{
|
|
"pass struct as argument",
|
|
`
|
|
package foo
|
|
|
|
type Bar struct {
|
|
amount int
|
|
}
|
|
|
|
func addToAmount(x int, bar Bar) int {
|
|
bar.amount = bar.amount + x
|
|
return bar.amount
|
|
}
|
|
|
|
func Main() int {
|
|
b := Bar{
|
|
amount: 10,
|
|
}
|
|
|
|
x := addToAmount(4, b)
|
|
return x
|
|
}
|
|
`,
|
|
big.NewInt(14),
|
|
},
|
|
}
|
|
|
|
func TestStructs(t *testing.T) {
|
|
runTestCases(t, structTestCases)
|
|
}
|