issue_test.go 6.3 KB
Newer Older
1 2 3 4
package commands

import (
	"testing"
5
	"time"
6 7 8 9 10 11 12 13 14 15 16 17 18

	"github.com/github/hub/github"
)

type formatIssueTest struct {
	name     string
	issue    github.Issue
	format   string
	colorize bool
	expect   string
}

func testFormatIssue(t *testing.T, tests []formatIssueTest) {
19
	t.Helper()
20 21 22 23 24 25 26 27
	for _, test := range tests {
		if got := formatIssue(test.issue, test.format, test.colorize); got != test.expect {
			t.Errorf("%s: formatIssue(..., %q, %t) = %q, want %q", test.name, test.format, test.colorize, got, test.expect)
		}
	}
}

func TestFormatIssue(t *testing.T) {
28
	format := "%sC%>(8)%i%Creset  %t%  l%n"
29 30 31 32
	testFormatIssue(t, []formatIssueTest{
		{
			name: "standard usage",
			issue: github.Issue{
33 34 35 36 37 38
				Number:    42,
				Title:     "Just an Issue",
				State:     "open",
				User:      &github.User{Login: "pcorpet"},
				Body:      "Body of the\nissue",
				Assignees: []github.User{{Login: "mislav"}},
39 40 41 42 43 44 45 46 47 48 49
			},
			format:   format,
			colorize: true,
			expect:   "\033[32m     #42\033[m  Just an Issue\n",
		},
		{
			name: "closed issue colored differently",
			issue: github.Issue{
				Number: 42,
				Title:  "Just an Issue",
				State:  "closed",
50
				User:   &github.User{Login: "octocat"},
51 52 53 54 55 56 57 58 59 60 61
			},
			format:   format,
			colorize: true,
			expect:   "\033[31m     #42\033[m  Just an Issue\n",
		},
		{
			name: "labels",
			issue: github.Issue{
				Number: 42,
				Title:  "An issue with labels",
				State:  "open",
62
				User:   &github.User{Login: "octocat"},
63 64 65 66 67 68 69
				Labels: []github.IssueLabel{
					{Name: "bug", Color: "800000"},
					{Name: "reproduced", Color: "55ff55"},
				},
			},
			format:   format,
			colorize: true,
70
			expect:   "\033[32m     #42\033[m  An issue with labels  \033[38;2;255;255;255;48;2;128;0;0m bug \033[m \033[38;2;0;0;0;48;2;85;255;85m reproduced \033[m\n",
71 72 73 74 75 76 77
		},
		{
			name: "not colorized",
			issue: github.Issue{
				Number: 42,
				Title:  "Just an Issue",
				State:  "open",
78
				User:   &github.User{Login: "octocat"},
79 80 81 82 83 84 85 86 87 88 89
			},
			format:   format,
			colorize: false,
			expect:   "     #42  Just an Issue\n",
		},
		{
			name: "labels not colorized",
			issue: github.Issue{
				Number: 42,
				Title:  "An issue with labels",
				State:  "open",
90
				User:   &github.User{Login: "octocat"},
91 92 93 94 95 96 97 98 99 100 101 102 103
				Labels: []github.IssueLabel{
					{Name: "bug", Color: "880000"},
					{Name: "reproduced", Color: "55ff55"},
				},
			},
			format:   format,
			colorize: false,
			expect:   "     #42  An issue with labels   bug   reproduced \n",
		},
	})
}

func TestFormatIssue_customFormatString(t *testing.T) {
104
	createdAt, err := time.Parse(time.RFC822Z, "16 Mar 15 12:34 +0000")
105 106 107
	if err != nil {
		t.Fatal(err)
	}
108
	updatedAt, err := time.Parse(time.RFC822Z, "17 Mar 15 12:34 +0900")
109 110 111 112
	if err != nil {
		t.Fatal(err)
	}

113
	issue := github.Issue{
114 115 116 117 118 119 120 121 122
		Number: 42,
		Title:  "Just an Issue",
		State:  "open",
		User:   &github.User{Login: "pcorpet"},
		Body:   "Body of the\nissue",
		Assignees: []github.User{
			{Login: "mislav"},
			{Login: "josh"},
		},
123 124
		Labels: []github.IssueLabel{
			{Name: "bug", Color: "880000"},
125 126
			{Name: "feature", Color: "008800"},
		},
127
		HTMLURL:  "the://url",
128 129 130 131
		Comments: 12,
		Milestone: &github.Milestone{
			Number: 31,
			Title:  "2.2-stable",
132
		},
133 134
		CreatedAt: createdAt,
		UpdatedAt: updatedAt,
135 136 137 138 139 140
	}

	testFormatIssue(t, []formatIssueTest{
		{
			name:     "number",
			issue:    issue,
141
			format:   "%I",
142 143 144 145 146 147
			colorize: true,
			expect:   "42",
		},
		{
			name:     "hashed number",
			issue:    issue,
148
			format:   "%i",
149 150 151 152 153 154
			colorize: true,
			expect:   "#42",
		},
		{
			name:     "state as text",
			issue:    issue,
155
			format:   "%S",
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
			colorize: true,
			expect:   "open",
		},
		{
			name:     "state as color switch",
			issue:    issue,
			format:   "%sC",
			colorize: true,
			expect:   "\033[32m",
		},
		{
			name:     "state as color switch non colorized",
			issue:    issue,
			format:   "%sC",
			colorize: false,
			expect:   "",
		},
		{
			name:     "title",
			issue:    issue,
			format:   "%t",
			colorize: true,
			expect:   "Just an Issue",
		},
		{
			name:     "label colorized",
			issue:    issue,
			format:   "%l",
			colorize: true,
185
			expect:   "\033[38;2;255;255;255;48;2;136;0;0m bug \033[m \033[38;2;255;255;255;48;2;0;136;0m feature \033[m",
186 187 188 189 190 191
		},
		{
			name:     "label not colorized",
			issue:    issue,
			format:   "%l",
			colorize: false,
192 193 194 195 196 197 198 199
			expect:   " bug   feature ",
		},
		{
			name:     "raw labels",
			issue:    issue,
			format:   "%L",
			colorize: true,
			expect:   "bug, feature",
200 201 202 203 204 205 206 207 208 209 210
		},
		{
			name:     "body",
			issue:    issue,
			format:   "%b",
			colorize: true,
			expect:   "Body of the\nissue",
		},
		{
			name:     "user login",
			issue:    issue,
211
			format:   "%au",
212 213 214 215 216 217
			colorize: true,
			expect:   "pcorpet",
		},
		{
			name:     "assignee login",
			issue:    issue,
218
			format:   "%as",
219
			colorize: true,
220
			expect:   "mislav, josh",
221 222 223 224 225 226 227
		},
		{
			name: "assignee login but not assigned",
			issue: github.Issue{
				State: "open",
				User:  &github.User{Login: "pcorpet"},
			},
228
			format:   "%as",
229 230 231
			colorize: true,
			expect:   "",
		},
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266
		{
			name:     "milestone number",
			issue:    issue,
			format:   "%Mn",
			colorize: true,
			expect:   "31",
		},
		{
			name:     "milestone title",
			issue:    issue,
			format:   "%Mt",
			colorize: true,
			expect:   "2.2-stable",
		},
		{
			name:     "comments number",
			issue:    issue,
			format:   "%Nc",
			colorize: true,
			expect:   "(12)",
		},
		{
			name:     "raw comments number",
			issue:    issue,
			format:   "%NC",
			colorize: true,
			expect:   "12",
		},
		{
			name:     "issue URL",
			issue:    issue,
			format:   "%U",
			colorize: true,
			expect:   "the://url",
		},
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
		{
			name:     "created date",
			issue:    issue,
			format:   "%cD",
			colorize: true,
			expect:   "16 Mar 2015",
		},
		{
			name:     "created time ISO 8601",
			issue:    issue,
			format:   "%cI",
			colorize: true,
			expect:   "2015-03-16T12:34:00Z",
		},
		{
			name:     "created time Unix",
			issue:    issue,
			format:   "%ct",
			colorize: true,
			expect:   "1426509240",
		},
		{
			name:     "updated date",
			issue:    issue,
			format:   "%uD",
			colorize: true,
			expect:   "17 Mar 2015",
		},
		{
			name:     "updated time ISO 8601",
			issue:    issue,
			format:   "%uI",
			colorize: true,
300
			expect:   "2015-03-17T12:34:00+09:00",
301 302 303 304 305 306
		},
		{
			name:     "updated time Unix",
			issue:    issue,
			format:   "%ut",
			colorize: true,
307
			expect:   "1426563240",
308
		},
309 310
	})
}