xgo/runtime/vm_inc_dec_test.go

22 lines
686 B
Go
Raw Normal View History

2019-01-11 13:27:28 +03:00
package runtime_test
2019-01-09 10:17:42 +03:00
import (
"testing"
)
func TestIncDec(t *testing.T) {
expect(t, `out = 0; out++`, nil, 1)
expect(t, `out = 0; out--`, nil, -1)
expect(t, `a := 0; a++; out = a`, nil, 1)
expect(t, `a := 0; a++; a--; out = a`, nil, 0)
2019-01-09 10:17:42 +03:00
2019-01-17 12:56:05 +03:00
// this seems strange but it works because 'a += b' is
// translated into 'a = a + b' and string type takes other types for + operator.
expect(t, `a := "foo"; a++; out = a`, nil, "foo1")
expectError(t, `a := "foo"; a--`, nil, "invalid operation")
2019-01-09 10:17:42 +03:00
expectError(t, `a++`, nil, "unresolved reference") // not declared
expectError(t, `a--`, nil, "unresolved reference") // not declared
expectError(t, `4++`, nil, "unresolved reference")
2019-01-09 10:17:42 +03:00
}