Skip to content

Commit 4427b40

Browse files
authored
Merge pull request #56 from deanishe/feature/stable-sort
Stable sort
2 parents 13d93f3 + 677d37c commit 4427b40

File tree

4 files changed

+29
-13
lines changed

4 files changed

+29
-13
lines changed

.travis.yml

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,14 @@ go:
77
before_install:
88
- go get github.com/mattn/goveralls
99
- go get github.com/schrej/godacov
10+
- go get golang.org/x/lint/golint
11+
- go get github.com/golangci/golangci-lint/cmd/golangci-lint
12+
- go get github.com/mfridman/tparse
1013

1114
script:
1215
- ./run-tests.sh -ic ./...
13-
- $GOPATH/bin/goveralls -coverprofile=coverage.out -service=travis-ci; true
14-
- $GOPATH/bin/godacov -t $CODACY_TOKEN -r ./coverage.out -c $TRAVIS_COMMIT; true
16+
# - $GOPATH/bin/goveralls -coverprofile=coverage.out -service=travis-ci; true
17+
# - $GOPATH/bin/godacov -t $CODACY_TOKEN -r ./coverage.out -c $TRAVIS_COMMIT; true
1518

16-
after_success:
17-
- bash <(curl -s https://codecov.io/bash); true
19+
# after_success:
20+
# - bash <(curl -s https://codecov.io/bash); true

feedback.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -494,8 +494,10 @@ func (fb *Feedback) Sort(query string, opts ...fuzzy.Option) []*fuzzy.Result {
494494
// It returns a slice of Result structs, which contain the results of the
495495
// fuzzy sorting.
496496
func (fb *Feedback) Filter(query string, opts ...fuzzy.Option) []*fuzzy.Result {
497-
var items []*Item
498-
var res []*fuzzy.Result
497+
var (
498+
items []*Item
499+
res []*fuzzy.Result
500+
)
499501

500502
r := fb.Sort(query, opts...)
501503
for i, it := range fb.Items {
@@ -524,7 +526,13 @@ func (fb *Feedback) Keywords(i int) string {
524526
func (fb *Feedback) Len() int { return len(fb.Items) }
525527

526528
// Less implements sort.Interface.
527-
func (fb *Feedback) Less(i, j int) bool { return fb.Keywords(i) < fb.Keywords(j) }
529+
func (fb *Feedback) Less(i, j int) bool {
530+
a, b := fb.Keywords(i), fb.Keywords(j)
531+
if a == b {
532+
return i < j
533+
}
534+
return a < b
535+
}
528536

529537
// Swap implements sort.Interface.
530538
func (fb *Feedback) Swap(i, j int) { fb.Items[i], fb.Items[j] = fb.Items[j], fb.Items[i] }

feedback_test.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -529,7 +529,7 @@ func TestFeedback_Sort(t *testing.T) {
529529
r := fb.Sort(td.q)
530530
for i, it := range fb.Items {
531531
assert.Equal(t, td.out[i], it.title, "unexpected title")
532-
assert.Equal(t, td.m[i], r[i].Match, "unexpected title")
532+
assert.Equal(t, td.m[i], r[i].Match, "unexpected match")
533533
}
534534
}
535535
}
@@ -558,6 +558,13 @@ var feedbackTitles = []struct {
558558
out: []string{"Safari", "see all fellows' armpits", "spanish harlem", "french canada"},
559559
m: []bool{true, true, false, false},
560560
},
561+
// sorting is stable
562+
{
563+
q: "test",
564+
in: []string{"test #1", "test #10", "test #2", "test #3"},
565+
out: []string{"test #1", "test #2", "test #3", "test #10"},
566+
m: []bool{true, true, true, true},
567+
},
561568
}
562569

563570
var filterTitles = []struct {

run-tests.sh

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ install() {
3737
local name=${p:t}
3838
installed "$name" || {
3939
log "installing $name ..."
40-
GO111MODULE=off go get -u $gopts $p
40+
GO111MODULE=off go get -u $vopt $p
4141
[[ $? -eq 0 ]] || fail "install $name failed"
4242
success "installed $name"
4343
}
@@ -94,6 +94,7 @@ while getopts ":CcghilrtvV" opt; do
9494
case $opt in
9595
c)
9696
cover=true
97+
gopts+=(-coverprofile="$covfile")
9798
;;
9899
g)
99100
usegocov=true
@@ -118,7 +119,6 @@ while getopts ":CcghilrtvV" opt; do
118119
colour=true
119120
;;
120121
V)
121-
gopts+=(-v)
122122
verbose=true
123123
vopt='-v'
124124
;;
@@ -155,8 +155,6 @@ $runlint && {
155155
exit 0
156156
}
157157

158-
$cover && gopts+=(-coverprofile="$covfile")
159-
160158
command mkdir $vopt -p "${testdir}"/{data,cache}
161159
$mkip touch $vopt "$iplist"
162160
trap "test -f \"$iplist\" && rm -f \"$iplist\"; test -d \"$testdir\" && rm -rf \"$testdir\";" EXIT INT TERM
@@ -173,7 +171,7 @@ pkgs=(./...)
173171
st=0
174172
$runtests && {
175173
install github.com/mfridman/tparse
176-
go test -cover -json $gopts $pkgs | tparse
174+
go test -cover -json $vopt $gopts $pkgs | tparse
177175
# gotestsum -- $gopts $pkgs
178176
st=$?
179177
[[ $st -eq 0 ]] && success "unit tests passed"

0 commit comments

Comments
 (0)