2021-04-02 15:36:43 -04:00
|
|
|
package document
|
2021-03-30 19:37:02 -04:00
|
|
|
|
|
|
|
import "testing"
|
|
|
|
|
2023-10-03 17:12:51 -04:00
|
|
|
var doc = NewDocument()
|
|
|
|
|
2021-03-30 19:37:02 -04:00
|
|
|
func TestNewRow(t *testing.T) {
|
|
|
|
str := "abcdefg"
|
2023-10-03 17:12:51 -04:00
|
|
|
row := newRow(doc, str)
|
2021-03-30 19:37:02 -04:00
|
|
|
|
|
|
|
got := string(row.chars)
|
|
|
|
want := str
|
|
|
|
|
|
|
|
if got != want {
|
2023-10-03 17:12:51 -04:00
|
|
|
t.Errorf("Row chars not equal to original string. Row: %q, String: %q", row.chars, str)
|
2021-03-30 19:37:02 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRowSize(t *testing.T) {
|
|
|
|
str := "abcdefg"
|
2023-10-03 17:12:51 -04:00
|
|
|
row := newRow(doc, str)
|
2021-03-30 19:37:02 -04:00
|
|
|
|
2021-04-02 14:52:44 -04:00
|
|
|
got := row.Size()
|
2021-03-30 19:37:02 -04:00
|
|
|
want := 7
|
|
|
|
|
|
|
|
if got != want {
|
|
|
|
t.Errorf("Row size not equal to number of runes in original string")
|
|
|
|
}
|
|
|
|
}
|
2021-03-31 19:17:04 -04:00
|
|
|
|
|
|
|
func TestRenderSize(t *testing.T) {
|
|
|
|
str := "\tabcdefg"
|
2023-10-03 17:12:51 -04:00
|
|
|
row := newRow(doc, str)
|
2021-03-31 19:17:04 -04:00
|
|
|
row.update()
|
|
|
|
|
2021-04-02 14:52:44 -04:00
|
|
|
got := row.RenderSize()
|
2021-03-31 19:17:04 -04:00
|
|
|
want := 11
|
|
|
|
|
|
|
|
if got != want {
|
|
|
|
t.Errorf("Row rsize not equal to number of runes in original string")
|
|
|
|
}
|
|
|
|
}
|
2021-04-01 16:17:13 -04:00
|
|
|
|
|
|
|
type insertRune struct {
|
|
|
|
initial string
|
2021-04-01 16:47:32 -04:00
|
|
|
ch rune
|
|
|
|
at int
|
2021-04-01 16:17:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
var insertRuneTests = []insertRune{
|
|
|
|
{"abde", 'c', 2},
|
|
|
|
{"bcde", 'a', 0},
|
|
|
|
{"abcd", 'e', 4},
|
|
|
|
{"abcd", 'e', 17},
|
|
|
|
{"abcd", 'e', -2},
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestInsertRune(t *testing.T) {
|
|
|
|
for _, test := range insertRuneTests {
|
2023-10-03 17:12:51 -04:00
|
|
|
row := newRow(doc, test.initial)
|
2021-04-01 16:17:13 -04:00
|
|
|
row.insertRune(test.ch, test.at)
|
|
|
|
|
2021-04-02 14:52:44 -04:00
|
|
|
if row.Size() != 5 {
|
|
|
|
t.Errorf("Row size after inserting rune at index [%d] is %d, should be %d", test.at, row.Size(), 5)
|
2021-04-01 16:17:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
got := row.toString()
|
|
|
|
want := "abcde"
|
|
|
|
|
|
|
|
if got != want {
|
|
|
|
t.Errorf("Row after update is '%s', should be '%s'", got, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|