github.com/shijuvar/go@v0.0.0-20141209052335-e8f13700b70c/src/go/doc/comment_test.go (about) 1 // Copyright 2011 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. 4 5 package doc 6 7 import ( 8 "bytes" 9 "reflect" 10 "testing" 11 ) 12 13 var 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 36 func 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 44 var blocksTests = []struct { 45 in string 46 out []block 47 text string 48 }{ 49 { 50 in: `Para 1. 51 Para 1 line 2. 52 53 Para 2. 54 55 Section 56 57 Para 3. 58 59 pre 60 pre1 61 62 Para 4. 63 64 pre 65 pre1 66 67 pre2 68 69 Para 5. 70 71 72 pre 73 74 75 pre1 76 pre2 77 78 Para 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 130 func TestBlocks(t *testing.T) { 131 for i, tt := range blocksTests { 132 b := blocks(tt.in) 133 if !reflect.DeepEqual(b, tt.out) { 134 t.Errorf("#%d: mismatch\nhave: %v\nwant: %v", i, b, tt.out) 135 } 136 } 137 } 138 139 func TestToText(t *testing.T) { 140 var buf bytes.Buffer 141 for i, tt := range blocksTests { 142 ToText(&buf, tt.in, ". ", "$\t", 40) 143 if have := buf.String(); have != tt.text { 144 t.Errorf("#%d: mismatch\nhave: %s\nwant: %s\nhave vs want:\n%q\n%q", i, have, tt.text, have, tt.text) 145 } 146 buf.Reset() 147 } 148 } 149 150 var emphasizeTests = []struct { 151 in, out string 152 }{ 153 {"http://www.google.com/", `<a href="http://www.google.com/">http://www.google.com/</a>`}, 154 {"https://www.google.com/", `<a href="https://www.google.com/">https://www.google.com/</a>`}, 155 {"http://www.google.com/path.", `<a href="http://www.google.com/path">http://www.google.com/path</a>.`}, 156 {"http://en.wikipedia.org/wiki/Camellia_(cipher)", `<a href="http://en.wikipedia.org/wiki/Camellia_(cipher)">http://en.wikipedia.org/wiki/Camellia_(cipher)</a>`}, 157 {"(http://www.google.com/)", `(<a href="http://www.google.com/">http://www.google.com/</a>)`}, 158 {"http://gmail.com)", `<a href="http://gmail.com">http://gmail.com</a>)`}, 159 {"((http://gmail.com))", `((<a href="http://gmail.com">http://gmail.com</a>))`}, 160 {"http://gmail.com ((http://gmail.com)) ()", `<a href="http://gmail.com">http://gmail.com</a> ((<a href="http://gmail.com">http://gmail.com</a>)) ()`}, 161 {"Foo bar http://example.com/ quux!", `Foo bar <a href="http://example.com/">http://example.com/</a> quux!`}, 162 {"Hello http://example.com/%2f/ /world.", `Hello <a href="http://example.com/%2f/">http://example.com/%2f/</a> /world.`}, 163 {"Lorem http: ipsum //host/path", "Lorem http: ipsum //host/path"}, 164 {"javascript://is/not/linked", "javascript://is/not/linked"}, 165 } 166 167 func TestEmphasize(t *testing.T) { 168 for i, tt := range emphasizeTests { 169 var buf bytes.Buffer 170 emphasize(&buf, tt.in, nil, true) 171 out := buf.String() 172 if out != tt.out { 173 t.Errorf("#%d: mismatch\nhave: %v\nwant: %v", i, out, tt.out) 174 } 175 } 176 } 177 178 var pairedParensPrefixLenTests = []struct { 179 in, out string 180 }{ 181 {"", ""}, 182 {"foo", "foo"}, 183 {"()", "()"}, 184 {"foo()", "foo()"}, 185 {"foo()()()", "foo()()()"}, 186 {"foo()((()()))", "foo()((()()))"}, 187 {"foo()((()()))bar", "foo()((()()))bar"}, 188 {"foo)", "foo"}, 189 {"foo))", "foo"}, 190 {"foo)))))", "foo"}, 191 {"(foo", ""}, 192 {"((foo", ""}, 193 {"(((((foo", ""}, 194 {"(foo)", "(foo)"}, 195 {"((((foo))))", "((((foo))))"}, 196 {"foo()())", "foo()()"}, 197 {"foo((()())", "foo"}, 198 {"foo((()())) (() foo ", "foo((()())) "}, 199 } 200 201 func TestPairedParensPrefixLen(t *testing.T) { 202 for i, tt := range pairedParensPrefixLenTests { 203 if out := tt.in[:pairedParensPrefixLen(tt.in)]; out != tt.out { 204 t.Errorf("#%d: mismatch\nhave: %q\nwant: %q", i, out, tt.out) 205 } 206 } 207 }