]> Cypherpunks repositories - gostls13.git/commitdiff
go/doc: fix ToText
authorRobert Griesemer <gri@golang.org>
Thu, 3 Apr 2014 22:52:04 +0000 (15:52 -0700)
committerRobert Griesemer <gri@golang.org>
Thu, 3 Apr 2014 22:52:04 +0000 (15:52 -0700)
Fixes #6769.

LGTM=bradfitz
R=bgarcia, rsc, bradfitz
CC=golang-codereviews
https://golang.org/cl/84220044

src/pkg/go/doc/comment.go
src/pkg/go/doc/comment_test.go

index 5c8c43e0c1aa6ee77469512c31460d6ace89a0de..274a625cf0d807a74cc5ee0bdff58707c60cdec3 100644 (file)
@@ -392,7 +392,9 @@ func ToText(w io.Writer, text string, indent, preIndent string, width int) {
                case opPre:
                        w.Write(nl)
                        for _, line := range b.lines {
-                               if !isBlank(line) {
+                               if isBlank(line) {
+                                       w.Write([]byte("\n"))
+                               } else {
                                        w.Write([]byte(preIndent))
                                        w.Write([]byte(line))
                                }
index aa21b8d1b30765e8699445f97f8f105f1ee19721..9f29a61153150e86ede683cb58819a8291d475f5 100644 (file)
@@ -42,8 +42,9 @@ func TestIsHeading(t *testing.T) {
 }
 
 var blocksTests = []struct {
-       in  string
-       out []block
+       in   string
+       out  []block
+       text string
 }{
        {
                in: `Para 1.
@@ -59,6 +60,22 @@ Para 3.
        pre1
 
 Para 4.
+
+       pre
+       pre1
+
+       pre2
+
+Para 5.
+
+
+       pre
+
+
+       pre1
+       pre2
+
+Para 6.
        pre
        pre2
 `,
@@ -69,8 +86,44 @@ Para 4.
                        {opPara, []string{"Para 3.\n"}},
                        {opPre, []string{"pre\n", "pre1\n"}},
                        {opPara, []string{"Para 4.\n"}},
+                       {opPre, []string{"pre\n", "pre1\n", "\n", "pre2\n"}},
+                       {opPara, []string{"Para 5.\n"}},
+                       {opPre, []string{"pre\n", "\n", "\n", "pre1\n", "pre2\n"}},
+                       {opPara, []string{"Para 6.\n"}},
                        {opPre, []string{"pre\n", "pre2\n"}},
                },
+               text: `.   Para 1. Para 1 line 2.
+
+.   Para 2.
+
+
+.   Section
+
+.   Para 3.
+
+$      pre
+$      pre1
+
+.   Para 4.
+
+$      pre
+$      pre1
+
+$      pre2
+
+.   Para 5.
+
+$      pre
+
+
+$      pre1
+$      pre2
+
+.   Para 6.
+
+$      pre
+$      pre2
+`,
        },
 }
 
@@ -83,6 +136,17 @@ func TestBlocks(t *testing.T) {
        }
 }
 
+func TestToText(t *testing.T) {
+       var buf bytes.Buffer
+       for i, tt := range blocksTests {
+               ToText(&buf, tt.in, ".   ", "$\t", 40)
+               if have := buf.String(); have != tt.text {
+                       t.Errorf("#%d: mismatch\nhave: %s\nwant: %s\nhave vs want:\n%q\n%q", i, have, tt.text, have, tt.text)
+               }
+               buf.Reset()
+       }
+}
+
 var emphasizeTests = []struct {
        in  string
        out string