1 // Copyright 2012 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 func TestParseCode(t *testing.T) {
15 // Enable play but revert the change at the end.
16 defer func(play bool) { PlayEnabled = play }(PlayEnabled)
25 fmt.Println("hello, test")
28 helloTestHTML := template.HTML(`
29 <pre><span num="2">package main</span>
31 <span num="4">import "fmt"</span>
33 <span num="6">func main() {</span>
34 <span num="7"> fmt.Println("hello, test")</span>
35 <span num="8">}</span>
38 helloTestHL := []byte(`
41 import "fmt" // HLimport
43 func main() { // HLfunc
44 fmt.Println("hello, test") // HL
47 highlight := func(h template.HTML, s string) template.HTML {
48 return template.HTML(strings.Replace(string(h), s, "<b>"+s+"</b>", -1))
50 read := func(b []byte, err error) func(string) ([]byte, error) {
51 return func(string) ([]byte, error) { return b, err }
56 readFile func(string) ([]byte, error)
63 name: "all code, no play",
64 readFile: read(helloTest, nil),
65 sourceFile: "main.go",
75 name: "all code, play",
76 readFile: read(helloTest, nil),
77 sourceFile: "main.go",
88 name: "all code, highlighted",
89 readFile: read(helloTestHL, nil),
90 sourceFile: "main.go",
96 Text: highlight(helloTestHTML, "fmt.Println("hello, test")"),
100 name: "highlight only func",
101 readFile: read(helloTestHL, nil),
102 sourceFile: "main.go",
103 cmd: ".code main.go HLfunc",
108 Raw: []byte("package main\n\nimport \"fmt\" // HLimport\n\nfunc main() { // HLfunc\n\tfmt.Println(\"hello, test\") // HL\n}"),
109 Text: highlight(helloTestHTML, "func main() {"),
113 name: "bad highlight syntax",
114 readFile: read(helloTest, nil),
115 sourceFile: "main.go",
116 cmd: ".code main.go HL",
117 err: "invalid highlight syntax",
120 name: "error reading file",
121 readFile: read(nil, fmt.Errorf("nope")),
122 sourceFile: "main.go",
123 cmd: ".code main.go",
124 err: "main.go:0: nope",
127 name: "from func main to the end",
128 readFile: read(helloTest, nil),
129 sourceFile: "main.go",
130 cmd: ".code main.go /func main/,",
135 Raw: []byte("func main() {\n\tfmt.Println(\"hello, test\")\n}"),
136 Text: "<pre><span num=\"6\">func main() {</span>\n<span num=\"7\"> fmt.Println("hello, test")</span>\n<span num=\"8\">}</span>\n</pre>",
140 name: "just func main",
141 readFile: read(helloTest, nil),
142 sourceFile: "main.go",
143 cmd: ".code main.go /func main/",
148 Raw: []byte("func main() {"),
149 Text: "<pre><span num=\"6\">func main() {</span>\n</pre>",
154 readFile: read(helloTest, nil),
155 sourceFile: "main.go",
156 cmd: ".code main.go /function main/",
157 err: "main.go:0: no match for function main",
160 name: "all code with numbers",
161 readFile: read(helloTest, nil),
162 sourceFile: "main.go",
163 cmd: ".code -numbers main.go",
168 // Replacing the first "<pre>"
169 Text: "<pre class=\"numbers\">" + helloTestHTML[6:],
173 name: "all code editable",
174 readFile: read(helloTest, nil),
175 sourceFile: "main.go",
176 cmd: ".code -edit main.go",
181 Text: "<pre contenteditable=\"true\" spellcheck=\"false\">" + helloTestHTML[6:],
186 trimHTML := func(t template.HTML) string { return strings.TrimSpace(string(t)) }
187 trimBytes := func(b []byte) string { return strings.TrimSpace(string(b)) }
189 for _, tt := range tests {
190 ctx := &Context{tt.readFile}
191 e, err := parseCode(ctx, tt.sourceFile, 0, tt.cmd)
194 t.Errorf("%s: unexpected error %v", tt.name, err)
195 } else if !strings.Contains(err.Error(), tt.err) {
196 t.Errorf("%s: expected error %s; got %v", tt.name, tt.err, err)
201 t.Errorf("%s: expected error %s; but got none", tt.name, tt.err)
206 t.Errorf("%s: expected a Code value; got %T", tt.name, e)
209 if c.FileName != tt.FileName {
210 t.Errorf("%s: expected FileName %s; got %s", tt.name, tt.FileName, c.FileName)
213 t.Errorf("%s: expected Ext %s; got %s", tt.name, tt.Ext, c.Ext)
215 if c.Play != tt.Play {
216 t.Errorf("%s: expected Play %v; got %v", tt.name, tt.Play, c.Play)
218 if got, wants := trimBytes(c.Raw), trimBytes(tt.Raw); got != wants {
219 t.Errorf("%s: expected Raw \n%q\n; got \n%q\n", tt.name, wants, got)
221 if got, wants := trimHTML(c.Text), trimHTML(tt.Text); got != wants {
222 t.Errorf("%s: expected Text \n%q\n; got \n%q\n", tt.name, wants, got)