Skip to content

Commit

Permalink
feat: add new tests, update output_gen_test.go
Browse files Browse the repository at this point in the history
  • Loading branch information
nedpals committed Feb 3, 2024
1 parent 2895e85 commit 0a775d0
Showing 1 changed file with 244 additions and 3 deletions.
247 changes: 244 additions & 3 deletions output_gen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

func TestOutputGenerator(t *testing.T) {
parser := sitter.NewParser()
doc, err := lib.ParseDocument("program.test", strings.NewReader("a = xyz\nb = 123\nxyz = \"test\""), parser, lib.TestLanguage, nil)
doc, err := lib.ParseDocument("program.test", strings.NewReader("a = xyz\nb = 123"), parser, lib.TestLanguage, nil)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -55,7 +55,249 @@ In line 1, replace ` + "`xyz`" + ` with ` + "`\"test\"`" + `.
- a = xyz
+ a = "test"
b = 123
xyz = "test"
` + "```" + ``

if output != expected {
t.Errorf("exp %s, got %s", expected, output)
}
})

t.Run("With sections", func(t *testing.T) {
defer gen.Reset()

bugFix := lib.NewBugFixGenerator(doc)
explain := lib.NewExplainGeneratorForError("NameError")

// create a fake name error explanation
explain.Add("The variable you are trying to use is not defined. In this case, the variable `xyz` is not defined.")

// add a section
explain.CreateSection("More info").
Add("This error is usually caused by a typo or a missing variable definition.")

// create a fake bug fix suggestion
bugFix.Add("Define the variable `xyz` before using it.", func(s *lib.BugFixSuggestion) {
s.AddStep("In line 1, replace `xyz` with `\"test\"`.").
AddFix(lib.FixSuggestion{
NewText: "\"test\"",
StartPosition: lib.Position{
Line: 0,
Column: 4,
},
EndPosition: lib.Position{
Line: 0,
Column: 7,
},
})
})

// generate the output
output := gen.Generate(explain, bugFix)

// check if the output is correct
expected := `# NameError
The variable you are trying to use is not defined. In this case, the variable ` + "`xyz`" + ` is not defined.
## More info
This error is usually caused by a typo or a missing variable definition.
## Steps to fix
### Define the variable ` + "`xyz`" + ` before using it.
In line 1, replace ` + "`xyz`" + ` with ` + "`\"test\"`" + `.
` + "```diff" + `
- a = xyz
+ a = "test"
b = 123
` + "```" + ``
if output != expected {
t.Errorf("exp %s, got %s", expected, output)
}
})

t.Run("With multiple suggestions", func(t *testing.T) {
defer gen.Reset()

bugFix := lib.NewBugFixGenerator(doc)
explain := lib.NewExplainGeneratorForError("NameError")

// create a fake name error explanation
explain.Add("The variable you are trying to use is not defined. In this case, the variable `xyz` is not defined.")

// create a fake bug fix suggestion
bugFix.Add("Define the variable `xyz` before using it.", func(s *lib.BugFixSuggestion) {
s.AddStep("In line 1, replace `xyz` with `\"test\"`.").
AddFix(lib.FixSuggestion{
NewText: "\"test\"",
StartPosition: lib.Position{
Line: 0,
Column: 4,
},
EndPosition: lib.Position{
Line: 0,
Column: 7,
},
})
})

bugFix.Add("Define the variable `xyz` before using it.", func(s *lib.BugFixSuggestion) {
s.AddStep("In line 1, declare a new variable named `xyz`").
AddFix(lib.FixSuggestion{
NewText: "xyz = \"test\"\n",
StartPosition: lib.Position{
Line: 0,
Column: 0,
},
EndPosition: lib.Position{
Line: 0,
Column: 0,
},
})
})

// generate the output
output := gen.Generate(explain, bugFix)

// check if the output is correct
expected := `# NameError
The variable you are trying to use is not defined. In this case, the variable ` + "`xyz`" + ` is not defined.
## Steps to fix
### 1. Define the variable ` + "`xyz`" + ` before using it.
In line 1, replace ` + "`xyz`" + ` with ` + "`\"test\"`" + `.
` + "```diff" + `
- a = xyz
+ a = "test"
b = 123
` + "```" + `
### 2. Define the variable ` + "`xyz`" + ` before using it.
In line 1, declare a new variable named ` + "`xyz`" + `.
` + "```diff" + `
- a = xyz
+ xyz = "test"
+ a = xyz
b = 123
` + "```" + ``

if output != expected {
t.Errorf("exp %s, got %s", expected, output)
}
})

t.Run("With fix description", func(t *testing.T) {
defer gen.Reset()

bugFix := lib.NewBugFixGenerator(doc)
explain := lib.NewExplainGeneratorForError("NameError")

// create a fake name error explanation
explain.Add("The variable you are trying to use is not defined. In this case, the variable `xyz` is not defined.")

// create a fake bug fix suggestion
bugFix.Add("Define the variable `xyz` before using it.", func(s *lib.BugFixSuggestion) {
s.AddStep("In line 1, replace `xyz` with `\"test\"`.").
AddFix(lib.FixSuggestion{
Description: "This is a test description.",
NewText: "\"test\"",
StartPosition: lib.Position{
Line: 0,
Column: 4,
},
EndPosition: lib.Position{
Line: 0,
Column: 7,
},
})
})

// generate the output
output := gen.Generate(explain, bugFix)

// check if the output is correct
expected := `# NameError
The variable you are trying to use is not defined. In this case, the variable ` + "`xyz`" + ` is not defined.
## Steps to fix
### Define the variable ` + "`xyz`" + ` before using it.
In line 1, replace ` + "`xyz`" + ` with ` + "`\"test\"`" + `.
` + "```diff" + `
- a = xyz
+ a = "test"
b = 123
` + "```" + `
This is a test description.`

if output != expected {
t.Errorf("exp %s, got %s", expected, output)
}
})

t.Run("With GenAfterExplain", func(t *testing.T) {
defer gen.Reset()

bugFix := lib.NewBugFixGenerator(doc)
explain := lib.NewExplainGeneratorForError("NameError")

// create a fake name error explanation
explain.Add("The variable you are trying to use is not defined. In this case, the variable `xyz` is not defined.")

// create a fake bug fix suggestion
bugFix.Add("Define the variable `xyz` before using it.", func(s *lib.BugFixSuggestion) {
s.AddStep("In line 1, replace `xyz` with `\"test\"`.").
AddFix(lib.FixSuggestion{
NewText: "\"test\"",
StartPosition: lib.Position{
Line: 0,
Column: 4,
},
EndPosition: lib.Position{
Line: 0,
Column: 7,
},
})
})

// add a code snippet that points to the error
gen.GenAfterExplain = func(gen *lib.OutputGenerator) {
startLineNr := 0
startLines := doc.LinesAt(startLineNr, startLineNr+1)
endLines := doc.LinesAt(startLineNr+1, startLineNr+2)

gen.Writeln("```")
gen.WriteLines(startLines...)

for i := 0; i < 4; i++ {
if startLines[len(startLines)-1][i] == '\t' {
gen.Builder.WriteString(" ")
} else {
gen.Builder.WriteByte(' ')
}
}

for i := 0; i < 3; i++ {
gen.Builder.WriteByte('^')
}

gen.Break()
gen.WriteLines(endLines...)
gen.Writeln("```")
}

// generate the output
output := gen.Generate(explain, bugFix)

// check if the output is correct
expected := `# NameError
The variable you are trying to use is not defined. In this case, the variable ` + "`xyz`" + ` is not defined.
` + "```" + `
a = xyz
b = 123
^^^
b = 123
` + "```" + `
## Steps to fix
### Define the variable ` + "`xyz`" + ` before using it.
In line 1, replace ` + "`xyz`" + ` with ` + "`\"test\"`" + `.
` + "```diff" + `
- a = xyz
+ a = "test"
b = 123
` + "```" + ``

if output != expected {
Expand Down Expand Up @@ -98,7 +340,6 @@ In line 1, replace ` + "`xyz`" + ` with ` + "`\"test\"`" + `.
- a = xyz
+ a = "test"
b = 123
xyz = "test"
` + "```" + ``
if output != expected {
t.Errorf("exp %s, got %s", expected, output)
Expand Down

0 comments on commit 0a775d0

Please sign in to comment.