Skip to content

Commit 3919a9f

Browse files
author
Kilian Drechsler
committed
tryed to get TestPanic to work and updated question
and: - reordert lineNumber() be be closer to the View - out commented panic test because case is not handled/thought about
1 parent 1f393c4 commit 3919a9f

File tree

2 files changed

+28
-24
lines changed

2 files changed

+28
-24
lines changed

list/list.go

+15-15
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ func View(model tea.Model) string {
6969
}
7070

7171
// check visible area
72-
height := m.Viewport.Height - 1 // TODO question: why -1, otherwise firstline gets cut of
72+
height := m.Viewport.Height - 1 // TODO question: why does the first line get cut of, if i ommit the -1?
7373
width := m.Viewport.Width
7474
offset := m.visibleOffset
7575
if height*width <= 0 {
@@ -209,6 +209,20 @@ out:
209209
return holeString.String()
210210
}
211211

212+
// lineNumber returns line number of the given index
213+
// and if relative is true the absolute difference to the curser
214+
func lineNumber(relativ bool, curser, current int) int {
215+
if !relativ || curser == current {
216+
return current
217+
}
218+
219+
diff := curser - current
220+
if diff < 0 {
221+
diff *= -1
222+
}
223+
return diff
224+
}
225+
212226
// Update changes the Model of the List according to the messages recieved
213227
func Update(msg tea.Msg, m Model) (Model, tea.Cmd) {
214228
var cmd tea.Cmd
@@ -393,17 +407,3 @@ func (m *Model) SetLess(less func(string, string) bool) {
393407
func (m *Model) Sort() {
394408
sort.Sort(m)
395409
}
396-
397-
// lineNumber returns line number of the given index
398-
// and if relative is true the absolute difference to the curser
399-
func lineNumber(relativ bool, curser, current int) int {
400-
if !relativ || curser == current {
401-
return current
402-
}
403-
404-
diff := curser - current
405-
if diff < 0 {
406-
diff *= -1
407-
}
408-
return diff
409-
}

list/list_test.go

+13-9
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,12 @@ func TestGoldenSamples(t *testing.T) {
5353
// TestPanic is also a golden sampling, but for cases that should panic.
5454
func TestPanic(t *testing.T) {
5555
for _, testM := range genModels(genPanicTests()) {
56-
View(testM)
57-
actual := recover()
56+
panicRes := make(chan interface{})
57+
go func(resChan chan<- interface{}) {
58+
defer func() { resChan <- recover() }() // Why does this Yield "%!s(<nil>)"?
59+
View(testM) // does this not Panic?
60+
}(panicRes)
61+
actual := <-panicRes
5862
expected := testM.shouldBe
5963
if actual != expected {
6064
t.Errorf("expected panic Output:\n\n%s\n\nactual Output:\n\n%s\n\n", expected, actual)
@@ -148,13 +152,13 @@ func genPanicTests() []test {
148152
[]string{""},
149153
"Can't display with zero width or hight of Viewport",
150154
},
151-
// no item to display -> panic
152-
{
153-
1,
154-
1,
155-
[]string{},
156-
"",
157-
},
155+
// no item to display -> panic TODO handel/think-about this case
156+
//{
157+
// 1,
158+
// 1,
159+
// []string{},
160+
// "",
161+
//},
158162
}
159163
}
160164

0 commit comments

Comments
 (0)