Skip to content

Commit

Permalink
refactor: switch to htmlformat
Browse files Browse the repository at this point in the history
  • Loading branch information
a-h committed Apr 17, 2023
1 parent 2b59b10 commit 086703c
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 12 deletions.
53 changes: 53 additions & 0 deletions generator/htmldiff/diff.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package htmldiff

import (
"context"
"errors"
"fmt"
"io"
"strings"
"sync"

"github.com/a-h/htmlformat"
"github.com/a-h/templ"
"github.com/google/go-cmp/cmp"
)

func Diff(input templ.Component, expected string) (diff string, err error) {
var wg sync.WaitGroup
wg.Add(2)

var errs []error

// Format the expected value.
go func() {
defer wg.Done()
e := new(strings.Builder)
err := htmlformat.Fragment(e, strings.NewReader(expected))
if err != nil {
errs = append(errs, fmt.Errorf("expected html formatting error: %w", err))
}
expected = e.String()
}()

// Pipe via the HTML formatter.
actual := new(strings.Builder)
r, w := io.Pipe()
go func() {
defer wg.Done()
err := htmlformat.Fragment(actual, r)
if err != nil {
errs = append(errs, fmt.Errorf("actual html formatting error: %w", err))
}
}()

// Render the component.
err = input.Render(context.Background(), w)
if err != nil {
errs = append(errs, fmt.Errorf("failed to render component: %w", err))
}

wg.Wait()

return cmp.Diff(expected, actual.String()), errors.Join(errs...)
}
16 changes: 9 additions & 7 deletions generator/test-css-usage/render_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,27 @@ package testcssusage
import (
"context"
_ "embed"
"io"
"strings"
"testing"

"github.com/a-h/htmlformat"
"github.com/a-h/templ/generator/htmldiff"
"github.com/google/go-cmp/cmp"
"github.com/yosssi/gohtml"
)

//go:embed expected.html
var expected string

func TestHTML(t *testing.T) {
w := new(strings.Builder)
err := ThreeButtons().Render(context.Background(), w)
component := ThreeButtons()

diff, err := htmldiff.Diff(component, expected)
if err != nil {
t.Errorf("failed to render: %v", err)
t.Fatal(err)
}
actual := gohtml.Format(w.String())
expected = gohtml.Format(expected)
if diff := cmp.Diff(expected, actual); diff != "" {
if diff != "" {
if err != nil {
t.Error(diff)
}
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.20

require (
github.com/PuerkitoBio/goquery v1.8.1
github.com/a-h/htmlformat v0.0.0-20230417105637-bdff660c6c36
github.com/a-h/lexical v0.0.53
github.com/a-h/parse v0.0.0-20230402144745-e6c8bc86e846
github.com/a-h/pathvars v0.0.12
Expand All @@ -21,7 +22,6 @@ require (
github.com/andybalholm/cascadia v1.3.1 // indirect
github.com/segmentio/asm v1.2.0 // indirect
github.com/segmentio/encoding v0.3.6 // indirect
github.com/yosssi/gohtml v0.0.0-20201013000340-ee4748c638f4 // indirect
go.lsp.dev/pkg v0.0.0-20210717090340-384b27a52fb2 // indirect
go.uber.org/atomic v1.10.0 // indirect
go.uber.org/multierr v1.9.0 // indirect
Expand Down
6 changes: 2 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
github.com/PuerkitoBio/goquery v1.8.1 h1:uQxhNlArOIdbrH1tr0UXwdVFgDcZDrZVdcpygAcwmWM=
github.com/PuerkitoBio/goquery v1.8.1/go.mod h1:Q8ICL1kNUJ2sXGoAhPGUdYDJvgQgHzJsnnd3H7Ho5jQ=
github.com/a-h/htmlformat v0.0.0-20230417105637-bdff660c6c36 h1:mF1/+Wa/LKXK2iNi3tnbeN6+8a9RMk2R2fgjGIdmjms=
github.com/a-h/htmlformat v0.0.0-20230417105637-bdff660c6c36/go.mod h1:WnXGBO3J4bxTkUrZQMa/A7EfAd2IY/MtPLaonv7TZlQ=
github.com/a-h/lexical v0.0.53 h1:uXaV05/iWmVe8A/TxUXxPrpe7z3/8AVbWmOUEbYPe+Q=
github.com/a-h/lexical v0.0.53/go.mod h1:d73jw5cgKXuYypRozNBuxRNFrTWQ3y5hVMG7rUjh1Qw=
github.com/a-h/parse v0.0.0-20230402144745-e6c8bc86e846 h1:4yqkQ38CwznwKvf/K6UbBWmcz7Ok/ZSzAXYe1Y9k7fU=
Expand Down Expand Up @@ -27,8 +29,6 @@ github.com/segmentio/asm v1.2.0/go.mod h1:BqMnlJP91P8d+4ibuonYZw9mfnzI9HfxselHZr
github.com/segmentio/encoding v0.3.6 h1:E6lVLyDPseWEulBmCmAKPanDd3jiyGDo5gMcugCRwZQ=
github.com/segmentio/encoding v0.3.6/go.mod h1:n0JeuIqEQrQoPDGsjo8UNd1iA0U8d8+oHAA4E3G3OxM=
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
github.com/yosssi/gohtml v0.0.0-20201013000340-ee4748c638f4 h1:0sw0nJM544SpsihWx1bkXdYLQDlzRflMgFJQ4Yih9ts=
github.com/yosssi/gohtml v0.0.0-20201013000340-ee4748c638f4/go.mod h1:+ccdNT0xMY1dtc5XBxumbYfOUhmduiGudqaDgD2rVRE=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
go.lsp.dev/jsonrpc2 v0.10.0 h1:Pr/YcXJoEOTMc/b6OTmcR1DPJ3mSWl/SWiU1Cct6VmI=
go.lsp.dev/jsonrpc2 v0.10.0/go.mod h1:fmEzIdXPi/rf6d4uFcayi8HpFP1nBF99ERP1htC72Ac=
Expand All @@ -52,7 +52,6 @@ golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLL
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20210916014120-12bc252f5db8/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/net v0.7.0 h1:rJrUqqhjsgNp7KqAIc25s9pZnjU7TUcSY7HcVZjdn1g=
golang.org/x/net v0.7.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
golang.org/x/net v0.9.0 h1:aWJ/m6xSmxWBx+V0XRHTlrYrPG56jKsLdTFmsSsCzOM=
golang.org/x/net v0.9.0/go.mod h1:d48xBJpPfHeWQsugry2m+kC02ZBRGRgulfHnEXEuWns=
Expand All @@ -65,7 +64,6 @@ golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20211110154304-99a53858aa08/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0 h1:MUK/U/4lj1t1oPg0HfuXDN/Z1wv31ZJ/YcPiGccS4DU=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.7.0 h1:3jlCCIQZPdOYu1h8BkNvLz8Kgwtae2cagcG/VamtZRU=
golang.org/x/sys v0.7.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
Expand Down

0 comments on commit 086703c

Please sign in to comment.