mirror of
https://github.com/mgechev/revive.git
synced 2024-11-21 17:16:40 +02:00
refactor: add tests for isGenerated in the lint package (#1083)
This commit is contained in:
parent
21344f1bd5
commit
d29323985f
31
lint/linter_test.go
Normal file
31
lint/linter_test.go
Normal file
@ -0,0 +1,31 @@
|
||||
package lint
|
||||
|
||||
import "testing"
|
||||
|
||||
// TestIsGenerated tests isGenerated function.
|
||||
func TestIsGenerated(t *testing.T) { //revive:disable-line:exported
|
||||
tests := []struct {
|
||||
source string
|
||||
generated bool
|
||||
}{
|
||||
{"// Code Generated by some tool. DO NOT EDIT.", false},
|
||||
{"// Code generated by some tool. DO NOT EDIT.", true},
|
||||
{"// Code generated by some tool. DO NOT EDIT", false},
|
||||
{"// Code generated DO NOT EDIT.", true},
|
||||
{"// Code generated DO NOT EDIT.", false},
|
||||
{"\t\t// Code generated by some tool. DO NOT EDIT.\npackage foo\n", false},
|
||||
{"// Code generated by some tool. DO NOT EDIT.\npackage foo\n", true},
|
||||
{"package foo\n// Code generated by some tool. DO NOT EDIT.\ntype foo int\n", true},
|
||||
{"package foo\n // Code generated by some tool. DO NOT EDIT.\ntype foo int\n", false},
|
||||
{"package foo\n// Code generated by some tool. DO NOT EDIT. \ntype foo int\n", false},
|
||||
{"package foo\ntype foo int\n// Code generated by some tool. DO NOT EDIT.\n", true},
|
||||
{"package foo\ntype foo int\n// Code generated by some tool. DO NOT EDIT.", true},
|
||||
}
|
||||
|
||||
for i, test := range tests {
|
||||
got := isGenerated([]byte(test.source))
|
||||
if got != test.generated {
|
||||
t.Errorf("test %d, isGenerated() = %v, want %v", i, got, test.generated)
|
||||
}
|
||||
}
|
||||
}
|
@ -1,8 +1,6 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"go/ast"
|
||||
@ -387,49 +385,3 @@ func TestExportedType(t *testing.T) { //revive:disable-line:exported
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var (
|
||||
genHdr = []byte("// Code generated ")
|
||||
genFtr = []byte(" DO NOT EDIT.")
|
||||
)
|
||||
|
||||
// isGenerated reports whether the source file is generated code
|
||||
// according the rules from https://golang.org/s/generatedcode.
|
||||
func isGenerated(src []byte) bool {
|
||||
sc := bufio.NewScanner(bytes.NewReader(src))
|
||||
for sc.Scan() {
|
||||
b := sc.Bytes()
|
||||
if bytes.HasPrefix(b, genHdr) && bytes.HasSuffix(b, genFtr) && len(b) >= len(genHdr)+len(genFtr) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// TestIsGenerated tests isGenerated function
|
||||
func TestIsGenerated(t *testing.T) { //revive:disable-line:exported
|
||||
tests := []struct {
|
||||
source string
|
||||
generated bool
|
||||
}{
|
||||
{"// Code Generated by some tool. DO NOT EDIT.", false},
|
||||
{"// Code generated by some tool. DO NOT EDIT.", true},
|
||||
{"// Code generated by some tool. DO NOT EDIT", false},
|
||||
{"// Code generated DO NOT EDIT.", true},
|
||||
{"// Code generated DO NOT EDIT.", false},
|
||||
{"\t\t// Code generated by some tool. DO NOT EDIT.\npackage foo\n", false},
|
||||
{"// Code generated by some tool. DO NOT EDIT.\npackage foo\n", true},
|
||||
{"package foo\n// Code generated by some tool. DO NOT EDIT.\ntype foo int\n", true},
|
||||
{"package foo\n // Code generated by some tool. DO NOT EDIT.\ntype foo int\n", false},
|
||||
{"package foo\n// Code generated by some tool. DO NOT EDIT. \ntype foo int\n", false},
|
||||
{"package foo\ntype foo int\n// Code generated by some tool. DO NOT EDIT.\n", true},
|
||||
{"package foo\ntype foo int\n// Code generated by some tool. DO NOT EDIT.", true},
|
||||
}
|
||||
|
||||
for i, test := range tests {
|
||||
got := isGenerated([]byte(test.source))
|
||||
if got != test.generated {
|
||||
t.Errorf("test %d, isGenerated() = %v, want %v", i, got, test.generated)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user