elements_test.go 5.4 KB

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