internal/control/search_test.go
54 lines · 1968 bytes
1package control
2
3import (
4 "bytes"
5 "strings"
6 "testing"
7)
8
9// A repo hit has no state, an issue/mr hit does. At a terminal both must
10// still put TITLE under the TITLE header, not have the repo's title slide
11// left under STATE.
12func TestSearchTableAlignsTitleAtTerminal(t *testing.T) {
13 results := []SearchResult{
14 {Kind: "repo", Repo: "alice/webapp", Title: "a web application"},
15 {Kind: "issue", Repo: "alice/webapp", Number: 4, Title: "memory leak", State: "open"},
16 }
17 var b bytes.Buffer
18 writeSearchTable(&Ctx{Term: Term{Cols: 100}}, &b, results)
19
20 lines := strings.Split(strings.TrimRight(b.String(), "\n"), "\n")
21 if len(lines) != 3 {
22 t.Fatalf("want header + 2 rows, got %d lines:\n%s", len(lines), b.String())
23 }
24 header, repoRow, issueRow := lines[0], lines[1], lines[2]
25 titleAt := strings.Index(header, "TITLE")
26 repoTitleAt := strings.Index(repoRow, "a web application")
27 issueTitleAt := strings.Index(issueRow, "memory leak")
28 if titleAt < 0 || repoTitleAt < 0 || issueTitleAt < 0 {
29 t.Fatalf("columns not found:\n%s", b.String())
30 }
31 if repoTitleAt != issueTitleAt {
32 t.Errorf("titles not aligned: repo row at %d, issue row at %d\n%s", repoTitleAt, issueTitleAt, b.String())
33 }
34 if repoTitleAt != titleAt {
35 t.Errorf("title not under TITLE header: header at %d, repo row at %d\n%s", titleAt, repoTitleAt, b.String())
36 }
37}
38
39// Plain output has no header to align to, so a repo row stays 3 cells —
40// bytes must not change from before the terminal fix.
41func TestSearchTablePlainRepoRowIsThreeCells(t *testing.T) {
42 results := []SearchResult{
43 {Kind: "repo", Repo: "alice/webapp", Title: "a web application"},
44 {Kind: "issue", Repo: "alice/webapp", Number: 4, Title: "memory leak", State: "open"},
45 }
46 var b bytes.Buffer
47 writeSearchTable(&Ctx{}, &b, results)
48
49 want := "repo\talice/webapp\ta web application\n" +
50 "issue\talice/webapp#4\topen\tmemory leak\n"
51 if b.String() != want {
52 t.Errorf("plain:\n%q\nwant\n%q", b.String(), want)
53 }
54}