ted/viewmodel_test.go
Leon Mika 5424a6b927 Allowed open-right to work on all columns
Previously it only worked on the right-most column.  Also bounded this command to "O" (this might change).
2022-07-01 16:15:54 +10:00

99 lines
2.5 KiB
Go

package main
import (
"fmt"
"github.com/stretchr/testify/assert"
"testing"
)
func TestModelViewCtrl_OpenRight(t *testing.T) {
t.Run("should move cols to the right within the model", func(t *testing.T) {
rwModel := NewStdModelFromSlice([][]string{
{"letters", "numbers", "greek"},
{"a", "1", "alpha"},
{"b", "2", "bravo"},
{"c", "3", "charlie"},
})
mvc := NewGridViewModel(rwModel)
err := mvc.OpenRight(1)
assert.NoError(t, err)
assertModel(t, rwModel, [][]string{
{"letters", "numbers", "", "greek"},
{"a", "1", "", "alpha"},
{"b", "2", "", "bravo"},
{"c", "3", "", "charlie"},
})
})
t.Run("should move cols to the right at the left of the model", func(t *testing.T) {
rwModel := NewStdModelFromSlice([][]string{
{"letters", "numbers", "greek"},
{"a", "1", "alpha"},
{"b", "2", "bravo"},
{"c", "3", "charlie"},
})
mvc := NewGridViewModel(rwModel)
err := mvc.OpenRight(0)
assert.NoError(t, err)
assertModel(t, rwModel, [][]string{
{"letters", "", "numbers", "greek"},
{"a", "", "1", "alpha"},
{"b", "", "2", "bravo"},
{"c", "", "3", "charlie"},
})
})
t.Run("should move cols to the right at the right of the model", func(t *testing.T) {
rwModel := NewStdModelFromSlice([][]string{
{"letters", "numbers", "greek"},
{"a", "1", "alpha"},
{"b", "2", "bravo"},
{"c", "3", "charlie"},
})
mvc := NewGridViewModel(rwModel)
err := mvc.OpenRight(2)
assert.NoError(t, err)
assertModel(t, rwModel, [][]string{
{"letters", "numbers", "greek", ""},
{"a", "1", "alpha", ""},
{"b", "2", "bravo", ""},
{"c", "3", "charlie", ""},
})
})
t.Run("should return error if row out of bounds", func(t *testing.T) {
scenario := []int{-1, 3, 12}
for _, scenario := range scenario {
t.Run(fmt.Sprint(scenario), func(t *testing.T) {
rwModel := NewStdModelFromSlice([][]string{
{"letters", "numbers", "greek"},
{"a", "1", "alpha"},
{"b", "2", "bravo"},
{"c", "3", "charlie"},
})
mvc := NewGridViewModel(rwModel)
err := mvc.OpenRight(scenario)
assert.Error(t, err)
})
}
})
}
func assertModel(t *testing.T, actual Model, expected [][]string) {
dr, dc := actual.Dimensions()
assert.Equalf(t, len(expected), dr, "number of rows in model")
for r, row := range expected {
assert.Equalf(t, len(row), dc, "number of cols in row %v", r)
for c, cell := range row {
assert.Equalf(t, cell, actual.CellValue(r, c), "cell value at row %v, col %v", r, c)
}
}
}