2023-07-31 09:59:51 +00:00
|
|
|
package noliteral
|
|
|
|
|
|
|
|
import (
|
|
|
|
"go/ast"
|
|
|
|
"go/parser"
|
|
|
|
"go/token"
|
|
|
|
"path/filepath"
|
|
|
|
"runtime"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"golang.org/x/tools/go/analysis"
|
|
|
|
)
|
|
|
|
|
2023-08-04 11:26:40 +00:00
|
|
|
func init() {
|
|
|
|
Config.ConstantsPackage = "git.frostfs.info/TrueCloudLab/frostfs-node/internal/logs"
|
2023-08-16 14:45:32 +00:00
|
|
|
Config.Enable = true
|
2023-08-04 11:26:40 +00:00
|
|
|
}
|
|
|
|
|
2023-07-31 09:59:51 +00:00
|
|
|
func TestAnalyzer_negative(t *testing.T) {
|
2023-08-01 11:40:00 +00:00
|
|
|
const countNegativeCases = 4
|
|
|
|
|
2023-08-04 11:26:40 +00:00
|
|
|
_, filename, _, _ := runtime.Caller(0)
|
|
|
|
dir := filepath.Dir(filename)
|
|
|
|
fset := token.NewFileSet()
|
|
|
|
f, err := parser.ParseFile(fset, filepath.Join(dir, "test-case/literals/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/literals/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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAnalyzer_const_location_negative(t *testing.T) {
|
|
|
|
const countNegativeCases = 3
|
2023-07-31 09:59:51 +00:00
|
|
|
_, filename, _, _ := runtime.Caller(0)
|
|
|
|
dir := filepath.Dir(filename)
|
|
|
|
|
|
|
|
fset := token.NewFileSet()
|
2023-08-04 11:26:40 +00:00
|
|
|
f, err := parser.ParseFile(fset, filepath.Join(dir, "test-case/const-location/negative._go"), nil, parser.AllErrors)
|
2023-07-31 09:59:51 +00:00
|
|
|
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)
|
|
|
|
|
2023-08-01 11:40:00 +00:00
|
|
|
if Count != countNegativeCases {
|
2023-07-31 09:59:51 +00:00
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-04 11:26:40 +00:00
|
|
|
func TestAnalyzer_const_location_positive(t *testing.T) {
|
2023-07-31 09:59:51 +00:00
|
|
|
_, filename, _, _ := runtime.Caller(0)
|
|
|
|
dir := filepath.Dir(filename)
|
|
|
|
|
|
|
|
fset := token.NewFileSet()
|
2023-08-04 11:26:40 +00:00
|
|
|
f, err := parser.ParseFile(fset, filepath.Join(dir, "test-case/const-location/positive._go"), nil, parser.AllErrors)
|
2023-07-31 09:59:51 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|