1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394
|
package viewport
import (
"strings"
"testing"
)
const defaultHorizontalStep = 6
func TestNew(t *testing.T) {
t.Parallel()
t.Run("default values on create by New", func(t *testing.T) {
t.Parallel()
m := New(10, 10)
m.horizontalStep = defaultHorizontalStep // remove on v2
if !m.initialized {
t.Errorf("on create by New, Model should be initialized")
}
if m.horizontalStep != defaultHorizontalStep {
t.Errorf("default horizontalStep should be %d, got %d", defaultHorizontalStep, m.horizontalStep)
}
if m.MouseWheelDelta != 3 {
t.Errorf("default MouseWheelDelta should be 3, got %d", m.MouseWheelDelta)
}
if !m.MouseWheelEnabled {
t.Error("mouse wheel should be enabled by default")
}
})
}
func TestSetInitialValues(t *testing.T) {
t.Parallel()
t.Run("default horizontalStep", func(t *testing.T) {
t.Parallel()
m := Model{}
m.horizontalStep = defaultHorizontalStep // remove on v2
m.setInitialValues()
if m.horizontalStep != defaultHorizontalStep {
t.Errorf("default horizontalStep should be %d, got %d", defaultHorizontalStep, m.horizontalStep)
}
})
}
func TestSetHorizontalStep(t *testing.T) {
t.Parallel()
t.Run("change default", func(t *testing.T) {
t.Parallel()
m := New(10, 10)
m.horizontalStep = defaultHorizontalStep // remove on v2
if m.horizontalStep != defaultHorizontalStep {
t.Errorf("default horizontalStep should be %d, got %d", defaultHorizontalStep, m.horizontalStep)
}
newStep := 8
m.SetHorizontalStep(newStep)
if m.horizontalStep != newStep {
t.Errorf("horizontalStep should be %d, got %d", newStep, m.horizontalStep)
}
})
t.Run("no negative", func(t *testing.T) {
t.Parallel()
m := New(10, 10)
m.horizontalStep = defaultHorizontalStep // remove on v2
if m.horizontalStep != defaultHorizontalStep {
t.Errorf("default horizontalStep should be %d, got %d", defaultHorizontalStep, m.horizontalStep)
}
zero := 0
m.SetHorizontalStep(-1)
if m.horizontalStep != zero {
t.Errorf("horizontalStep should be %d, got %d", zero, m.horizontalStep)
}
})
}
func TestScrollLeft(t *testing.T) {
t.Parallel()
zeroPosition := 0
t.Run("zero position", func(t *testing.T) {
t.Parallel()
m := New(10, 10)
m.longestLineWidth = 100
if m.xOffset != zeroPosition {
t.Errorf("default indent should be %d, got %d", zeroPosition, m.xOffset)
}
m.ScrollLeft(m.horizontalStep)
if m.xOffset != zeroPosition {
t.Errorf("indent should be %d, got %d", zeroPosition, m.xOffset)
}
})
t.Run("scroll", func(t *testing.T) {
t.Parallel()
m := New(10, 10)
m.horizontalStep = defaultHorizontalStep // remove on v2
m.longestLineWidth = 100
if m.xOffset != zeroPosition {
t.Errorf("default indent should be %d, got %d", zeroPosition, m.xOffset)
}
m.xOffset = defaultHorizontalStep * 2
m.ScrollLeft(m.horizontalStep)
newIndent := defaultHorizontalStep
if m.xOffset != newIndent {
t.Errorf("indent should be %d, got %d", newIndent, m.xOffset)
}
})
}
func TestScrollRight(t *testing.T) {
t.Parallel()
t.Run("scroll", func(t *testing.T) {
t.Parallel()
zeroPosition := 0
m := New(10, 10)
m.SetHorizontalStep(defaultHorizontalStep)
m.SetContent("Some line that is longer than width")
if m.xOffset != zeroPosition {
t.Errorf("default indent should be %d, got %d", zeroPosition, m.xOffset)
}
m.ScrollRight(m.horizontalStep)
newIndent := defaultHorizontalStep
if m.xOffset != newIndent {
t.Errorf("indent should be %d, got %d", newIndent, m.xOffset)
}
})
}
func TestResetIndent(t *testing.T) {
t.Parallel()
t.Run("reset", func(t *testing.T) {
t.Parallel()
zeroPosition := 0
m := New(10, 10)
m.xOffset = 500
m.SetXOffset(0)
if m.xOffset != zeroPosition {
t.Errorf("indent should be %d, got %d", zeroPosition, m.xOffset)
}
})
}
func TestVisibleLines(t *testing.T) {
t.Parallel()
defaultList := []string{
`57 Precepts of narcissistic comedy character Zote from an awesome "Hollow knight" game (https://store.steampowered.com/app/367520/Hollow_Knight/).`,
`Precept One: 'Always Win Your Battles'. Losing a battle earns you nothing and teaches you nothing. Win your battles, or don't engage in them at all!`,
`Precept Two: 'Never Let Them Laugh at You'. Fools laugh at everything, even at their superiors. But beware, laughter isn't harmless! Laughter spreads like a disease, and soon everyone is laughing at you. You need to strike at the source of this perverse merriment quickly to stop it from spreading.`,
`Precept Three: 'Always Be Rested'. Fighting and adventuring take their toll on your body. When you rest, your body strengthens and repairs itself. The longer you rest, the stronger you become.`,
`Precept Four: 'Forget Your Past'. The past is painful, and thinking about your past can only bring you misery. Think about something else instead, such as the future, or some food.`,
`Precept Five: 'Strength Beats Strength'. Is your opponent strong? No matter! Simply overcome their strength with even more strength, and they'll soon be defeated.`,
`Precept Six: 'Choose Your Own Fate'. Our elders teach that our fate is chosen for us before we are even born. I disagree.`,
`Precept Seven: 'Mourn Not the Dead'. When we die, do things get better for us or worse? There's no way to tell, so we shouldn't bother mourning. Or celebrating for that matter.`,
`Precept Eight: 'Travel Alone'. You can rely on nobody, and nobody will always be loyal. Therefore, nobody should be your constant companion.`,
`Precept Nine: 'Keep Your Home Tidy'. Your home is where you keep your most prized possession - yourself. Therefore, you should make an effort to keep it nice and clean.`,
`Precept Ten: 'Keep Your Weapon Sharp'. I make sure that my weapon, 'Life Ender', is kept well-sharpened at all times. This makes it much easier to cut things.`,
`Precept Eleven: 'Mothers Will Always Betray You'. This Precept explains itself.`,
`Precept Twelve: 'Keep Your Cloak Dry'. If your cloak gets wet, dry it as soon as you can. Wearing wet cloaks is unpleasant, and can lead to illness.`,
`Precept Thirteen: 'Never Be Afraid'. Fear can only hold you back. Facing your fears can be a tremendous effort. Therefore, you should just not be afraid in the first place.`,
`Precept Fourteen: 'Respect Your Superiors'. If someone is your superior in strength or intellect or both, you need to show them your respect. Don't ignore them or laugh at them.`,
`Precept Fifteen: 'One Foe, One Blow'. You should only use a single blow to defeat an enemy. Any more is a waste. Also, by counting your blows as you fight, you'll know how many foes you've defeated.`,
`...`,
}
t.Run("empty list", func(t *testing.T) {
t.Parallel()
m := New(10, 10)
list := m.visibleLines()
if len(list) != 0 {
t.Errorf("list should be empty, got %d", len(list))
}
})
t.Run("empty list: with indent", func(t *testing.T) {
t.Parallel()
m := New(10, 10)
list := m.visibleLines()
m.xOffset = 5
if len(list) != 0 {
t.Errorf("list should be empty, got %d", len(list))
}
})
t.Run("list", func(t *testing.T) {
t.Parallel()
numberOfLines := 10
m := New(10, numberOfLines)
m.SetContent(strings.Join(defaultList, "\n"))
list := m.visibleLines()
if len(list) != numberOfLines {
t.Errorf("list should have %d lines, got %d", numberOfLines, len(list))
}
lastItemIdx := numberOfLines - 1
// we trim line if it doesn't fit to width of the viewport
shouldGet := defaultList[lastItemIdx][:m.Width]
if list[lastItemIdx] != shouldGet {
t.Errorf(`%dth list item should be '%s', got '%s'`, lastItemIdx, shouldGet, list[lastItemIdx])
}
})
t.Run("list: with y offset", func(t *testing.T) {
t.Parallel()
numberOfLines := 10
m := New(10, numberOfLines)
m.SetContent(strings.Join(defaultList, "\n"))
m.YOffset = 5
list := m.visibleLines()
if len(list) != numberOfLines {
t.Errorf("list should have %d lines, got %d", numberOfLines, len(list))
}
if list[0] == defaultList[0] {
t.Error("first item of list should not be the first item of initial list because of Y offset")
}
lastItemIdx := numberOfLines - 1
// we trim line if it doesn't fit to width of the viewport
shouldGet := defaultList[m.YOffset+lastItemIdx][:m.Width]
if list[lastItemIdx] != shouldGet {
t.Errorf(`%dth list item should be '%s', got '%s'`, lastItemIdx, shouldGet, list[lastItemIdx])
}
})
t.Run("list: with y offset: horizontal scroll", func(t *testing.T) {
t.Parallel()
numberOfLines := 10
m := New(10, numberOfLines)
m.horizontalStep = defaultHorizontalStep // remove on v2
m.SetContent(strings.Join(defaultList, "\n"))
m.SetYOffset(7)
// default list
list := m.visibleLines()
if len(list) != numberOfLines {
t.Errorf("list should have %d lines, got %d", numberOfLines, len(list))
}
lastItem := numberOfLines - 1
defaultLastItem := len(defaultList) - 1
if list[lastItem] != defaultList[defaultLastItem] {
t.Errorf("%dth list item should the the same as %dth default list item", lastItem, defaultLastItem)
}
perceptPrefix := "Precept"
if !strings.HasPrefix(list[0], perceptPrefix) {
t.Errorf("first list item has to have prefix %s", perceptPrefix)
}
// scroll right
m.ScrollRight(m.horizontalStep)
list = m.visibleLines()
newPrefix := perceptPrefix[m.xOffset:]
if !strings.HasPrefix(list[0], newPrefix) {
t.Errorf("first list item has to have prefix %s, get %s", newPrefix, list[0])
}
if list[lastItem] != "" {
t.Errorf("last item should be empty, got %s", list[lastItem])
}
// scroll left
m.ScrollLeft(m.horizontalStep)
list = m.visibleLines()
if !strings.HasPrefix(list[0], perceptPrefix) {
t.Errorf("first list item has to have prefix %s", perceptPrefix)
}
if list[lastItem] != defaultList[defaultLastItem] {
t.Errorf("%dth list item should the the same as %dth default list item", lastItem, defaultLastItem)
}
})
t.Run("list: with 2 cells symbols: horizontal scroll", func(t *testing.T) {
t.Parallel()
const horizontalStep = 5
initList := []string{
"あいうえお",
"Aあいうえお",
"あいうえお",
"Aあいうえお",
}
numberOfLines := len(initList)
m := New(20, numberOfLines)
m.lines = initList
m.longestLineWidth = 30 // dirty hack: not checking right overscroll for this test case
// default list
list := m.visibleLines()
if len(list) != numberOfLines {
t.Errorf("list should have %d lines, got %d", numberOfLines, len(list))
}
lastItemIdx := numberOfLines - 1
initLastItem := len(initList) - 1
shouldGet := initList[initLastItem]
if list[lastItemIdx] != shouldGet {
t.Errorf("%dth list item should the the same as %dth default list item", lastItemIdx, initLastItem)
}
// scroll right
m.ScrollRight(horizontalStep)
list = m.visibleLines()
for i := range list {
cutLine := "うえお"
if list[i] != cutLine {
t.Errorf("line must be `%s`, get `%s`", cutLine, list[i])
}
}
// scroll left
m.ScrollLeft(horizontalStep)
list = m.visibleLines()
for i := range list {
if list[i] != initList[i] {
t.Errorf("line must be `%s`, get `%s`", list[i], initList[i])
}
}
// scroll left second times do not change lites if indent == 0
m.xOffset = 0
m.ScrollLeft(horizontalStep)
list = m.visibleLines()
for i := range list {
if list[i] != initList[i] {
t.Errorf("line must be `%s`, get `%s`", list[i], initList[i])
}
}
})
}
func TestRightOverscroll(t *testing.T) {
t.Parallel()
t.Run("prevent right overscroll", func(t *testing.T) {
t.Parallel()
content := "Content is short"
m := New(len(content)+1, 5)
m.SetContent(content)
for i := 0; i < 10; i++ {
m.ScrollRight(m.horizontalStep)
}
visibleLines := m.visibleLines()
visibleLine := visibleLines[0]
if visibleLine != content {
t.Error("visible line should stay the same as content")
}
})
}
|