Skip to content

Commit

Permalink
refactor: unify test language for tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nedpals committed Jan 29, 2024
1 parent 5328aca commit b87fb9e
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 85 deletions.
43 changes: 5 additions & 38 deletions error_template_test.go
Original file line number Diff line number Diff line change
@@ -1,51 +1,18 @@
package errgoengine_test

import (
"context"
"testing"

lib "github.com/nedpals/errgoengine"
testutils "github.com/nedpals/errgoengine/test_utils"
)

type TestAnalyzer struct{}

func (TestAnalyzer) FallbackSymbol() lib.Symbol {
return nil
}

func (TestAnalyzer) FindSymbol(string) lib.Symbol { return nil }

func (TestAnalyzer) AnalyzeNode(context.Context, lib.SyntaxNode) lib.Symbol {
return nil
}

func (TestAnalyzer) AnalyzeImport(lib.ImportParams) lib.ResolvedImport {
return lib.ResolvedImport{}
}

var testLanguage = &lib.Language{
Name: "TestLang",
FilePatterns: []string{".test"},
StackTracePattern: `\sin (?P<symbol>\S+) at (?P<path>\S+):(?P<position>\d+)`,
LocationConverter: func(ctx lib.LocationConverterContext) lib.Location {
return lib.Location{
DocumentPath: ctx.Path,
StartPos: lib.Position{0, 0, 0},
EndPos: lib.Position{0, 0, 0},
}
},
AnalyzerFactory: func(cd *lib.ContextData) lib.LanguageAnalyzer {
return TestAnalyzer{}
},
}

func emptyExplainFn(cd *lib.ContextData, gen *lib.ExplainGenerator) {}
func emptyBugFixFn(cd *lib.ContextData, gen *lib.BugFixGenerator) {}

func setupTemplate(template lib.ErrorTemplate) (*lib.CompiledErrorTemplate, error) {
errorTemplates := lib.ErrorTemplates{}
return errorTemplates.Add(testLanguage, template)
return errorTemplates.Add(lib.TestLanguage, template)
}

func TestErrorTemplate(t *testing.T) {
Expand All @@ -62,7 +29,7 @@ func TestErrorTemplate(t *testing.T) {
}

testutils.Equals(t, tmp.Name, "SampleError")
testutils.Equals(t, tmp.Language, testLanguage)
testutils.Equals(t, tmp.Language, lib.TestLanguage)
testutils.Equals(t, tmp.Pattern.String(), `(?m)^This is a sample error(?P<stacktrace>(?:.|\s)*)$`)
testutils.Equals(t, tmp.StackTraceRegex().String(), `(?m)\sin (?P<symbol>\S+) at (?P<path>\S+):(?P<position>\d+)`)
testutils.ExpectNil(t, tmp.StackTracePattern)
Expand All @@ -82,7 +49,7 @@ func TestErrorTemplate(t *testing.T) {
}

testutils.Equals(t, tmp.Name, "SampleError2")
testutils.Equals(t, tmp.Language, testLanguage)
testutils.Equals(t, tmp.Language, lib.TestLanguage)
testutils.Equals(t, tmp.Pattern.String(), `(?m)^This is a sample error with stack trace(?P<stacktrace>(?:.|\s)*)$`)
testutils.Equals(t, tmp.StackTraceRegex().String(), `(?P<symbol>\S+):(?P<path>\S+):(?P<position>\d+)`)
})
Expand All @@ -100,7 +67,7 @@ func TestErrorTemplate(t *testing.T) {
}

testutils.Equals(t, tmp.Name, "SampleError3")
testutils.Equals(t, tmp.Language, testLanguage)
testutils.Equals(t, tmp.Language, lib.TestLanguage)
testutils.Equals(t, tmp.Pattern.String(), `(?m)^Stack trace in middle (?P<stacktrace>(?:.|\s)*)test$`)
testutils.ExpectNil(t, tmp.StackTracePattern)
})
Expand All @@ -118,7 +85,7 @@ func TestStackTraceRegex(t *testing.T) {
t.Fatal(err)
}

testutils.Equals(t, tmp.StackTraceRegex().String(), "(?m)"+testLanguage.StackTracePattern)
testutils.Equals(t, tmp.StackTraceRegex().String(), "(?m)"+lib.TestLanguage.StackTracePattern)
})

t.Run("With custom stack trace", func(t *testing.T) {
Expand Down
50 changes: 3 additions & 47 deletions source_test.go
Original file line number Diff line number Diff line change
@@ -1,60 +1,16 @@
package errgoengine

import (
"context"
"strings"
"testing"

sitter "github.com/smacker/go-tree-sitter"
"github.com/smacker/go-tree-sitter/python"
)

var testLanguage = &Language{
Name: "Python",
FilePatterns: []string{".py"},
SitterLanguage: python.GetLanguage(),
StackTracePattern: `\s+File "(?P<path>\S+)", line (?P<position>\d+), in (?P<symbol>\S+)`,
ErrorPattern: `Traceback \(most recent call last\):$stacktrace$message`,
AnalyzerFactory: func(cd *ContextData) LanguageAnalyzer {
return &testAnalyzer{cd}
},
SymbolsToCapture: `
(expression_statement
(assignment
left: (identifier) @assignment.name
right: (identifier) @assignment.content) @assignment)
`,
}

type testAnalyzer struct {
*ContextData
}

func (an *testAnalyzer) FallbackSymbol() Symbol {
return Builtin("any")
}

func (an *testAnalyzer) FindSymbol(name string) Symbol {
return nil
}

func (an *testAnalyzer) AnalyzeNode(_ context.Context, n SyntaxNode) Symbol {
// TODO:
return Builtin("void")
}

func (an *testAnalyzer) AnalyzeImport(params ImportParams) ResolvedImport {
// TODO:

return ResolvedImport{
Path: "",
}
}

func TestParseDocument(t *testing.T) {
parser := sitter.NewParser()

doc, err := ParseDocument("test", strings.NewReader(`hello = 1`), parser, testLanguage, nil)
doc, err := ParseDocument("test", strings.NewReader(`hello = 1`), parser, TestLanguage, nil)
if err != nil {
t.Error(err)
}
Expand All @@ -67,7 +23,7 @@ func TestParseDocument(t *testing.T) {
func TestEditableDocument(t *testing.T) {
parser := sitter.NewParser()

doc, err := ParseDocument("test", strings.NewReader(`hello = 1`), parser, testLanguage, nil)
doc, err := ParseDocument("test", strings.NewReader(`hello = 1`), parser, TestLanguage, nil)
if err != nil {
t.Error(err)
} else if doc.TotalLines() < 1 {
Expand Down Expand Up @@ -371,7 +327,7 @@ func TestEditableDocument(t *testing.T) {
})

t.Run("EditableDocument.ReplaceWithPadding", func(t *testing.T) {
doc, err := ParseDocument("test", strings.NewReader(` hello = 1`), parser, testLanguage, nil)
doc, err := ParseDocument("test", strings.NewReader(` hello = 1`), parser, TestLanguage, nil)
if err != nil {
t.Error(err)
} else if doc.TotalLines() < 1 {
Expand Down
52 changes: 52 additions & 0 deletions testlang.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package errgoengine

import (
"context"
)

var TestLanguage = &Language{
Name: "TestLang",
FilePatterns: []string{".test"},
StackTracePattern: `\sin (?P<symbol>\S+) at (?P<path>\S+):(?P<position>\d+)`,
LocationConverter: func(ctx LocationConverterContext) Location {
return Location{
DocumentPath: ctx.Path,
StartPos: Position{Line: 0, Column: 0, Index: 0},
EndPos: Position{Line: 0, Column: 0, Index: 0},
}
},
AnalyzerFactory: func(cd *ContextData) LanguageAnalyzer {
return &testAnalyzer{}
},
SymbolsToCapture: `
(expression_statement
(assignment
left: (identifier) @assignment.name
right: (identifier) @assignment.content) @assignment)
`,
}

type testAnalyzer struct {
*ContextData
}

func (an *testAnalyzer) FallbackSymbol() Symbol {
return Builtin("any")
}

func (an *testAnalyzer) FindSymbol(name string) Symbol {
return nil
}

func (an *testAnalyzer) AnalyzeNode(_ context.Context, n SyntaxNode) Symbol {
// TODO:
return Builtin("void")
}

func (an *testAnalyzer) AnalyzeImport(params ImportParams) ResolvedImport {
// TODO:

return ResolvedImport{
Path: "",
}
}

0 comments on commit b87fb9e

Please sign in to comment.