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