-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel_test.go
92 lines (75 loc) · 1.99 KB
/
model_test.go
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
package rebase
import (
"testing"
tea "github.com/charmbracelet/bubbletea"
"github.com/google/go-cmp/cmp"
"gotest.tools/v3/assert"
)
func TestNoChange(t *testing.T) {
m := prepareModel(Commits{
{command: CmdPick, hash: "00000000", title: "foo"},
})
m, cmd := applyKeyMsgs(m, []tea.KeyMsg{
{Type: tea.KeyEnter},
})
assert.Equal(t, cmd(), tea.Quit())
assertCommits(t, m, Commits{
{command: CmdPick, hash: "00000000", title: "foo"},
})
}
func TestCancel(t *testing.T) {
m := prepareModel(Commits{
{command: CmdPick, hash: "00000001", title: "1st"},
})
m, cmd := applyKeyMsgs(m, []tea.KeyMsg{
{Type: tea.KeyEsc},
})
assert.Equal(t, cmd(), tea.Quit())
// returns empty commits to abort rebasing
assertCommits(t, m, Commits{})
}
func TestCommandChange(t *testing.T) {
m := prepareModel(Commits{
{command: CmdPick, hash: "00000001", title: "1st"},
{command: CmdPick, hash: "00000002", title: "2nd"},
})
m, _ = applyKeyMsgs(m, []tea.KeyMsg{
{Type: tea.KeyDown},
{Type: tea.KeyRunes, Runes: []rune{'e'}},
})
assertCommits(t, m, Commits{
{command: CmdPick, hash: "00000001", title: "1st"},
{command: CmdEdit, hash: "00000002", title: "2nd"},
})
}
func TestMove(t *testing.T) {
m := prepareModel(Commits{
{command: CmdPick, hash: "00000001", title: "1st"},
{command: CmdPick, hash: "00000002", title: "2nd"},
})
m, _ = applyKeyMsgs(m, []tea.KeyMsg{
{Type: tea.KeyDown},
{Type: tea.KeyLeft},
{Type: tea.KeyEnter},
})
assertCommits(t, m, Commits{
{command: CmdPick, hash: "00000002", title: "2nd"},
{command: CmdPick, hash: "00000001", title: "1st"},
})
}
func prepareModel(commits Commits) tea.Model {
m := NewModel(commits)
m.Init()
return m
}
func applyKeyMsgs(m tea.Model, msgs []tea.KeyMsg) (tea.Model, tea.Cmd) {
var cmd tea.Cmd
for _, v := range msgs {
m, cmd = m.Update(v)
}
return m, cmd
}
func assertCommits(t *testing.T, m tea.Model, commits Commits) {
model := m.(Model)
assert.DeepEqual(t, model.commits, commits, cmp.AllowUnexported(Commit{}))
}