From 344c96d2806c344547898a326ac82d45f86b0248 Mon Sep 17 00:00:00 2001 From: Daniel Kang Date: Tue, 15 Jan 2019 10:35:51 -0800 Subject: [PATCH] add objects tests --- compiler/compiler_test.go | 23 ++++++++++ objects/object_test.go | 96 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 119 insertions(+) diff --git a/compiler/compiler_test.go b/compiler/compiler_test.go index f1d90dd..da2894e 100644 --- a/compiler/compiler_test.go +++ b/compiler/compiler_test.go @@ -832,6 +832,29 @@ func() { intObject(0), intObject(10), 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 { diff --git a/objects/object_test.go b/objects/object_test.go index c1eea8e..d658346 100644 --- a/objects/object_test.go +++ b/objects/object_test.go @@ -4,6 +4,7 @@ import ( "testing" "github.com/d5/tengo/assert" + "github.com/d5/tengo/compiler/token" "github.com/d5/tengo/objects" ) @@ -29,6 +30,20 @@ func TestObject_TypeName(t *testing.T) { assert.Equal(t, "string-iterator", o.TypeName()) o = &objects.MapIterator{} 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) { @@ -57,4 +72,85 @@ func TestObject_IsFalsy(t *testing.T) { 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 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) }