add objects tests

This commit is contained in:
Daniel Kang 2019-01-15 10:35:51 -08:00
parent 7f9fa836ee
commit 344c96d280
2 changed files with 119 additions and 0 deletions

View file

@ -832,6 +832,29 @@ func() {
intObject(0), intObject(0),
intObject(10), intObject(10),
intObject(1)))) intObject(1))))
expect(t, `a := 0; a == 0 && a != 1 || a < 1`,
bytecode(
concat(
compiler.MakeInstruction(compiler.OpConstant, 0),
compiler.MakeInstruction(compiler.OpSetGlobal, 0),
compiler.MakeInstruction(compiler.OpGetGlobal, 0),
compiler.MakeInstruction(compiler.OpConstant, 1),
compiler.MakeInstruction(compiler.OpEqual),
compiler.MakeInstruction(compiler.OpAndJump, 23),
compiler.MakeInstruction(compiler.OpGetGlobal, 0),
compiler.MakeInstruction(compiler.OpConstant, 2),
compiler.MakeInstruction(compiler.OpNotEqual),
compiler.MakeInstruction(compiler.OpOrJump, 33),
compiler.MakeInstruction(compiler.OpConstant, 3),
compiler.MakeInstruction(compiler.OpGetGlobal, 0),
compiler.MakeInstruction(compiler.OpGreaterThan),
compiler.MakeInstruction(compiler.OpPop)),
objectsArray(
intObject(0),
intObject(0),
intObject(1),
intObject(1))))
} }
func concat(insts ...[]byte) []byte { func concat(insts ...[]byte) []byte {

View file

@ -4,6 +4,7 @@ import (
"testing" "testing"
"github.com/d5/tengo/assert" "github.com/d5/tengo/assert"
"github.com/d5/tengo/compiler/token"
"github.com/d5/tengo/objects" "github.com/d5/tengo/objects"
) )
@ -29,6 +30,20 @@ func TestObject_TypeName(t *testing.T) {
assert.Equal(t, "string-iterator", o.TypeName()) assert.Equal(t, "string-iterator", o.TypeName())
o = &objects.MapIterator{} o = &objects.MapIterator{}
assert.Equal(t, "map-iterator", o.TypeName()) assert.Equal(t, "map-iterator", o.TypeName())
o = &objects.Break{}
assert.Equal(t, "break", o.TypeName())
o = &objects.Continue{}
assert.Equal(t, "continue", o.TypeName())
o = &objects.BuiltinFunction{}
assert.Equal(t, "builtin-function", o.TypeName())
o = &objects.Closure{}
assert.Equal(t, "closure", o.TypeName())
o = &objects.CompiledFunction{}
assert.Equal(t, "compiled-function", o.TypeName())
o = &objects.ReturnValue{}
assert.Equal(t, "return-value", o.TypeName())
o = &objects.Undefined{}
assert.Equal(t, "undefined", o.TypeName())
} }
func TestObject_IsFalsy(t *testing.T) { func TestObject_IsFalsy(t *testing.T) {
@ -57,4 +72,85 @@ func TestObject_IsFalsy(t *testing.T) {
assert.True(t, o.IsFalsy()) assert.True(t, o.IsFalsy())
o = &objects.Map{Value: map[string]objects.Object{"a": nil}} // nil is not valid but still count as 1 element o = &objects.Map{Value: map[string]objects.Object{"a": nil}} // nil is not valid but still count as 1 element
assert.False(t, o.IsFalsy()) assert.False(t, o.IsFalsy())
o = &objects.Break{}
assert.False(t, o.IsFalsy())
o = &objects.Continue{}
assert.False(t, o.IsFalsy())
o = &objects.BuiltinFunction{}
assert.False(t, o.IsFalsy())
o = &objects.Closure{}
assert.False(t, o.IsFalsy())
o = &objects.CompiledFunction{}
assert.False(t, o.IsFalsy())
o = &objects.ReturnValue{}
assert.False(t, o.IsFalsy())
o = &objects.Undefined{}
assert.True(t, o.IsFalsy())
}
func TestObject_String(t *testing.T) {
var o objects.Object
o = &objects.Int{Value: 0}
assert.Equal(t, "0", o.String())
o = &objects.Int{Value: 1}
assert.Equal(t, "1", o.String())
o = &objects.Float{Value: 0}
assert.Equal(t, "0", o.String())
o = &objects.Float{Value: 1}
assert.Equal(t, "1", o.String())
o = &objects.Char{Value: ' '}
assert.Equal(t, " ", o.String())
o = &objects.Char{Value: 'T'}
assert.Equal(t, "T", o.String())
o = &objects.String{Value: ""}
assert.Equal(t, `""`, o.String())
o = &objects.String{Value: " "}
assert.Equal(t, `" "`, o.String())
o = &objects.Array{Value: nil}
assert.Equal(t, "[]", o.String())
o = &objects.Map{Value: nil}
assert.Equal(t, "{}", o.String())
}
func TestObject_BinaryOp(t *testing.T) {
var o objects.Object
o = &objects.Char{}
_, err := o.BinaryOp(token.Add, objects.UndefinedValue)
assert.Error(t, err)
o = &objects.Bool{}
_, err = o.BinaryOp(token.Add, objects.UndefinedValue)
assert.Error(t, err)
o = &objects.Map{}
_, err = o.BinaryOp(token.Add, objects.UndefinedValue)
assert.Error(t, err)
o = &objects.ArrayIterator{}
_, err = o.BinaryOp(token.Add, objects.UndefinedValue)
assert.Error(t, err)
o = &objects.StringIterator{}
_, err = o.BinaryOp(token.Add, objects.UndefinedValue)
assert.Error(t, err)
o = &objects.MapIterator{}
_, err = o.BinaryOp(token.Add, objects.UndefinedValue)
assert.Error(t, err)
o = &objects.Break{}
_, err = o.BinaryOp(token.Add, objects.UndefinedValue)
assert.Error(t, err)
o = &objects.Continue{}
_, err = o.BinaryOp(token.Add, objects.UndefinedValue)
assert.Error(t, err)
o = &objects.BuiltinFunction{}
_, err = o.BinaryOp(token.Add, objects.UndefinedValue)
assert.Error(t, err)
o = &objects.Closure{}
_, err = o.BinaryOp(token.Add, objects.UndefinedValue)
assert.Error(t, err)
o = &objects.CompiledFunction{}
_, err = o.BinaryOp(token.Add, objects.UndefinedValue)
assert.Error(t, err)
o = &objects.ReturnValue{}
_, err = o.BinaryOp(token.Add, objects.UndefinedValue)
assert.Error(t, err)
o = &objects.Undefined{}
_, err = o.BinaryOp(token.Add, objects.UndefinedValue)
assert.Error(t, err)
} }