| 1 | package docs |
| 2 | |
| 3 | import ( |
| 4 | "reflect" |
| 5 | "testing" |
| 6 | ) |
| 7 | |
| 8 | // The shelf takes the newest rows across groups, in order; a group the |
| 9 | // shelf emptied is dropped, and one it split keeps its label. |
| 10 | func TestSplitRecent(t *testing.T) { |
| 11 | rows := func(titles ...string) []docRow { |
| 12 | var out []docRow |
| 13 | for _, s := range titles { |
| 14 | out = append(out, docRow{Title: s}) |
| 15 | } |
| 16 | return out |
| 17 | } |
| 18 | titles := func(rs []docRow) []string { |
| 19 | var out []string |
| 20 | for _, r := range rs { |
| 21 | out = append(out, r.Title) |
| 22 | } |
| 23 | return out |
| 24 | } |
| 25 | groups := []docGroup{ |
| 26 | {Label: "Today", Rows: rows("a", "b")}, |
| 27 | {Label: "Yesterday", Rows: rows("c")}, |
| 28 | {Label: "Earlier", Rows: rows("d", "e", "f", "g")}, |
| 29 | } |
| 30 | |
| 31 | recent, rest := splitRecent(groups, 5) |
| 32 | if got := titles(recent); !reflect.DeepEqual(got, []string{"a", "b", "c", "d", "e"}) { |
| 33 | t.Errorf("shelf = %v", got) |
| 34 | } |
| 35 | if len(rest) != 1 || rest[0].Label != "Earlier" || !reflect.DeepEqual(titles(rest[0].Rows), []string{"f", "g"}) { |
| 36 | t.Errorf("rows after the shelf = %+v", rest) |
| 37 | } |
| 38 | |
| 39 | recent, rest = splitRecent(groups[:1], 5) |
| 40 | if len(recent) != 2 || rest != nil { |
| 41 | t.Errorf("fewer documents than the shelf holds: shelf %d, groups %+v", len(recent), rest) |
| 42 | } |
| 43 | if recent, rest = splitRecent(nil, 5); recent != nil || rest != nil { |
| 44 | t.Errorf("no documents: shelf %v, groups %v", recent, rest) |
| 45 | } |
| 46 | } |
| 47 | |