Skip to content

Commit 0a775d0

Browse files
committed
feat: add new tests, update output_gen_test.go
1 parent 2895e85 commit 0a775d0

File tree

1 file changed

+244
-3
lines changed

1 file changed

+244
-3
lines changed

output_gen_test.go

+244-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010

1111
func TestOutputGenerator(t *testing.T) {
1212
parser := sitter.NewParser()
13-
doc, err := lib.ParseDocument("program.test", strings.NewReader("a = xyz\nb = 123\nxyz = \"test\""), parser, lib.TestLanguage, nil)
13+
doc, err := lib.ParseDocument("program.test", strings.NewReader("a = xyz\nb = 123"), parser, lib.TestLanguage, nil)
1414
if err != nil {
1515
t.Fatal(err)
1616
}
@@ -55,7 +55,249 @@ In line 1, replace ` + "`xyz`" + ` with ` + "`\"test\"`" + `.
5555
- a = xyz
5656
+ a = "test"
5757
b = 123
58-
xyz = "test"
58+
` + "```" + ``
59+
60+
if output != expected {
61+
t.Errorf("exp %s, got %s", expected, output)
62+
}
63+
})
64+
65+
t.Run("With sections", func(t *testing.T) {
66+
defer gen.Reset()
67+
68+
bugFix := lib.NewBugFixGenerator(doc)
69+
explain := lib.NewExplainGeneratorForError("NameError")
70+
71+
// create a fake name error explanation
72+
explain.Add("The variable you are trying to use is not defined. In this case, the variable `xyz` is not defined.")
73+
74+
// add a section
75+
explain.CreateSection("More info").
76+
Add("This error is usually caused by a typo or a missing variable definition.")
77+
78+
// create a fake bug fix suggestion
79+
bugFix.Add("Define the variable `xyz` before using it.", func(s *lib.BugFixSuggestion) {
80+
s.AddStep("In line 1, replace `xyz` with `\"test\"`.").
81+
AddFix(lib.FixSuggestion{
82+
NewText: "\"test\"",
83+
StartPosition: lib.Position{
84+
Line: 0,
85+
Column: 4,
86+
},
87+
EndPosition: lib.Position{
88+
Line: 0,
89+
Column: 7,
90+
},
91+
})
92+
})
93+
94+
// generate the output
95+
output := gen.Generate(explain, bugFix)
96+
97+
// check if the output is correct
98+
expected := `# NameError
99+
The variable you are trying to use is not defined. In this case, the variable ` + "`xyz`" + ` is not defined.
100+
## More info
101+
This error is usually caused by a typo or a missing variable definition.
102+
## Steps to fix
103+
### Define the variable ` + "`xyz`" + ` before using it.
104+
In line 1, replace ` + "`xyz`" + ` with ` + "`\"test\"`" + `.
105+
` + "```diff" + `
106+
- a = xyz
107+
+ a = "test"
108+
b = 123
109+
` + "```" + ``
110+
if output != expected {
111+
t.Errorf("exp %s, got %s", expected, output)
112+
}
113+
})
114+
115+
t.Run("With multiple suggestions", func(t *testing.T) {
116+
defer gen.Reset()
117+
118+
bugFix := lib.NewBugFixGenerator(doc)
119+
explain := lib.NewExplainGeneratorForError("NameError")
120+
121+
// create a fake name error explanation
122+
explain.Add("The variable you are trying to use is not defined. In this case, the variable `xyz` is not defined.")
123+
124+
// create a fake bug fix suggestion
125+
bugFix.Add("Define the variable `xyz` before using it.", func(s *lib.BugFixSuggestion) {
126+
s.AddStep("In line 1, replace `xyz` with `\"test\"`.").
127+
AddFix(lib.FixSuggestion{
128+
NewText: "\"test\"",
129+
StartPosition: lib.Position{
130+
Line: 0,
131+
Column: 4,
132+
},
133+
EndPosition: lib.Position{
134+
Line: 0,
135+
Column: 7,
136+
},
137+
})
138+
})
139+
140+
bugFix.Add("Define the variable `xyz` before using it.", func(s *lib.BugFixSuggestion) {
141+
s.AddStep("In line 1, declare a new variable named `xyz`").
142+
AddFix(lib.FixSuggestion{
143+
NewText: "xyz = \"test\"\n",
144+
StartPosition: lib.Position{
145+
Line: 0,
146+
Column: 0,
147+
},
148+
EndPosition: lib.Position{
149+
Line: 0,
150+
Column: 0,
151+
},
152+
})
153+
})
154+
155+
// generate the output
156+
output := gen.Generate(explain, bugFix)
157+
158+
// check if the output is correct
159+
expected := `# NameError
160+
The variable you are trying to use is not defined. In this case, the variable ` + "`xyz`" + ` is not defined.
161+
## Steps to fix
162+
### 1. Define the variable ` + "`xyz`" + ` before using it.
163+
In line 1, replace ` + "`xyz`" + ` with ` + "`\"test\"`" + `.
164+
` + "```diff" + `
165+
- a = xyz
166+
+ a = "test"
167+
b = 123
168+
` + "```" + `
169+
170+
### 2. Define the variable ` + "`xyz`" + ` before using it.
171+
In line 1, declare a new variable named ` + "`xyz`" + `.
172+
` + "```diff" + `
173+
- a = xyz
174+
+ xyz = "test"
175+
+ a = xyz
176+
b = 123
177+
` + "```" + ``
178+
179+
if output != expected {
180+
t.Errorf("exp %s, got %s", expected, output)
181+
}
182+
})
183+
184+
t.Run("With fix description", func(t *testing.T) {
185+
defer gen.Reset()
186+
187+
bugFix := lib.NewBugFixGenerator(doc)
188+
explain := lib.NewExplainGeneratorForError("NameError")
189+
190+
// create a fake name error explanation
191+
explain.Add("The variable you are trying to use is not defined. In this case, the variable `xyz` is not defined.")
192+
193+
// create a fake bug fix suggestion
194+
bugFix.Add("Define the variable `xyz` before using it.", func(s *lib.BugFixSuggestion) {
195+
s.AddStep("In line 1, replace `xyz` with `\"test\"`.").
196+
AddFix(lib.FixSuggestion{
197+
Description: "This is a test description.",
198+
NewText: "\"test\"",
199+
StartPosition: lib.Position{
200+
Line: 0,
201+
Column: 4,
202+
},
203+
EndPosition: lib.Position{
204+
Line: 0,
205+
Column: 7,
206+
},
207+
})
208+
})
209+
210+
// generate the output
211+
output := gen.Generate(explain, bugFix)
212+
213+
// check if the output is correct
214+
expected := `# NameError
215+
The variable you are trying to use is not defined. In this case, the variable ` + "`xyz`" + ` is not defined.
216+
## Steps to fix
217+
### Define the variable ` + "`xyz`" + ` before using it.
218+
In line 1, replace ` + "`xyz`" + ` with ` + "`\"test\"`" + `.
219+
` + "```diff" + `
220+
- a = xyz
221+
+ a = "test"
222+
b = 123
223+
` + "```" + `
224+
This is a test description.`
225+
226+
if output != expected {
227+
t.Errorf("exp %s, got %s", expected, output)
228+
}
229+
})
230+
231+
t.Run("With GenAfterExplain", func(t *testing.T) {
232+
defer gen.Reset()
233+
234+
bugFix := lib.NewBugFixGenerator(doc)
235+
explain := lib.NewExplainGeneratorForError("NameError")
236+
237+
// create a fake name error explanation
238+
explain.Add("The variable you are trying to use is not defined. In this case, the variable `xyz` is not defined.")
239+
240+
// create a fake bug fix suggestion
241+
bugFix.Add("Define the variable `xyz` before using it.", func(s *lib.BugFixSuggestion) {
242+
s.AddStep("In line 1, replace `xyz` with `\"test\"`.").
243+
AddFix(lib.FixSuggestion{
244+
NewText: "\"test\"",
245+
StartPosition: lib.Position{
246+
Line: 0,
247+
Column: 4,
248+
},
249+
EndPosition: lib.Position{
250+
Line: 0,
251+
Column: 7,
252+
},
253+
})
254+
})
255+
256+
// add a code snippet that points to the error
257+
gen.GenAfterExplain = func(gen *lib.OutputGenerator) {
258+
startLineNr := 0
259+
startLines := doc.LinesAt(startLineNr, startLineNr+1)
260+
endLines := doc.LinesAt(startLineNr+1, startLineNr+2)
261+
262+
gen.Writeln("```")
263+
gen.WriteLines(startLines...)
264+
265+
for i := 0; i < 4; i++ {
266+
if startLines[len(startLines)-1][i] == '\t' {
267+
gen.Builder.WriteString(" ")
268+
} else {
269+
gen.Builder.WriteByte(' ')
270+
}
271+
}
272+
273+
for i := 0; i < 3; i++ {
274+
gen.Builder.WriteByte('^')
275+
}
276+
277+
gen.Break()
278+
gen.WriteLines(endLines...)
279+
gen.Writeln("```")
280+
}
281+
282+
// generate the output
283+
output := gen.Generate(explain, bugFix)
284+
285+
// check if the output is correct
286+
expected := `# NameError
287+
The variable you are trying to use is not defined. In this case, the variable ` + "`xyz`" + ` is not defined.
288+
` + "```" + `
289+
a = xyz
290+
b = 123
291+
^^^
292+
b = 123
293+
` + "```" + `
294+
## Steps to fix
295+
### Define the variable ` + "`xyz`" + ` before using it.
296+
In line 1, replace ` + "`xyz`" + ` with ` + "`\"test\"`" + `.
297+
` + "```diff" + `
298+
- a = xyz
299+
+ a = "test"
300+
b = 123
59301
` + "```" + ``
60302

61303
if output != expected {
@@ -98,7 +340,6 @@ In line 1, replace ` + "`xyz`" + ` with ` + "`\"test\"`" + `.
98340
- a = xyz
99341
+ a = "test"
100342
b = 123
101-
xyz = "test"
102343
` + "```" + ``
103344
if output != expected {
104345
t.Errorf("exp %s, got %s", expected, output)

0 commit comments

Comments
 (0)