Skip to content

Commit

Permalink
better message for method call for hash
Browse files Browse the repository at this point in the history
  • Loading branch information
gravataLonga committed Jul 3, 2022
1 parent 7ab17d9 commit a6f749f
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 18 deletions.
33 changes: 22 additions & 11 deletions evaluator/hash_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -297,24 +297,35 @@ func TestHashMethodWrongUsage(t *testing.T) {
}{
{
`{}.keys(1)`,
"hash.keys() expect 0 arguments. Got: [1]",
"TypeError: hash.keys() takes exactly 0 argument (1 given)",
},
{
`{}.has(1, 2)`,
"hash.has() expect at least 1 argument. got: [1, 2]",
"TypeError: hash.has() takes exactly 1 argument (2 given)",
},
{
`{}.has()`,
"TypeError: hash.has() takes exactly 1 argument (0 given)",
},
{
`{}.type(1)`,
"TypeError: hash.type() takes exactly 0 argument (1 given)",
},
}

for _, tt := range tests {
evaluated := testEval(tt.input, t)
for i, tt := range tests {
t.Run(fmt.Sprintf("TestHashMethodWrongUsage[%d]", i), func(t *testing.T) {
evaluated := testEval(tt.input, t)

errObj, ok := evaluated.(*object.Error)
if !ok {
t.Fatalf("no error object returned. got=%T(%+v)", evaluated, evaluated)
}
errObj, ok := evaluated.(*object.Error)
if !ok {
t.Fatalf("no error object returned. got=%T(%+v)", evaluated, evaluated)
}

if errObj.Message != tt.expectedErrorMessage {
t.Errorf("erro expected \"%s\". Got: %s", tt.expectedErrorMessage, errObj.Message)
}
})

if errObj.Message != tt.expectedErrorMessage {
t.Errorf("erro expected \"%s\". Got: %s", tt.expectedErrorMessage, errObj.Message)
}
}
}
33 changes: 26 additions & 7 deletions object/hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,16 @@ func (h *Hash) Inspect() string {
func (s *Hash) Call(objectCall *ast.ObjectCall, method string, env *Environment, args ...Object) Object {
switch method {
case "type":
if len(args) > 0 {
argStr := InspectObject(args...)
return NewErrorFormat("method type not accept any arguments. got: %s", argStr)
err := Check(
"hash.type",
args,
ExactArgs(0),
)

if err != nil {
return NewError(err.Error())
}

return &String{Value: HASH_OBJ}
case "keys":
return hashKeys(s.Pairs, args...)
Expand All @@ -50,9 +56,16 @@ func (s *Hash) Call(objectCall *ast.ObjectCall, method string, env *Environment,
}

func hashKeys(keys map[HashKey]HashPair, args ...Object) Object {
if len(args) != 0 {
return NewErrorFormat("hash.keys() expect 0 arguments. Got: %s", InspectObject(args...))
err := Check(
"hash.keys",
args,
ExactArgs(0),
)

if err != nil {
return NewError(err.Error())
}

elements := make([]Object, len(keys))
i := 0
for _, pair := range keys {
Expand All @@ -64,8 +77,14 @@ func hashKeys(keys map[HashKey]HashPair, args ...Object) Object {
}

func hashHas(keys map[HashKey]HashPair, args ...Object) Object {
if len(args) != 1 {
return NewErrorFormat("hash.has() expect at least 1 argument. got: %s", InspectObject(args...))
err := Check(
"hash.has",
args,
ExactArgs(1),
)

if err != nil {
return NewError(err.Error())
}

hashable, ok := args[0].(Hashable)
Expand Down

0 comments on commit a6f749f

Please sign in to comment.