1package source 2 3import ( 4 "bytes" 5 "reflect" 6 "strings" 7 "testing" 8) 9 10// This file is a copy of go/doc/comment_test.go with the exception for 11// the test cases for TestEmphasize and TestCommentEscape 12 13var headingTests = []struct { 14 line string 15 ok bool 16}{ 17 {"Section", true}, 18 {"A typical usage", true}, 19 {"ΔΛΞ is Greek", true}, 20 {"Foo 42", true}, 21 {"", false}, 22 {"section", false}, 23 {"A typical usage:", false}, 24 {"This code:", false}, 25 {"δ is Greek", false}, 26 {"Foo §", false}, 27 {"Fermat's Last Sentence", true}, 28 {"Fermat's", true}, 29 {"'sX", false}, 30 {"Ted 'Too' Bar", false}, 31 {"Use n+m", false}, 32 {"Scanning:", false}, 33 {"N:M", false}, 34} 35 36func TestIsHeading(t *testing.T) { 37 for _, tt := range headingTests { 38 if h := heading(tt.line); (len(h) > 0) != tt.ok { 39 t.Errorf("isHeading(%q) = %v, want %v", tt.line, h, tt.ok) 40 } 41 } 42} 43 44var blocksTests = []struct { 45 in string 46 out []block 47 text string 48}{ 49 { 50 in: `Para 1. 51Para 1 line 2. 52 53Para 2. 54 55Section 56 57Para 3. 58 59 pre 60 pre1 61 62Para 4. 63 64 pre 65 pre1 66 67 pre2 68 69Para 5. 70 71 72 pre 73 74 75 pre1 76 pre2 77 78Para 6. 79 pre 80 pre2 81`, 82 out: []block{ 83 {opPara, []string{"Para 1.\n", "Para 1 line 2.\n"}}, 84 {opPara, []string{"Para 2.\n"}}, 85 {opHead, []string{"Section"}}, 86 {opPara, []string{"Para 3.\n"}}, 87 {opPre, []string{"pre\n", "pre1\n"}}, 88 {opPara, []string{"Para 4.\n"}}, 89 {opPre, []string{"pre\n", "pre1\n", "\n", "pre2\n"}}, 90 {opPara, []string{"Para 5.\n"}}, 91 {opPre, []string{"pre\n", "\n", "\n", "pre1\n", "pre2\n"}}, 92 {opPara, []string{"Para 6.\n"}}, 93 {opPre, []string{"pre\n", "pre2\n"}}, 94 }, 95 text: `. Para 1. Para 1 line 2. 96 97. Para 2. 98 99 100. Section 101 102. Para 3. 103 104$ pre 105$ pre1 106 107. Para 4. 108 109$ pre 110$ pre1 111 112$ pre2 113 114. Para 5. 115 116$ pre 117 118 119$ pre1 120$ pre2 121 122. Para 6. 123 124$ pre 125$ pre2 126`, 127 }, 128 { 129 in: "Para.\n\tshould not be ``escaped''", 130 out: []block{ 131 {opPara, []string{"Para.\n"}}, 132 {opPre, []string{"should not be ``escaped''"}}, 133 }, 134 text: ". Para.\n\n$ should not be ``escaped''", 135 }, 136 { 137 in: "// A very long line of 46 char for line wrapping.", 138 out: []block{ 139 {opPara, []string{"// A very long line of 46 char for line wrapping."}}, 140 }, 141 text: `. // A very long line of 46 char for line 142. // wrapping. 143`, 144 }, 145 { 146 in: `/* A very long line of 46 char for line wrapping. 147A very long line of 46 char for line wrapping. */`, 148 out: []block{ 149 {opPara, []string{"/* A very long line of 46 char for line wrapping.\n", "A very long line of 46 char for line wrapping. */"}}, 150 }, 151 text: `. /* A very long line of 46 char for line 152. wrapping. A very long line of 46 char 153. for line wrapping. */ 154`, 155 }, 156} 157 158func TestBlocks(t *testing.T) { 159 for i, tt := range blocksTests { 160 b := blocks(tt.in) 161 if !reflect.DeepEqual(b, tt.out) { 162 t.Errorf("#%d: mismatch\nhave: %v\nwant: %v", i, b, tt.out) 163 } 164 } 165} 166 167// This has been modified from go/doc to use markdown links instead of html ones 168// and use markdown escaping instead oh html 169var emphasizeTests = []struct { 170 in, out string 171}{ 172 {"", ""}, 173 {"http://[::1]:8080/foo.txt", `[http\:\/\/\[\:\:1\]\:8080\/foo\.txt](http://[::1]:8080/foo.txt)`}, 174 {"before (https://www.google.com) after", `before \([https\:\/\/www\.google\.com](https://www.google.com)\) after`}, 175 {"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`}, 176 {"http://www.google.com/path/:;!-/?query=%34b#093124", `[http\:\/\/www\.google\.com\/path\/\:\;\!\-\/\?query\=\%34b\#093124](http://www.google.com/path/:;!-/?query=%34b#093124)`}, 177 {"http://www.google.com/path/:;!-/?query=%34bar#093124", `[http\:\/\/www\.google\.com\/path\/\:\;\!\-\/\?query\=\%34bar\#093124](http://www.google.com/path/:;!-/?query=%34bar#093124)`}, 178 {"http://www.google.com/index.html! After", `[http\:\/\/www\.google\.com\/index\.html](http://www.google.com/index.html)\! After`}, 179 {"http://www.google.com/", `[http\:\/\/www\.google\.com\/](http://www.google.com/)`}, 180 {"https://www.google.com/", `[https\:\/\/www\.google\.com\/](https://www.google.com/)`}, 181 {"http://www.google.com/path.", `[http\:\/\/www\.google\.com\/path](http://www.google.com/path)\.`}, 182 {"http://en.wikipedia.org/wiki/Camellia_(cipher)", `[http\:\/\/en\.wikipedia\.org\/wiki\/Camellia\_\(cipher\)](http://en.wikipedia.org/wiki/Camellia_\(cipher\))`}, 183 {"(http://www.google.com/)", `\([http\:\/\/www\.google\.com\/](http://www.google.com/)\)`}, 184 {"http://gmail.com)", `[http\:\/\/gmail\.com](http://gmail.com)\)`}, 185 {"((http://gmail.com))", `\(\([http\:\/\/gmail\.com](http://gmail.com)\)\)`}, 186 {"http://gmail.com ((http://gmail.com)) ()", `[http\:\/\/gmail\.com](http://gmail.com) \(\([http\:\/\/gmail\.com](http://gmail.com)\)\) \(\)`}, 187 {"Foo bar http://example.com/ quux!", `Foo bar [http\:\/\/example\.com\/](http://example.com/) quux\!`}, 188 {"Hello http://example.com/%2f/ /world.", `Hello [http\:\/\/example\.com\/\%2f\/](http://example.com/%2f/) \/world\.`}, 189 {"Lorem http: ipsum //host/path", `Lorem http\: ipsum \/\/host\/path`}, 190 {"javascript://is/not/linked", `javascript\:\/\/is\/not\/linked`}, 191 {"http://foo", `[http\:\/\/foo](http://foo)`}, 192 {"art by [[https://www.example.com/person/][Person Name]]", `art by \[\[[https\:\/\/www\.example\.com\/person\/](https://www.example.com/person/)\]\[Person Name\]\]`}, 193 {"please visit (http://golang.org/)", `please visit \([http\:\/\/golang\.org\/](http://golang.org/)\)`}, 194 {"please visit http://golang.org/hello())", `please visit [http\:\/\/golang\.org\/hello\(\)](http://golang.org/hello\(\))\)`}, 195 {"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)`}, 196 {"https://foo.bar/bal/x(])", `[https\:\/\/foo\.bar\/bal\/x\(](https://foo.bar/bal/x\()\]\)`}, 197 {"foo [ http://bar(])", `foo \[ [http\:\/\/bar\(](http://bar\()\]\)`}, 198} 199 200func TestEmphasize(t *testing.T) { 201 for i, tt := range emphasizeTests { 202 var buf bytes.Buffer 203 emphasize(&buf, tt.in, true) 204 out := buf.String() 205 if out != tt.out { 206 t.Errorf("#%d: mismatch\nhave: %v\nwant: %v", i, out, tt.out) 207 } 208 } 209} 210 211func TestCommentEscape(t *testing.T) { 212 //ldquo -> ulquo and rdquo -> urquo 213 commentTests := []struct { 214 in, out string 215 }{ 216 {"typically invoked as ``go tool asm'',", "typically invoked as " + ulquo + "go tool asm" + urquo + ","}, 217 {"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}} 218 for i, tt := range commentTests { 219 var buf strings.Builder 220 commentEscape(&buf, tt.in, true) 221 out := buf.String() 222 if out != tt.out { 223 t.Errorf("#%d: mismatch\nhave: %q\nwant: %q", i, out, tt.out) 224 } 225 } 226} 227