diff --git a/Makefile b/Makefile index caf263c..3cb6edf 100644 --- a/Makefile +++ b/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 . diff --git a/bisect.go b/bisect.go deleted file mode 100644 index 2bcef3a..0000000 --- a/bisect.go +++ /dev/null @@ -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 - }) -} diff --git a/bisect_test.go b/bisect_test.go deleted file mode 100644 index ff51c2f..0000000 --- a/bisect_test.go +++ /dev/null @@ -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) - } -} diff --git a/document.go b/document.go index 94a410e..3392888 100644 --- a/document.go +++ b/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 + }) +} diff --git a/document_test.go b/document_test.go index 001dad0..068d243 100644 --- a/document_test.go +++ b/document_test.go @@ -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) + } +} diff --git a/posix_input_test.go b/posix_input_test.go new file mode 100644 index 0000000..4ab0d8c --- /dev/null +++ b/posix_input_test.go @@ -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) + } + } +} diff --git a/posix_output.go b/posix_output.go index 04ce255..f1144d5 100644 --- a/posix_output.go +++ b/posix_output.go @@ -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 } diff --git a/posix_output_test.go b/posix_output_test.go new file mode 100644 index 0000000..09f0077 --- /dev/null +++ b/posix_output_test.go @@ -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) + } + } +}