Skip to content

Commit

Permalink
feat: add tests for explaingen and bugfix gen
Browse files Browse the repository at this point in the history
  • Loading branch information
nedpals committed Feb 3, 2024
1 parent de60a4e commit 240d01c
Show file tree
Hide file tree
Showing 4 changed files with 607 additions and 34 deletions.
4 changes: 2 additions & 2 deletions errgoengine.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func (e *ErrgoEngine) Analyze(workingPath, msg string) (*CompiledErrorTemplate,
}

func (e *ErrgoEngine) Translate(template *CompiledErrorTemplate, contextData *ContextData) (mainExp string, fullExp string) {
expGen := &ExplainGenerator{errorName: template.Name}
expGen := &ExplainGenerator{ErrorName: template.Name}
fixGen := &BugFixGenerator{}
if contextData.MainError != nil {
fixGen.Document = contextData.MainError.Document
Expand All @@ -128,7 +128,7 @@ func (e *ErrgoEngine) Translate(template *CompiledErrorTemplate, contextData *Co
output := e.OutputGen.Generate(contextData, expGen, fixGen)
defer e.OutputGen.Reset()

return expGen.mainExp.String(), output
return expGen.Builder.String(), output
}

func ParseFromStackTrace(contextData *ContextData, defaultLanguage *Language, files fs.ReadFileFS) error {
Expand Down
12 changes: 6 additions & 6 deletions output_gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ func (gen *OutputGenerator) _break() {
}

func (gen *OutputGenerator) generateFromExp(level int, explain *ExplainGenerator) {
if explain.mainExp != nil {
gen.write(explain.mainExp.String())
if explain.Builder != nil {
gen.write(explain.Builder.String())
}

if explain.sections != nil {
for sectionName, exp := range explain.sections {
if explain.Sections != nil {
for sectionName, exp := range explain.Sections {
gen._break()
gen.heading(level+1, sectionName)
gen.generateFromExp(level+1, exp)
Expand Down Expand Up @@ -75,8 +75,8 @@ func (gen *OutputGenerator) Generate(cd *ContextData, explain *ExplainGenerator,
gen.wr = &strings.Builder{}
}

if len(explain.errorName) != 0 {
gen.heading(1, explain.errorName)
if len(explain.ErrorName) != 0 {
gen.heading(1, explain.ErrorName)
}

gen.generateFromExp(1, explain)
Expand Down
80 changes: 54 additions & 26 deletions translation.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,45 +9,52 @@ import (
type GenExplainFn func(*ContextData, *ExplainGenerator)

type ExplainGenerator struct {
errorName string
mainExp *strings.Builder
sections map[string]*ExplainGenerator
ErrorName string
Builder *strings.Builder
Sections map[string]*ExplainGenerator
}

func (gen *ExplainGenerator) Add(text string, data ...any) {
if gen.mainExp == nil {
gen.mainExp = &strings.Builder{}
if gen.Builder == nil {
gen.Builder = &strings.Builder{}
}

if len(data) != 0 {
gen.mainExp.WriteString(fmt.Sprintf(text, data...))
gen.Builder.WriteString(fmt.Sprintf(text, data...))
} else {
gen.mainExp.WriteString(text)
gen.Builder.WriteString(text)
}
}

func (gen *ExplainGenerator) CreateSection(name string) *ExplainGenerator {
if gen.sections == nil {
gen.sections = map[string]*ExplainGenerator{}
if len(name) == 0 {
return nil
}
_, ok := gen.sections[name]

if gen.Sections == nil {
gen.Sections = map[string]*ExplainGenerator{}
}
_, ok := gen.Sections[name]
if !ok {
gen.sections[name] = &ExplainGenerator{}
gen.Sections[name] = &ExplainGenerator{}
}
return gen.sections[name]
return gen.Sections[name]
}

type GenBugFixFn func(*ContextData, *BugFixGenerator)

type BugFixSuggestion struct {
Title string
Description string
Steps []*BugFixStep
diffPosition Position
Doc *EditableDocument
}

func (gen *BugFixSuggestion) addStep(doc *EditableDocument, content string, d ...any) *BugFixStep {
func (gen *BugFixSuggestion) addStep(isCopyable bool, content string, d ...any) (*BugFixStep, error) {
if len(content) == 0 {
return nil, fmt.Errorf("content cannot be empty")
}

if gen.Steps == nil {
gen.Steps = []*BugFixStep{}
}
Expand All @@ -56,23 +63,34 @@ func (gen *BugFixSuggestion) addStep(doc *EditableDocument, content string, d ..
content += "."
}

doc := gen.Doc
if isCopyable {
if len(gen.Steps) == 0 {
doc = gen.Doc.Copy()
} else {
// use the last document as the base
doc = gen.Steps[len(gen.Steps)-1].Doc.Copy()
}
}

gen.Steps = append(gen.Steps, &BugFixStep{
suggestion: gen,
Content: fmt.Sprintf(content, d...),
Doc: gen.Doc,
Doc: doc,
isCopyable: isCopyable,
})

return gen.Steps[len(gen.Steps)-1]
return gen.Steps[len(gen.Steps)-1], nil
}

func (gen *BugFixSuggestion) AddStep(content string, d ...any) *BugFixStep {
s := gen.addStep(gen.Doc.Copy(), content, d...)
s.isCopyable = true
return s
}

func (gen *BugFixSuggestion) AddDescription(exp string, d ...any) {
gen.Description = fmt.Sprintf(exp, d...)
step, err := gen.addStep(true, content, d...)
// we cannot panic here because we need to return the step
// to get the error, use recover() to catch the panic
if err != nil {
panic(err)
}
return step
}

type BugFixStep struct {
Expand Down Expand Up @@ -174,16 +192,26 @@ type BugFixGenerator struct {
Suggestions []*BugFixSuggestion
}

func (gen *BugFixGenerator) Add(title string, makerFn func(s *BugFixSuggestion)) {
func (gen *BugFixGenerator) Add(title string, makerFn func(s *BugFixSuggestion)) error {
if len(title) == 0 {
return fmt.Errorf("title cannot be empty")
}

if makerFn == nil {
return fmt.Errorf("maker function cannot be nil")
}

if gen.Suggestions == nil {
gen.Suggestions = []*BugFixSuggestion{}
}

suggestion := &BugFixSuggestion{
Title: title,
Doc: gen.Document.Editable(),
// Copy the document to avoid modifying the original document
Doc: gen.Document.Editable(),
}
makerFn(suggestion)

makerFn(suggestion)
gen.Suggestions = append(gen.Suggestions, suggestion)
return nil
}
Loading

0 comments on commit 240d01c

Please sign in to comment.