github.com/MontFerret/ferret@v0.18.0/pkg/stdlib/testing/len_test.go (about)

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