forked from TrueCloudLab/linters
75 lines
1.2 KiB
Go
75 lines
1.2 KiB
Go
|
package usestrconv
|
||
|
|
||
|
import (
|
||
|
"go/ast"
|
||
|
"go/parser"
|
||
|
"go/token"
|
||
|
"path/filepath"
|
||
|
"runtime"
|
||
|
"testing"
|
||
|
|
||
|
"golang.org/x/tools/go/analysis"
|
||
|
)
|
||
|
|
||
|
func init() {
|
||
|
Config.Enable = true
|
||
|
}
|
||
|
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/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_literals_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/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)
|
||
|
}
|
||
|
}
|