github.com/onsi/gomega@v1.32.0/matchers/match_xml_matcher_test.go (about) 1 package matchers_test 2 3 import ( 4 . "github.com/onsi/ginkgo/v2" 5 . "github.com/onsi/gomega" 6 . "github.com/onsi/gomega/matchers" 7 ) 8 9 var _ = Describe("MatchXMLMatcher", func() { 10 11 var ( 12 sample_01 = readFileContents("test_data/xml/sample_01.xml") 13 sample_02 = readFileContents("test_data/xml/sample_02.xml") 14 sample_03 = readFileContents("test_data/xml/sample_03.xml") 15 sample_04 = readFileContents("test_data/xml/sample_04.xml") 16 sample_05 = readFileContents("test_data/xml/sample_05.xml") 17 sample_06 = readFileContents("test_data/xml/sample_06.xml") 18 sample_07 = readFileContents("test_data/xml/sample_07.xml") 19 sample_08 = readFileContents("test_data/xml/sample_08.xml") 20 sample_09 = readFileContents("test_data/xml/sample_09.xml") 21 sample_10 = readFileContents("test_data/xml/sample_10.xml") 22 sample_11 = readFileContents("test_data/xml/sample_11.xml") 23 ) 24 25 When("passed stringifiables", func() { 26 It("matches documents regardless of the attribute order", func() { 27 a := `<a foo="bar" ka="boom"></a>` 28 b := `<a ka="boom" foo="bar"></a>` 29 Expect(b).Should(MatchXML(a)) 30 Expect(a).Should(MatchXML(b)) 31 }) 32 33 It("should succeed if the XML matches", func() { 34 Expect(sample_01).Should(MatchXML(sample_01)) // same XML 35 Expect(sample_01).Should(MatchXML(sample_02)) // same XML with blank lines 36 Expect(sample_01).Should(MatchXML(sample_03)) // same XML with different formatting 37 Expect(sample_01).ShouldNot(MatchXML(sample_04)) // same structures with different values 38 Expect(sample_01).ShouldNot(MatchXML(sample_05)) // different structures 39 Expect(sample_06).ShouldNot(MatchXML(sample_07)) // same xml names with different namespaces 40 Expect(sample_07).ShouldNot(MatchXML(sample_08)) // same structures with different values 41 Expect(sample_09).ShouldNot(MatchXML(sample_10)) // same structures with different attribute values 42 Expect(sample_11).Should(MatchXML(sample_11)) // with non UTF-8 encoding 43 }) 44 }) 45 46 When("the expected is not valid XML", func() { 47 It("should error and explain why", func() { 48 success, err := (&MatchXMLMatcher{XMLToMatch: sample_01}).Match(`oops`) 49 Expect(success).Should(BeFalse()) 50 Expect(err).Should(HaveOccurred()) 51 Expect(err.Error()).Should(ContainSubstring("Actual 'oops' should be valid XML")) 52 }) 53 }) 54 55 When("the actual is not valid XML", func() { 56 It("should error and explain why", func() { 57 success, err := (&MatchXMLMatcher{XMLToMatch: `oops`}).Match(sample_01) 58 Expect(success).Should(BeFalse()) 59 Expect(err).Should(HaveOccurred()) 60 Expect(err.Error()).Should(ContainSubstring("Expected 'oops' should be valid XML")) 61 }) 62 }) 63 64 When("the expected is neither a string nor a stringer nor a byte array", func() { 65 It("should error", func() { 66 success, err := (&MatchXMLMatcher{XMLToMatch: 2}).Match(sample_01) 67 Expect(success).Should(BeFalse()) 68 Expect(err).Should(HaveOccurred()) 69 Expect(err.Error()).Should(ContainSubstring("MatchXMLMatcher matcher requires a string, stringer, or []byte. Got expected:\n <int>: 2")) 70 71 success, err = (&MatchXMLMatcher{XMLToMatch: nil}).Match(sample_01) 72 Expect(success).Should(BeFalse()) 73 Expect(err).Should(HaveOccurred()) 74 Expect(err.Error()).Should(ContainSubstring("MatchXMLMatcher matcher requires a string, stringer, or []byte. Got expected:\n <nil>: nil")) 75 }) 76 }) 77 78 When("the actual is neither a string nor a stringer nor a byte array", func() { 79 It("should error", func() { 80 success, err := (&MatchXMLMatcher{XMLToMatch: sample_01}).Match(2) 81 Expect(success).Should(BeFalse()) 82 Expect(err).Should(HaveOccurred()) 83 Expect(err.Error()).Should(ContainSubstring("MatchXMLMatcher matcher requires a string, stringer, or []byte. Got actual:\n <int>: 2")) 84 85 success, err = (&MatchXMLMatcher{XMLToMatch: sample_01}).Match(nil) 86 Expect(success).Should(BeFalse()) 87 Expect(err).Should(HaveOccurred()) 88 Expect(err.Error()).Should(ContainSubstring("MatchXMLMatcher matcher requires a string, stringer, or []byte. Got actual:\n <nil>: nil")) 89 }) 90 }) 91 92 It("shows failure message", func() { 93 failuresMessages := InterceptGomegaFailures(func() { 94 Expect("<xml/>").To(MatchXML("<yml/>")) 95 }) 96 Expect(failuresMessages).To(Equal([]string{"Expected\n<xml/>\nto match XML of\n<yml/>"})) 97 }) 98 99 It("shows negated failure message", func() { 100 failuresMessages := InterceptGomegaFailures(func() { 101 Expect("<xml/>").ToNot(MatchXML("<xml/>")) 102 }) 103 Expect(failuresMessages).To(Equal([]string{"Expected\n<xml/>\nnot to match XML of\n<xml/>"})) 104 }) 105 })