...

Source file src/github.com/onsi/gomega/matchers/consist_of_test.go

Documentation: github.com/onsi/gomega/matchers

     1  package matchers_test
     2  
     3  import (
     4  	. "github.com/onsi/ginkgo/v2"
     5  	. "github.com/onsi/gomega"
     6  )
     7  
     8  var _ = Describe("ConsistOf", func() {
     9  	Context("with a slice", func() {
    10  		It("should do the right thing", func() {
    11  			Expect([]string{"foo", "bar", "baz"}).Should(ConsistOf("foo", "bar", "baz"))
    12  			Expect([]string{"foo", "bar", "baz"}).Should(ConsistOf("foo", "bar", "baz"))
    13  			Expect([]string{"foo", "bar", "baz"}).Should(ConsistOf("baz", "bar", "foo"))
    14  			Expect([]string{"foo", "bar", "baz"}).ShouldNot(ConsistOf("baz", "bar", "foo", "foo"))
    15  			Expect([]string{"foo", "bar", "baz"}).ShouldNot(ConsistOf("baz", "foo"))
    16  		})
    17  	})
    18  
    19  	Context("with an array", func() {
    20  		It("should do the right thing", func() {
    21  			Expect([3]string{"foo", "bar", "baz"}).Should(ConsistOf("foo", "bar", "baz"))
    22  			Expect([3]string{"foo", "bar", "baz"}).Should(ConsistOf("baz", "bar", "foo"))
    23  			Expect([3]string{"foo", "bar", "baz"}).ShouldNot(ConsistOf("baz", "bar", "foo", "foo"))
    24  			Expect([3]string{"foo", "bar", "baz"}).ShouldNot(ConsistOf("baz", "foo"))
    25  		})
    26  	})
    27  
    28  	Context("with a map", func() {
    29  		It("should apply to the values", func() {
    30  			Expect(map[int]string{1: "foo", 2: "bar", 3: "baz"}).Should(ConsistOf("foo", "bar", "baz"))
    31  			Expect(map[int]string{1: "foo", 2: "bar", 3: "baz"}).Should(ConsistOf("baz", "bar", "foo"))
    32  			Expect(map[int]string{1: "foo", 2: "bar", 3: "baz"}).ShouldNot(ConsistOf("baz", "bar", "foo", "foo"))
    33  			Expect(map[int]string{1: "foo", 2: "bar", 3: "baz"}).ShouldNot(ConsistOf("baz", "foo"))
    34  		})
    35  
    36  	})
    37  
    38  	Context("with anything else", func() {
    39  		It("should error", func() {
    40  			failures := InterceptGomegaFailures(func() {
    41  				Expect("foo").Should(ConsistOf("f", "o", "o"))
    42  			})
    43  
    44  			Expect(failures).Should(HaveLen(1))
    45  		})
    46  	})
    47  
    48  	When("passed matchers", func() {
    49  		It("should pass if the matchers pass", func() {
    50  			Expect([]string{"foo", "bar", "baz"}).Should(ConsistOf("foo", MatchRegexp("^ba"), "baz"))
    51  			Expect([]string{"foo", "bar", "baz"}).ShouldNot(ConsistOf("foo", MatchRegexp("^ba")))
    52  			Expect([]string{"foo", "bar", "baz"}).ShouldNot(ConsistOf("foo", MatchRegexp("^ba"), MatchRegexp("foo")))
    53  			Expect([]string{"foo", "bar", "baz"}).Should(ConsistOf("foo", MatchRegexp("^ba"), MatchRegexp("^ba")))
    54  			Expect([]string{"foo", "bar", "baz"}).ShouldNot(ConsistOf("foo", MatchRegexp("^ba"), MatchRegexp("turducken")))
    55  		})
    56  
    57  		It("should not depend on the order of the matchers", func() {
    58  			Expect([][]int{{1, 2}, {2}}).Should(ConsistOf(ContainElement(1), ContainElement(2)))
    59  			Expect([][]int{{1, 2}, {2}}).Should(ConsistOf(ContainElement(2), ContainElement(1)))
    60  		})
    61  
    62  		When("a matcher errors", func() {
    63  			It("should soldier on", func() {
    64  				Expect([]string{"foo", "bar", "baz"}).ShouldNot(ConsistOf(BeFalse(), "foo", "bar"))
    65  				Expect([]interface{}{"foo", "bar", false}).Should(ConsistOf(BeFalse(), ContainSubstring("foo"), "bar"))
    66  			})
    67  		})
    68  	})
    69  
    70  	When("passed exactly one argument, and that argument is a slice", func() {
    71  		It("should match against the elements of that argument", func() {
    72  			Expect([]string{"foo", "bar", "baz"}).Should(ConsistOf([]string{"foo", "bar", "baz"}))
    73  			Expect([]string{"foo", "bar", "baz"}).ShouldNot(ConsistOf([]string{"foo", "bar"}))
    74  		})
    75  	})
    76  
    77  	When("provided an expectation that has a nil value", func() {
    78  		It("should match without failure", func() {
    79  			Expect([]any{1, "two", nil}).Should(ConsistOf([]any{nil, 1, "two"}))
    80  			Expect([]any{1, "two", "nil", nil}).ShouldNot(ConsistOf([]any{nil, nil, 1, "two"}))
    81  		})
    82  	})
    83  
    84  	Describe("FailureMessage", func() {
    85  		When("actual contains an extra element", func() {
    86  			It("prints the extra element", func() {
    87  				failures := InterceptGomegaFailures(func() {
    88  					Expect([]int{1, 2}).Should(ConsistOf(2))
    89  				})
    90  
    91  				expected := "Expected\n.*\\[1, 2\\]\nto consist of\n.*\\[2\\]\nthe extra elements were\n.*\\[1\\]"
    92  				Expect(failures).To(ConsistOf(MatchRegexp(expected)))
    93  			})
    94  		})
    95  
    96  		When("actual misses an element", func() {
    97  			It("prints the missing element", func() {
    98  				failures := InterceptGomegaFailures(func() {
    99  					Expect([]int{2}).Should(ConsistOf(1, 2))
   100  				})
   101  
   102  				expected := "Expected\n.*\\[2\\]\nto consist of\n.*\\[1, 2\\]\nthe missing elements were\n.*\\[1\\]"
   103  				Expect(failures).To(ConsistOf(MatchRegexp(expected)))
   104  			})
   105  		})
   106  
   107  		When("actual misses a nil element", func() {
   108  			It("prints the missing element", func() {
   109  				failures := InterceptGomegaFailures(func() {
   110  					Expect([]int{2}).Should(ConsistOf(nil, 2))
   111  				})
   112  
   113  				expected := "Expected\n.*\\[2\\]\nto consist of\n.*\\[nil, <int>2\\]\nthe missing elements were\n.*\\[nil\\]"
   114  				Expect(failures).To(ConsistOf(MatchRegexp(expected)))
   115  			})
   116  		})
   117  
   118  		When("actual contains an extra element and misses an element", func() {
   119  			It("prints both the extra and missing element", func() {
   120  				failures := InterceptGomegaFailures(func() {
   121  					Expect([]int{1, 2}).Should(ConsistOf(2, 3))
   122  				})
   123  
   124  				expected := "Expected\n.*\\[1, 2\\]\nto consist of\n.*\\[2, 3\\]\nthe missing elements were\n.*\\[3\\]\nthe extra elements were\n.*\\[1\\]"
   125  				Expect(failures).To(ConsistOf(MatchRegexp(expected)))
   126  			})
   127  		})
   128  
   129  		When("expected was specified as an array", func() {
   130  			It("flattens the array in the expectation message", func() {
   131  				failures := InterceptGomegaFailures(func() {
   132  					Expect([]string{"A", "B", "C"}).To(ConsistOf([]string{"A", "B"}))
   133  				})
   134  
   135  				expected := `Expected\n.*\["A", "B", "C"\]\nto consist of\n.*: \["A", "B"\]\nthe extra elements were\n.*\["C"\]`
   136  				Expect(failures).To(ConsistOf(MatchRegexp(expected)))
   137  			})
   138  
   139  			It("flattens the array in the negated expectation message", func() {
   140  				failures := InterceptGomegaFailures(func() {
   141  					Expect([]string{"A", "B"}).NotTo(ConsistOf([]string{"A", "B"}))
   142  				})
   143  
   144  				expected := `Expected\n.*\["A", "B"\]\nnot to consist of\n.*: \["A", "B"\]`
   145  				Expect(failures).To(ConsistOf(MatchRegexp(expected)))
   146  			})
   147  		})
   148  
   149  		When("the expected values are the same type", func() {
   150  			It("uses that type for the expectation slice", func() {
   151  				failures := InterceptGomegaFailures(func() {
   152  					Expect([]string{"A", "B"}).To(ConsistOf("A", "C"))
   153  				})
   154  
   155  				expected := `to consist of
   156  \s*<\[\]string \| len:2, cap:2>: \["A", "C"\]
   157  the missing elements were
   158  \s*<\[\]string \| len:1, cap:1>: \["C"\]
   159  the extra elements were
   160  \s*<\[\]string \| len:1, cap:1>: \["B"\]`
   161  				Expect(failures).To(ConsistOf(MatchRegexp(expected)))
   162  			})
   163  
   164  			It("uses that type for the negated expectation slice", func() {
   165  				failures := InterceptGomegaFailures(func() {
   166  					Expect([]uint64{1, 2}).NotTo(ConsistOf(uint64(1), uint64(2)))
   167  				})
   168  
   169  				expected := `not to consist of\n\s*<\[\]uint64 \| len:2, cap:2>: \[1, 2\]`
   170  				Expect(failures).To(ConsistOf(MatchRegexp(expected)))
   171  			})
   172  		})
   173  
   174  		When("the expected values are different types", func() {
   175  			It("uses interface{} for the expectation slice", func() {
   176  				failures := InterceptGomegaFailures(func() {
   177  					Expect([]interface{}{1, true}).To(ConsistOf(1, "C"))
   178  				})
   179  
   180  				expected := `to consist of
   181  \s*<\[\]interface {} \| len:2, cap:2>: \[<int>1, <string>"C"\]
   182  the missing elements were
   183  \s*<\[\]string \| len:1, cap:1>: \["C"\]
   184  the extra elements were
   185  \s*<\[\]bool \| len:1, cap:1>: \[true\]`
   186  				Expect(failures).To(ConsistOf(MatchRegexp(expected)))
   187  			})
   188  
   189  			It("uses interface{} for the negated expectation slice", func() {
   190  				failures := InterceptGomegaFailures(func() {
   191  					Expect([]interface{}{1, "B"}).NotTo(ConsistOf(1, "B"))
   192  				})
   193  
   194  				expected := `not to consist of\n\s*<\[\]interface {} \| len:2, cap:2>: \[<int>1, <string>"B"\]`
   195  				Expect(failures).To(ConsistOf(MatchRegexp(expected)))
   196  			})
   197  		})
   198  	})
   199  })
   200  

View as plain text