keys_test.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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("Map", func() {
  8. allKeys := map[string]string{"A": "a", "B": "b"}
  9. missingKeys := map[string]string{"A": "a"}
  10. extraKeys := map[string]string{"A": "a", "B": "b", "C": "c"}
  11. emptyKeys := map[string]string{}
  12. It("should strictly match all keys", func() {
  13. m := MatchAllKeys(Keys{
  14. "B": Equal("b"),
  15. "A": Equal("a"),
  16. })
  17. Expect(allKeys).Should(m, "should match all keys")
  18. Expect(missingKeys).ShouldNot(m, "should fail with missing keys")
  19. Expect(extraKeys).ShouldNot(m, "should fail with extra keys")
  20. Expect(emptyKeys).ShouldNot(m, "should fail with empty keys")
  21. m = MatchAllKeys(Keys{
  22. "A": Equal("a"),
  23. "B": Equal("fail"),
  24. })
  25. Expect(allKeys).ShouldNot(m, "should run nested matchers")
  26. })
  27. It("should handle empty maps", func() {
  28. m := MatchAllKeys(Keys{})
  29. Expect(map[string]string{}).Should(m, "should handle empty maps")
  30. Expect(allKeys).ShouldNot(m, "should fail with extra keys")
  31. })
  32. It("should ignore missing keys", func() {
  33. m := MatchKeys(IgnoreMissing, Keys{
  34. "B": Equal("b"),
  35. "A": Equal("a"),
  36. })
  37. Expect(allKeys).Should(m, "should match all keys")
  38. Expect(missingKeys).Should(m, "should ignore missing keys")
  39. Expect(extraKeys).ShouldNot(m, "should fail with extra keys")
  40. Expect(emptyKeys).Should(m, "should match empty keys")
  41. })
  42. It("should ignore extra keys", func() {
  43. m := MatchKeys(IgnoreExtras, Keys{
  44. "B": Equal("b"),
  45. "A": Equal("a"),
  46. })
  47. Expect(allKeys).Should(m, "should match all keys")
  48. Expect(missingKeys).ShouldNot(m, "should fail with missing keys")
  49. Expect(extraKeys).Should(m, "should ignore extra keys")
  50. Expect(emptyKeys).ShouldNot(m, "should fail with empty keys")
  51. })
  52. It("should ignore missing and extra keys", func() {
  53. m := MatchKeys(IgnoreMissing|IgnoreExtras, Keys{
  54. "B": Equal("b"),
  55. "A": Equal("a"),
  56. })
  57. Expect(allKeys).Should(m, "should match all keys")
  58. Expect(missingKeys).Should(m, "should ignore missing keys")
  59. Expect(extraKeys).Should(m, "should ignore extra keys")
  60. Expect(emptyKeys).Should(m, "should match empty keys")
  61. m = MatchKeys(IgnoreMissing|IgnoreExtras, Keys{
  62. "A": Equal("a"),
  63. "B": Equal("fail"),
  64. })
  65. Expect(allKeys).ShouldNot(m, "should run nested matchers")
  66. })
  67. It("should produce sensible error messages", func() {
  68. m := MatchAllKeys(Keys{
  69. "B": Equal("b"),
  70. "A": Equal("a"),
  71. })
  72. actual := map[string]string{"A": "b", "C": "c"}
  73. //Because the order of the constituent errors can't be guaranteed,
  74. //we do a number of checks to make sure everything's included
  75. m.Match(actual)
  76. Expect(m.FailureMessage(actual)).Should(HavePrefix(
  77. "Expected\n <string>: \nto match keys: {\n",
  78. ))
  79. Expect(m.FailureMessage(actual)).Should(ContainSubstring(
  80. ".\"A\":\n Expected\n <string>: b\n to equal\n <string>: a\n",
  81. ))
  82. Expect(m.FailureMessage(actual)).Should(ContainSubstring(
  83. "missing expected key B\n",
  84. ))
  85. Expect(m.FailureMessage(actual)).Should(ContainSubstring(
  86. ".\"C\":\n unexpected key C: map[",
  87. ))
  88. })
  89. })