Debian Codegraph @

cmd/go-fix-acronym/rewrite_test.go

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 /*
2 Copyright The containerd Authors.
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 */
16
17 package main
18
19 import (
20 "bytes"
21 "go/format"
22 "go/parser"
23 "go/token"
24 "strings"
25 "testing"
26 )
27
28 func testRewrite(t *testing.T, input, expected string, c config) {
29 fset := token.NewFileSet()
30 n, err := parser.ParseFile(fset, "", input, parser.ParseComments)
31 if err != nil {
32 t.Fatalf("failed to parse: %s", err)
33 }
34
35 p, err := compilePattern(c)
36 if err != nil {
37 t.Fatalf("failed to compile: %s", err)
38 }
39
40 rewriteNode(p, n)
41
42 out := &bytes.Buffer{}
43 format.Node(out, fset, n)
44 if out.String() != expected {
45 t.Fatalf("expected %q, but got %q", expected, out)
46 }
47 }
48
49 func TestRewrite(t *testing.T) {
50 testcases := []struct {
51 name string
52 input string
53 expected string
54 c config
55 }{
56 {
57 name: "Simple",
58 c: config{acronyms: []string{"Cpu"}},
•59 input: "// hello\npackage main\nfunc GetCpu(){}",
60 expected: "// hello\npackage main\n\nfunc GetCPU() {}",
61 },
62 {
63 name: "Multiple matches",
64 c: config{acronyms: []string{"Cpu"}},
65 input: `package main
66
67 func GetCpuFromCpuList() {}`,
68 expected: `package main
69
70 func GetCPUFromCPUList() {}`,
71 },
72 {
73 name: "Submatches",
74 c: config{acronyms: []string{"Runtime(Ns)"}},
75 input: `package main
76
77 func KernelTime_100Ns() {}
78 func RuntimeNsAndNsAndSomeSuffix() {}`,
79 expected: `package main
80
81 func KernelTime_100Ns() {}
82 func RuntimeNSAndNsAndSomeSuffix() {}`,
83 },
84 {
85 name: "Multiple submatches",
86 c: config{acronyms: []string{"(Id|Vm)$", "[a-z](Ns)$"}},
87 input: `package main
88
89 func Vm() {}
90 func Time_100Ns() {}
91 func RuntimeNs() {}
92 `,
93 expected: `package main
94
95 func VM() {}
96 func Time_100Ns() {}
97 func RuntimeNS() {}`,
98 },
99 }
100 for _, tc := range testcases {
101 t.Run(tc.name, func(t *testing.T) {
102 testRewrite(t, tc.input, strings.ReplaceAll(tc.expected, "\t", "")+"\n", tc.c)
103 })
104 }
105 }