Debian Codegraph @

gopls/internal/test/marker/testdata/codeaction/invertif.txt

raw
1 This test exercises the 'invert if condition' code action.
2
3 -- p.go --
4 package invertif
5
6 import (
7 "fmt"
8 "os"
9 )
10
11 func Boolean() {
12 b := true
13 if b { //@codeaction("if b", "refactor.rewrite.invertIf", edit=boolean)
14 fmt.Println("A")
15 } else {
16 fmt.Println("B")
17 }
18 }
19
20 func BooleanFn() {
21 if os.IsPathSeparator('X') { //@codeaction("if os.IsPathSeparator('X')", "refactor.rewrite.invertIf", edit=boolean_fn)
22 fmt.Println("A")
23 } else {
24 fmt.Println("B")
25 }
26 }
27
28 // Note that the comment here jumps to the wrong location.
29 func DontRemoveParens() {
30 a := false
31 b := true
32 if !(a ||
33 b) { //@codeaction("b", "refactor.rewrite.invertIf", edit=dont_remove_parens)
34 fmt.Println("A")
35 } else {
36 fmt.Println("B")
37 }
38 }
39
40 func ElseIf() {
41 // No inversion expected when there's not else clause
42 if len(os.Args) > 2 {
43 fmt.Println("A")
44 }
45
46 // No inversion expected for else-if, that would become unreadable
47 if len(os.Args) > 2 {
48 fmt.Println("A")
49 } else if os.Args[0] == "X" { //@codeaction(re"if os.Args.0. == .X.", "refactor.rewrite.invertIf", edit=else_if)
50 fmt.Println("B")
51 } else {
52 fmt.Println("C")
53 }
54 }
55
56 func GreaterThan() {
57 if len(os.Args) > 2 { //@codeaction("i", "refactor.rewrite.invertIf", edit=greater_than)
58 fmt.Println("A")
59 } else {
60 fmt.Println("B")
61 }
62 }
63
64 func NotBoolean() {
65 b := true
66 if !b { //@codeaction("if !b", "refactor.rewrite.invertIf", edit=not_boolean)
67 fmt.Println("A")
68 } else {
69 fmt.Println("B")
70 }
71 }
72
73 func RemoveElse() {
74 if true { //@codeaction("if true", "refactor.rewrite.invertIf", edit=remove_else)
75 fmt.Println("A")
76 } else {
77 fmt.Println("B")
78 return
79 }
80
81 fmt.Println("C")
82 }
83
84 func RemoveParens() {
85 b := true
86 if !(b) { //@codeaction("if", "refactor.rewrite.invertIf", edit=remove_parens)
87 fmt.Println("A")
88 } else {
89 fmt.Println("B")
90 }
91 }
92
93 func Semicolon() {
94 if _, err := fmt.Println("x"); err != nil { //@codeaction("if", "refactor.rewrite.invertIf", edit=semicolon)
95 fmt.Println("A")
96 } else {
97 fmt.Println("B")
98 }
99 }
100
101 func SemicolonAnd() {
102 if n, err := fmt.Println("x"); err != nil && n > 0 { //@codeaction("f", "refactor.rewrite.invertIf", edit=semicolon_and)
103 fmt.Println("A")
104 } else {
105 fmt.Println("B")
106 }
107 }
108
109 func SemicolonOr() {
110 if n, err := fmt.Println("x"); err != nil || n < 5 { //@codeaction(re"if n, err := fmt.Println..x..; err != nil .. n < 5", "refactor.rewrite.invertIf", edit=semicolon_or)
111 fmt.Println("A")
112 } else {
113 fmt.Println("B")
114 }
115 }
116
117 -- @boolean/p.go --
118 @@ -10,3 +10 @@
119 - if b { //@codeaction("if b", "refactor.rewrite.invertIf", edit=boolean)
120 - fmt.Println("A")
121 - } else {
122 + if !b {
123 @@ -14 +12,2 @@
124 + } else { //@codeaction("if b", "refactor.rewrite.invertIf", edit=boolean)
125 + fmt.Println("A")
126 -- @boolean_fn/p.go --
127 @@ -18,3 +18 @@
128 - if os.IsPathSeparator('X') { //@codeaction("if os.IsPathSeparator('X')", "refactor.rewrite.invertIf", edit=boolean_fn)
129 - fmt.Println("A")
130 - } else {
131 + if !os.IsPathSeparator('X') {
132 @@ -22 +20,2 @@
133 + } else { //@codeaction("if os.IsPathSeparator('X')", "refactor.rewrite.invertIf", edit=boolean_fn)
134 + fmt.Println("A")
135 -- @dont_remove_parens/p.go --
136 @@ -29,4 +29,2 @@
137 - if !(a ||
138 - b) { //@codeaction("b", "refactor.rewrite.invertIf", edit=dont_remove_parens)
139 - fmt.Println("A")
140 - } else {
141 + if (a ||
142 + b) {
143 @@ -34 +32,2 @@
144 + } else { //@codeaction("b", "refactor.rewrite.invertIf", edit=dont_remove_parens)
145 + fmt.Println("A")
146 -- @else_if/p.go --
147 @@ -46,3 +46 @@
148 - } else if os.Args[0] == "X" { //@codeaction(re"if os.Args.0. == .X.", "refactor.rewrite.invertIf", edit=else_if)
149 - fmt.Println("B")
150 - } else {
151 + } else if os.Args[0] != "X" {
152 @@ -50 +48,2 @@
153 + } else { //@codeaction(re"if os.Args.0. == .X.", "refactor.rewrite.invertIf", edit=else_if)
154 + fmt.Println("B")
155 -- @greater_than/p.go --
156 @@ -54,3 +54 @@
157 - if len(os.Args) > 2 { //@codeaction("i", "refactor.rewrite.invertIf", edit=greater_than)
158 - fmt.Println("A")
159 - } else {
160 + if len(os.Args) <= 2 {
161 @@ -58 +56,2 @@
162 + } else { //@codeaction("i", "refactor.rewrite.invertIf", edit=greater_than)
163 + fmt.Println("A")
164 -- @not_boolean/p.go --
165 @@ -63,3 +63 @@
166 - if !b { //@codeaction("if !b", "refactor.rewrite.invertIf", edit=not_boolean)
167 - fmt.Println("A")
168 - } else {
169 + if b {
170 @@ -67 +65,2 @@
171 + } else { //@codeaction("if !b", "refactor.rewrite.invertIf", edit=not_boolean)
172 + fmt.Println("A")
173 -- @remove_else/p.go --
174 @@ -71,3 +71 @@
175 - if true { //@codeaction("if true", "refactor.rewrite.invertIf", edit=remove_else)
176 - fmt.Println("A")
177 - } else {
178 + if false {
179 @@ -78 +76,3 @@
180 + //@codeaction("if true", "refactor.rewrite.invertIf", edit=remove_else)
181 + fmt.Println("A")
182 +
183 -- @remove_parens/p.go --
184 @@ -83,3 +83 @@
185 - if !(b) { //@codeaction("if", "refactor.rewrite.invertIf", edit=remove_parens)
186 - fmt.Println("A")
187 - } else {
188 + if b {
189 @@ -87 +85,2 @@
190 + } else { //@codeaction("if", "refactor.rewrite.invertIf", edit=remove_parens)
191 + fmt.Println("A")
192 -- @semicolon/p.go --
193 @@ -91,3 +91 @@
194 - if _, err := fmt.Println("x"); err != nil { //@codeaction("if", "refactor.rewrite.invertIf", edit=semicolon)
195 - fmt.Println("A")
196 - } else {
197 + if _, err := fmt.Println("x"); err == nil {
198 @@ -95 +93,2 @@
199 + } else { //@codeaction("if", "refactor.rewrite.invertIf", edit=semicolon)
200 + fmt.Println("A")
201 -- @semicolon_and/p.go --
202 @@ -99,3 +99 @@
203 - if n, err := fmt.Println("x"); err != nil && n > 0 { //@codeaction("f", "refactor.rewrite.invertIf", edit=semicolon_and)
204 - fmt.Println("A")
205 - } else {
206 + if n, err := fmt.Println("x"); err == nil || n <= 0 {
207 @@ -103 +101,2 @@
208 + } else { //@codeaction("f", "refactor.rewrite.invertIf", edit=semicolon_and)
209 + fmt.Println("A")
210 -- @semicolon_or/p.go --
211 @@ -107,3 +107 @@
212 - if n, err := fmt.Println("x"); err != nil || n < 5 { //@codeaction(re"if n, err := fmt.Println..x..; err != nil .. n < 5", "refactor.rewrite.invertIf", edit=semicolon_or)
213 - fmt.Println("A")
214 - } else {
215 + if n, err := fmt.Println("x"); err == nil && n >= 5 {
216 @@ -111 +109,2 @@
217 + } else { //@codeaction(re"if n, err := fmt.Println..x..; err != nil .. n < 5", "refactor.rewrite.invertIf", edit=semicolon_or)
218 + fmt.Println("A")