Skip to content

Commit

Permalink
fix: create outputgen test
Browse files Browse the repository at this point in the history
  • Loading branch information
nedpals committed Feb 3, 2024
1 parent 91a6860 commit 6b8ad0f
Show file tree
Hide file tree
Showing 2 changed files with 156 additions and 6 deletions.
13 changes: 9 additions & 4 deletions output_gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,12 @@ func (gen *OutputGenerator) _break() {
gen.Builder.WriteByte('\n')
}

func (gen *OutputGenerator) ExpGen(level int, explain *ExplainGenerator) {
func (gen *OutputGenerator) FromExplanation(level int, explain *ExplainGenerator) {
if level == 1 && (explain.Builder == nil || explain.Builder.Len() == 0) && explain.Sections == nil {
gen.Writeln("No explanation found for this error.")
return
}

if explain.Builder != nil {
gen.Write(explain.Builder.String())
}
Expand All @@ -33,7 +38,7 @@ func (gen *OutputGenerator) ExpGen(level int, explain *ExplainGenerator) {
for sectionName, exp := range explain.Sections {
gen._break()
gen.Heading(level+1, sectionName)
gen.ExpGen(level+1, exp)
gen.FromExplanation(level+1, exp)
}
} else {
gen._break()
Expand Down Expand Up @@ -79,7 +84,7 @@ func (gen *OutputGenerator) Generate(explain *ExplainGenerator, bugFix *BugFixGe
gen.Heading(1, explain.ErrorName)
}

gen.ExpGen(1, explain)
gen.FromExplanation(1, explain)
if gen.GenAfterExplain != nil {
gen.GenAfterExplain(gen)
}
Expand Down Expand Up @@ -194,7 +199,7 @@ func (gen *OutputGenerator) Generate(explain *ExplainGenerator, bugFix *BugFixGe

}
} else {
gen.Writeln("Nothing to fix")
gen.Writeln("No bug fixes found for this error.")
}

return strings.TrimSpace(gen.Builder.String())
Expand Down
149 changes: 147 additions & 2 deletions output_gen_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,148 @@
package errgoengine
package errgoengine_test

// TODO:
import (
"strings"
"testing"

lib "github.com/nedpals/errgoengine"
sitter "github.com/smacker/go-tree-sitter"
)

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)
if err != nil {
t.Fatal(err)
}

gen := &lib.OutputGenerator{}

t.Run("Simple", 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,
},
})
})

// 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
xyz = "test"
` + "```" + ``

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

t.Run("Empty explanation", func(t *testing.T) {
defer gen.Reset()

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

// generate bug fix
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
No explanation found for this error.
## Steps to fix
### Define the variable ` + "`xyz`" + ` before using it.
In line 1, replace ` + "`xyz`" + ` with ` + "`\"test\"`" + `.
` + "```diff" + `
- a = xyz
+ a = "test"
b = 123
xyz = "test"
` + "```" + ``
if output != expected {
t.Errorf("exp %s, got %s", expected, output)
}
})

t.Run("Empty bug fixes", 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.")

// 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
No bug fixes found for this error.`
if output != expected {
t.Errorf("exp %s, got %s", expected, output)
}
})

t.Run("Empty explanation + bug fixes", func(t *testing.T) {
defer gen.Reset()

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

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

// check if the output is correct
expected := `# NameError
No explanation found for this error.
## Steps to fix
No bug fixes found for this error.`
if output != expected {
t.Errorf("exp %s, got %s", expected, output)
}
})
}

0 comments on commit 6b8ad0f

Please sign in to comment.