bug_test.go

 1package bugcmd
 2
 3import (
 4	"encoding/json"
 5	"testing"
 6
 7	"github.com/stretchr/testify/require"
 8
 9	"github.com/MichaelMure/git-bug/commands/bug/testenv"
10	"github.com/MichaelMure/git-bug/commands/cmdjson"
11)
12
13func Test_repairQuery(t *testing.T) {
14	cases := []struct {
15		args   []string
16		output string
17	}{
18		{
19			[]string{""},
20			"",
21		},
22		{
23			[]string{"foo"},
24			"foo",
25		},
26		{
27			[]string{"foo", "bar"},
28			"foo bar",
29		},
30		{
31			[]string{"foo bar", "baz"},
32			"\"foo bar\" baz",
33		},
34		{
35			[]string{"foo:bar", "baz"},
36			"foo:bar baz",
37		},
38		{
39			[]string{"foo:bar boo", "baz"},
40			"foo:\"bar boo\" baz",
41		},
42	}
43
44	for _, tc := range cases {
45		require.Equal(t, tc.output, repairQuery(tc.args))
46	}
47}
48
49func TestBug_Format(t *testing.T) {
50	const expOrgMode = `^#+TODO: OPEN | CLOSED
51[*] 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 ::
52[*]{2} Last Edited: \[\d\d\d\d-\d\d-\d\d [[:alpha:]]{3} \d\d:\d\d\]
53[*]{2} Actors:
54: [0-9a-f]{7} John Doe
55[*]{2} Participants:
56: [0-9a-f]{7} John Doe
57$`
58
59	cases := []struct {
60		format string
61		exp    string
62	}{
63		{"default", "^[0-9a-f]{7}\topen\tthis is a bug title                      John Doe        \n$"},
64		{"plain", "^[0-9a-f]{7}\topen\tthis is a bug title\n$"},
65		{"id", "^[0-9a-f]{64}\n$"},
66		{"org-mode", expOrgMode},
67		{"json", ".*"},
68	}
69
70	for _, testcase := range cases {
71		t.Run(testcase.format, func(t *testing.T) {
72			env, _ := testenv.NewTestEnvAndBug(t)
73
74			opts := bugOptions{
75				sortDirection: "asc",
76				sortBy:        "creation",
77				outputFormat:  testcase.format,
78			}
79
80			require.NoError(t, runBug(env, opts, []string{}))
81
82			switch testcase.format {
83			case "json":
84				var bugs []cmdjson.BugExcerpt
85				require.NoError(t, json.Unmarshal(env.Out.Bytes(), &bugs))
86				require.Len(t, bugs, 1)
87			default:
88				require.Regexp(t, testcase.exp, env.Out.String())
89			}
90		})
91	}
92}