Make fzf pass go vet
Before adoption: this commit came in with the history the project adopted, so no landing records it.
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
authored by Ryan Boehning · committed by Junegunn Choi · parent 2423686 · Browse files · 7 files·+41 added−18 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
7 files+41 added−18 removed
- other:+41 added−18 removed
Provenance
- Commit
21b94d2- Tree
d7255fc- Author
Ryan Boehning <ryanboehning@gmail.com>- Committer
Junegunn Choi <junegunn.c@gmail.com>- Parent
2423686
7 files changed+41 added−18 removed
modified:src/util/chars.go+6 added
1 package util2 3 import (4+ "fmt"5 "unicode"6 "unicode/utf8"7 "unsafe"8 )85 unmodified lines94 }95 return len(chars.slice)96 }97 98+// String returns the string representation of a Chars object.99+func (chars *Chars) String() string {100+ 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)101+}102+103 // TrimLength returns the length after trimming leading and trailing whitespaces104 func (chars *Chars) TrimLength() uint16 {105 if chars.trimLengthKnown {106 return chars.trimLength67 unmodified linesmodified:src/ansi_test.go+1 added-1 removed
24 unmodified lines25 check := func(assertion func(ansiOffsets *[]ansiOffset, state *ansiState)) {26 output, ansiOffsets, newState := extractColor(src, state, nil)27 state = newState28 if output != "hello world" {29- t.Errorf("Invalid output: %s %s", output, []rune(output))29+ t.Errorf("Invalid output: %s %v", output, []rune(output))30 }31 fmt.Println(src, ansiOffsets, clean)32 assertion(ansiOffsets, state)33 }125 unmodified linesmodified:src/options_test.go+6 added-6 removed
48 unmodified lines49 tokens[1].text.ToString() != "--*" ||50 tokens[2].text.ToString() != "---*" ||51 tokens[3].text.ToString() != "*" ||52 tokens[4].text.ToString() != "---" {53- t.Errorf("%s %s %d", delim, tokens, len(tokens))53+ t.Errorf("%s %v %d", delim, tokens, len(tokens))54 }55 }56 57 func TestDelimiterRegexRegex(t *testing.T) {12 unmodified lines70 ranges := splitNth("..")71 if len(ranges) != 1 ||72 ranges[0].begin != rangeEllipsis ||73 ranges[0].end != rangeEllipsis {74- t.Errorf("%s", ranges)74+ t.Errorf("%v", ranges)75 }76 }77 {78 ranges := splitNth("..3,1..,2..3,4..-1,-3..-2,..,2,-2,2..-2,1..-1")7 unmodified lines86 ranges[6].begin != 2 || ranges[6].end != 2 ||87 ranges[7].begin != -2 || ranges[7].end != -2 ||88 ranges[8].begin != 2 || ranges[8].end != -2 ||89 ranges[9].begin != rangeEllipsis || ranges[9].end != rangeEllipsis {90- t.Errorf("%s", ranges)90+ t.Errorf("%v", ranges)91 }92 }93 }94 3 unmodified lines98 words := []string{"--nth", "..", "-x"}99 parseOptions(opts, words)100 postProcessOptions(opts)101 if len(opts.Nth) != 0 {102- t.Errorf("nth should be empty: %s", opts.Nth)102+ t.Errorf("nth should be empty: %v", opts.Nth)103 }104 }105 for _, words := range [][]string{[]string{"--nth", "..,3", "+x"}, []string{"--nth", "3,1..", "+x"}, []string{"--nth", "..-1,1", "+x"}} {106 {107 opts := defaultOptions()108 parseOptions(opts, words)109 postProcessOptions(opts)110 if len(opts.Nth) != 0 {111- t.Errorf("nth should be empty: %s", opts.Nth)111+ t.Errorf("nth should be empty: %v", opts.Nth)112 }113 }114 {115 opts := defaultOptions()116 words = append(words, "-x")117 parseOptions(opts, words)118 postProcessOptions(opts)119 if len(opts.Nth) != 2 {120- t.Errorf("nth should not be empty: %s", opts.Nth)120+ t.Errorf("nth should not be empty: %v", opts.Nth)121 }122 }123 }124 }299 unmodified linesmodified:src/pattern_test.go+6 added-6 removed
29 unmodified lines30 terms[8][0].typ != termPrefix || terms[8][0].inv ||31 terms[8][1].typ != termExact || terms[8][1].inv ||32 terms[8][2].typ != termSuffix || terms[8][2].inv ||33 terms[8][3].typ != termExact || !terms[8][3].inv {34- t.Errorf("%s", terms)34+ t.Errorf("%v", terms)35 }36 for _, termSet := range terms[:8] {37 term := termSet[0]38 if len(term.text) != 3 {39- t.Errorf("%s", term)39+ t.Errorf("%v", term)40 }41 }42 }43 8 unmodified lines52 terms[4][0].typ != termExact || !terms[4][0].inv || len(terms[4][0].text) != 3 ||53 terms[5][0].typ != termFuzzy || !terms[5][0].inv || len(terms[5][0].text) != 3 ||54 terms[6][0].typ != termPrefix || !terms[6][0].inv || len(terms[6][0].text) != 3 ||55 terms[7][0].typ != termSuffix || !terms[7][0].inv || len(terms[7][0].text) != 3 {56- t.Errorf("%s", terms)56+ t.Errorf("%v", terms)57 }58 }59 60 func TestParseTermsEmpty(t *testing.T) {61 terms := parseTerms(true, CaseSmart, false, "' ^ !' !^")62 if len(terms) != 0 {63- t.Errorf("%s", terms)63+ t.Errorf("%v", terms)64 }65 }66 67 func TestExact(t *testing.T) {4 unmodified lines72 chars := util.ToChars([]byte("aabbcc abc"))73 res, pos := algo.ExactMatchNaive(74 pattern.caseSensitive, pattern.normalize, pattern.forward, &chars, pattern.termSets[0][0].text, true, nil)75 if res.Start != 7 || res.End != 10 {76- t.Errorf("%s / %d / %d", pattern.termSets, res.Start, res.End)76+ t.Errorf("%v / %d / %d", pattern.termSets, res.Start, res.End)77 }78 if pos != nil {79 t.Errorf("pos is expected to be nil")80 }8 unmodified lines89 chars := util.ToChars([]byte(str))90 res, pos := algo.EqualMatch(91 pattern.caseSensitive, pattern.normalize, pattern.forward, &chars, pattern.termSets[0][0].text, true, nil)92 if res.Start != sidxExpected || res.End != eidxExpected {93- t.Errorf("%s / %d / %d", pattern.termSets, res.Start, res.End)93+ t.Errorf("%v / %d / %d", pattern.termSets, res.Start, res.End)94 }95 if pos != nil {96 t.Errorf("pos is expected to be nil")97 }109 unmodified linesmodified:src/pattern.go+6 added
1 package fzf2 3 import (4+ "fmt"5 "regexp"6 "strings"7 8 "github.com/junegunn/fzf/src/algo"25 unmodified lines34 text []rune35 caseSensitive bool36 }37 38+// String returns the string representation of a term.39+func (t term) String() string {40+ return fmt.Sprintf("term{typ: %d, inv: %v, text: []rune(%q), caseSensitive: %v}", t.typ, t.inv, string(t.text), t.caseSensitive)41+}42+43 type termSet []term44 45 // Pattern represents search pattern46 type Pattern struct {362 unmodified linesmodified:src/tokenizer_test.go+5 added-5 removed
7 unmodified lines8 {9 i := ".."10 r, _ := ParseRange(&i)11 if r.begin != rangeEllipsis || r.end != rangeEllipsis {12- t.Errorf("%s", r)12+ t.Errorf("%v", r)13 }14 }15 {16 i := "3.."17 r, _ := ParseRange(&i)18 if r.begin != 3 || r.end != rangeEllipsis {19- t.Errorf("%s", r)19+ t.Errorf("%v", r)20 }21 }22 {23 i := "3..5"24 r, _ := ParseRange(&i)25 if r.begin != 3 || r.end != 5 {26- t.Errorf("%s", r)26+ t.Errorf("%v", r)27 }28 }29 {30 i := "-3..-5"31 r, _ := ParseRange(&i)32 if r.begin != -3 || r.end != -5 {33- t.Errorf("%s", r)33+ t.Errorf("%v", r)34 }35 }36 {37 i := "3"38 r, _ := ParseRange(&i)39 if r.begin != 3 || r.end != 3 {40- t.Errorf("%s", r)40+ t.Errorf("%v", r)41 }42 }43 }44 68 unmodified linesmodified:src/tokenizer.go+11 added
1 package fzf2 3 import (4 "bytes"5+ "fmt"6 "regexp"7 "strconv"8 "strings"9 13 unmodified lines23 text *util.Chars24 prefixLength int3225 }26 27+// String returns the string representation of a Token.28+func (t Token) String() string {29+ return fmt.Sprintf("Token{text: %s, prefixLength: %d}", t.text, t.prefixLength)30+}31+32 // Delimiter for tokenizing the input33 type Delimiter struct {34 regex *regexp.Regexp35 str *string36 }37 38+// String returns the string representation of a Delimeter.39+func (d Delimiter) String() string {40+ return fmt.Sprintf("Delimiter{regex: %v, str: &%q}", d.regex, *d.str)41+}42+43 func newRange(begin int, end int) Range {44 if begin == 1 {45 begin = rangeEllipsis46 }207 unmodified lines