Make fzf pass go vet

Add String() methods to types, so they can be printed with %s. Change
some %s format specifiers to %v, when the default string representation
is good enough. In Go 1.10, `go test` triggers a parallel `go vet`. So
this also makes fzf pass `go test`.

Close #1236
Close #1219
This commit is contained in:
Ryan Boehning 2018-02-17 14:01:06 -08:00 committed by Junegunn Choi
parent 24236860c8
commit 21b94d2de5
No known key found for this signature in database
GPG Key ID: 254BC280FEF9C627
7 changed files with 41 additions and 18 deletions

View File

@ -26,7 +26,7 @@ func TestExtractColor(t *testing.T) {
output, ansiOffsets, newState := extractColor(src, state, nil) output, ansiOffsets, newState := extractColor(src, state, nil)
state = newState state = newState
if output != "hello world" { if output != "hello world" {
t.Errorf("Invalid output: %s %s", output, []rune(output)) t.Errorf("Invalid output: %s %v", output, []rune(output))
} }
fmt.Println(src, ansiOffsets, clean) fmt.Println(src, ansiOffsets, clean)
assertion(ansiOffsets, state) assertion(ansiOffsets, state)

View File

@ -50,7 +50,7 @@ func TestDelimiterRegexString(t *testing.T) {
tokens[2].text.ToString() != "---*" || tokens[2].text.ToString() != "---*" ||
tokens[3].text.ToString() != "*" || tokens[3].text.ToString() != "*" ||
tokens[4].text.ToString() != "---" { tokens[4].text.ToString() != "---" {
t.Errorf("%s %s %d", delim, tokens, len(tokens)) t.Errorf("%s %v %d", delim, tokens, len(tokens))
} }
} }
@ -71,7 +71,7 @@ func TestSplitNth(t *testing.T) {
if len(ranges) != 1 || if len(ranges) != 1 ||
ranges[0].begin != rangeEllipsis || ranges[0].begin != rangeEllipsis ||
ranges[0].end != rangeEllipsis { ranges[0].end != rangeEllipsis {
t.Errorf("%s", ranges) t.Errorf("%v", ranges)
} }
} }
{ {
@ -87,7 +87,7 @@ func TestSplitNth(t *testing.T) {
ranges[7].begin != -2 || ranges[7].end != -2 || ranges[7].begin != -2 || ranges[7].end != -2 ||
ranges[8].begin != 2 || ranges[8].end != -2 || ranges[8].begin != 2 || ranges[8].end != -2 ||
ranges[9].begin != rangeEllipsis || ranges[9].end != rangeEllipsis { ranges[9].begin != rangeEllipsis || ranges[9].end != rangeEllipsis {
t.Errorf("%s", ranges) t.Errorf("%v", ranges)
} }
} }
} }
@ -99,7 +99,7 @@ func TestIrrelevantNth(t *testing.T) {
parseOptions(opts, words) parseOptions(opts, words)
postProcessOptions(opts) postProcessOptions(opts)
if len(opts.Nth) != 0 { if len(opts.Nth) != 0 {
t.Errorf("nth should be empty: %s", opts.Nth) t.Errorf("nth should be empty: %v", opts.Nth)
} }
} }
for _, words := range [][]string{[]string{"--nth", "..,3", "+x"}, []string{"--nth", "3,1..", "+x"}, []string{"--nth", "..-1,1", "+x"}} { for _, words := range [][]string{[]string{"--nth", "..,3", "+x"}, []string{"--nth", "3,1..", "+x"}, []string{"--nth", "..-1,1", "+x"}} {
@ -108,7 +108,7 @@ func TestIrrelevantNth(t *testing.T) {
parseOptions(opts, words) parseOptions(opts, words)
postProcessOptions(opts) postProcessOptions(opts)
if len(opts.Nth) != 0 { if len(opts.Nth) != 0 {
t.Errorf("nth should be empty: %s", opts.Nth) t.Errorf("nth should be empty: %v", opts.Nth)
} }
} }
{ {
@ -117,7 +117,7 @@ func TestIrrelevantNth(t *testing.T) {
parseOptions(opts, words) parseOptions(opts, words)
postProcessOptions(opts) postProcessOptions(opts)
if len(opts.Nth) != 2 { if len(opts.Nth) != 2 {
t.Errorf("nth should not be empty: %s", opts.Nth) t.Errorf("nth should not be empty: %v", opts.Nth)
} }
} }
} }

View File

@ -1,6 +1,7 @@
package fzf package fzf
import ( import (
"fmt"
"regexp" "regexp"
"strings" "strings"
@ -34,6 +35,11 @@ type term struct {
caseSensitive bool caseSensitive bool
} }
// String returns the string representation of a term.
func (t term) String() string {
return fmt.Sprintf("term{typ: %d, inv: %v, text: []rune(%q), caseSensitive: %v}", t.typ, t.inv, string(t.text), t.caseSensitive)
}
type termSet []term type termSet []term
// Pattern represents search pattern // Pattern represents search pattern

View File

@ -31,12 +31,12 @@ func TestParseTermsExtended(t *testing.T) {
terms[8][1].typ != termExact || terms[8][1].inv || terms[8][1].typ != termExact || terms[8][1].inv ||
terms[8][2].typ != termSuffix || terms[8][2].inv || terms[8][2].typ != termSuffix || terms[8][2].inv ||
terms[8][3].typ != termExact || !terms[8][3].inv { terms[8][3].typ != termExact || !terms[8][3].inv {
t.Errorf("%s", terms) t.Errorf("%v", terms)
} }
for _, termSet := range terms[:8] { for _, termSet := range terms[:8] {
term := termSet[0] term := termSet[0]
if len(term.text) != 3 { if len(term.text) != 3 {
t.Errorf("%s", term) t.Errorf("%v", term)
} }
} }
} }
@ -53,14 +53,14 @@ func TestParseTermsExtendedExact(t *testing.T) {
terms[5][0].typ != termFuzzy || !terms[5][0].inv || len(terms[5][0].text) != 3 || terms[5][0].typ != termFuzzy || !terms[5][0].inv || len(terms[5][0].text) != 3 ||
terms[6][0].typ != termPrefix || !terms[6][0].inv || len(terms[6][0].text) != 3 || terms[6][0].typ != termPrefix || !terms[6][0].inv || len(terms[6][0].text) != 3 ||
terms[7][0].typ != termSuffix || !terms[7][0].inv || len(terms[7][0].text) != 3 { terms[7][0].typ != termSuffix || !terms[7][0].inv || len(terms[7][0].text) != 3 {
t.Errorf("%s", terms) t.Errorf("%v", terms)
} }
} }
func TestParseTermsEmpty(t *testing.T) { func TestParseTermsEmpty(t *testing.T) {
terms := parseTerms(true, CaseSmart, false, "' ^ !' !^") terms := parseTerms(true, CaseSmart, false, "' ^ !' !^")
if len(terms) != 0 { if len(terms) != 0 {
t.Errorf("%s", terms) t.Errorf("%v", terms)
} }
} }
@ -73,7 +73,7 @@ func TestExact(t *testing.T) {
res, pos := algo.ExactMatchNaive( res, pos := algo.ExactMatchNaive(
pattern.caseSensitive, pattern.normalize, pattern.forward, &chars, pattern.termSets[0][0].text, true, nil) pattern.caseSensitive, pattern.normalize, pattern.forward, &chars, pattern.termSets[0][0].text, true, nil)
if res.Start != 7 || res.End != 10 { if res.Start != 7 || res.End != 10 {
t.Errorf("%s / %d / %d", pattern.termSets, res.Start, res.End) t.Errorf("%v / %d / %d", pattern.termSets, res.Start, res.End)
} }
if pos != nil { if pos != nil {
t.Errorf("pos is expected to be nil") t.Errorf("pos is expected to be nil")
@ -90,7 +90,7 @@ func TestEqual(t *testing.T) {
res, pos := algo.EqualMatch( res, pos := algo.EqualMatch(
pattern.caseSensitive, pattern.normalize, pattern.forward, &chars, pattern.termSets[0][0].text, true, nil) pattern.caseSensitive, pattern.normalize, pattern.forward, &chars, pattern.termSets[0][0].text, true, nil)
if res.Start != sidxExpected || res.End != eidxExpected { if res.Start != sidxExpected || res.End != eidxExpected {
t.Errorf("%s / %d / %d", pattern.termSets, res.Start, res.End) t.Errorf("%v / %d / %d", pattern.termSets, res.Start, res.End)
} }
if pos != nil { if pos != nil {
t.Errorf("pos is expected to be nil") t.Errorf("pos is expected to be nil")

View File

@ -2,6 +2,7 @@ package fzf
import ( import (
"bytes" "bytes"
"fmt"
"regexp" "regexp"
"strconv" "strconv"
"strings" "strings"
@ -23,12 +24,22 @@ type Token struct {
prefixLength int32 prefixLength int32
} }
// String returns the string representation of a Token.
func (t Token) String() string {
return fmt.Sprintf("Token{text: %s, prefixLength: %d}", t.text, t.prefixLength)
}
// Delimiter for tokenizing the input // Delimiter for tokenizing the input
type Delimiter struct { type Delimiter struct {
regex *regexp.Regexp regex *regexp.Regexp
str *string str *string
} }
// String returns the string representation of a Delimeter.
func (d Delimiter) String() string {
return fmt.Sprintf("Delimiter{regex: %v, str: &%q}", d.regex, *d.str)
}
func newRange(begin int, end int) Range { func newRange(begin int, end int) Range {
if begin == 1 { if begin == 1 {
begin = rangeEllipsis begin = rangeEllipsis

View File

@ -9,35 +9,35 @@ func TestParseRange(t *testing.T) {
i := ".." i := ".."
r, _ := ParseRange(&i) r, _ := ParseRange(&i)
if r.begin != rangeEllipsis || r.end != rangeEllipsis { if r.begin != rangeEllipsis || r.end != rangeEllipsis {
t.Errorf("%s", r) t.Errorf("%v", r)
} }
} }
{ {
i := "3.." i := "3.."
r, _ := ParseRange(&i) r, _ := ParseRange(&i)
if r.begin != 3 || r.end != rangeEllipsis { if r.begin != 3 || r.end != rangeEllipsis {
t.Errorf("%s", r) t.Errorf("%v", r)
} }
} }
{ {
i := "3..5" i := "3..5"
r, _ := ParseRange(&i) r, _ := ParseRange(&i)
if r.begin != 3 || r.end != 5 { if r.begin != 3 || r.end != 5 {
t.Errorf("%s", r) t.Errorf("%v", r)
} }
} }
{ {
i := "-3..-5" i := "-3..-5"
r, _ := ParseRange(&i) r, _ := ParseRange(&i)
if r.begin != -3 || r.end != -5 { if r.begin != -3 || r.end != -5 {
t.Errorf("%s", r) t.Errorf("%v", r)
} }
} }
{ {
i := "3" i := "3"
r, _ := ParseRange(&i) r, _ := ParseRange(&i)
if r.begin != 3 || r.end != 3 { if r.begin != 3 || r.end != 3 {
t.Errorf("%s", r) t.Errorf("%v", r)
} }
} }
} }

View File

@ -1,6 +1,7 @@
package util package util
import ( import (
"fmt"
"unicode" "unicode"
"unicode/utf8" "unicode/utf8"
"unsafe" "unsafe"
@ -94,6 +95,11 @@ func (chars *Chars) Length() int {
return len(chars.slice) return len(chars.slice)
} }
// String returns the string representation of a Chars object.
func (chars *Chars) String() string {
return fmt.Sprintf("Chars{slice: []byte(%q), inBytes: %v, trimLengthKnown: %v, trimLength: %d, Index: %d}", chars.slice, chars.inBytes, chars.trimLengthKnown, chars.trimLength, chars.Index)
}
// TrimLength returns the length after trimming leading and trailing whitespaces // TrimLength returns the length after trimming leading and trailing whitespaces
func (chars *Chars) TrimLength() uint16 { func (chars *Chars) TrimLength() uint16 {
if chars.trimLengthKnown { if chars.trimLengthKnown {