github.com/MontFerret/ferret@v0.18.0/pkg/stdlib/objects/merge_test.go (about)

     1  package objects_test
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  
     7  	"github.com/MontFerret/ferret/pkg/runtime/values"
     8  	"github.com/MontFerret/ferret/pkg/stdlib/objects"
     9  
    10  	. "github.com/smartystreets/goconvey/convey"
    11  )
    12  
    13  func TestMerge(t *testing.T) {
    14  	Convey("When not enough arguments", t, func() {
    15  		obj, err := objects.Merge(context.Background())
    16  
    17  		So(err, ShouldBeError)
    18  		So(obj.Compare(values.None), ShouldEqual, 0)
    19  	})
    20  
    21  	Convey("When wrong type of arguments", t, func() {
    22  		obj, err := objects.Merge(context.Background(), values.NewInt(0))
    23  
    24  		So(err, ShouldBeError)
    25  		So(obj.Compare(values.None), ShouldEqual, 0)
    26  
    27  		obj, err = objects.Merge(context.Background(), values.NewObject(), values.NewInt(0))
    28  
    29  		So(err, ShouldBeError)
    30  		So(obj.Compare(values.None), ShouldEqual, 0)
    31  	})
    32  
    33  	Convey("When too many arrays", t, func() {
    34  		obj, err := objects.Merge(context.Background(), values.NewArray(0), values.NewArray(0))
    35  
    36  		So(err, ShouldBeError)
    37  		So(obj.Compare(values.None), ShouldEqual, 0)
    38  	})
    39  
    40  	Convey("Merged object should be independent of source objects", t, func() {
    41  		obj1 := values.NewObjectWith(
    42  			values.NewObjectProperty("prop1", values.NewInt(1)),
    43  			values.NewObjectProperty("prop2", values.NewString("str")),
    44  		)
    45  		obj2 := values.NewObjectWith(
    46  			values.NewObjectProperty("prop3", values.NewInt(3)),
    47  		)
    48  
    49  		result := values.NewObjectWith(
    50  			values.NewObjectProperty("prop1", values.NewInt(1)),
    51  			values.NewObjectProperty("prop2", values.NewString("str")),
    52  			values.NewObjectProperty("prop3", values.NewInt(3)),
    53  		)
    54  
    55  		merged, err := objects.Merge(context.Background(), obj1, obj2)
    56  
    57  		So(err, ShouldBeNil)
    58  
    59  		obj1.Remove(values.NewString("prop1"))
    60  
    61  		So(merged.Compare(result), ShouldEqual, 0)
    62  	})
    63  }
    64  
    65  func TestMergeObjects(t *testing.T) {
    66  	Convey("Merge single object", t, func() {
    67  		obj1 := values.NewObjectWith(
    68  			values.NewObjectProperty("prop1", values.NewInt(1)),
    69  			values.NewObjectProperty("prop2", values.NewString("str")),
    70  		)
    71  		result := values.NewObjectWith(
    72  			values.NewObjectProperty("prop1", values.NewInt(1)),
    73  			values.NewObjectProperty("prop2", values.NewString("str")),
    74  		)
    75  
    76  		merged, err := objects.Merge(context.Background(), obj1)
    77  
    78  		So(err, ShouldBeNil)
    79  		So(merged.Compare(result), ShouldEqual, 0)
    80  	})
    81  
    82  	Convey("Merge two objects", t, func() {
    83  		obj1 := values.NewObjectWith(
    84  			values.NewObjectProperty("prop1", values.NewInt(1)),
    85  			values.NewObjectProperty("prop2", values.NewString("str")),
    86  		)
    87  		obj2 := values.NewObjectWith(
    88  			values.NewObjectProperty("prop3", values.NewInt(3)),
    89  		)
    90  
    91  		result := values.NewObjectWith(
    92  			values.NewObjectProperty("prop1", values.NewInt(1)),
    93  			values.NewObjectProperty("prop2", values.NewString("str")),
    94  			values.NewObjectProperty("prop3", values.NewInt(3)),
    95  		)
    96  
    97  		merged, err := objects.Merge(context.Background(), obj1, obj2)
    98  
    99  		So(err, ShouldBeNil)
   100  		So(merged.Compare(result), ShouldEqual, 0)
   101  	})
   102  
   103  	Convey("When keys are repeated", t, func() {
   104  		obj1 := values.NewObjectWith(
   105  			values.NewObjectProperty("prop1", values.NewInt(1)),
   106  			values.NewObjectProperty("prop2", values.NewString("str")),
   107  		)
   108  		obj2 := values.NewObjectWith(
   109  			values.NewObjectProperty("prop1", values.NewInt(3)),
   110  		)
   111  		result := values.NewObjectWith(
   112  			values.NewObjectProperty("prop1", values.NewInt(3)),
   113  			values.NewObjectProperty("prop2", values.NewString("str")),
   114  		)
   115  
   116  		merged, err := objects.Merge(context.Background(), obj1, obj2)
   117  
   118  		So(err, ShouldBeNil)
   119  		So(merged.Compare(result), ShouldEqual, 0)
   120  	})
   121  }
   122  
   123  func TestMergeArray(t *testing.T) {
   124  	Convey("Merge array", t, func() {
   125  		objArr := values.NewArrayWith(
   126  			values.NewObjectWith(
   127  				values.NewObjectProperty("prop1", values.NewInt(1)),
   128  			),
   129  			values.NewObjectWith(
   130  				values.NewObjectProperty("prop2", values.NewInt(2)),
   131  			),
   132  		)
   133  		result := values.NewObjectWith(
   134  			values.NewObjectProperty("prop1", values.NewInt(1)),
   135  			values.NewObjectProperty("prop2", values.NewInt(2)),
   136  		)
   137  
   138  		merged, err := objects.Merge(context.Background(), objArr)
   139  
   140  		So(err, ShouldBeNil)
   141  		So(merged.Compare(result), ShouldEqual, 0)
   142  	})
   143  
   144  	Convey("Merge empty array", t, func() {
   145  		objArr := values.NewArray(0)
   146  		result := values.NewObject()
   147  
   148  		merged, err := objects.Merge(context.Background(), objArr)
   149  
   150  		So(err, ShouldBeNil)
   151  		So(merged.Compare(result), ShouldEqual, 0)
   152  	})
   153  
   154  	Convey("When there is not object element inside the array", t, func() {
   155  		objArr := values.NewArrayWith(
   156  			values.NewObject(),
   157  			values.NewInt(0),
   158  		)
   159  
   160  		obj, err := objects.Merge(context.Background(), objArr)
   161  
   162  		So(err, ShouldBeError)
   163  		So(obj.Compare(values.None), ShouldEqual, 0)
   164  	})
   165  }