Convert returns second argument Object as it self in case of failure of conversion of first argument

objects/builtin_convert.go
- Changed type check of in case of args[0] failure to always return
  args[1] if set

runtime/vm_builtin_test.go:
- Changed tests according to changes in objects/builtin_convert.go
This commit is contained in:
Mike Bazuin 2019-01-18 23:12:16 +01:00
parent a747c98fb5
commit fae85000cf
2 changed files with 17 additions and 32 deletions

View file

@ -16,9 +16,7 @@ func builtinString(args ...Object) (Object, error) {
}
if argsLen == 2 {
if _, ok := args[1].(*String); ok {
return args[1], nil
}
return args[1], nil
}
return UndefinedValue, nil
@ -40,9 +38,7 @@ func builtinInt(args ...Object) (Object, error) {
}
if argsLen == 2 {
if _, ok := args[1].(*Int); ok {
return args[1], nil
}
return args[1]
}
return UndefinedValue, nil
@ -64,12 +60,7 @@ func builtinFloat(args ...Object) (Object, error) {
}
if argsLen == 2 {
if _, ok := args[1].(*Float); ok {
return args[1], nil
} else if _, ok := args[1].(*Int); ok {
v, _ := ToFloat64(args[1])
return &Float{Value: v}, nil
}
return args[1]
}
return UndefinedValue, nil
@ -108,9 +99,7 @@ func builtinChar(args ...Object) (Object, error) {
}
if argsLen == 2 {
if _, ok := args[1].(*Char); ok {
return args[1], nil
}
return args[1]
}
return UndefinedValue, nil
@ -133,15 +122,7 @@ func builtinBytes(args ...Object) (Object, error) {
}
if argsLen == 2 {
// bytes(N) => create a new bytes with given size N
if n, ok := args[1].(*Int); ok {
return &Bytes{Value: make([]byte, int(n.Value))}, nil
}
v, ok = ToByteSlice(args[1])
if ok {
return &Bytes{Value: v}, nil
}
return args[1]
}
return UndefinedValue, nil

View file

@ -29,7 +29,8 @@ func TestBuiltinFunction(t *testing.T) {
expect(t, `out = int(undefined)`, undefined())
expect(t, `out = int("-522", 1)`, -522)
expect(t, `out = int(undefined, 1)`, 1)
expect(t, `out = int(undefined, 1.8)`, undefined())
expect(t, `out = int(undefined, 1.8)`, 1.8)
expect(t, `out = int(undefined, string(1))`, "1")
expect(t, `out = int(undefined, undefined)`, undefined())
expect(t, `out = string(1)`, "1")
@ -54,9 +55,10 @@ func TestBuiltinFunction(t *testing.T) {
expect(t, `out = float({a: 1, b: "foo"})`, undefined())
expect(t, `out = float(undefined)`, undefined())
expect(t, `out = float("-52.2", 1.8)`, -52.2)
expect(t, `out = float(undefined, 1)`, 1.0)
expect(t, `out = float(undefined, 1)`, 1)
expect(t, `out = float(undefined, 1.8)`, 1.8)
expect(t, `out = float(undefined, "-52.2")`, undefined())
expect(t, `out = float(undefined, "-52.2")`, "-52.2")
expect(t, `out = float(undefined, char(56))`, '8')
expect(t, `out = float(undefined, undefined)`, undefined())
expect(t, `out = char(56)`, '8')
@ -70,8 +72,9 @@ func TestBuiltinFunction(t *testing.T) {
expect(t, `out = char(undefined)`, undefined())
expect(t, `out = char(56, 'a')`, '8')
expect(t, `out = char(undefined, '8')`, '8')
expect(t, `out = char(undefined, 56)`, undefined())
expect(t, `out = char(undefined, "-52.2")`, undefined())
expect(t, `out = char(undefined, 56)`, 56)
expect(t, `out = char(undefined, "-52.2")`, "-52.2")
expect(t, `out = char(undefined, undefined)`, undefined())
expect(t, `out = bool(1)`, true) // non-zero integer: true
expect(t, `out = bool(0)`, false) // zero: true
@ -99,9 +102,10 @@ func TestBuiltinFunction(t *testing.T) {
expect(t, `out = bytes({a: 1})`, undefined())
expect(t, `out = bytes(undefined)`, undefined())
expect(t, `out = bytes("-522", ['8'])`, []byte{'-', '5', '2', '2'})
expect(t, `out = bytes(undefined, "-522")`, []byte{'-', '5', '2', '2'})
expect(t, `out = bytes(undefined, 1)`, []byte{0})
expect(t, `out = bytes(undefined, 1.8)`, undefined())
expect(t, `out = bytes(undefined, "-522")`, "-522")
expect(t, `out = bytes(undefined, 1)`, 1)
expect(t, `out = bytes(undefined, 1.8)`, 1.8)
expect(t, `out = bytes(undefined, int("-522"))`, -522)
expect(t, `out = bytes(undefined, undefined)`, undefined())
expect(t, `out = is_error(error(1))`, true)