bug_test.go

  1package bugcmd
  2
  3import (
  4	"testing"
  5
  6	"github.com/stretchr/testify/require"
  7
  8	"github.com/MichaelMure/git-bug/commands/bug/testenv"
  9	. "github.com/MichaelMure/git-bug/commands/cmdtest"
 10)
 11
 12func Test_repairQuery(t *testing.T) {
 13	cases := []struct {
 14		args   []string
 15		output string
 16	}{
 17		{
 18			[]string{""},
 19			"",
 20		},
 21		{
 22			[]string{"foo"},
 23			"foo",
 24		},
 25		{
 26			[]string{"foo", "bar"},
 27			"foo bar",
 28		},
 29		{
 30			[]string{"foo bar", "baz"},
 31			"\"foo bar\" baz",
 32		},
 33		{
 34			[]string{"foo:bar", "baz"},
 35			"foo:bar baz",
 36		},
 37		{
 38			[]string{"foo:bar boo", "baz"},
 39			"foo:\"bar boo\" baz",
 40		},
 41	}
 42
 43	for _, tc := range cases {
 44		require.Equal(t, tc.output, repairQuery(tc.args))
 45	}
 46}
 47
 48func TestBug_Format(t *testing.T) {
 49	const expOrgMode = `#+TODO: OPEN | CLOSED
 50* OPEN   ` + ExpHumanId + ` [` + ExpOrgModeDate + `] John Doe: this is a bug title ::
 51** Last Edited: [` + ExpOrgModeDate + `]
 52** Actors:
 53: ` + ExpHumanId + ` John Doe
 54** Participants:
 55: ` + ExpHumanId + ` John Doe
 56`
 57
 58	const expJson = `[
 59    {
 60        "id": "` + ExpId + `",
 61        "human_id": "` + ExpHumanId + `",
 62        "create_time": {
 63            "timestamp": ` + ExpTimestamp + `,
 64            "time": "` + ExpISO8601 + `",
 65            "lamport": 2
 66        },
 67        "edit_time": {
 68            "timestamp": ` + ExpTimestamp + `,
 69            "time": "` + ExpISO8601 + `",
 70            "lamport": 2
 71        },
 72        "status": "open",
 73        "labels": null,
 74        "title": "this is a bug title",
 75        "actors": [
 76            {
 77                "id": "` + ExpId + `",
 78                "human_id": "` + ExpHumanId + `",
 79                "name": "John Doe",
 80                "login": ""
 81            }
 82        ],
 83        "participants": [
 84            {
 85                "id": "` + ExpId + `",
 86                "human_id": "` + ExpHumanId + `",
 87                "name": "John Doe",
 88                "login": ""
 89            }
 90        ],
 91        "author": {
 92            "id": "` + ExpId + `",
 93            "human_id": "` + ExpHumanId + `",
 94            "name": "John Doe",
 95            "login": ""
 96        },
 97        "comments": 1,
 98        "metadata": {}
 99    }
100]
101`
102
103	cases := []struct {
104		format string
105		exp    string
106	}{
107		{"default", ExpHumanId + "\topen\tthis is a bug title                      John Doe        \n"},
108		{"plain", ExpHumanId + "\topen\tthis is a bug title\n"},
109		{"id", ExpId + "\n"},
110		{"org-mode", expOrgMode},
111		{"json", expJson},
112	}
113
114	for _, testcase := range cases {
115		t.Run(testcase.format, func(t *testing.T) {
116			env, _ := testenv.NewTestEnvAndBug(t)
117
118			opts := bugOptions{
119				sortDirection:       "asc",
120				sortBy:              "creation",
121				outputFormat:        testcase.format,
122				outputFormatChanged: true, // disable auto-detect
123			}
124
125			require.NoError(t, runBug(env, opts, []string{}))
126			require.Regexp(t, MakeExpectedRegex(testcase.exp), env.Out.String())
127		})
128	}
129}