github.com/MontFerret/ferret@v0.18.0/pkg/stdlib/objects/values_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 TestValues(t *testing.T) { 14 Convey("Invalid arguments", t, func() { 15 Convey("When there is no arguments", func() { 16 actual, err := objects.Values(context.Background()) 17 18 So(err, ShouldBeError) 19 So(actual.Compare(values.None), ShouldEqual, 0) 20 }) 21 22 Convey("When 2 arguments", func() { 23 obj := values.NewObjectWith( 24 values.NewObjectProperty("k1", values.NewInt(0)), 25 values.NewObjectProperty("k2", values.NewInt(1)), 26 ) 27 28 actual, err := objects.Values(context.Background(), obj, obj) 29 30 So(err, ShouldBeError) 31 So(actual.Compare(values.None), ShouldEqual, 0) 32 33 actual, err = objects.Values(context.Background(), obj, values.NewInt(0)) 34 35 So(err, ShouldBeError) 36 So(actual.Compare(values.None), ShouldEqual, 0) 37 }) 38 39 Convey("When there is not object argument", func() { 40 actual, err := objects.Values(context.Background(), values.NewInt(0)) 41 42 So(err, ShouldBeError) 43 So(actual.Compare(values.None), ShouldEqual, 0) 44 }) 45 }) 46 47 Convey("When simple type attributes (same type)", t, func() { 48 obj := values.NewObjectWith( 49 values.NewObjectProperty("k1", values.NewInt(0)), 50 values.NewObjectProperty("k2", values.NewInt(1)), 51 ) 52 expected := values.NewArrayWith( 53 values.NewInt(0), values.NewInt(1), 54 ).Sort() 55 56 actual, err := objects.Values(context.Background(), obj) 57 actualSorted := actual.(*values.Array).Sort() 58 59 So(err, ShouldBeNil) 60 So(actualSorted.Compare(expected), ShouldEqual, 0) 61 }) 62 63 Convey("When simple type attributes (different types)", t, func() { 64 obj := values.NewObjectWith( 65 values.NewObjectProperty("k1", values.NewInt(0)), 66 values.NewObjectProperty("k2", values.NewString("v2")), 67 ) 68 expected := values.NewArrayWith( 69 values.NewInt(0), values.NewString("v2"), 70 ).Sort() 71 72 actual, err := objects.Values(context.Background(), obj) 73 actualSorted := actual.(*values.Array).Sort() 74 75 So(err, ShouldBeNil) 76 So(actualSorted.Compare(expected), ShouldEqual, 0) 77 }) 78 79 Convey("When complex type attributes (array)", t, func() { 80 arr1 := values.NewArrayWith( 81 values.NewInt(0), values.NewInt(1), 82 ) 83 arr2 := values.NewArrayWith( 84 values.NewInt(2), values.NewInt(3), 85 ) 86 obj := values.NewObjectWith( 87 values.NewObjectProperty("k1", arr1), 88 values.NewObjectProperty("k2", arr2), 89 ) 90 expected := values.NewArrayWith(arr1, arr2).Sort() 91 92 actual, err := objects.Values(context.Background(), obj) 93 actualSorted := actual.(*values.Array).Sort() 94 95 So(err, ShouldBeNil) 96 So(actualSorted.Compare(expected), ShouldEqual, 0) 97 }) 98 99 Convey("When complex type attributes (object)", t, func() { 100 obj1 := values.NewObjectWith( 101 values.NewObjectProperty("int0", values.NewInt(0)), 102 ) 103 obj2 := values.NewObjectWith( 104 values.NewObjectProperty("int1", values.NewInt(1)), 105 ) 106 obj := values.NewObjectWith( 107 values.NewObjectProperty("k1", obj1), 108 values.NewObjectProperty("k2", obj2), 109 ) 110 expected := values.NewArrayWith(obj1, obj2).Sort() 111 112 actual, err := objects.Values(context.Background(), obj) 113 actualSorted := actual.(*values.Array).Sort() 114 115 So(err, ShouldBeNil) 116 So(actualSorted.Compare(expected), ShouldEqual, 0) 117 }) 118 119 Convey("When complex type attributes (object and array)", t, func() { 120 obj1 := values.NewObjectWith( 121 values.NewObjectProperty("k1", values.NewInt(0)), 122 ) 123 arr1 := values.NewArrayWith( 124 values.NewInt(0), values.NewInt(1), 125 ) 126 obj := values.NewObjectWith( 127 values.NewObjectProperty("obj", obj1), 128 values.NewObjectProperty("arr", arr1), 129 ) 130 expected := values.NewArrayWith(obj1, arr1).Sort() 131 132 actual, err := objects.Values(context.Background(), obj) 133 actualSorted := actual.(*values.Array).Sort() 134 135 So(err, ShouldBeNil) 136 So(actualSorted.Compare(expected), ShouldEqual, 0) 137 }) 138 139 Convey("When both type attributes", t, func() { 140 obj1 := values.NewObjectWith( 141 values.NewObjectProperty("k1", values.NewInt(0)), 142 ) 143 arr1 := values.NewArrayWith( 144 values.NewInt(0), values.NewInt(1), 145 ) 146 int1 := values.NewInt(0) 147 obj := values.NewObjectWith( 148 values.NewObjectProperty("obj", obj1), 149 values.NewObjectProperty("arr", arr1), 150 values.NewObjectProperty("int", int1), 151 ) 152 expected := values.NewArrayWith(obj1, arr1, int1).Sort() 153 154 actual, err := objects.Values(context.Background(), obj) 155 actualSorted := actual.(*values.Array).Sort() 156 157 So(err, ShouldBeNil) 158 So(actualSorted.Compare(expected), ShouldEqual, 0) 159 }) 160 161 Convey("Result is independent on the source object (array)", t, func() { 162 arr := values.NewArrayWith(values.NewInt(0)) 163 obj := values.NewObjectWith( 164 values.NewObjectProperty("arr", arr), 165 ) 166 expected := values.NewArrayWith( 167 values.NewArrayWith( 168 values.NewInt(0), 169 ), 170 ) 171 172 actual, err := objects.Values(context.Background(), obj) 173 actualSorted := actual.(*values.Array).Sort() 174 175 So(err, ShouldBeNil) 176 177 arr.Push(values.NewInt(1)) 178 179 So(actualSorted.Compare(expected), ShouldEqual, 0) 180 }) 181 182 Convey("Result is independent on the source object (object)", t, func() { 183 nested := values.NewObjectWith( 184 values.NewObjectProperty("int", values.NewInt(0)), 185 ) 186 obj := values.NewObjectWith( 187 values.NewObjectProperty("nested", nested), 188 ) 189 expected := values.NewArrayWith( 190 values.NewObjectWith( 191 values.NewObjectProperty("int", values.NewInt(0)), 192 ), 193 ) 194 195 actual, err := objects.Values(context.Background(), obj) 196 actualSorted := actual.(*values.Array).Sort() 197 198 So(err, ShouldBeNil) 199 200 nested.Set("new", values.NewInt(1)) 201 202 So(actualSorted.Compare(expected), ShouldEqual, 0) 203 }) 204 } 205 206 func TestValuesStress(t *testing.T) { 207 Convey("Stress", t, func() { 208 for i := 0; i < 100; i++ { 209 obj1 := values.NewObjectWith( 210 values.NewObjectProperty("int0", values.NewInt(0)), 211 ) 212 obj2 := values.NewObjectWith( 213 values.NewObjectProperty("int1", values.NewInt(1)), 214 ) 215 obj := values.NewObjectWith( 216 values.NewObjectProperty("k1", obj1), 217 values.NewObjectProperty("k2", obj2), 218 ) 219 expected := values.NewArrayWith(obj2, obj1).Sort() 220 221 actual, err := objects.Values(context.Background(), obj) 222 actualSorted := actual.(*values.Array).Sort() 223 224 So(err, ShouldBeNil) 225 So(actualSorted.Length(), ShouldEqual, expected.Length()) 226 So(actualSorted.Compare(expected), ShouldEqual, 0) 227 } 228 }) 229 }