github.com/onsi/gomega@v1.32.0/gstruct/elements_test.go (about)

     1  package gstruct_test
     2  
     3  import (
     4  	. "github.com/onsi/ginkgo/v2"
     5  	. "github.com/onsi/gomega"
     6  	. "github.com/onsi/gomega/gstruct"
     7  )
     8  
     9  var _ = Describe("Slice", func() {
    10  	allElements := []string{"a", "b"}
    11  	missingElements := []string{"a"}
    12  	extraElements := []string{"a", "b", "c"}
    13  	duplicateElements := []string{"a", "a", "b"}
    14  	empty := []string{}
    15  	var nils []string
    16  
    17  	It("should strictly match all elements", func() {
    18  		m := MatchAllElements(id, Elements{
    19  			"b": Equal("b"),
    20  			"a": Equal("a"),
    21  		})
    22  		Expect(allElements).Should(m, "should match all elements")
    23  		Expect(missingElements).ShouldNot(m, "should fail with missing elements")
    24  		Expect(extraElements).ShouldNot(m, "should fail with extra elements")
    25  		Expect(duplicateElements).ShouldNot(m, "should fail with duplicate elements")
    26  		Expect(nils).ShouldNot(m, "should fail with an uninitialized slice")
    27  
    28  		m = MatchAllElements(id, Elements{
    29  			"a": Equal("a"),
    30  			"b": Equal("fail"),
    31  		})
    32  		Expect(allElements).ShouldNot(m, "should run nested matchers")
    33  
    34  		m = MatchAllElements(id, Elements{})
    35  		Expect(empty).Should(m, "should handle empty slices")
    36  		Expect(allElements).ShouldNot(m, "should handle only empty slices")
    37  		Expect(nils).Should(m, "should handle nil slices")
    38  	})
    39  
    40  	It("should ignore extra elements", func() {
    41  		m := MatchElements(id, IgnoreExtras, Elements{
    42  			"b": Equal("b"),
    43  			"a": Equal("a"),
    44  		})
    45  		Expect(allElements).Should(m, "should match all elements")
    46  		Expect(missingElements).ShouldNot(m, "should fail with missing elements")
    47  		Expect(extraElements).Should(m, "should ignore extra elements")
    48  		Expect(duplicateElements).ShouldNot(m, "should fail with duplicate elements")
    49  		Expect(nils).ShouldNot(m, "should fail with an uninitialized slice")
    50  	})
    51  
    52  	It("should ignore missing elements", func() {
    53  		m := MatchElements(id, IgnoreMissing, Elements{
    54  			"a": Equal("a"),
    55  			"b": Equal("b"),
    56  		})
    57  		Expect(allElements).Should(m, "should match all elements")
    58  		Expect(missingElements).Should(m, "should ignore missing elements")
    59  		Expect(extraElements).ShouldNot(m, "should fail with extra elements")
    60  		Expect(duplicateElements).ShouldNot(m, "should fail with duplicate elements")
    61  		Expect(nils).Should(m, "should ignore an uninitialized slice")
    62  	})
    63  
    64  	It("should ignore missing and extra elements", func() {
    65  		m := MatchElements(id, IgnoreMissing|IgnoreExtras, Elements{
    66  			"a": Equal("a"),
    67  			"b": Equal("b"),
    68  		})
    69  		Expect(allElements).Should(m, "should match all elements")
    70  		Expect(missingElements).Should(m, "should ignore missing elements")
    71  		Expect(extraElements).Should(m, "should ignore extra elements")
    72  		Expect(duplicateElements).ShouldNot(m, "should fail with duplicate elements")
    73  		Expect(nils).Should(m, "should ignore an uninitialized slice")
    74  
    75  		m = MatchElements(id, IgnoreExtras|IgnoreMissing, Elements{
    76  			"a": Equal("a"),
    77  			"b": Equal("fail"),
    78  		})
    79  		Expect(allElements).ShouldNot(m, "should run nested matchers")
    80  	})
    81  
    82  	Context("with elements that share a key", func() {
    83  		nonUniqueID := func(element interface{}) string {
    84  			return element.(string)[0:1]
    85  		}
    86  
    87  		allElements := []string{"a123", "a213", "b321"}
    88  		includingBadElements := []string{"a123", "b123", "b5555"}
    89  		extraElements := []string{"a123", "b1234", "c345"}
    90  		missingElements := []string{"b123", "b1234", "b1345"}
    91  
    92  		It("should strictly allow multiple matches", func() {
    93  			m := MatchElements(nonUniqueID, AllowDuplicates, Elements{
    94  				"a": ContainSubstring("1"),
    95  				"b": ContainSubstring("1"),
    96  			})
    97  			Expect(allElements).Should(m, "should match all elements")
    98  			Expect(includingBadElements).ShouldNot(m, "should reject if a member fails the matcher")
    99  			Expect(extraElements).ShouldNot(m, "should reject with extra keys")
   100  			Expect(missingElements).ShouldNot(m, "should reject with missing keys")
   101  			Expect(nils).ShouldNot(m, "should fail with an uninitialized slice")
   102  		})
   103  
   104  		It("should ignore missing", func() {
   105  			m := MatchElements(nonUniqueID, AllowDuplicates|IgnoreMissing, Elements{
   106  				"a": ContainSubstring("1"),
   107  				"b": ContainSubstring("1"),
   108  			})
   109  			Expect(allElements).Should(m, "should match all elements")
   110  			Expect(includingBadElements).ShouldNot(m, "should reject if a member fails the matcher")
   111  			Expect(extraElements).ShouldNot(m, "should reject with extra keys")
   112  			Expect(missingElements).Should(m, "should allow missing keys")
   113  			Expect(nils).Should(m, "should allow an uninitialized slice")
   114  		})
   115  
   116  		It("should ignore extras", func() {
   117  			m := MatchElements(nonUniqueID, AllowDuplicates|IgnoreExtras, Elements{
   118  				"a": ContainSubstring("1"),
   119  				"b": ContainSubstring("1"),
   120  			})
   121  			Expect(allElements).Should(m, "should match all elements")
   122  			Expect(includingBadElements).ShouldNot(m, "should reject if a member fails the matcher")
   123  			Expect(extraElements).Should(m, "should allow extra keys")
   124  			Expect(missingElements).ShouldNot(m, "should reject missing keys")
   125  			Expect(nils).ShouldNot(m, "should reject an uninitialized slice")
   126  		})
   127  
   128  		It("should ignore missing and extras", func() {
   129  			m := MatchElements(nonUniqueID, AllowDuplicates|IgnoreExtras|IgnoreMissing, Elements{
   130  				"a": ContainSubstring("1"),
   131  				"b": ContainSubstring("1"),
   132  			})
   133  			Expect(allElements).Should(m, "should match all elements")
   134  			Expect(includingBadElements).ShouldNot(m, "should reject if a member fails the matcher")
   135  			Expect(extraElements).Should(m, "should allow extra keys")
   136  			Expect(missingElements).Should(m, "should allow missing keys")
   137  			Expect(nils).Should(m, "should allow an uninitialized slice")
   138  		})
   139  	})
   140  
   141  	Context("with index identifier", func() {
   142  		allElements := []string{"a", "b"}
   143  		missingElements := []string{"a"}
   144  		extraElements := []string{"a", "b", "c"}
   145  		duplicateElements := []string{"a", "a", "b"}
   146  		empty := []string{}
   147  		var nils []string
   148  
   149  		It("should use index", func() {
   150  			m := MatchAllElementsWithIndex(IndexIdentity, Elements{
   151  				"0": Equal("a"),
   152  				"1": Equal("b"),
   153  			})
   154  			Expect(allElements).Should(m, "should match all elements")
   155  			Expect(missingElements).ShouldNot(m, "should fail with missing elements")
   156  			Expect(extraElements).ShouldNot(m, "should fail with extra elements")
   157  			Expect(duplicateElements).ShouldNot(m, "should fail with duplicate elements")
   158  			Expect(nils).ShouldNot(m, "should fail with an uninitialized slice")
   159  
   160  			m = MatchAllElementsWithIndex(IndexIdentity, Elements{
   161  				"0": Equal("a"),
   162  				"1": Equal("fail"),
   163  			})
   164  			Expect(allElements).ShouldNot(m, "should run nested matchers")
   165  
   166  			m = MatchAllElementsWithIndex(IndexIdentity, Elements{})
   167  			Expect(empty).Should(m, "should handle empty slices")
   168  			Expect(allElements).ShouldNot(m, "should handle only empty slices")
   169  			Expect(nils).Should(m, "should handle nil slices")
   170  		})
   171  	})
   172  })
   173  
   174  func id(element interface{}) string {
   175  	return element.(string)
   176  }