代码拉取完成,页面将自动刷新
package checkstyle
import (
"go/parser"
"go/token"
"io/ioutil"
"reflect"
"testing"
)
const baseDir = "testdata/"
func readFile(fileName string) []byte {
file, _ := ioutil.ReadFile(baseDir + fileName)
return file
}
func TestFileLine(t *testing.T) {
fileName := "fileline.go"
file := readFile(fileName)
_checkerOk := checker{FileLine: 9}
ps, err := _checkerOk.Check(fileName, file)
if err != nil {
t.Fatal(err)
}
if len(ps) != 0 {
t.Fatal("expect no error")
}
_checkerFail := checker{FileLine: 8}
ps, _ = _checkerFail.Check(fileName, file)
if len(ps) != 1 || ps[0].Type != FileLine {
t.Fatal("expect an error")
}
//pos is at file end
fset := token.NewFileSet()
f, _ := parser.ParseFile(fset, fileName, file, parser.ParseComments)
if reflect.DeepEqual(ps[0], fset.Position(f.End())) {
t.Fatal("file line problem position not match")
}
}
func TestFunctionLine(t *testing.T) {
fileName := "functionline.go"
file := readFile(fileName)
_checkerOk := checker{FunctionLine: 9}
ps, err := _checkerOk.Check(fileName, file)
if err != nil {
t.Fatal(err)
}
if len(ps) != 0 {
t.Fatal("expect no error")
}
_checkerFail := checker{FunctionLine: 8}
ps, _ = _checkerFail.Check(fileName, file)
if len(ps) != 1 || ps[0].Type != FunctionLine {
t.Fatal("expect an error")
}
if ps[0].Position.Filename != fileName {
t.Fatal("file name is not correct")
}
if ps[0].Position.Line != 7 {
t.Fatal("start position is not correct")
}
}
func TestParamsNum(t *testing.T) {
fileName := "params_num.go"
file := readFile(fileName)
_checkerOk := checker{ParamsNum: 4}
ps, err := _checkerOk.Check(fileName, file)
if err != nil {
t.Fatal(err)
}
if len(ps) != 0 {
t.Fatal("expect no error")
}
_checkerFail := checker{ParamsNum: 3}
ps, _ = _checkerFail.Check(fileName, file)
if len(ps) != 1 || ps[0].Type != ParamsNum {
t.Fatal("expect an error")
}
if ps[0].Position.Filename != fileName {
t.Fatal("file name is not correct")
}
if ps[0].Position.Line != 7 {
t.Fatal("start position is not correct")
}
_checkerFail = checker{ParamsNum: 2}
ps, _ = _checkerFail.Check(fileName, file)
if len(ps) != 2 {
t.Fatal("expect 2 error")
}
}
func TestResulsNum(t *testing.T) {
fileName := "results_num.go"
file := readFile(fileName)
_checkerOk := checker{ResultsNum: 4}
ps, err := _checkerOk.Check(fileName, file)
if err != nil {
t.Fatal(err)
}
if len(ps) != 0 {
t.Fatal("expect no error")
}
_checkerFail := checker{ResultsNum: 3}
ps, _ = _checkerFail.Check(fileName, file)
if len(ps) != 1 || ps[0].Type != ResultsNum {
t.Fatal("expect an error")
}
if ps[0].Position.Filename != fileName {
t.Fatal("file name is not correct")
}
if ps[0].Position.Line != 7 {
t.Fatal("start position is not correct")
}
_checkerFail = checker{ResultsNum: 2}
ps, _ = _checkerFail.Check(fileName, file)
if len(ps) != 2 {
t.Fatal("expect 2 error")
}
}
func TestFormated(t *testing.T) {
fileName := "formated.go"
file := readFile(fileName)
_checker := checker{Formated: true}
ps, err := _checker.Check(fileName, file)
if err != nil {
t.Fatal(err)
}
if len(ps) != 0 {
t.Fatal("expect no error")
}
fileName = "unformated.go"
file = readFile(fileName)
ps, _ = _checker.Check(fileName, file)
if len(ps) != 1 || ps[0].Type != Formated {
t.Fatal("expect an error")
}
//pos is at file begin
fset := token.NewFileSet()
f, _ := parser.ParseFile(fset, fileName, file, parser.ParseComments)
if reflect.DeepEqual(ps[0], fset.Position(f.Pos())) {
t.Fatal("file line problem position not match")
}
}
func TestPackageName(t *testing.T) {
fileName := "caps_pkg.go"
file := readFile(fileName)
_checker := checker{PackageName: false}
ps, err := _checker.Check(fileName, file)
if err != nil {
t.Fatal(err)
}
if len(ps) != 0 {
t.Fatal("expect no error")
}
fileName = "underscore_pkg.go"
file = readFile(fileName)
ps, err = _checker.Check(fileName, file)
if err != nil {
t.Fatal(err)
}
if len(ps) != 0 {
t.Fatal("expect no error")
}
fileName = "caps_pkg.go"
file = readFile(fileName)
_checkerFail := checker{PackageName: true}
ps, err = _checkerFail.Check(fileName, file)
if err != nil {
t.Fatal(err)
}
if len(ps) == 0 {
t.Fatal("expect 1 error")
}
fileName = "underscore_pkg.go"
file = readFile(fileName)
ps, err = _checkerFail.Check(fileName, file)
if err != nil {
t.Fatal(err)
}
if len(ps) == 0 {
t.Fatal("expect 1 error")
}
}
func TestCamelName(t *testing.T) {
fileName := "underscore_name.go"
file := readFile(fileName)
_checker := checker{CamelName: false}
ps, err := _checker.Check(fileName, file)
if err != nil {
t.Fatal(err)
}
if len(ps) != 0 {
t.Fatal("expect no error")
}
_checkerFail := checker{CamelName: true}
ps, err = _checkerFail.Check(fileName, file)
if err != nil {
t.Fatal(err)
}
if len(ps) != 30 {
t.Fatal("expect 30 error but ", len(ps))
}
fileName = "camel_name.go"
file = readFile(fileName)
ps, err = _checkerFail.Check(fileName, file)
if err != nil {
t.Fatal(err)
}
if len(ps) != 0 {
t.Fatal("expect no error")
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。