Merge pull request #14 from mdbazuin/feature/7-type-conversion-defaults
Type conversion builtin take default values
This commit is contained in:
commit
fa294d9731
2 changed files with 64 additions and 5 deletions
|
@ -1,7 +1,8 @@
|
|||
package objects
|
||||
|
||||
func builtinString(args ...Object) (Object, error) {
|
||||
if len(args) != 1 {
|
||||
argsLen := len(args)
|
||||
if !(argsLen == 1 || argsLen == 2) {
|
||||
return nil, ErrWrongNumArguments
|
||||
}
|
||||
|
||||
|
@ -14,11 +15,16 @@ func builtinString(args ...Object) (Object, error) {
|
|||
return &String{Value: v}, nil
|
||||
}
|
||||
|
||||
if argsLen == 2 {
|
||||
return args[1], nil
|
||||
}
|
||||
|
||||
return UndefinedValue, nil
|
||||
}
|
||||
|
||||
func builtinInt(args ...Object) (Object, error) {
|
||||
if len(args) != 1 {
|
||||
argsLen := len(args)
|
||||
if !(argsLen == 1 || argsLen == 2) {
|
||||
return nil, ErrWrongNumArguments
|
||||
}
|
||||
|
||||
|
@ -31,11 +37,16 @@ func builtinInt(args ...Object) (Object, error) {
|
|||
return &Int{Value: v}, nil
|
||||
}
|
||||
|
||||
if argsLen == 2 {
|
||||
return args[1]
|
||||
}
|
||||
|
||||
return UndefinedValue, nil
|
||||
}
|
||||
|
||||
func builtinFloat(args ...Object) (Object, error) {
|
||||
if len(args) != 1 {
|
||||
argsLen := len(args)
|
||||
if !(argsLen == 1 || argsLen == 2) {
|
||||
return nil, ErrWrongNumArguments
|
||||
}
|
||||
|
||||
|
@ -48,6 +59,10 @@ func builtinFloat(args ...Object) (Object, error) {
|
|||
return &Float{Value: v}, nil
|
||||
}
|
||||
|
||||
if argsLen == 2 {
|
||||
return args[1]
|
||||
}
|
||||
|
||||
return UndefinedValue, nil
|
||||
}
|
||||
|
||||
|
@ -69,7 +84,8 @@ func builtinBool(args ...Object) (Object, error) {
|
|||
}
|
||||
|
||||
func builtinChar(args ...Object) (Object, error) {
|
||||
if len(args) != 1 {
|
||||
argsLen := len(args)
|
||||
if !(argsLen == 1 || argsLen == 2) {
|
||||
return nil, ErrWrongNumArguments
|
||||
}
|
||||
|
||||
|
@ -82,11 +98,16 @@ func builtinChar(args ...Object) (Object, error) {
|
|||
return &Char{Value: v}, nil
|
||||
}
|
||||
|
||||
if argsLen == 2 {
|
||||
return args[1]
|
||||
}
|
||||
|
||||
return UndefinedValue, nil
|
||||
}
|
||||
|
||||
func builtinBytes(args ...Object) (Object, error) {
|
||||
if len(args) != 1 {
|
||||
argsLen := len(args)
|
||||
if !(argsLen == 1 || argsLen == 2) {
|
||||
return nil, ErrWrongNumArguments
|
||||
}
|
||||
|
||||
|
@ -100,5 +121,9 @@ func builtinBytes(args ...Object) (Object, error) {
|
|||
return &Bytes{Value: v}, nil
|
||||
}
|
||||
|
||||
if argsLen == 2 {
|
||||
return args[1]
|
||||
}
|
||||
|
||||
return UndefinedValue, nil
|
||||
}
|
||||
|
|
|
@ -27,6 +27,11 @@ func TestBuiltinFunction(t *testing.T) {
|
|||
expect(t, `out = int([1])`, undefined())
|
||||
expect(t, `out = int({a: 1})`, undefined())
|
||||
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)`, 1.8)
|
||||
expect(t, `out = int(undefined, string(1))`, "1")
|
||||
expect(t, `out = int(undefined, undefined)`, undefined())
|
||||
|
||||
expect(t, `out = string(1)`, "1")
|
||||
expect(t, `out = string(1.8)`, "1.8")
|
||||
|
@ -37,6 +42,8 @@ func TestBuiltinFunction(t *testing.T) {
|
|||
expect(t, `out = string([1,8.1,true,3])`, "[1, 8.1, true, 3]")
|
||||
expect(t, `out = string({b: "foo"})`, `{b: "foo"}`)
|
||||
expect(t, `out = string(undefined)`, undefined()) // not "undefined"
|
||||
expect(t, `out = string(1, "-522")`, "1")
|
||||
expect(t, `out = string(undefined, "-522")`, "-522") // not "undefined"
|
||||
|
||||
expect(t, `out = float(1)`, 1.0)
|
||||
expect(t, `out = float(1.8)`, 1.8)
|
||||
|
@ -47,6 +54,12 @@ func TestBuiltinFunction(t *testing.T) {
|
|||
expect(t, `out = float([1,8.1,true,3])`, undefined())
|
||||
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)
|
||||
expect(t, `out = float(undefined, 1.8)`, 1.8)
|
||||
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')
|
||||
expect(t, `out = char(1.8)`, undefined())
|
||||
|
@ -57,6 +70,11 @@ func TestBuiltinFunction(t *testing.T) {
|
|||
expect(t, `out = char([1,8.1,true,3])`, undefined())
|
||||
expect(t, `out = char({a: 1, b: "foo"})`, undefined())
|
||||
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)`, 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
|
||||
|
@ -74,6 +92,22 @@ func TestBuiltinFunction(t *testing.T) {
|
|||
expect(t, `out = bool({})`, false) // empty maps: false
|
||||
expect(t, `out = bool(undefined)`, false) // undefined: false
|
||||
|
||||
expect(t, `out = bytes(1)`, []byte{0})
|
||||
expect(t, `out = bytes(1.8)`, undefined())
|
||||
expect(t, `out = bytes("-522")`, []byte{'-', '5', '2', '2'})
|
||||
expect(t, `out = bytes(true)`, undefined())
|
||||
expect(t, `out = bytes(false)`, undefined())
|
||||
expect(t, `out = bytes('8')`, undefined())
|
||||
expect(t, `out = bytes([1])`, undefined())
|
||||
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")`, "-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)
|
||||
expect(t, `out = is_error(1)`, false)
|
||||
|
||||
|
|
Loading…
Reference in a new issue