1package commands
2
3import (
4 "encoding/json"
5 "fmt"
6 "testing"
7
8 "github.com/stretchr/testify/require"
9)
10
11func Test_repairQuery(t *testing.T) {
12 cases := []struct {
13 args []string
14 output string
15 }{
16 {
17 []string{""},
18 "",
19 },
20 {
21 []string{"foo"},
22 "foo",
23 },
24 {
25 []string{"foo", "bar"},
26 "foo bar",
27 },
28 {
29 []string{"foo bar", "baz"},
30 "\"foo bar\" baz",
31 },
32 {
33 []string{"foo:bar", "baz"},
34 "foo:bar baz",
35 },
36 {
37 []string{"foo:bar boo", "baz"},
38 "foo:\"bar boo\" baz",
39 },
40 }
41
42 for _, tc := range cases {
43 require.Equal(t, tc.output, repairQuery(tc.args))
44 }
45}
46
47func TestLs_Format(t *testing.T) {
48 const expOrgMode = `^#+TODO: OPEN | CLOSED
49[*] OPEN [0-9a-f]{7} \[\d\d\d\d-\d\d-\d\d [[:alpha:]]{3} \d\d:\d\d\] John Doe: this is a bug title ::
50[*]{2} Last Edited: \[\d\d\d\d-\d\d-\d\d [[:alpha:]]{3} \d\d:\d\d\]
51[*]{2} Actors:
52: [0-9a-f]{7} John Doe
53[*]{2} Participants:
54: [0-9a-f]{7} John Doe
55$`
56
57 cases := []struct {
58 format string
59 exp string
60 }{
61 {"default", "^[0-9a-f]{7}\topen\tthis is a bug title \tJohn Doe \t\n$"},
62 {"plain", "^[0-9a-f]{7} \\[open\\] this is a bug title\n$"},
63 {"compact", "^[0-9a-f]{7} open this is a bug title John Doe\n$"},
64 {"org-mode", expOrgMode},
65 }
66
67 for _, testcase := range cases {
68 opts := lsOptions{
69 sortDirection: "asc",
70 sortBy: "creation",
71 outputFormat: testcase.format,
72 }
73
74 name := fmt.Sprintf("with %s format", testcase.format)
75
76 t.Run(name, func(t *testing.T) {
77 env, _, _ := newTestEnvAndBug(t)
78
79 require.NoError(t, runLs(env.env, opts, []string{}))
80 t.Log(env.out.String())
81 require.Regexp(t, testcase.exp, env.out.String())
82 require.Equal(t, "", env.err.String())
83 })
84 }
85
86 t.Run("with JSON format", func(t *testing.T) {
87 opts := lsOptions{
88 sortDirection: "asc",
89 sortBy: "creation",
90 outputFormat: "json",
91 }
92
93 env, _, _ := newTestEnvAndBug(t)
94
95 require.NoError(t, runLs(env.env, opts, []string{}))
96
97 bugs := []JSONBugExcerpt{}
98 require.NoError(t, json.Unmarshal(env.out.Bytes(), &bugs))
99 require.Empty(t, env.err.Bytes())
100
101 require.Len(t, bugs, 1)
102 })
103}