File: list_test.go

package info (click to toggle)
golang-github-manifoldco-promptui 0.8.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 264 kB
  • sloc: makefile: 33
file content (164 lines) | stat: -rw-r--r-- 3,691 bytes parent folder | download | duplicates (2)
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
package list

import (
	"fmt"
	"reflect"
	"testing"
)

func TestListNew(t *testing.T) {
	t.Run("when items a slice nil", func(t *testing.T) {
		_, err := New([]int{1, 2, 3}, 3)
		if err != nil {
			t.Errorf("Expected no errors, error %v", err)
		}
	})

	t.Run("when items is nil", func(t *testing.T) {
		_, err := New(nil, 3)
		if err == nil {
			t.Errorf("Expected error got none")
		}
	})

	t.Run("when items is not a slice", func(t *testing.T) {
		_, err := New("1,2,3", 3)
		if err == nil {
			t.Errorf("Expected error got none")
		}
	})
}

func TestListMovement(t *testing.T) {
	letters := []rune{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'}

	l, err := New(letters, 4)
	if err != nil {
		t.Fatalf("Expected no error, got %v", err)
	}

	tcs := []struct {
		expect   []rune
		move     string
		selected rune
	}{
		{move: "next", selected: 'b', expect: []rune{'a', 'b', 'c', 'd'}},
		{move: "prev", selected: 'a', expect: []rune{'a', 'b', 'c', 'd'}},
		{move: "prev", selected: 'a', expect: []rune{'a', 'b', 'c', 'd'}},
		{move: "next", selected: 'b', expect: []rune{'a', 'b', 'c', 'd'}},
		{move: "next", selected: 'c', expect: []rune{'a', 'b', 'c', 'd'}},
		{move: "next", selected: 'd', expect: []rune{'a', 'b', 'c', 'd'}},
		{move: "next", selected: 'e', expect: []rune{'b', 'c', 'd', 'e'}},
		{move: "prev", selected: 'd', expect: []rune{'b', 'c', 'd', 'e'}},
		{move: "up", selected: 'a', expect: []rune{'a', 'b', 'c', 'd'}},
		{move: "up", selected: 'a', expect: []rune{'a', 'b', 'c', 'd'}},
		{move: "down", selected: 'e', expect: []rune{'e', 'f', 'g', 'h'}},
		{move: "down", selected: 'g', expect: []rune{'g', 'h', 'i', 'j'}},
		{move: "down", selected: 'j', expect: []rune{'g', 'h', 'i', 'j'}},
	}

	for _, tc := range tcs {
		t.Run(fmt.Sprintf("list %s", tc.move), func(t *testing.T) {
			switch tc.move {
			case "next":
				l.Next()
			case "prev":
				l.Prev()
			case "up":
				l.PageUp()
			case "down":
				l.PageDown()
			default:
				t.Fatalf("unknown move %q", tc.move)
			}

			list, idx := l.Items()

			got := castList(list)

			if !reflect.DeepEqual(tc.expect, got) {
				t.Errorf("expected %q, got %q", tc.expect, got)
			}

			selected := list[idx]

			if tc.selected != selected {
				t.Errorf("expected selected to be %q, got %q", tc.selected, selected)
			}
		})
	}
}

func TestListPageDown(t *testing.T) {
	t.Run("when list has fewer items than page size", func(t *testing.T) {
		letters := []rune{'a', 'b'}
		l, err := New(letters, 4)
		if err != nil {
			t.Fatalf("Expected no error, got %v", err)
		}

		l.PageDown()
		list, idx := l.Items()

		expected := 'b'
		selected := list[idx]

		if selected != expected {
			t.Errorf("expected selected to be %q, got %q", expected, selected)
		}
	})
}

func TestListComparion(t *testing.T) {
	t.Run("when item supports comparison", func(t *testing.T) {
		type comparable struct {
			Number int
		}

		structs := []comparable{
			{Number: 1},
			{Number: 2},
		}

		l, err := New(structs, 4)
		if err != nil {
			t.Fatalf("Expected no error, got %v", err)
		}

		idx := l.Index()

		if idx != 0 {
			t.Errorf("expected index to be first, got %d", idx)
		}
	})

	t.Run("when item doesn't support comparison", func(t *testing.T) {
		type uncomparable struct {
			Numbers []int
		}

		structs := []uncomparable{
			{Numbers: []int{1}},
			{Numbers: []int{2}},
		}

		l, err := New(structs, 4)
		if err != nil {
			t.Fatalf("Expected no error, got %v", err)
		}

		idx := l.Index()

		if idx != 0 {
			t.Errorf("expected index to be first, got %d", idx)
		}
	})
}

func castList(list []interface{}) []rune {
	result := make([]rune, len(list))
	for i, l := range list {
		result[i] = l.(rune)
	}
	return result
}