-
Notifications
You must be signed in to change notification settings - Fork 305
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
builtin delete for maps and arrays (#250)
* added builtin delete function and unit tests * added vm tests for builtin delete * added doc for builtin delete * update doc
- Loading branch information
Ozan HACIBEKİROĞLU
authored
Feb 19, 2020
1 parent
6fb0df7
commit ac53405
Showing
4 changed files
with
185 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
package tengo | ||
|
||
import ( | ||
"errors" | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
func Test_builtinDelete(t *testing.T) { | ||
type args struct { | ||
args []Object | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
want Object | ||
wantErr bool | ||
wantedErr error | ||
target interface{} | ||
}{ | ||
//Map | ||
{name: "invalid-arg", args: args{[]Object{&String{}, &String{}}}, wantErr: true, | ||
wantedErr: ErrInvalidArgumentType{Name: "first", Expected: "map", Found: "string"}}, | ||
{name: "no-args", wantErr: true, wantedErr: ErrWrongNumArguments}, | ||
{name: "empty-args", args: args{[]Object{}}, wantErr: true, wantedErr: ErrWrongNumArguments}, | ||
{name: "3-args", args: args{[]Object{(*Map)(nil), (*String)(nil), (*String)(nil)}}, wantErr: true, wantedErr: ErrWrongNumArguments}, | ||
{name: "nil-map-empty-key", args: args{[]Object{&Map{}, &String{}}}, want: UndefinedValue}, | ||
{name: "nil-map-nonstr-key", args: args{[]Object{&Map{}, &Int{}}}, wantErr: true, | ||
wantedErr: ErrInvalidArgumentType{Name: "second", Expected: "string", Found: "int"}}, | ||
{name: "nil-map-no-key", args: args{[]Object{&Map{}}}, wantErr: true, | ||
wantedErr: ErrWrongNumArguments}, | ||
{name: "map-missing-key", | ||
args: args{ | ||
[]Object{ | ||
&Map{Value: map[string]Object{ | ||
"key": &String{Value: "value"}, | ||
}}, | ||
&String{Value: "key1"}, | ||
}}, | ||
want: UndefinedValue, | ||
target: &Map{Value: map[string]Object{"key": &String{Value: "value"}}}, | ||
}, | ||
{name: "map-emptied", | ||
args: args{ | ||
[]Object{ | ||
&Map{Value: map[string]Object{ | ||
"key": &String{Value: "value"}, | ||
}}, | ||
&String{Value: "key"}, | ||
}}, | ||
want: UndefinedValue, | ||
target: &Map{Value: map[string]Object{}}, | ||
}, | ||
{name: "map-multi-keys", | ||
args: args{ | ||
[]Object{ | ||
&Map{Value: map[string]Object{ | ||
"key1": &String{Value: "value1"}, | ||
"key2": &Int{Value: 10}, | ||
}}, | ||
&String{Value: "key1"}, | ||
}}, | ||
want: UndefinedValue, | ||
target: &Map{Value: map[string]Object{"key2": &Int{Value: 10}}}, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
got, err := builtinDelete(tt.args.args...) | ||
if (err != nil) != tt.wantErr { | ||
t.Errorf("builtinDelete() error = %v, wantErr %v", err, tt.wantErr) | ||
return | ||
} | ||
if tt.wantErr && !errors.Is(err, tt.wantedErr) { | ||
if err.Error() != tt.wantedErr.Error() { | ||
t.Errorf("builtinDelete() error = %v, wantedErr %v", err, tt.wantedErr) | ||
return | ||
} | ||
} | ||
if got != tt.want { | ||
t.Errorf("builtinDelete() = %v, want %v", got, tt.want) | ||
return | ||
} | ||
if !tt.wantErr && tt.target != nil { | ||
switch v := tt.args.args[0].(type) { | ||
case *Map, *Array: | ||
if !reflect.DeepEqual(tt.target, tt.args.args[0]) { | ||
t.Errorf("builtinDelete() objects are not equal got: %+v, want: %+v", tt.args.args[0], tt.target) | ||
} | ||
default: | ||
t.Errorf("builtinDelete() unsuporrted arg[0] type %s", v.TypeName()) | ||
return | ||
} | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters