Add some tests
This commit is contained in:
parent
19a0980a21
commit
8bec780c71
5
Makefile
5
Makefile
|
@ -21,6 +21,11 @@ lint: ## Run golint and go vet.
|
|||
test: ## Run the tests.
|
||||
@go test .
|
||||
|
||||
.PHONY: coverage
|
||||
cover: ## Run the tests.
|
||||
@go test -coverprofile=coverage.o
|
||||
@go tool cover -func=coverage.o
|
||||
|
||||
.PHONY: race-test
|
||||
race-test: ## Checking the race condition.
|
||||
@go test -race .
|
||||
|
|
27
bisect.go
27
bisect.go
|
@ -1,27 +0,0 @@
|
|||
package prompt
|
||||
|
||||
import "sort"
|
||||
|
||||
// BisectLeft to Locate the insertion point for v in a to maintain sorted order.
|
||||
func BisectLeft(a []int, v int) int {
|
||||
return bisectLeftRange(a, v, 0, len(a))
|
||||
}
|
||||
|
||||
func bisectLeftRange(a []int, v int, lo, hi int) int {
|
||||
s := a[lo:hi]
|
||||
return sort.Search(len(s), func(i int) bool {
|
||||
return s[i] >= v
|
||||
})
|
||||
}
|
||||
|
||||
// BisectRight to Locate the insertion point for v in a to maintain sorted order.
|
||||
func BisectRight(a []int, v int) int {
|
||||
return bisectRightRange(a, v, 0, len(a))
|
||||
}
|
||||
|
||||
func bisectRightRange(a []int, v int, lo, hi int) int {
|
||||
s := a[lo:hi]
|
||||
return sort.Search(len(s), func(i int) bool {
|
||||
return s[i] > v
|
||||
})
|
||||
}
|
|
@ -1,21 +0,0 @@
|
|||
package prompt
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
// Thanks!! https://play.golang.org/p/y9NRj_XVIW
|
||||
|
||||
func TestBisectRight(t *testing.T) {
|
||||
in := []int{1, 2, 3, 3, 3, 6, 7}
|
||||
|
||||
r := BisectRight(in, 0)
|
||||
if r != 0 {
|
||||
t.Errorf("number 0 should inserted at 0 position, but got %d", r)
|
||||
}
|
||||
|
||||
r = BisectRight(in, 4)
|
||||
if r != 5 {
|
||||
t.Errorf("number 4 should inserted at 5 position, but got %d", r)
|
||||
}
|
||||
}
|
15
document.go
15
document.go
|
@ -1,6 +1,7 @@
|
|||
package prompt
|
||||
|
||||
import (
|
||||
"sort"
|
||||
"strings"
|
||||
"unicode/utf8"
|
||||
)
|
||||
|
@ -140,7 +141,7 @@ func (d *Document) lineStartIndexes() []int {
|
|||
// the first character on that line.
|
||||
func (d *Document) findLineStartIndex(index int) (pos int, lineStartIndex int) {
|
||||
indexes := d.lineStartIndexes()
|
||||
pos = BisectRight(indexes, index) - 1
|
||||
pos = bisectRight(indexes, index) - 1
|
||||
lineStartIndex = indexes[pos]
|
||||
return
|
||||
}
|
||||
|
@ -280,3 +281,15 @@ func (d *Document) leadingWhitespaceInCurrentLine() (margin string) {
|
|||
margin = d.CurrentLine()[:len(d.CurrentLine())-len(trimmed)]
|
||||
return
|
||||
}
|
||||
|
||||
// bisectRight to Locate the insertion point for v in a to maintain sorted order.
|
||||
func bisectRight(a []int, v int) int {
|
||||
return bisectRightRange(a, v, 0, len(a))
|
||||
}
|
||||
|
||||
func bisectRightRange(a []int, v int, lo, hi int) int {
|
||||
s := a[lo:hi]
|
||||
return sort.Search(len(s), func(i int) bool {
|
||||
return s[i] > v
|
||||
})
|
||||
}
|
||||
|
|
|
@ -403,3 +403,18 @@ func TestDocument_GetEndOfLinePosition(t *testing.T) {
|
|||
t.Errorf("Should be %#v, got %#v", ex, ac)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBisectRight(t *testing.T) {
|
||||
// Thanks!! https://play.golang.org/p/y9NRj_XVIW
|
||||
in := []int{1, 2, 3, 3, 3, 6, 7}
|
||||
|
||||
r := bisectRight(in, 0)
|
||||
if r != 0 {
|
||||
t.Errorf("number 0 should inserted at 0 position, but got %d", r)
|
||||
}
|
||||
|
||||
r = bisectRight(in, 4)
|
||||
if r != 5 {
|
||||
t.Errorf("number 4 should inserted at 5 position, but got %d", r)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,31 @@
|
|||
// +build !windows
|
||||
|
||||
package prompt
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestPosixParserGetKey(t *testing.T) {
|
||||
pp := &PosixParser{}
|
||||
scenarioTable := []struct {
|
||||
input []byte
|
||||
expected Key
|
||||
}{
|
||||
{
|
||||
input: []byte{0x1b},
|
||||
expected: Escape,
|
||||
},
|
||||
{
|
||||
input: []byte{'a'},
|
||||
expected: NotDefined,
|
||||
},
|
||||
}
|
||||
|
||||
for _, s := range scenarioTable {
|
||||
key := pp.GetKey(s.input)
|
||||
if key != s.expected {
|
||||
t.Errorf("Should be %s, but got %s", key, s.expected)
|
||||
}
|
||||
}
|
||||
}
|
|
@ -18,8 +18,6 @@ type PosixWriter struct {
|
|||
// WriteRaw to write raw byte array
|
||||
func (w *PosixWriter) WriteRaw(data []byte) {
|
||||
w.buffer = append(w.buffer, data...)
|
||||
// Flush because sometimes the render is broken when a large amount data in buffer.
|
||||
w.Flush()
|
||||
return
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,83 @@
|
|||
// +build !windows
|
||||
|
||||
package prompt
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestPosixWriterWrite(t *testing.T) {
|
||||
scenarioTable := []struct {
|
||||
input []byte
|
||||
expected []byte
|
||||
}{
|
||||
{
|
||||
input: []byte{0x1b},
|
||||
expected: []byte{'?'},
|
||||
},
|
||||
{
|
||||
input: []byte{'a'},
|
||||
expected: []byte{'a'},
|
||||
},
|
||||
}
|
||||
|
||||
for _, s := range scenarioTable {
|
||||
pw := &PosixWriter{}
|
||||
pw.Write(s.input)
|
||||
|
||||
if !bytes.Equal(pw.buffer, s.expected) {
|
||||
t.Errorf("Should be %+#v, but got %+#v", pw.buffer, s.expected)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestPosixWriterWriteStr(t *testing.T) {
|
||||
scenarioTable := []struct {
|
||||
input string
|
||||
expected []byte
|
||||
}{
|
||||
{
|
||||
input: "\x1b",
|
||||
expected: []byte{'?'},
|
||||
},
|
||||
{
|
||||
input: "a",
|
||||
expected: []byte{'a'},
|
||||
},
|
||||
}
|
||||
|
||||
for _, s := range scenarioTable {
|
||||
pw := &PosixWriter{}
|
||||
pw.WriteStr(s.input)
|
||||
|
||||
if !bytes.Equal(pw.buffer, s.expected) {
|
||||
t.Errorf("Should be %+#v, but got %+#v", pw.buffer, s.expected)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestPosixWriterWriteRawStr(t *testing.T) {
|
||||
scenarioTable := []struct {
|
||||
input string
|
||||
expected []byte
|
||||
}{
|
||||
{
|
||||
input: "\x1b",
|
||||
expected: []byte{0x1b},
|
||||
},
|
||||
{
|
||||
input: "a",
|
||||
expected: []byte{'a'},
|
||||
},
|
||||
}
|
||||
|
||||
for _, s := range scenarioTable {
|
||||
pw := &PosixWriter{}
|
||||
pw.WriteRawStr(s.input)
|
||||
|
||||
if !bytes.Equal(pw.buffer, s.expected) {
|
||||
t.Errorf("Should be %+#v, but got %+#v", pw.buffer, s.expected)
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue