| 1 |
package iterator_test |
| 2 |
|
| 3 |
import ( |
| 4 |
"testing" |
| 5 |
|
| 6 |
"git.sr.ht/~rjarry/aerc/lib/iterator" |
| 7 |
"git.sr.ht/~rjarry/aerc/models" |
| 8 |
"git.sr.ht/~rjarry/aerc/worker/types" |
| 9 |
) |
| 10 |
|
| 11 |
func toThreads(uids []models.UID) []*types.Thread { |
| 12 |
threads := make([]*types.Thread, len(uids)) |
| 13 |
for i, u := range uids { |
| 14 |
threads[i] = &types.Thread{Uid: u} |
| 15 |
} |
| 16 |
return threads |
| 17 |
} |
| 18 |
|
| 19 |
func TestIterator_DefaultFactory(t *testing.T) { |
| 20 |
input := []models.UID{"1", "2", "3", "4", "5", "6", "7", "8", "9"} |
| 21 |
want := []models.UID{"9", "8", "7", "6", "5", "4", "3", "2", "1"} |
| 22 |
|
| 23 |
factory := iterator.NewFactory(false) |
| 24 |
if factory == nil { |
| 25 |
t.Errorf("could not create factory") |
| 26 |
} |
| 27 |
start, end := len(input)-1, 0 |
| 28 |
checkUids(t, factory, input, want, start, end) |
| 29 |
checkThreads(t, factory, toThreads(input), |
| 30 |
toThreads(want), start, end) |
| 31 |
} |
| 32 |
|
| 33 |
func TestIterator_ReverseFactory(t *testing.T) { |
| 34 |
input := []models.UID{"1", "2", "3", "4", "5", "6", "7", "8", "9"} |
| 35 |
want := []models.UID{"1", "2", "3", "4", "5", "6", "7", "8", "9"} |
| 36 |
|
| 37 |
factory := iterator.NewFactory(true) |
| 38 |
if factory == nil { |
| 39 |
t.Errorf("could not create factory") |
| 40 |
} |
| 41 |
|
| 42 |
start, end := 0, len(input)-1 |
| 43 |
checkUids(t, factory, input, want, start, end) |
| 44 |
checkThreads(t, factory, toThreads(input), |
| 45 |
toThreads(want), start, end) |
| 46 |
} |
| 47 |
|
| 48 |
func checkUids(t *testing.T, factory iterator.Factory, |
| 49 |
input []models.UID, want []models.UID, start, end int, |
| 50 |
) { |
| 51 |
label := "uids" |
| 52 |
got := make([]models.UID, 0) |
| 53 |
iter := factory.NewIterator(input) |
| 54 |
for iter.Next() { |
| 55 |
got = append(got, iter.Value().(models.UID)) |
| 56 |
} |
| 57 |
if len(got) != len(want) { |
| 58 |
t.Errorf("%s: number of elements not correct", label) |
| 59 |
} |
| 60 |
for i, u := range want { |
| 61 |
if got[i] != u { |
| 62 |
t.Errorf("%s: order not correct", label) |
| 63 |
} |
| 64 |
} |
| 65 |
if iter.StartIndex() != start { |
| 66 |
t.Errorf("%s: start index not correct", label) |
| 67 |
} |
| 68 |
if iter.EndIndex() != end { |
| 69 |
t.Errorf("%s: end index not correct", label) |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
func checkThreads(t *testing.T, factory iterator.Factory, |
| 74 |
input []*types.Thread, want []*types.Thread, start, end int, |
| 75 |
) { |
| 76 |
label := "threads" |
| 77 |
got := make([]*types.Thread, 0) |
| 78 |
iter := factory.NewIterator(input) |
| 79 |
for iter.Next() { |
| 80 |
got = append(got, iter.Value().(*types.Thread)) |
| 81 |
} |
| 82 |
if len(got) != len(want) { |
| 83 |
t.Errorf("%s: number of elements not correct", label) |
| 84 |
} |
| 85 |
for i, th := range want { |
| 86 |
if got[i].Uid != th.Uid { |
| 87 |
t.Errorf("%s: order not correct", label) |
| 88 |
} |
| 89 |
} |
| 90 |
if iter.StartIndex() != start { |
| 91 |
t.Errorf("%s: start index not correct", label) |
| 92 |
} |
| 93 |
if iter.EndIndex() != end { |
| 94 |
t.Errorf("%s: end index not correct", label) |
| 95 |
} |
| 96 |
} |