From efd33120d9e83e5b9c9c1b06272d5bfcce246436 Mon Sep 17 00:00:00 2001 From: Adrian Hesketh Date: Fri, 29 Sep 2023 17:29:51 +0100 Subject: [PATCH] chore: extend tests to cover HTML tags within comments --- generator/test-html-comment/expected.html | 1 + generator/test-html-comment/template.templ | 2 + generator/test-html-comment/template_templ.go | 21 ++++++--- generator/test-method/expected.html | 1 + generator/test-method/render_test.go | 26 ++++++++++ generator/test-method/template.templ | 10 ++++ generator/test-method/template_templ.go | 47 +++++++++++++++++++ 7 files changed, 102 insertions(+), 6 deletions(-) create mode 100644 generator/test-method/expected.html create mode 100644 generator/test-method/render_test.go create mode 100644 generator/test-method/template.templ create mode 100644 generator/test-method/template_templ.go diff --git a/generator/test-html-comment/expected.html b/generator/test-html-comment/expected.html index df013f4f5..e4c421bfd 100644 --- a/generator/test-html-comment/expected.html +++ b/generator/test-html-comment/expected.html @@ -11,3 +11,4 @@

third paragraph

sample content + diff --git a/generator/test-html-comment/template.templ b/generator/test-html-comment/template.templ index dd0c71ea3..50b4ce770 100644 --- a/generator/test-html-comment/template.templ +++ b/generator/test-html-comment/template.templ @@ -14,8 +14,10 @@ templ render(content string) { @paragraph("third paragraph") { content } + } templ paragraph(content string) {

{ content }

} + diff --git a/generator/test-html-comment/template_templ.go b/generator/test-html-comment/template_templ.go index 6d7cdf7a1..984db6824 100644 --- a/generator/test-html-comment/template_templ.go +++ b/generator/test-html-comment/template_templ.go @@ -96,7 +96,16 @@ func render(content string) templ.Component { if err != nil { return err } - _, err = templBuffer.WriteString("") + _, err = templBuffer.WriteString("") if err != nil { return err } @@ -115,17 +124,17 @@ func paragraph(content string) templ.Component { defer templ.ReleaseBuffer(templBuffer) } ctx = templ.InitializeContext(ctx) - var_7 := templ.GetChildren(ctx) - if var_7 == nil { - var_7 = templ.NopComponent + var_8 := templ.GetChildren(ctx) + if var_8 == nil { + var_8 = templ.NopComponent } ctx = templ.ClearChildren(ctx) _, err = templBuffer.WriteString("

") if err != nil { return err } - var var_8 string = content - _, err = templBuffer.WriteString(templ.EscapeString(var_8)) + var var_9 string = content + _, err = templBuffer.WriteString(templ.EscapeString(var_9)) if err != nil { return err } diff --git a/generator/test-method/expected.html b/generator/test-method/expected.html new file mode 100644 index 000000000..7bdc726d5 --- /dev/null +++ b/generator/test-method/expected.html @@ -0,0 +1 @@ +

You can implement methods on a type.
diff --git a/generator/test-method/render_test.go b/generator/test-method/render_test.go new file mode 100644 index 000000000..09951e83a --- /dev/null +++ b/generator/test-method/render_test.go @@ -0,0 +1,26 @@ +package testmethod + +import ( + _ "embed" + "testing" + + "github.com/a-h/templ/generator/htmldiff" +) + +//go:embed expected.html +var expected string + +func Test(t *testing.T) { + d := Data{ + message: "You can implement methods on a type.", + } + component := d.Method() + + diff, err := htmldiff.Diff(component, expected) + if err != nil { + t.Fatal(err) + } + if diff != "" { + t.Error(diff) + } +} diff --git a/generator/test-method/template.templ b/generator/test-method/template.templ new file mode 100644 index 000000000..1e392117f --- /dev/null +++ b/generator/test-method/template.templ @@ -0,0 +1,10 @@ +package testmethod + +type Data struct { + message string +} + +templ (d Data) Method() { +
{ d.message }
+} + diff --git a/generator/test-method/template_templ.go b/generator/test-method/template_templ.go new file mode 100644 index 000000000..9cf171220 --- /dev/null +++ b/generator/test-method/template_templ.go @@ -0,0 +1,47 @@ +// Code generated by templ@(devel) DO NOT EDIT. + +package testmethod + +//lint:file-ignore SA4006 This context is only used if a nested component is present. + +import "github.com/a-h/templ" +import "context" +import "io" +import "bytes" + +type Data struct { + message string +} + +func (d Data) Method() templ.Component { + return templ.ComponentFunc(func(ctx context.Context, w io.Writer) (err error) { + templBuffer, templIsBuffer := w.(*bytes.Buffer) + if !templIsBuffer { + templBuffer = templ.GetBuffer() + defer templ.ReleaseBuffer(templBuffer) + } + ctx = templ.InitializeContext(ctx) + var_1 := templ.GetChildren(ctx) + if var_1 == nil { + var_1 = templ.NopComponent + } + ctx = templ.ClearChildren(ctx) + _, err = templBuffer.WriteString("
") + if err != nil { + return err + } + var var_2 string = d.message + _, err = templBuffer.WriteString(templ.EscapeString(var_2)) + if err != nil { + return err + } + _, err = templBuffer.WriteString("
") + if err != nil { + return err + } + if !templIsBuffer { + _, err = templBuffer.WriteTo(w) + } + return err + }) +}