Adding a type that holds data in the text input field.

This commit is contained in:
Jakub Sobon 2019-04-07 22:37:28 -04:00
parent bf72b5ddc2
commit b031be6046
No known key found for this signature in database
GPG Key ID: F2451A77FB05D3B7
2 changed files with 142 additions and 0 deletions

View File

@ -0,0 +1,46 @@
// Copyright 2019 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package textinput
// editor.go contains data types that edit the content of the text input field.
import (
"bytes"
)
// fieldData are the data currently present inside the text input field.
type fieldData []rune
// String implements fmt.Stringer.
func (fd fieldData) String() string {
var b bytes.Buffer
for _, r := range fd {
b.WriteRune(r)
}
return b.String()
}
// insertAt inserts rune at the specified index.
func (fd *fieldData) insertAt(idx int, r rune) {
*fd = append(
(*fd)[:idx],
append(fieldData{r}, (*fd)[idx:]...)...,
)
}
// deleteAt deletes rune at the specified index.
func (fd *fieldData) deleteAt(idx int) {
*fd = append((*fd)[:idx], (*fd)[idx+1:]...)
}

View File

@ -0,0 +1,96 @@
// Copyright 2019 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package textinput
import (
"fmt"
"testing"
"github.com/kylelemons/godebug/pretty"
)
func TestData(t *testing.T) {
tests := []struct {
desc string
data fieldData
ops func(*fieldData)
want fieldData
}{
{
desc: "appends to empty data",
ops: func(fd *fieldData) {
fd.insertAt(0, 'a')
},
want: fieldData{'a'},
},
{
desc: "appends at the end of non-empty data",
data: fieldData{'a'},
ops: func(fd *fieldData) {
fd.insertAt(1, 'b')
fd.insertAt(2, 'c')
},
want: fieldData{'a', 'b', 'c'},
},
{
desc: "appends at the beginning of non-empty data",
data: fieldData{'a'},
ops: func(fd *fieldData) {
fd.insertAt(0, 'b')
fd.insertAt(0, 'c')
},
want: fieldData{'c', 'b', 'a'},
},
{
desc: "deletes the last rune, result in empty",
data: fieldData{'a'},
ops: func(fd *fieldData) {
fd.deleteAt(0)
},
want: fieldData{},
},
{
desc: "deletes the last rune, result in non-empty",
data: fieldData{'a', 'b'},
ops: func(fd *fieldData) {
fd.deleteAt(1)
},
want: fieldData{'a'},
},
{
desc: "deletes runes in the middle",
data: fieldData{'a', 'b', 'c', 'd'},
ops: func(fd *fieldData) {
fd.deleteAt(1)
fd.deleteAt(1)
},
want: fieldData{'a', 'd'},
},
}
for _, tc := range tests {
t.Run(tc.desc, func(t *testing.T) {
got := tc.data
if tc.ops != nil {
tc.ops(&got)
}
t.Logf(fmt.Sprintf("got: %s", got))
if diff := pretty.Compare(tc.want, got); diff != "" {
t.Errorf("fieldData => unexpected diff (-want, +got):\n%s\n got: %q\nwant: %q", diff, got, tc.want)
}
})
}
}