From 0a775d0fc166cb9d371ac4d78a8d3b97a56e9af6 Mon Sep 17 00:00:00 2001 From: Ned Palacios Date: Sat, 3 Feb 2024 23:48:16 +0800 Subject: [PATCH] feat: add new tests, update output_gen_test.go --- output_gen_test.go | 247 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 244 insertions(+), 3 deletions(-) diff --git a/output_gen_test.go b/output_gen_test.go index 94bf30d..49374f9 100644 --- a/output_gen_test.go +++ b/output_gen_test.go @@ -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) } @@ -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 { @@ -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)