forked from TrueCloudLab/linters
72 lines
1.2 KiB
Go
72 lines
1.2 KiB
Go
package noliteral
|
|
|
|
import (
|
|
"go/ast"
|
|
"go/parser"
|
|
"go/token"
|
|
"path/filepath"
|
|
"runtime"
|
|
"testing"
|
|
|
|
"golang.org/x/tools/go/analysis"
|
|
)
|
|
|
|
func TestAnalyzer_negative(t *testing.T) {
|
|
const countNegativeCases = 4
|
|
|
|
_, filename, _, _ := runtime.Caller(0)
|
|
dir := filepath.Dir(filename)
|
|
|
|
fset := token.NewFileSet()
|
|
f, err := parser.ParseFile(fset, filepath.Join(dir, "test-case/src/negative._go"), nil, parser.AllErrors)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
Count := 0
|
|
pass := &analysis.Pass{
|
|
Fset: fset,
|
|
Files: []*ast.File{f},
|
|
Report: func(diag analysis.Diagnostic) {
|
|
Count++
|
|
},
|
|
}
|
|
|
|
_, err = run(pass)
|
|
|
|
if Count != countNegativeCases {
|
|
t.Fail()
|
|
}
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
func TestAnalyzer_positive(t *testing.T) {
|
|
_, filename, _, _ := runtime.Caller(0)
|
|
dir := filepath.Dir(filename)
|
|
|
|
fset := token.NewFileSet()
|
|
f, err := parser.ParseFile(fset, filepath.Join(dir, "test-case/src/positive._go"), nil, parser.AllErrors)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
flag := false
|
|
pass := &analysis.Pass{
|
|
Fset: fset,
|
|
Files: []*ast.File{f},
|
|
Report: func(diag analysis.Diagnostic) {
|
|
flag = true
|
|
},
|
|
}
|
|
_, err = run(pass)
|
|
|
|
if flag {
|
|
t.Fail()
|
|
}
|
|
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|