Skip to content

Commit

Permalink
fix test failures (2)
Browse files Browse the repository at this point in the history
Signed-off-by: Xavier Coulon <[email protected]>
  • Loading branch information
xcoulon committed Mar 3, 2022
1 parent be80d3d commit 811b0c1
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 19 deletions.
5 changes: 1 addition & 4 deletions libasciidoc_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package libasciidoc_test

import (
"io/ioutil"
"os"
"strings"
"time"
Expand Down Expand Up @@ -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,
Expand All @@ -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
Expand Down
File renamed without changes.
15 changes: 0 additions & 15 deletions test/pending_fixtures_test.go

This file was deleted.

53 changes: 53 additions & 0 deletions testsupport/html5_matcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

0 comments on commit 811b0c1

Please sign in to comment.