| 1 |
package patch |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"reflect" |
| 6 |
"strings" |
| 7 |
"testing" |
| 8 |
|
| 9 |
"git.sr.ht/~rjarry/aerc/lib/pama/models" |
| 10 |
) |
| 11 |
|
| 12 |
func TestRebase_reorder(t *testing.T) { |
| 13 |
newCommits := func(order []string) []models.Commit { |
| 14 |
var commits []models.Commit |
| 15 |
for _, s := range order { |
| 16 |
commits = append(commits, models.Commit{ID: s}) |
| 17 |
} |
| 18 |
return commits |
| 19 |
} |
| 20 |
tests := []struct { |
| 21 |
name string |
| 22 |
commits []models.Commit |
| 23 |
now []string |
| 24 |
by []string |
| 25 |
want []models.Commit |
| 26 |
}{ |
| 27 |
{ |
| 28 |
name: "nothing to reorder", |
| 29 |
commits: newCommits([]string{"1", "2", "3"}), |
| 30 |
now: []string{"1", "2", "3"}, |
| 31 |
by: []string{"1", "2", "3"}, |
| 32 |
want: newCommits([]string{"1", "2", "3"}), |
| 33 |
}, |
| 34 |
{ |
| 35 |
name: "reorder", |
| 36 |
commits: newCommits([]string{"1", "3", "2"}), |
| 37 |
now: []string{"1", "3", "2"}, |
| 38 |
by: []string{"1", "2", "3"}, |
| 39 |
want: newCommits([]string{"1", "2", "3"}), |
| 40 |
}, |
| 41 |
{ |
| 42 |
name: "reorder inverted", |
| 43 |
commits: newCommits([]string{"3", "2", "1"}), |
| 44 |
now: []string{"3", "2", "1"}, |
| 45 |
by: []string{"1", "2", "3"}, |
| 46 |
want: newCommits([]string{"1", "2", "3"}), |
| 47 |
}, |
| 48 |
{ |
| 49 |
name: "changed hash: do not sort", |
| 50 |
commits: newCommits([]string{"1", "6", "3"}), |
| 51 |
now: []string{"1", "6", "3"}, |
| 52 |
by: []string{"1", "2", "3"}, |
| 53 |
want: newCommits([]string{"1", "6", "3"}), |
| 54 |
}, |
| 55 |
} |
| 56 |
|
| 57 |
for _, test := range tests { |
| 58 |
reorder(test.commits, test.now, test.by) |
| 59 |
if !reflect.DeepEqual(test.commits, test.want) { |
| 60 |
t.Errorf("test '%s' failed to reorder: got %v but "+ |
| 61 |
"want %v", test.name, test.commits, test.want) |
| 62 |
} |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
func newCommit(id, subj, tag string) models.Commit { |
| 67 |
return models.Commit{ |
| 68 |
ID: id, |
| 69 |
Subject: subj, |
| 70 |
Tag: tag, |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
func TestRebase_parse(t *testing.T) { |
| 75 |
input := ` |
| 76 |
# some header info |
| 77 |
hello_v1 123 same info |
| 78 |
hello_v1 456 same info |
| 79 |
untracked 789 same info |
| 80 |
hello_v2 012 diff info |
| 81 |
untracked 345 diff info # not very useful comment |
| 82 |
# some footer info |
| 83 |
` |
| 84 |
commits := []models.Commit{ |
| 85 |
newCommit("123123", "same info", "hello_v1"), |
| 86 |
newCommit("456456", "same info", "hello_v1"), |
| 87 |
newCommit("789789", "same info", models.Untracked), |
| 88 |
newCommit("012012", "diff info", "hello_v2"), |
| 89 |
newCommit("345345", "diff info", models.Untracked), |
| 90 |
} |
| 91 |
|
| 92 |
var order []string |
| 93 |
for _, c := range commits { |
| 94 |
order = append(order, fmt.Sprintf("%3.3s", c.ID)) |
| 95 |
} |
| 96 |
|
| 97 |
table := make(map[string]models.Commit) |
| 98 |
for i, shortId := range order { |
| 99 |
table[shortId] = commits[i] |
| 100 |
} |
| 101 |
|
| 102 |
rebase := &rebase{ |
| 103 |
commits: commits, |
| 104 |
table: table, |
| 105 |
order: order, |
| 106 |
} |
| 107 |
|
| 108 |
results := rebase.parse(strings.NewReader(input)) |
| 109 |
|
| 110 |
if len(results) != 3 { |
| 111 |
t.Errorf("failed to return correct number of commits: "+ |
| 112 |
"got %d but wanted 3", len(results)) |
| 113 |
} |
| 114 |
} |