Skip to content

Commit

Permalink
Remove unnecessary code and add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Jacalz committed Jan 13, 2024
1 parent 30b92c9 commit ac12c0d
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 16 deletions.
16 changes: 3 additions & 13 deletions internal/transport/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ https://github.com/psanford/wormhole-william/blob/master/cmd/completion.go and
https://github.com/psanford/wormhole-william/blob/master/internal/crypto/crypto.go.
*/

// CompleteRecvCode returns completion suggestions for the receiver code.
func (c *Client) CompleteRecvCode(toComplete string) []string {
// GenerateCodeCompletion returns completion suggestions for the receiver code.
func (c *Client) GenerateCodeCompletion(toComplete string) []string {
separators := strings.Count(toComplete, "-")
if separators == 0 {
return c.completeNameplates(toComplete)
Expand Down Expand Up @@ -49,7 +49,7 @@ func (c *Client) CompleteRecvCode(toComplete string) []string {

var candidates []string

// Search forward for prefix matches from found index.
// Search forward for the other prefix matches.
for i := index; i < 256; i++ {
candidate, match := lookupWordMatch(byte(i), completionMatch, even)
if !match {
Expand All @@ -59,16 +59,6 @@ func (c *Client) CompleteRecvCode(toComplete string) []string {
candidates = append(candidates, prefix+candidate)
}

// Search backwards for prefix matches from before the found index.
for i := index - 1; i >= 0; i-- {
candidate, match := lookupWordMatch(byte(i), completionMatch, even)
if !match {
break // Sorted in increasing alphabetical order. No more matches.
}

candidates = append(candidates, prefix+candidate)
}

return candidates
}

Expand Down
30 changes: 28 additions & 2 deletions internal/transport/completion_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package transport

import "testing"
import (
"testing"

"github.com/stretchr/testify/assert"
)

var completions []string

Expand All @@ -10,8 +14,30 @@ func BenchmarkNameplateCompletion(b *testing.B) {
local := []string{}

for i := 0; i < b.N; i++ {
local = c.CompleteRecvCode("5-letterhead-be")
local = c.GenerateCodeCompletion("1-letterhead-be")
}

completions = local
}

func TestCompletionGeneration_Progressive(t *testing.T) {
c := Client{}

expected := []string{"1-uncut", "1-unearth", "1-unwind", "1-uproot", "1-upset", "1-upshot"}
assert.Equal(t, expected, c.GenerateCodeCompletion("1-u"))

expected = []string{"1-uproot", "1-upset", "1-upshot"}
assert.Equal(t, expected, c.GenerateCodeCompletion("1-up"))

expected = []string{"1-upset", "1-upshot"}
assert.Equal(t, expected, c.GenerateCodeCompletion("1-ups"))

expected = []string{"1-upset"}
assert.Equal(t, expected, c.GenerateCodeCompletion("1-upse"))

expected = []string{"1-upshot"}
assert.Equal(t, expected, c.GenerateCodeCompletion("1-upsh"))

expected = []string{"1-upset-unicorn", "1-upset-unify", "1-upset-universe"}
assert.Equal(t, expected, c.GenerateCodeCompletion("1-upset-uni"))
}
2 changes: 1 addition & 1 deletion internal/ui/recv.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ func newCompletionEntry(client *transport.Client, canvas fyne.Canvas) *completio
PlaceHolder: "Enter code", Scroll: container.ScrollHorizontalOnly, Validator: util.CodeValidator,
},
}
entry.complete.Generate = client.CompleteRecvCode
entry.complete.Generate = client.GenerateCodeCompletion
entry.ExtendBaseWidget(entry)
return entry
}

0 comments on commit ac12c0d

Please sign in to comment.