Debian Codegraph @

present/code_test.go

raw symbols SCIP
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.
4
5 package presentscip-go gomod golang.org/x/tools . `golang.org/x/tools/present`/
6
7 import (
8 "fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/"
9 "html/templatescip-go gomod github.com/golang/go/src go1.25.0 `html/template`/"
10 "stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/"
11 "testingscip-go gomod github.com/golang/go/src go1.25.0 testing/"
12 )
13
14 func TestParseCodescip-go gomod golang.org/x/tools . `golang.org/x/tools/present`/TestParseCode().(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#) {
15 // Enable play but revert the change at the end.
16 defer func(play bool) { PlayEnabledscip-go gomod golang.org/x/tools . `golang.org/x/tools/present`/PlayEnabled. = play }(PlayEnabledscip-go gomod golang.org/x/tools . `golang.org/x/tools/present`/PlayEnabled.)
17 PlayEnabledscip-go gomod golang.org/x/tools . `golang.org/x/tools/present`/PlayEnabled. = true
18
19 helloTest := []byte(`
20 package main
21
22 import "fmt"
23
24 func main() {
25 fmt.Println("hello, test")
26 }
27 `)
28 helloTestHTML := templatescip-go gomod github.com/golang/go/src go1.25.0 `html/template`/.HTMLscip-go gomod github.com/golang/go/src go1.25.0 `html/template`/HTML#(`
29 <pre><span num="2">package main</span>
30 <span num="3"></span>
31 <span num="4">import &#34;fmt&#34;</span>
32 <span num="5"></span>
33 <span num="6">func main() {</span>
34 <span num="7"> fmt.Println(&#34;hello, test&#34;)</span>
35 <span num="8">}</span>
36 </pre>
37 `)
38 helloTestHL := []byte(`
39 package main
40
41 import "fmt" // HLimport
42
43 func main() { // HLfunc
44 fmt.Println("hello, test") // HL
45 }
46 `)
47 highlight := func(h templatescip-go gomod github.com/golang/go/src go1.25.0 `html/template`/.HTMLscip-go gomod github.com/golang/go/src go1.25.0 `html/template`/HTML#, s string) templatescip-go gomod github.com/golang/go/src go1.25.0 `html/template`/.HTMLscip-go gomod github.com/golang/go/src go1.25.0 `html/template`/HTML# {
48 return templatescip-go gomod github.com/golang/go/src go1.25.0 `html/template`/.HTMLscip-go gomod github.com/golang/go/src go1.25.0 `html/template`/HTML#(stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.Replacescip-go gomod github.com/golang/go/src go1.25.0 strings/Replace().(string(h), s, "<b>"+s+"</b>", -1))
49 }
50 read := func(b []byte, err error) func(string) ([]byte, error) {
51 return func(string) ([]byte, error) { return b, err }
52 }
53
54 tests := []struct {
55 name string
56 readFile func(string) ([]byte, error)
57 sourceFile string
58 cmd string
59 err string
60 Codescip-go gomod golang.org/x/tools . `golang.org/x/tools/present`/Code#
61 }{
62 {
63 name: "all code, no play",
64 readFile: read(helloTest, nil),
65 sourceFile: "main.go",
66 cmd: ".code main.go",
67 Code: Codescip-go gomod golang.org/x/tools . `golang.org/x/tools/present`/Code#{
68 Extscip-go gomod golang.org/x/tools . `golang.org/x/tools/present`/Code#Ext.: ".go",
69 FileNamescip-go gomod golang.org/x/tools . `golang.org/x/tools/present`/Code#FileName.: "main.go",
70 Rawscip-go gomod golang.org/x/tools . `golang.org/x/tools/present`/Code#Raw.: helloTest,
71 Textscip-go gomod golang.org/x/tools . `golang.org/x/tools/present`/Code#Text.: helloTestHTML,
72 },
73 },
74 {
75 name: "all code, play",
76 readFile: read(helloTest, nil),
77 sourceFile: "main.go",
78 cmd: ".play main.go",
79 Code: Codescip-go gomod golang.org/x/tools . `golang.org/x/tools/present`/Code#{
80 Extscip-go gomod golang.org/x/tools . `golang.org/x/tools/present`/Code#Ext.: ".go",
81 FileNamescip-go gomod golang.org/x/tools . `golang.org/x/tools/present`/Code#FileName.: "main.go",
82 Playscip-go gomod golang.org/x/tools . `golang.org/x/tools/present`/Code#Play.: true,
83 Rawscip-go gomod golang.org/x/tools . `golang.org/x/tools/present`/Code#Raw.: helloTest,
84 Textscip-go gomod golang.org/x/tools . `golang.org/x/tools/present`/Code#Text.: helloTestHTML,
85 },
86 },
87 {
88 name: "all code, highlighted",
89 readFile: read(helloTestHL, nil),
90 sourceFile: "main.go",
91 cmd: ".code main.go",
92 Code: Codescip-go gomod golang.org/x/tools . `golang.org/x/tools/present`/Code#{
93 Extscip-go gomod golang.org/x/tools . `golang.org/x/tools/present`/Code#Ext.: ".go",
94 FileNamescip-go gomod golang.org/x/tools . `golang.org/x/tools/present`/Code#FileName.: "main.go",
95 Rawscip-go gomod golang.org/x/tools . `golang.org/x/tools/present`/Code#Raw.: helloTestHL,
96 Textscip-go gomod golang.org/x/tools . `golang.org/x/tools/present`/Code#Text.: highlight(helloTestHTML, "fmt.Println(&#34;hello, test&#34;)"),
97 },
98 },
99 {
100 name: "highlight only func",
101 readFile: read(helloTestHL, nil),
102 sourceFile: "main.go",
103 cmd: ".code main.go HLfunc",
104 Code: Codescip-go gomod golang.org/x/tools . `golang.org/x/tools/present`/Code#{
105 Extscip-go gomod golang.org/x/tools . `golang.org/x/tools/present`/Code#Ext.: ".go",
106 FileNamescip-go gomod golang.org/x/tools . `golang.org/x/tools/present`/Code#FileName.: "main.go",
107 Playscip-go gomod golang.org/x/tools . `golang.org/x/tools/present`/Code#Play.: false,
108 Rawscip-go gomod golang.org/x/tools . `golang.org/x/tools/present`/Code#Raw.: []byte("package main\n\nimport \"fmt\" // HLimport\n\nfunc main() { // HLfunc\n\tfmt.Println(\"hello, test\") // HL\n}"),
109 Textscip-go gomod golang.org/x/tools . `golang.org/x/tools/present`/Code#Text.: highlight(helloTestHTML, "func main() {"),
110 },
111 },
112 {
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",
118 },
119 {
120 name: "error reading file",
121 readFile: read(nil, fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Errorf().("nope")),
122 sourceFile: "main.go",
123 cmd: ".code main.go",
124 err: "main.go:0: nope",
125 },
126 {
127 name: "from func main to the end",
128 readFile: read(helloTest, nil),
129 sourceFile: "main.go",
130 cmd: ".code main.go /func main/,",
131 Code: Codescip-go gomod golang.org/x/tools . `golang.org/x/tools/present`/Code#{
132 Extscip-go gomod golang.org/x/tools . `golang.org/x/tools/present`/Code#Ext.: ".go",
133 FileNamescip-go gomod golang.org/x/tools . `golang.org/x/tools/present`/Code#FileName.: "main.go",
134 Playscip-go gomod golang.org/x/tools . `golang.org/x/tools/present`/Code#Play.: false,
135 Rawscip-go gomod golang.org/x/tools . `golang.org/x/tools/present`/Code#Raw.: []byte("func main() {\n\tfmt.Println(\"hello, test\")\n}"),
136 Textscip-go gomod golang.org/x/tools . `golang.org/x/tools/present`/Code#Text.: "<pre><span num=\"6\">func main() {</span>\n<span num=\"7\"> fmt.Println(&#34;hello, test&#34;)</span>\n<span num=\"8\">}</span>\n</pre>",
137 },
138 },
139 {
140 name: "just func main",
141 readFile: read(helloTest, nil),
142 sourceFile: "main.go",
143 cmd: ".code main.go /func main/",
144 Code: Codescip-go gomod golang.org/x/tools . `golang.org/x/tools/present`/Code#{
145 Extscip-go gomod golang.org/x/tools . `golang.org/x/tools/present`/Code#Ext.: ".go",
146 FileNamescip-go gomod golang.org/x/tools . `golang.org/x/tools/present`/Code#FileName.: "main.go",
147 Playscip-go gomod golang.org/x/tools . `golang.org/x/tools/present`/Code#Play.: false,
148 Rawscip-go gomod golang.org/x/tools . `golang.org/x/tools/present`/Code#Raw.: []byte("func main() {"),
149 Textscip-go gomod golang.org/x/tools . `golang.org/x/tools/present`/Code#Text.: "<pre><span num=\"6\">func main() {</span>\n</pre>",
150 },
151 },
152 {
153 name: "bad address",
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",
158 },
159 {
160 name: "all code with numbers",
161 readFile: read(helloTest, nil),
162 sourceFile: "main.go",
163 cmd: ".code -numbers main.go",
164 Code: Codescip-go gomod golang.org/x/tools . `golang.org/x/tools/present`/Code#{
165 Extscip-go gomod golang.org/x/tools . `golang.org/x/tools/present`/Code#Ext.: ".go",
166 FileNamescip-go gomod golang.org/x/tools . `golang.org/x/tools/present`/Code#FileName.: "main.go",
167 Rawscip-go gomod golang.org/x/tools . `golang.org/x/tools/present`/Code#Raw.: helloTest,
168 // Replacing the first "<pre>"
169 Textscip-go gomod golang.org/x/tools . `golang.org/x/tools/present`/Code#Text.: "<pre class=\"numbers\">" + helloTestHTML[6:],
170 },
171 },
172 {
173 name: "all code editable",
174 readFile: read(helloTest, nil),
175 sourceFile: "main.go",
176 cmd: ".code -edit main.go",
177 Code: Codescip-go gomod golang.org/x/tools . `golang.org/x/tools/present`/Code#{
178 Extscip-go gomod golang.org/x/tools . `golang.org/x/tools/present`/Code#Ext.: ".go",
179 FileNamescip-go gomod golang.org/x/tools . `golang.org/x/tools/present`/Code#FileName.: "main.go",
180 Rawscip-go gomod golang.org/x/tools . `golang.org/x/tools/present`/Code#Raw.: helloTest,
181 Textscip-go gomod golang.org/x/tools . `golang.org/x/tools/present`/Code#Text.: "<pre contenteditable=\"true\" spellcheck=\"false\">" + helloTestHTML[6:],
182 },
183 },
184 }
185
186 trimHTML := func(t templatescip-go gomod github.com/golang/go/src go1.25.0 `html/template`/.HTMLscip-go gomod github.com/golang/go/src go1.25.0 `html/template`/HTML#) string { return stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.TrimSpacescip-go gomod github.com/golang/go/src go1.25.0 strings/TrimSpace().(string(t)) }
187 trimBytes := func(b []byte) string { return stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.TrimSpacescip-go gomod github.com/golang/go/src go1.25.0 strings/TrimSpace().(string(b)) }
188
189 for _, tt := range tests {
190 ctx := &Contextscip-go gomod golang.org/x/tools . `golang.org/x/tools/present`/Context#{tt.readFile}
191 e, err := parseCodescip-go gomod golang.org/x/tools . `golang.org/x/tools/present`/parseCode().(ctx, tt.sourceFile, 0, tt.cmd)
192 if err != nil {
193 if tt.err == "" {
194 t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("%s: unexpected error %v", tt.name, err)
195 } else if !stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.Containsscip-go gomod github.com/golang/go/src go1.25.0 strings/Contains().(err.Error(), tt.err) {
196 t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("%s: expected error %s; got %v", tt.name, tt.err, err)
197 }
198 continue
199 }
200 if tt.err != "" {
201 t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("%s: expected error %s; but got none", tt.name, tt.err)
202 continue
203 }
204 c, ok := e.(Codescip-go gomod golang.org/x/tools . `golang.org/x/tools/present`/Code#)
205 if !ok {
206 t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("%s: expected a Code value; got %T", tt.name, e)
207 continue
208 }
209 if c.FileNamescip-go gomod golang.org/x/tools . `golang.org/x/tools/present`/Code#FileName. != tt.FileNamescip-go gomod golang.org/x/tools . `golang.org/x/tools/present`/Code#FileName. {
210 t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("%s: expected FileName %s; got %s", tt.name, tt.FileNamescip-go gomod golang.org/x/tools . `golang.org/x/tools/present`/Code#FileName., c.FileNamescip-go gomod golang.org/x/tools . `golang.org/x/tools/present`/Code#FileName.)
211 }
212 if c.Extscip-go gomod golang.org/x/tools . `golang.org/x/tools/present`/Code#Ext. != tt.Extscip-go gomod golang.org/x/tools . `golang.org/x/tools/present`/Code#Ext. {
213 t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("%s: expected Ext %s; got %s", tt.name, tt.Extscip-go gomod golang.org/x/tools . `golang.org/x/tools/present`/Code#Ext., c.Extscip-go gomod golang.org/x/tools . `golang.org/x/tools/present`/Code#Ext.)
214 }
215 if c.Playscip-go gomod golang.org/x/tools . `golang.org/x/tools/present`/Code#Play. != tt.Playscip-go gomod golang.org/x/tools . `golang.org/x/tools/present`/Code#Play. {
216 t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("%s: expected Play %v; got %v", tt.name, tt.Playscip-go gomod golang.org/x/tools . `golang.org/x/tools/present`/Code#Play., c.Playscip-go gomod golang.org/x/tools . `golang.org/x/tools/present`/Code#Play.)
217 }
218 if got, wants := trimBytes(c.Rawscip-go gomod golang.org/x/tools . `golang.org/x/tools/present`/Code#Raw.), trimBytes(tt.Rawscip-go gomod golang.org/x/tools . `golang.org/x/tools/present`/Code#Raw.); got != wants {
219 t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("%s: expected Raw \n%q\n; got \n%q\n", tt.name, wants, got)
220 }
221 if got, wants := trimHTML(c.Textscip-go gomod golang.org/x/tools . `golang.org/x/tools/present`/Code#Text.), trimHTML(tt.Textscip-go gomod golang.org/x/tools . `golang.org/x/tools/present`/Code#Text.); got != wants {
222 t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("%s: expected Text \n%q\n; got \n%q\n", tt.name, wants, got)
223 }
224 }
225 }