...

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

Documentation: github.com/onsi/gomega/matchers

     1  package matchers_test
     2  
     3  import (
     4  	"time"
     5  
     6  	. "github.com/onsi/ginkgo/v2"
     7  	. "github.com/onsi/gomega"
     8  )
     9  
    10  var _ = Describe("HaveExistingField", func() {
    11  
    12  	var book Book
    13  	BeforeEach(func() {
    14  		book = Book{
    15  			Title: "Les Miserables",
    16  			Author: person{
    17  				FirstName: "Victor",
    18  				LastName:  "Hugo",
    19  				DOB:       time.Date(1802, 2, 26, 0, 0, 0, 0, time.UTC),
    20  			},
    21  			Pages: 2783,
    22  			Sequel: &Book{
    23  				Title: "Les Miserables 2",
    24  			},
    25  		}
    26  	})
    27  
    28  	DescribeTable("traversing the struct works",
    29  		func(field string) {
    30  			Ω(book).Should(HaveExistingField(field))
    31  		},
    32  		Entry("Top-level field", "Title"),
    33  		Entry("Nested field", "Author.FirstName"),
    34  		Entry("Top-level method", "AuthorName()"),
    35  		Entry("Nested method", "Author.DOB.Year()"),
    36  		Entry("Traversing past a method", "AbbreviatedAuthor().FirstName"),
    37  		Entry("Traversing a pointer", "Sequel.Title"),
    38  	)
    39  
    40  	DescribeTable("negation works",
    41  		func(field string) {
    42  			Ω(book).ShouldNot(HaveExistingField(field))
    43  		},
    44  		Entry("Top-level field", "Class"),
    45  		Entry("Nested field", "Author.Class"),
    46  		Entry("Top-level method", "ClassName()"),
    47  		Entry("Nested method", "Author.DOB.BOT()"),
    48  		Entry("Traversing past a method", "AbbreviatedAuthor().LastButOneName"),
    49  		Entry("Traversing a pointer", "Sequel.Titles"),
    50  	)
    51  
    52  	It("errors appropriately", func() {
    53  		success, err := HaveExistingField("Pages.Count").Match(book)
    54  		Ω(success).Should(BeFalse())
    55  		Ω(err.Error()).Should(Equal("HaveExistingField encountered:\n    <int>: 2783\nWhich is not a struct."))
    56  
    57  		success, err = HaveExistingField("Prequel.Title").Match(book)
    58  		Ω(success).Should(BeFalse())
    59  		Ω(err.Error()).Should(ContainSubstring("HaveExistingField encountered nil while dereferencing a pointer of type *matchers_test.Book."))
    60  
    61  		success, err = HaveExistingField("HasArg()").Match(book)
    62  		Ω(success).Should(BeFalse())
    63  		Ω(err.Error()).Should(ContainSubstring("HaveExistingField found an invalid method named 'HasArg()' in struct of type matchers_test.Book.\nMethods must take no arguments and return exactly one value."))
    64  	})
    65  
    66  	It("renders failure messages", func() {
    67  		matcher := HaveExistingField("Turtle")
    68  		success, err := matcher.Match(book)
    69  		Ω(success).Should(BeFalse())
    70  		Ω(err).ShouldNot(HaveOccurred())
    71  
    72  		msg := matcher.FailureMessage(book)
    73  		Ω(msg).Should(MatchRegexp(`(?s)Expected\n\s+<matchers_test\.Book>: .*\nto have field 'Turtle'`))
    74  
    75  		matcher = HaveExistingField("Title")
    76  		success, err = matcher.Match(book)
    77  		Ω(success).Should(BeTrue())
    78  		Ω(err).ShouldNot(HaveOccurred())
    79  
    80  		msg = matcher.NegatedFailureMessage(book)
    81  		Ω(msg).Should(MatchRegexp(`(?s)Expected\n\s+<matchers_test\.Book>: .*\nnot to have field 'Title'`))
    82  	})
    83  
    84  })
    85  

View as plain text