|
| 1 | +package matchers_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "time" |
| 6 | + |
| 7 | + . "github.com/onsi/ginkgo" |
| 8 | + . "github.com/onsi/ginkgo/extensions/table" |
| 9 | + . "github.com/onsi/gomega" |
| 10 | +) |
| 11 | + |
| 12 | +type Book struct { |
| 13 | + Title string |
| 14 | + Author person |
| 15 | + Pages int |
| 16 | +} |
| 17 | + |
| 18 | +func (book Book) AuthorName() string { |
| 19 | + return fmt.Sprintf("%s %s", book.Author.FirstName, book.Author.LastName) |
| 20 | +} |
| 21 | + |
| 22 | +func (book Book) AbbreviatedAuthor() person { |
| 23 | + return person{ |
| 24 | + FirstName: book.Author.FirstName[0:3], |
| 25 | + LastName: book.Author.LastName[0:3], |
| 26 | + DOB: book.Author.DOB, |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +func (book Book) NoReturn() { |
| 31 | +} |
| 32 | + |
| 33 | +func (book Book) TooManyReturn() (string, error) { |
| 34 | + return "", nil |
| 35 | +} |
| 36 | + |
| 37 | +func (book Book) HasArg(arg string) string { |
| 38 | + return arg |
| 39 | +} |
| 40 | + |
| 41 | +type person struct { |
| 42 | + FirstName string |
| 43 | + LastName string |
| 44 | + DOB time.Time |
| 45 | +} |
| 46 | + |
| 47 | +var _ = Describe("HaveField", func() { |
| 48 | + var book Book |
| 49 | + BeforeEach(func() { |
| 50 | + book = Book{ |
| 51 | + Title: "Les Miserables", |
| 52 | + Author: person{ |
| 53 | + FirstName: "Victor", |
| 54 | + LastName: "Hugo", |
| 55 | + DOB: time.Date(1802, 2, 26, 0, 0, 0, 0, time.UTC), |
| 56 | + }, |
| 57 | + Pages: 2783, |
| 58 | + } |
| 59 | + }) |
| 60 | + |
| 61 | + DescribeTable("traversing the struct works", |
| 62 | + func(field string, expected interface{}) { |
| 63 | + Ω(book).Should(HaveField(field, expected)) |
| 64 | + }, |
| 65 | + Entry("Top-level field with default submatcher", "Title", "Les Miserables"), |
| 66 | + Entry("Top-level field with custom submatcher", "Title", ContainSubstring("Les Mis")), |
| 67 | + Entry("Nested field", "Author.FirstName", "Victor"), |
| 68 | + Entry("Top-level method", "AuthorName()", "Victor Hugo"), |
| 69 | + Entry("Nested method", "Author.DOB.Year()", BeNumerically("<", 1900)), |
| 70 | + Entry("Traversing past a method", "AbbreviatedAuthor().FirstName", Equal("Vic")), |
| 71 | + ) |
| 72 | + |
| 73 | + DescribeTable("negation works", |
| 74 | + func(field string, expected interface{}) { |
| 75 | + Ω(book).ShouldNot(HaveField(field, expected)) |
| 76 | + }, |
| 77 | + Entry("Top-level field with default submatcher", "Title", "Les Mis"), |
| 78 | + Entry("Top-level field with custom submatcher", "Title", ContainSubstring("Notre Dame")), |
| 79 | + Entry("Nested field", "Author.FirstName", "Hugo"), |
| 80 | + Entry("Top-level method", "AuthorName()", "Victor M. Hugo"), |
| 81 | + Entry("Nested method", "Author.DOB.Year()", BeNumerically(">", 1900)), |
| 82 | + ) |
| 83 | + |
| 84 | + Describe("when field lookup fails", func() { |
| 85 | + It("errors appropriately", func() { |
| 86 | + success, err := HaveField("BookName", "Les Miserables").Match(book) |
| 87 | + Ω(success).Should(BeFalse()) |
| 88 | + Ω(err.Error()).Should(ContainSubstring("HaveField could not find field named '%s' in struct:", "BookName")) |
| 89 | + |
| 90 | + success, err = HaveField("BookName", "Les Miserables").Match(book) |
| 91 | + Ω(success).Should(BeFalse()) |
| 92 | + Ω(err.Error()).Should(ContainSubstring("HaveField could not find field named '%s' in struct:", "BookName")) |
| 93 | + |
| 94 | + success, err = HaveField("AuthorName", "Victor Hugo").Match(book) |
| 95 | + Ω(success).Should(BeFalse()) |
| 96 | + Ω(err.Error()).Should(ContainSubstring("HaveField could not find field named '%s' in struct:", "AuthorName")) |
| 97 | + |
| 98 | + success, err = HaveField("Title()", "Les Miserables").Match(book) |
| 99 | + Ω(success).Should(BeFalse()) |
| 100 | + Ω(err.Error()).Should(ContainSubstring("HaveField could not find method named '%s' in struct of type matchers_test.Book.", "Title()")) |
| 101 | + |
| 102 | + success, err = HaveField("NoReturn()", "Les Miserables").Match(book) |
| 103 | + Ω(success).Should(BeFalse()) |
| 104 | + Ω(err.Error()).Should(ContainSubstring("HaveField found an invalid method named 'NoReturn()' in struct of type matchers_test.Book.\nMethods must take no arguments and return exactly one value.")) |
| 105 | + |
| 106 | + success, err = HaveField("TooManyReturn()", "Les Miserables").Match(book) |
| 107 | + Ω(success).Should(BeFalse()) |
| 108 | + Ω(err.Error()).Should(ContainSubstring("HaveField found an invalid method named 'TooManyReturn()' in struct of type matchers_test.Book.\nMethods must take no arguments and return exactly one value.")) |
| 109 | + |
| 110 | + success, err = HaveField("HasArg()", "Les Miserables").Match(book) |
| 111 | + Ω(success).Should(BeFalse()) |
| 112 | + Ω(err.Error()).Should(ContainSubstring("HaveField found an invalid method named 'HasArg()' in struct of type matchers_test.Book.\nMethods must take no arguments and return exactly one value.")) |
| 113 | + |
| 114 | + success, err = HaveField("Pages.Count", 2783).Match(book) |
| 115 | + Ω(success).Should(BeFalse()) |
| 116 | + Ω(err.Error()).Should(Equal("HaveField encountered:\n <int>: 2783\nWhich is not a struct.")) |
| 117 | + |
| 118 | + success, err = HaveField("Author.Abbreviation", "Vic").Match(book) |
| 119 | + Ω(success).Should(BeFalse()) |
| 120 | + Ω(err.Error()).Should(ContainSubstring("HaveField could not find field named '%s' in struct:", "Abbreviation")) |
| 121 | + }) |
| 122 | + }) |
| 123 | + |
| 124 | + Describe("Failure Messages", func() { |
| 125 | + It("renders the underlying matcher failure", func() { |
| 126 | + matcher := HaveField("Title", "Les Mis") |
| 127 | + success, err := matcher.Match(book) |
| 128 | + Ω(success).Should(BeFalse()) |
| 129 | + Ω(err).ShouldNot(HaveOccurred()) |
| 130 | + |
| 131 | + msg := matcher.FailureMessage(book) |
| 132 | + Ω(msg).Should(Equal("Value for field 'Title' failed to satisfy matcher.\nExpected\n <string>: Les Miserables\nto equal\n <string>: Les Mis")) |
| 133 | + |
| 134 | + matcher = HaveField("Title", "Les Miserables") |
| 135 | + success, err = matcher.Match(book) |
| 136 | + Ω(success).Should(BeTrue()) |
| 137 | + Ω(err).ShouldNot(HaveOccurred()) |
| 138 | + |
| 139 | + msg = matcher.NegatedFailureMessage(book) |
| 140 | + Ω(msg).Should(Equal("Value for field 'Title' satisfied matcher, but should not have.\nExpected\n <string>: Les Miserables\nnot to equal\n <string>: Les Miserables")) |
| 141 | + }) |
| 142 | + }) |
| 143 | +}) |
0 commit comments