Skip to content

Commit

Permalink
Add args-test to Integer#to_d
Browse files Browse the repository at this point in the history
  • Loading branch information
hachi8833 committed Apr 15, 2018
1 parent 182e7c9 commit ba10e33
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
4 changes: 4 additions & 0 deletions vm/integer.go
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,10 @@ func builtinIntegerInstanceMethods() []*BuiltinMethodObject {
Name: "to_d",
Fn: func(receiver Object, sourceLine int) builtinMethodBody {
return func(t *thread, args []Object, blockFrame *normalCallFrame) Object {
if len(args) > 1 {
return t.vm.initErrorObject(errors.ArgumentError, sourceLine, "Expect 0 or 1 argument. got: %d", len(args))
}
// TODO: implement the digit truncation in other PR
r := receiver.(*IntegerObject)
return t.vm.initDecimalObject(intToDecimal(r))
}
Expand Down
14 changes: 14 additions & 0 deletions vm/integer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,20 @@ func TestIntegerConversion(t *testing.T) {
}
}

func TestIntegerConversonFail(t *testing.T) {
testsFail := []errorTestCase{
{`100.to_d(1, 2, 3)`, "ArgumentError: Expect 0 or 1 argument. got: 3", 1},
}

for i, tt := range testsFail {
v := initTestVM()
evaluated := v.testEval(t, tt.input, getFilename())
checkErrorMsg(t, i, evaluated, tt.expected)
v.checkCFP(t, i, tt.expectedCFP)
v.checkSP(t, i, 1)
}
}

// Method test

func TestIntegerEvenMethod(t *testing.T) {
Expand Down

0 comments on commit ba10e33

Please sign in to comment.