1 // Copyright 2019 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
14 // This file is a copy of go/doc/comment_test.go with the exception for
15 // the test cases for TestEmphasize and TestCommentEscape
17 var headingTests = []struct {
22 {"A typical usage", true},
23 {"ΔΛΞ is Greek", true},
27 {"A typical usage:", false},
28 {"This code:", false},
29 {"δ is Greek", false},
31 {"Fermat's Last Sentence", true},
34 {"Ted 'Too' Bar", false},
40 func TestIsHeading(t *testing.T) {
41 for _, tt := range headingTests {
42 if h := heading(tt.line); (len(h) > 0) != tt.ok {
43 t.Errorf("isHeading(%q) = %v, want %v", tt.line, h, tt.ok)
48 var blocksTests = []struct {
87 {opPara, []string{"Para 1.\n", "Para 1 line 2.\n"}},
88 {opPara, []string{"Para 2.\n"}},
89 {opHead, []string{"Section"}},
90 {opPara, []string{"Para 3.\n"}},
91 {opPre, []string{"pre\n", "pre1\n"}},
92 {opPara, []string{"Para 4.\n"}},
93 {opPre, []string{"pre\n", "pre1\n", "\n", "pre2\n"}},
94 {opPara, []string{"Para 5.\n"}},
95 {opPre, []string{"pre\n", "\n", "\n", "pre1\n", "pre2\n"}},
96 {opPara, []string{"Para 6.\n"}},
97 {opPre, []string{"pre\n", "pre2\n"}},
99 text: `. Para 1. Para 1 line 2.
133 in: "Para.\n\tshould not be ``escaped''",
135 {opPara, []string{"Para.\n"}},
136 {opPre, []string{"should not be ``escaped''"}},
138 text: ". Para.\n\n$ should not be ``escaped''",
141 in: "// A very long line of 46 char for line wrapping.",
143 {opPara, []string{"// A very long line of 46 char for line wrapping."}},
145 text: `. // A very long line of 46 char for line
150 in: `/* A very long line of 46 char for line wrapping.
151 A very long line of 46 char for line wrapping. */`,
153 {opPara, []string{"/* A very long line of 46 char for line wrapping.\n", "A very long line of 46 char for line wrapping. */"}},
155 text: `. /* A very long line of 46 char for line
156 . wrapping. A very long line of 46 char
157 . for line wrapping. */
162 func TestBlocks(t *testing.T) {
163 for i, tt := range blocksTests {
165 if !reflect.DeepEqual(b, tt.out) {
166 t.Errorf("#%d: mismatch\nhave: %v\nwant: %v", i, b, tt.out)
171 // This has been modified from go/doc to use markdown links instead of html ones
172 // and use markdown escaping instead oh html
173 var emphasizeTests = []struct {
177 {"http://[::1]:8080/foo.txt", `[http\:\/\/\[\:\:1\]\:8080\/foo\.txt](http://[::1]:8080/foo.txt)`},
178 {"before (https://www.google.com) after", `before \([https\:\/\/www\.google\.com](https://www.google.com)\) after`},
179 {"before https://www.google.com:30/x/y/z:b::c. After", `before [https\:\/\/www\.google\.com\:30\/x\/y\/z\:b\:\:c](https://www.google.com:30/x/y/z:b::c)\. After`},
180 {"http://www.google.com/path/:;!-/?query=%34b#093124", `[http\:\/\/www\.google\.com\/path\/\:\;\!\-\/\?query\=\%34b\#093124](http://www.google.com/path/:;!-/?query=%34b#093124)`},
181 {"http://www.google.com/path/:;!-/?query=%34bar#093124", `[http\:\/\/www\.google\.com\/path\/\:\;\!\-\/\?query\=\%34bar\#093124](http://www.google.com/path/:;!-/?query=%34bar#093124)`},
182 {"http://www.google.com/index.html! After", `[http\:\/\/www\.google\.com\/index\.html](http://www.google.com/index.html)\! After`},
183 {"http://www.google.com/", `[http\:\/\/www\.google\.com\/](http://www.google.com/)`},
184 {"https://www.google.com/", `[https\:\/\/www\.google\.com\/](https://www.google.com/)`},
185 {"http://www.google.com/path.", `[http\:\/\/www\.google\.com\/path](http://www.google.com/path)\.`},
186 {"http://en.wikipedia.org/wiki/Camellia_(cipher)", `[http\:\/\/en\.wikipedia\.org\/wiki\/Camellia\_\(cipher\)](http://en.wikipedia.org/wiki/Camellia_\(cipher\))`},
187 {"(http://www.google.com/)", `\([http\:\/\/www\.google\.com\/](http://www.google.com/)\)`},
188 {"http://gmail.com)", `[http\:\/\/gmail\.com](http://gmail.com)\)`},
189 {"((http://gmail.com))", `\(\([http\:\/\/gmail\.com](http://gmail.com)\)\)`},
190 {"http://gmail.com ((http://gmail.com)) ()", `[http\:\/\/gmail\.com](http://gmail.com) \(\([http\:\/\/gmail\.com](http://gmail.com)\)\) \(\)`},
191 {"Foo bar http://example.com/ quux!", `Foo bar [http\:\/\/example\.com\/](http://example.com/) quux\!`},
192 {"Hello http://example.com/%2f/ /world.", `Hello [http\:\/\/example\.com\/\%2f\/](http://example.com/%2f/) \/world\.`},
193 {"Lorem http: ipsum //host/path", `Lorem http\: ipsum \/\/host\/path`},
194 {"javascript://is/not/linked", `javascript\:\/\/is\/not\/linked`},
195 {"http://foo", `[http\:\/\/foo](http://foo)`},
196 {"art by [[https://www.example.com/person/][Person Name]]", `art by \[\[[https\:\/\/www\.example\.com\/person\/](https://www.example.com/person/)\]\[Person Name\]\]`},
197 {"please visit (http://golang.org/)", `please visit \([http\:\/\/golang\.org\/](http://golang.org/)\)`},
198 {"please visit http://golang.org/hello())", `please visit [http\:\/\/golang\.org\/hello\(\)](http://golang.org/hello\(\))\)`},
199 {"http://git.qemu.org/?p=qemu.git;a=blob;f=qapi-schema.json;hb=HEAD", `[http\:\/\/git\.qemu\.org\/\?p\=qemu\.git\;a\=blob\;f\=qapi\-schema\.json\;hb\=HEAD](http://git.qemu.org/?p=qemu.git;a=blob;f=qapi-schema.json;hb=HEAD)`},
200 {"https://foo.bar/bal/x(])", `[https\:\/\/foo\.bar\/bal\/x\(](https://foo.bar/bal/x\()\]\)`},
201 {"foo [ http://bar(])", `foo \[ [http\:\/\/bar\(](http://bar\()\]\)`},
204 func TestEmphasize(t *testing.T) {
205 for i, tt := range emphasizeTests {
207 emphasize(&buf, tt.in, true)
210 t.Errorf("#%d: mismatch\nhave: %v\nwant: %v", i, out, tt.out)
215 func TestCommentEscape(t *testing.T) {
216 //ldquo -> ulquo and rdquo -> urquo
217 commentTests := []struct {
220 {"typically invoked as ``go tool asm'',", "typically invoked as " + ulquo + "go tool asm" + urquo + ","},
221 {"For more detail, run ``go help test'' and ``go help testflag''", "For more detail, run " + ulquo + "go help test" + urquo + " and " + ulquo + "go help testflag" + urquo}}
222 for i, tt := range commentTests {
223 var buf strings.Builder
224 commentEscape(&buf, tt.in, true)
227 t.Errorf("#%d: mismatch\nhave: %q\nwant: %q", i, out, tt.out)