Skip to content

Commit 811c4bb

Browse files
committedSep 23, 2020
don't escape comments - fixes #17
1 parent eb64133 commit 811c4bb

File tree

2 files changed

+38
-3
lines changed

2 files changed

+38
-3
lines changed
 

‎render/render.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,10 @@ func toJSONHelper(v interface{}) (template.HTML, error) {
4040
return template.HTML(b), nil
4141
}
4242

43-
func formatCommentText(s string) string {
43+
func formatCommentText(s string) template.HTML {
4444
var buf bytes.Buffer
4545
doc.ToText(&buf, s, "// ", "", 80)
46-
return buf.String()
46+
return template.HTML(buf.String())
4747
}
4848

4949
func formatCommentHTML(s string) template.HTML {

‎render/render_test.go

+36-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package render
22

33
import (
4+
"log"
45
"strings"
56
"testing"
67

@@ -31,6 +32,36 @@ package <%= def.PackageName %>`
3132
}
3233
}
3334

35+
// TestRenderCommentsWithQuotes addresses https://github.com/pacedotdev/oto/issues/17.
36+
func TestRenderCommentsWithQuotes(t *testing.T) {
37+
is := is.New(t)
38+
def := parser.Definition{
39+
PackageName: "services",
40+
Services: []parser.Service{
41+
{
42+
Comment: `This comment contains "quotes"`,
43+
Name: "MyService",
44+
},
45+
},
46+
}
47+
template := `
48+
<%= for (service) in def.Services { %>
49+
<%= format_comment_text(service.Comment) %>type <%= service.Name %> struct
50+
<% } %>
51+
`
52+
s, err := Render(template, def, nil)
53+
is.NoErr(err)
54+
log.Println(s)
55+
for _, should := range []string{
56+
`// This comment contains "quotes"`,
57+
} {
58+
if !strings.Contains(s, should) {
59+
t.Errorf("missing: %s", should)
60+
is.Fail()
61+
}
62+
}
63+
}
64+
3465
func TestCamelizeDown(t *testing.T) {
3566
for in, expected := range map[string]string{
3667
"CamelsAreGreat": "camelsAreGreat",
@@ -71,6 +102,10 @@ func TestFormatTags(t *testing.T) {
71102
func TestFormatCommentText(t *testing.T) {
72103
is := is.New(t)
73104

74-
actual := strings.TrimSpace(formatCommentText("card's"))
105+
actual := strings.TrimSpace(string(formatCommentText("card's")))
75106
is.Equal(actual, "// card's")
107+
108+
actual = strings.TrimSpace(string(formatCommentText(`What happens if I use "quotes"?`)))
109+
is.Equal(actual, `// What happens if I use "quotes"?`)
110+
76111
}

0 commit comments

Comments
 (0)
Please sign in to comment.