Fix a bug where len() builtin does not take immutable array

This commit is contained in:
Daniel Kang 2019-02-01 17:32:05 -08:00
parent 4f222b1650
commit c57a7f8f98
2 changed files with 10 additions and 0 deletions

View file

@ -12,6 +12,8 @@ func builtinLen(args ...Object) (Object, error) {
switch arg := args[0].(type) {
case *Array:
return &Int{Value: int64(len(arg.Value))}, nil
case *ImmutableArray:
return &Int{Value: int64(len(arg.Value))}, nil
case *String:
return &Int{Value: int64(len(arg.Value))}, nil
case *Bytes:

View file

@ -10,6 +10,14 @@ func TestBuiltinFunction(t *testing.T) {
expect(t, `out = len("")`, 0)
expect(t, `out = len("four")`, 4)
expect(t, `out = len("hello world")`, 11)
expect(t, `out = len([])`, 0)
expect(t, `out = len([1, 2, 3])`, 3)
expect(t, `out = len({})`, 0)
expect(t, `out = len({a:1, b:2})`, 2)
expect(t, `out = len(immutable([]))`, 0)
expect(t, `out = len(immutable([1, 2, 3]))`, 3)
expect(t, `out = len(immutable({}))`, 0)
expect(t, `out = len(immutable({a:1, b:2}))`, 2)
expectError(t, `len(1)`)
expectError(t, `len("one", "two")`)