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

     1  package objects_test
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  
     7  	. "github.com/smartystreets/goconvey/convey"
     8  
     9  	"github.com/MontFerret/ferret/pkg/runtime/core"
    10  	"github.com/MontFerret/ferret/pkg/runtime/values"
    11  	"github.com/MontFerret/ferret/pkg/stdlib/objects"
    12  )
    13  
    14  func TestKeepKeys(t *testing.T) {
    15  	Convey("When not enough arguments)", t, func() {
    16  		// there is no object
    17  		obj, err := objects.KeepKeys(context.Background())
    18  
    19  		So(err, ShouldBeError)
    20  		So(obj, ShouldEqual, values.None)
    21  
    22  		// there are no keys
    23  		obj, err = objects.KeepKeys(context.Background(), values.NewObject())
    24  
    25  		So(err, ShouldBeError)
    26  		So(obj, ShouldEqual, values.None)
    27  	})
    28  
    29  	Convey("When first argument isn't object", t, func() {
    30  		obj, err := objects.KeepKeys(context.Background(), values.NewInt(0))
    31  
    32  		So(err, ShouldBeError)
    33  		So(obj, ShouldEqual, values.None)
    34  	})
    35  
    36  	Convey("When wrong keys arguments", t, func() {
    37  		obj, err := objects.KeepKeys(context.Background(), values.NewObject(), values.NewInt(0))
    38  
    39  		So(err, ShouldBeError)
    40  		So(obj, ShouldEqual, values.None)
    41  
    42  		// looks like a valid case
    43  		// but there is another argument besides an array
    44  		obj, err = objects.KeepKeys(context.Background(), values.NewObject(), values.NewArray(0), values.NewInt(0))
    45  
    46  		So(err, ShouldBeError)
    47  		So(obj, ShouldEqual, values.None)
    48  	})
    49  
    50  	Convey("Result object is independent of the source object", t, func() {
    51  		arr := values.NewArrayWith(values.Int(0))
    52  		obj := values.NewObjectWith(
    53  			values.NewObjectProperty("a", arr),
    54  		)
    55  		resultObj := values.NewObjectWith(
    56  			values.NewObjectProperty("a", values.NewArrayWith(values.Int(0))),
    57  		)
    58  
    59  		afterKeepKeys, err := objects.KeepKeys(context.Background(), obj, values.NewString("a"))
    60  
    61  		So(err, ShouldBeNil)
    62  
    63  		arr.Push(values.NewInt(1))
    64  
    65  		So(afterKeepKeys.Compare(resultObj), ShouldEqual, 0)
    66  	})
    67  }
    68  
    69  func TestKeepKeysStrings(t *testing.T) {
    70  	Convey("KeepKeys key 'a'", t, func() {
    71  		obj := values.NewObjectWith(
    72  			values.NewObjectProperty("a", values.NewInt(1)),
    73  			values.NewObjectProperty("b", values.NewString("string")),
    74  		)
    75  		resultObj := values.NewObjectWith(
    76  			values.NewObjectProperty("a", values.NewInt(1)),
    77  		)
    78  
    79  		afterKeepKeys, err := objects.KeepKeys(context.Background(), obj, values.NewString("a"))
    80  
    81  		So(err, ShouldEqual, nil)
    82  		So(afterKeepKeys.Compare(resultObj), ShouldEqual, 0)
    83  	})
    84  
    85  	Convey("KeepKeys key doesn't exists", t, func() {
    86  		obj := values.NewObjectWith(
    87  			values.NewObjectProperty("a", values.NewInt(1)),
    88  			values.NewObjectProperty("b", values.NewString("string")),
    89  		)
    90  		resultObj := values.NewObject()
    91  
    92  		afterKeepKeys, err := objects.KeepKeys(context.Background(), obj, values.NewString("c"))
    93  
    94  		So(err, ShouldEqual, nil)
    95  		So(isEqualObjects(afterKeepKeys.(*values.Object), resultObj), ShouldEqual, true)
    96  	})
    97  
    98  	Convey("KeepKeys when there are more keys than object properties", t, func() {
    99  		obj := values.NewObjectWith(
   100  			values.NewObjectProperty("a", values.NewInt(1)),
   101  			values.NewObjectProperty("b", values.NewString("string")),
   102  		)
   103  		resultObj := values.NewObjectWith(
   104  			values.NewObjectProperty("a", values.NewInt(1)),
   105  			values.NewObjectProperty("b", values.NewString("string")),
   106  		)
   107  
   108  		afterKeepKeys, err := objects.KeepKeys(context.Background(), obj,
   109  			values.NewString("a"), values.NewString("b"), values.NewString("c"),
   110  		)
   111  
   112  		So(err, ShouldEqual, nil)
   113  		So(isEqualObjects(afterKeepKeys.(*values.Object), resultObj), ShouldEqual, true)
   114  	})
   115  }
   116  
   117  func TestKeepKeysArray(t *testing.T) {
   118  	Convey("KeepKeys array", t, func() {
   119  		obj := values.NewObjectWith(
   120  			values.NewObjectProperty("a", values.NewInt(1)),
   121  			values.NewObjectProperty("b", values.NewString("string")),
   122  		)
   123  		keys := values.NewArrayWith(values.NewString("a"))
   124  		resultObj := values.NewObjectWith(
   125  			values.NewObjectProperty("a", values.NewInt(1)),
   126  		)
   127  
   128  		afterKeepKeys, err := objects.KeepKeys(context.Background(), obj, keys)
   129  
   130  		So(err, ShouldEqual, nil)
   131  		So(isEqualObjects(afterKeepKeys.(*values.Object), resultObj), ShouldEqual, true)
   132  	})
   133  
   134  	Convey("KeepKeys empty array", t, func() {
   135  		obj := values.NewObjectWith(
   136  			values.NewObjectProperty("a", values.NewInt(1)),
   137  			values.NewObjectProperty("b", values.NewString("string")),
   138  		)
   139  		keys := values.NewArray(0)
   140  		resultObj := values.NewObject()
   141  
   142  		afterKeepKeys, err := objects.KeepKeys(context.Background(), obj, keys)
   143  
   144  		So(err, ShouldEqual, nil)
   145  		So(isEqualObjects(afterKeepKeys.(*values.Object), resultObj), ShouldEqual, true)
   146  	})
   147  
   148  	Convey("KeepKeys when there are more keys than object properties", t, func() {
   149  		obj := values.NewObjectWith(
   150  			values.NewObjectProperty("a", values.NewInt(1)),
   151  			values.NewObjectProperty("b", values.NewString("string")),
   152  		)
   153  		keys := values.NewArrayWith(
   154  			values.NewString("a"), values.NewString("b"), values.NewString("c"),
   155  		)
   156  		resultObj := values.NewObjectWith(
   157  			values.NewObjectProperty("a", values.NewInt(1)),
   158  			values.NewObjectProperty("b", values.NewString("string")),
   159  		)
   160  
   161  		afterKeepKeys, err := objects.KeepKeys(context.Background(), obj, keys)
   162  
   163  		So(err, ShouldEqual, nil)
   164  		So(isEqualObjects(afterKeepKeys.(*values.Object), resultObj), ShouldEqual, true)
   165  	})
   166  
   167  	Convey("When there is not string key", t, func() {
   168  		obj := values.NewObjectWith(
   169  			values.NewObjectProperty("a", values.NewInt(1)),
   170  			values.NewObjectProperty("b", values.NewString("string")),
   171  		)
   172  		keys := values.NewArrayWith(
   173  			values.NewString("a"),
   174  			values.NewInt(0),
   175  		)
   176  
   177  		afterKeepKeys, err := objects.KeepKeys(context.Background(), obj, keys)
   178  
   179  		So(err, ShouldBeError)
   180  		So(afterKeepKeys, ShouldEqual, values.None)
   181  	})
   182  }
   183  
   184  func isEqualObjects(obj1 *values.Object, obj2 *values.Object) bool {
   185  	var val1 core.Value
   186  	var val2 core.Value
   187  
   188  	for _, key := range obj1.Keys() {
   189  		val1, _ = obj1.Get(key)
   190  		val2, _ = obj2.Get(key)
   191  		if val1.Compare(val2) != 0 {
   192  			return false
   193  		}
   194  	}
   195  	for _, key := range obj2.Keys() {
   196  		val1, _ = obj1.Get(key)
   197  		val2, _ = obj2.Get(key)
   198  		if val2.Compare(val1) != 0 {
   199  			return false
   200  		}
   201  	}
   202  	return true
   203  }