Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
increase test coverage even more
Browse files Browse the repository at this point in the history
MarkusFreitag committed Jan 17, 2022
1 parent f471d18 commit 91405c8
Showing 7 changed files with 152 additions and 2 deletions.
4 changes: 2 additions & 2 deletions object/array.go
Original file line number Diff line number Diff line change
@@ -123,7 +123,7 @@ func init() {
method: func(o Object, _ []Object) Object {
ao := o.(*Array)
if len(ao.Elements) == 0 {
return new(Null)
return NULL
}
return ao.Elements[0]
},
@@ -138,7 +138,7 @@ func init() {
method: func(o Object, _ []Object) Object {
ao := o.(*Array)
if len(ao.Elements) == 0 {
return new(Null)
return NULL
}
return ao.Elements[len(ao.Elements)-1]
},
26 changes: 26 additions & 0 deletions object/array_test.go
Original file line number Diff line number Diff line change
@@ -6,6 +6,27 @@ import (
"github.com/flipez/rocket-lang/object"
)

func TestNewArrayWithObjects(t *testing.T) {
arr := object.NewArrayWithObjects(object.NewString("a"))
if v := arr.Type(); v != object.ARRAY_OBJ {
t.Errorf("array.Type() return wrong type: %s", v)
}

if v := arr.Elements[0].Type(); v != object.STRING_OBJ {
t.Errorf("first array element should be a string object")
}
}

func TestArrayObject(t *testing.T) {
tests := []inputTestCase{
{"[1] == [1]", true},
{"[1] == [true]", false},
{"[1] == [true, 1]", false},
}

testInput(t, tests)
}

func TestArrayObjectMethods(t *testing.T) {
tests := []inputTestCase{
{`[1,2,3][0]`, 1},
@@ -24,6 +45,11 @@ func TestArrayObjectMethods(t *testing.T) {
{`[1,1,2].uniq().size()`, 2},
{`[true,true,2].uniq().size()`, 2},
{`["test","test",2].uniq().size()`, 2},
{`["12".reverse!()].uniq()`, "failed because element NULL is not hashable"},
{"[].first()", "NULL"},
{"[1,2,3].first()", 1},
{"[].last()", "NULL"},
{"[1,2,3].last()", 3},
}

testInput(t, tests)
2 changes: 2 additions & 0 deletions object/boolean_test.go
Original file line number Diff line number Diff line change
@@ -34,6 +34,8 @@ func TestBooleanObjectMethods(t *testing.T) {

// other
{"(true.wat().lines().size() == true.methods().size() + 1).plz_s()", "true"},
{"true == true", true},
{"true == false", false},
}

testInput(t, tests)
30 changes: 30 additions & 0 deletions object/builtin_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package object_test

import (
"testing"

"github.com/flipez/rocket-lang/object"
)

func TestBuiltinObjectMethods(t *testing.T) {
tests := []inputTestCase{
{`puts.nope()`, "Failed to invoke method: nope"},
}

testInput(t, tests)
}

func TestBuiltinType(t *testing.T) {
tests := []inputTestCase{
{"puts", "builtin function"},
}

for _, tt := range tests {
def := testEval(tt.input).(*object.Builtin)
defInspect := def.Inspect()

if defInspect != tt.expected {
t.Errorf("wrong string. expected=%#v, got=%#v", tt.expected, defInspect)
}
}
}
12 changes: 12 additions & 0 deletions object/hash_test.go
Original file line number Diff line number Diff line change
@@ -6,6 +6,18 @@ import (
"github.com/flipez/rocket-lang/object"
)

func TestHashObject(t *testing.T) {
tests := []inputTestCase{
{`{"a": 1} == {"a": 1}`, true},
{`{"a": 1} == {"a": 1, "b": 2}`, false},
{`{"a": 1} == {"b": 1}`, false},
{`{"a": 1} == {"a": "c"}`, false},
{`{{1: true}: "a"}.keys()`, `[{1: true}]`},
}

testInput(t, tests)
}

func TestHashObjectMethods(t *testing.T) {
tests := []inputTestCase{
{`{"a": 2}.keys()`, `["a"]`},
73 changes: 73 additions & 0 deletions object/object_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package object_test

import (
"errors"

"github.com/flipez/rocket-lang/evaluator"
"github.com/flipez/rocket-lang/lexer"
"github.com/flipez/rocket-lang/object"
@@ -44,6 +46,10 @@ func testInput(t *testing.T, tests []inputTestCase) {
testStringObject(t, strObj, expected)
continue
}
_, ok = evaluated.(*object.Null)
if ok {
continue
}

errObj, ok := evaluated.(*object.Error)
if !ok {
@@ -58,3 +64,70 @@ func testInput(t *testing.T, tests []inputTestCase) {
}
}
}

func TestIsError(t *testing.T) {
trueErrors := []object.Object{
object.NewError(errors.New("test error")),
object.NewError("test error"),
object.NewErrorFormat("test %s", "error"),
}

for _, err := range trueErrors {
if !object.IsError(err) {
t.Errorf("'%s' should be an error", err.Inspect())
}
}

falseErrors := []object.Object{
nil,
object.NewString("a"),
object.NULL,
}
for _, err := range falseErrors {
if object.IsError(nil) {
t.Errorf("'%#v' is not an error", err)
}
}
}

func TestIsNumber(t *testing.T) {
if !object.IsNumber(object.NewInteger(1)) {
t.Error("INTEGER_OBJ should be a number")
}
if !object.IsNumber(object.NewFloat(1.1)) {
t.Error("FLOAT_OBJ should be a number")
}
if object.IsNumber(object.NULL) {
t.Error("NULL_OBJ is not a number")
}
}

func TestIsTruthy(t *testing.T) {
if !object.IsTruthy(object.TRUE) {
t.Error("BOOLEAN_OBJ=true should be truthy")
}
if !object.IsTruthy(object.NewString("")) {
t.Error("STRING_OBJ should be truthy")
}
if object.IsTruthy(object.NULL) {
t.Error("NULL_OBJ should not be truthy")
}
if object.IsTruthy(object.FALSE) {
t.Errorf("BOOLEAN_OBJ=false, should not be truthy")
}
}

func TestIsFalsy(t *testing.T) {
if object.IsFalsy(object.TRUE) {
t.Error("BOOLEAN_OBJ=true should not be falsy")
}
if object.IsFalsy(object.NewString("")) {
t.Error("STRING_OBJ should not be falsy")
}
if !object.IsFalsy(object.NULL) {
t.Error("NULL_OBJ should be falsy")
}
if !object.IsFalsy(object.FALSE) {
t.Errorf("BOOLEAN_OBJ=false, should be falsy")
}
}
7 changes: 7 additions & 0 deletions object/string_test.go
Original file line number Diff line number Diff line change
@@ -20,6 +20,13 @@ func testStringObject(t *testing.T, obj object.Object, expected string) bool {
return true
}

func TestStringObject(t *testing.T) {
tests := []inputTestCase{
{`"a" == "a"`, true},
}
testInput(t, tests)
}

func TestStringObjectMethods(t *testing.T) {
tests := []inputTestCase{
{`"test".count("e")`, 1},

0 comments on commit 91405c8

Please sign in to comment.