Skip to content

Commit b87fb9e

Browse files
committed
refactor: unify test language for tests
1 parent 5328aca commit b87fb9e

File tree

3 files changed

+60
-85
lines changed

3 files changed

+60
-85
lines changed

error_template_test.go

+5-38
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,18 @@
11
package errgoengine_test
22

33
import (
4-
"context"
54
"testing"
65

76
lib "github.com/nedpals/errgoengine"
87
testutils "github.com/nedpals/errgoengine/test_utils"
98
)
109

11-
type TestAnalyzer struct{}
12-
13-
func (TestAnalyzer) FallbackSymbol() lib.Symbol {
14-
return nil
15-
}
16-
17-
func (TestAnalyzer) FindSymbol(string) lib.Symbol { return nil }
18-
19-
func (TestAnalyzer) AnalyzeNode(context.Context, lib.SyntaxNode) lib.Symbol {
20-
return nil
21-
}
22-
23-
func (TestAnalyzer) AnalyzeImport(lib.ImportParams) lib.ResolvedImport {
24-
return lib.ResolvedImport{}
25-
}
26-
27-
var testLanguage = &lib.Language{
28-
Name: "TestLang",
29-
FilePatterns: []string{".test"},
30-
StackTracePattern: `\sin (?P<symbol>\S+) at (?P<path>\S+):(?P<position>\d+)`,
31-
LocationConverter: func(ctx lib.LocationConverterContext) lib.Location {
32-
return lib.Location{
33-
DocumentPath: ctx.Path,
34-
StartPos: lib.Position{0, 0, 0},
35-
EndPos: lib.Position{0, 0, 0},
36-
}
37-
},
38-
AnalyzerFactory: func(cd *lib.ContextData) lib.LanguageAnalyzer {
39-
return TestAnalyzer{}
40-
},
41-
}
42-
4310
func emptyExplainFn(cd *lib.ContextData, gen *lib.ExplainGenerator) {}
4411
func emptyBugFixFn(cd *lib.ContextData, gen *lib.BugFixGenerator) {}
4512

4613
func setupTemplate(template lib.ErrorTemplate) (*lib.CompiledErrorTemplate, error) {
4714
errorTemplates := lib.ErrorTemplates{}
48-
return errorTemplates.Add(testLanguage, template)
15+
return errorTemplates.Add(lib.TestLanguage, template)
4916
}
5017

5118
func TestErrorTemplate(t *testing.T) {
@@ -62,7 +29,7 @@ func TestErrorTemplate(t *testing.T) {
6229
}
6330

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

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

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

121-
testutils.Equals(t, tmp.StackTraceRegex().String(), "(?m)"+testLanguage.StackTracePattern)
88+
testutils.Equals(t, tmp.StackTraceRegex().String(), "(?m)"+lib.TestLanguage.StackTracePattern)
12289
})
12390

12491
t.Run("With custom stack trace", func(t *testing.T) {

source_test.go

+3-47
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,16 @@
11
package errgoengine
22

33
import (
4-
"context"
54
"strings"
65
"testing"
76

87
sitter "github.com/smacker/go-tree-sitter"
9-
"github.com/smacker/go-tree-sitter/python"
108
)
119

12-
var testLanguage = &Language{
13-
Name: "Python",
14-
FilePatterns: []string{".py"},
15-
SitterLanguage: python.GetLanguage(),
16-
StackTracePattern: `\s+File "(?P<path>\S+)", line (?P<position>\d+), in (?P<symbol>\S+)`,
17-
ErrorPattern: `Traceback \(most recent call last\):$stacktrace$message`,
18-
AnalyzerFactory: func(cd *ContextData) LanguageAnalyzer {
19-
return &testAnalyzer{cd}
20-
},
21-
SymbolsToCapture: `
22-
(expression_statement
23-
(assignment
24-
left: (identifier) @assignment.name
25-
right: (identifier) @assignment.content) @assignment)
26-
`,
27-
}
28-
29-
type testAnalyzer struct {
30-
*ContextData
31-
}
32-
33-
func (an *testAnalyzer) FallbackSymbol() Symbol {
34-
return Builtin("any")
35-
}
36-
37-
func (an *testAnalyzer) FindSymbol(name string) Symbol {
38-
return nil
39-
}
40-
41-
func (an *testAnalyzer) AnalyzeNode(_ context.Context, n SyntaxNode) Symbol {
42-
// TODO:
43-
return Builtin("void")
44-
}
45-
46-
func (an *testAnalyzer) AnalyzeImport(params ImportParams) ResolvedImport {
47-
// TODO:
48-
49-
return ResolvedImport{
50-
Path: "",
51-
}
52-
}
53-
5410
func TestParseDocument(t *testing.T) {
5511
parser := sitter.NewParser()
5612

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

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

373329
t.Run("EditableDocument.ReplaceWithPadding", func(t *testing.T) {
374-
doc, err := ParseDocument("test", strings.NewReader(` hello = 1`), parser, testLanguage, nil)
330+
doc, err := ParseDocument("test", strings.NewReader(` hello = 1`), parser, TestLanguage, nil)
375331
if err != nil {
376332
t.Error(err)
377333
} else if doc.TotalLines() < 1 {

testlang.go

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package errgoengine
2+
3+
import (
4+
"context"
5+
)
6+
7+
var TestLanguage = &Language{
8+
Name: "TestLang",
9+
FilePatterns: []string{".test"},
10+
StackTracePattern: `\sin (?P<symbol>\S+) at (?P<path>\S+):(?P<position>\d+)`,
11+
LocationConverter: func(ctx LocationConverterContext) Location {
12+
return Location{
13+
DocumentPath: ctx.Path,
14+
StartPos: Position{Line: 0, Column: 0, Index: 0},
15+
EndPos: Position{Line: 0, Column: 0, Index: 0},
16+
}
17+
},
18+
AnalyzerFactory: func(cd *ContextData) LanguageAnalyzer {
19+
return &testAnalyzer{}
20+
},
21+
SymbolsToCapture: `
22+
(expression_statement
23+
(assignment
24+
left: (identifier) @assignment.name
25+
right: (identifier) @assignment.content) @assignment)
26+
`,
27+
}
28+
29+
type testAnalyzer struct {
30+
*ContextData
31+
}
32+
33+
func (an *testAnalyzer) FallbackSymbol() Symbol {
34+
return Builtin("any")
35+
}
36+
37+
func (an *testAnalyzer) FindSymbol(name string) Symbol {
38+
return nil
39+
}
40+
41+
func (an *testAnalyzer) AnalyzeNode(_ context.Context, n SyntaxNode) Symbol {
42+
// TODO:
43+
return Builtin("void")
44+
}
45+
46+
func (an *testAnalyzer) AnalyzeImport(params ImportParams) ResolvedImport {
47+
// TODO:
48+
49+
return ResolvedImport{
50+
Path: "",
51+
}
52+
}

0 commit comments

Comments
 (0)