github.com/MontFerret/ferret@v0.18.0/pkg/stdlib/testing/include_test.go (about) 1 package testing_test 2 3 import ( 4 "context" 5 t "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/testing" 12 "github.com/MontFerret/ferret/pkg/stdlib/testing/base" 13 ) 14 15 func TestInclude(t *t.T) { 16 Include := base.NewPositiveAssertion(testing.Include) 17 18 Convey("When arg is not passed", t, func() { 19 Convey("It should return an error", func() { 20 _, err := Include(context.Background()) 21 22 So(err, ShouldBeError) 23 24 _, err = Include(context.Background(), values.NewInt(1)) 25 26 So(err, ShouldBeError) 27 }) 28 }) 29 30 Convey("When value is a string", t, func() { 31 Convey("When 'Foo' and 'Bar'", func() { 32 Convey("It should return an error", func() { 33 _, err := Include(context.Background(), values.NewString("Foo"), values.NewString("Bar")) 34 35 So(err, ShouldBeError) 36 So(err.Error(), ShouldEqual, core.Error(base.ErrAssertion, "expected [string] 'Foo' to include [string] 'Bar'").Error()) 37 }) 38 }) 39 40 Convey("When 'FooBar' and 'Bar'", func() { 41 Convey("It should not return an error", func() { 42 _, err := Include(context.Background(), values.NewString("FooBar"), values.NewString("Bar")) 43 44 So(err, ShouldBeNil) 45 }) 46 }) 47 }) 48 49 Convey("When value is an array", t, func() { 50 Convey("When [1,2,3] and 4", func() { 51 Convey("It should return an error", func() { 52 _, err := Include( 53 context.Background(), 54 values.NewArrayWith(values.NewInt(1), values.NewInt(2), values.NewInt(3)), 55 values.NewInt(4), 56 ) 57 58 So(err, ShouldBeError) 59 So(err.Error(), ShouldEqual, core.Error(base.ErrAssertion, "expected [array] '[1,2,3]' to include [int] '4'").Error()) 60 }) 61 }) 62 63 Convey("When [1,2,3] and 2", func() { 64 Convey("It should not return an error", func() { 65 _, err := Include( 66 context.Background(), 67 values.NewArrayWith(values.NewInt(1), values.NewInt(2), values.NewInt(3)), 68 values.NewInt(2), 69 ) 70 71 So(err, ShouldBeNil) 72 }) 73 }) 74 }) 75 76 Convey("When value is an object", t, func() { 77 Convey("When {a:1,b:2,c:3} and 4", func() { 78 Convey("It should return an error", func() { 79 _, err := Include( 80 context.Background(), 81 values.NewObjectWith( 82 values.NewObjectProperty("a", values.NewInt(1)), 83 values.NewObjectProperty("b", values.NewInt(2)), 84 values.NewObjectProperty("c", values.NewInt(3)), 85 ), 86 values.NewInt(4), 87 ) 88 89 So(err, ShouldBeError) 90 So(err.Error(), ShouldEqual, core.Error(base.ErrAssertion, "expected [object] '{\"a\":1,\"b\":2,\"c\":3}' to include [int] '4'").Error()) 91 }) 92 }) 93 94 Convey("When {a:1,b:2,c:3} and 2", func() { 95 Convey("It should not return an error", func() { 96 _, err := Include( 97 context.Background(), 98 values.NewObjectWith( 99 values.NewObjectProperty("a", values.NewInt(1)), 100 values.NewObjectProperty("b", values.NewInt(2)), 101 values.NewObjectProperty("c", values.NewInt(3)), 102 ), 103 values.NewInt(2), 104 ) 105 106 So(err, ShouldBeNil) 107 }) 108 }) 109 }) 110 } 111 112 func TestNotInclude(t *t.T) { 113 NotInclude := base.NewNegativeAssertion(testing.Include) 114 115 Convey("When arg is not passed", t, func() { 116 Convey("It should return an error", func() { 117 _, err := NotInclude(context.Background()) 118 119 So(err, ShouldBeError) 120 121 _, err = NotInclude(context.Background(), values.NewInt(1)) 122 123 So(err, ShouldBeError) 124 }) 125 }) 126 127 Convey("When value is a string", t, func() { 128 Convey("When 'Foo' and 'Bar'", func() { 129 Convey("It should not return an error", func() { 130 _, err := NotInclude(context.Background(), values.NewString("Foo"), values.NewString("Bar")) 131 132 So(err, ShouldBeNil) 133 }) 134 }) 135 136 Convey("When 'FooBar' and 'Bar'", func() { 137 Convey("It should return an error", func() { 138 _, err := NotInclude(context.Background(), values.NewString("FooBar"), values.NewString("Bar")) 139 140 So(err, ShouldBeError) 141 So(err.Error(), ShouldEqual, core.Error(base.ErrAssertion, "expected [string] 'FooBar' not to include [string] 'Bar'").Error()) 142 }) 143 }) 144 }) 145 146 Convey("When value is an array", t, func() { 147 Convey("When [1,2,3] and 4", func() { 148 Convey("It should not return an error", func() { 149 _, err := NotInclude( 150 context.Background(), 151 values.NewArrayWith(values.NewInt(1), values.NewInt(2), values.NewInt(3)), 152 values.NewInt(4), 153 ) 154 155 So(err, ShouldBeNil) 156 }) 157 }) 158 159 Convey("When [1,2,3] and 2", func() { 160 Convey("It should return an error", func() { 161 _, err := NotInclude( 162 context.Background(), 163 values.NewArrayWith(values.NewInt(1), values.NewInt(2), values.NewInt(3)), 164 values.NewInt(2), 165 ) 166 167 So(err, ShouldBeError) 168 So(err.Error(), ShouldEqual, core.Error(base.ErrAssertion, "expected [array] '[1,2,3]' not to include [int] '2'").Error()) 169 }) 170 }) 171 }) 172 173 Convey("When value is an object", t, func() { 174 Convey("When {a:1,b:2,c:3} and 4", func() { 175 Convey("It should not return an error", func() { 176 _, err := NotInclude( 177 context.Background(), 178 values.NewObjectWith( 179 values.NewObjectProperty("a", values.NewInt(1)), 180 values.NewObjectProperty("b", values.NewInt(2)), 181 values.NewObjectProperty("c", values.NewInt(3)), 182 ), 183 values.NewInt(4), 184 ) 185 186 So(err, ShouldBeNil) 187 }) 188 }) 189 190 Convey("When {a:1,b:2,c:3} and 2", func() { 191 Convey("It should return an error", func() { 192 _, err := NotInclude( 193 context.Background(), 194 values.NewObjectWith( 195 values.NewObjectProperty("a", values.NewInt(1)), 196 values.NewObjectProperty("b", values.NewInt(2)), 197 values.NewObjectProperty("c", values.NewInt(3)), 198 ), 199 values.NewInt(2), 200 ) 201 202 So(err, ShouldBeError) 203 So(err.Error(), ShouldEqual, core.Error(base.ErrAssertion, "expected [object] '{\"a\":1,\"b\":2,\"c\":3}' not to include [int] '2'").Error()) 204 }) 205 }) 206 }) 207 }