diff --git a/libasciidoc_test.go b/libasciidoc_test.go index ed010186..27413e0e 100644 --- a/libasciidoc_test.go +++ b/libasciidoc_test.go @@ -1,7 +1,6 @@ package libasciidoc_test import ( - "io/ioutil" "os" "strings" "time" @@ -118,8 +117,6 @@ var _ = Describe("documents", func() { filename := "test/compat/demo.adoc" stat, err := os.Stat(filename) Expect(err).NotTo(HaveOccurred()) - expectedTmpl, err := ioutil.ReadFile("test/compat/demo-full.tmpl.html") - Expect(err).NotTo(HaveOccurred()) out := &strings.Builder{} _, err = libasciidoc.ConvertFile(out, @@ -129,7 +126,7 @@ var _ = Describe("documents", func() { configuration.WithCSS("path/to/style.css"), configuration.WithHeaderFooter(true))) Expect(err).NotTo(HaveOccurred()) - Expect(out.String()).To(MatchHTMLTemplate(string(expectedTmpl), + Expect(out.String()).To(MatchHTMLTemplateFile(string("test/compat/demo-full.tmpl.html"), struct { LastUpdated string Version string diff --git a/test/supported_fixtures_test.go b/test/compat_test.go similarity index 100% rename from test/supported_fixtures_test.go rename to test/compat_test.go diff --git a/test/pending_fixtures_test.go b/test/pending_fixtures_test.go deleted file mode 100644 index 8d5da658..00000000 --- a/test/pending_fixtures_test.go +++ /dev/null @@ -1,15 +0,0 @@ -// +build pending - -package test_test - -import ( - . "github.com/onsi/ginkgo" // nolint:golint - . "github.com/onsi/ginkgo/extensions/table" -) - -var _ = Describe("pending", func() { - - // verifies that all files in the `supported` subfolder match their sibling golden file - DescribeTable("supported", compare, entries("fixtures/pending/*.adoc")...) - -}) diff --git a/testsupport/html5_matcher.go b/testsupport/html5_matcher.go index 71837cca..ac6718e9 100644 --- a/testsupport/html5_matcher.go +++ b/testsupport/html5_matcher.go @@ -141,3 +141,56 @@ func (m *htmlTemplateMatcher) FailureMessage(_ interface{}) (message string) { func (m *htmlTemplateMatcher) NegatedFailureMessage(_ interface{}) (message string) { return fmt.Sprintf("expected HTML5 documents not to match:\n%s", m.diffs) } + +// ------------------------------------------------------ +// Match HTML from template file +// ------------------------------------------------------ + +// MatchHTMLTemplate a custom matcher to verify that a document renders as the given template +// which will be processed with the given args +func MatchHTMLTemplateFile(filename string, data interface{}) gomegatypes.GomegaMatcher { + return &htmlTemplateFileMatcher{ + filename: filename, + data: data, + } +} + +type htmlTemplateFileMatcher struct { + filename string + data interface{} + diffs string +} + +func (m *htmlTemplateFileMatcher) Match(actual interface{}) (success bool, err error) { + if _, ok := actual.(string); !ok { + return false, errors.Errorf("MatchHTMLTemplate matcher expects a string (actual: %T)", actual) + } + + expected, err := ioutil.ReadFile(m.filename) + if err != nil { + return false, err + } + expected = bytes.ReplaceAll(expected, []byte{'\r'}, []byte{}) + expectedTmpl, err := template.New("test").Parse(string(expected)) + if err != nil { + return false, err + } + out := new(bytes.Buffer) + err = expectedTmpl.Execute(out, m.data) + if err != nil { + return false, err + } + if out.String() != actual { + m.diffs = cmp.Diff(actual.(string), out.String()) + return false, nil + } + return true, nil +} + +func (m *htmlTemplateFileMatcher) FailureMessage(_ interface{}) (message string) { + return fmt.Sprintf("expected HTML5 documents to match:\n%s", m.diffs) +} + +func (m *htmlTemplateFileMatcher) NegatedFailureMessage(_ interface{}) (message string) { + return fmt.Sprintf("expected HTML5 documents not to match:\n%s", m.diffs) +}