diff --git a/output_gen.go b/output_gen.go index caff2eb..f8bb421 100644 --- a/output_gen.go +++ b/output_gen.go @@ -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()) } @@ -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() @@ -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) } @@ -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()) diff --git a/output_gen_test.go b/output_gen_test.go index 19c2721..94bf30d 100644 --- a/output_gen_test.go +++ b/output_gen_test.go @@ -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) + } + }) +}