Skip to content

Commit ff96f48

Browse files
committed
add ExpectLines helper
1 parent deb7ee6 commit ff96f48

File tree

4 files changed

+48
-98
lines changed

4 files changed

+48
-98
lines changed

command/issue_test.go

Lines changed: 9 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -316,18 +316,11 @@ func TestIssueView_preview(t *testing.T) {
316316

317317
eq(t, output.Stderr(), "")
318318

319-
expectedLines := []*regexp.Regexp{
320-
regexp.MustCompile(`ix of coins`),
321-
regexp.MustCompile(`opened by marseilles. 9 comments. \(tarot\)`),
322-
regexp.MustCompile(`bold story`),
323-
regexp.MustCompile(`View this issue on GitHub: https://github.com/OWNER/REPO/issues/123`),
324-
}
325-
for _, r := range expectedLines {
326-
if !r.MatchString(output.String()) {
327-
t.Errorf("output did not match regexp /%s/\n> output\n%s\n", r, output)
328-
return
329-
}
330-
}
319+
test.ExpectLines(t, output.String(),
320+
"ix of coins",
321+
`opened by marseilles. 9 comments. \(tarot\)`,
322+
"bold story",
323+
"View this issue on GitHub: https://github.com/OWNER/REPO/issues/123")
331324
}
332325

333326
func TestIssueView_previewWithEmptyBody(t *testing.T) {
@@ -362,17 +355,10 @@ func TestIssueView_previewWithEmptyBody(t *testing.T) {
362355

363356
eq(t, output.Stderr(), "")
364357

365-
expectedLines := []*regexp.Regexp{
366-
regexp.MustCompile(`ix of coins`),
367-
regexp.MustCompile(`opened by marseilles. 9 comments. \(tarot\)`),
368-
regexp.MustCompile(`View this issue on GitHub: https://github.com/OWNER/REPO/issues/123`),
369-
}
370-
for _, r := range expectedLines {
371-
if !r.MatchString(output.String()) {
372-
t.Errorf("output did not match regexp /%s/\n> output\n%s\n", r, output)
373-
return
374-
}
375-
}
358+
test.ExpectLines(t, output.String(),
359+
"ix of coins",
360+
`opened by marseilles. 9 comments. \(tarot\)`,
361+
"View this issue on GitHub: https://github.com/OWNER/REPO/issues/123")
376362
}
377363

378364
func TestIssueView_notFound(t *testing.T) {

command/pr_test.go

Lines changed: 14 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -416,18 +416,11 @@ func TestPRView_preview(t *testing.T) {
416416

417417
eq(t, output.Stderr(), "")
418418

419-
expectedLines := []*regexp.Regexp{
420-
regexp.MustCompile(`Blueberries are from a fork`),
421-
regexp.MustCompile(`nobody wants to merge 12 commits into master from blueberries`),
422-
regexp.MustCompile(`blueberries taste good`),
423-
regexp.MustCompile(`View this pull request on GitHub: https://github.com/OWNER/REPO/pull/12`),
424-
}
425-
for _, r := range expectedLines {
426-
if !r.MatchString(output.String()) {
427-
t.Errorf("output did not match regexp /%s/\n> output\n%s\n", r, output)
428-
return
429-
}
430-
}
419+
test.ExpectLines(t, output.String(),
420+
"Blueberries are from a fork",
421+
"nobody wants to merge 12 commits into master from blueberries",
422+
"blueberries taste good",
423+
"View this pull request on GitHub: https://github.com/OWNER/REPO/pull/12")
431424
}
432425

433426
func TestPRView_previewCurrentBranch(t *testing.T) {
@@ -451,18 +444,11 @@ func TestPRView_previewCurrentBranch(t *testing.T) {
451444

452445
eq(t, output.Stderr(), "")
453446

454-
expectedLines := []*regexp.Regexp{
455-
regexp.MustCompile(`Blueberries are a good fruit`),
456-
regexp.MustCompile(`nobody wants to merge 8 commits into master from blueberries`),
457-
regexp.MustCompile(`blueberries taste good`),
458-
regexp.MustCompile(`View this pull request on GitHub: https://github.com/OWNER/REPO/pull/10`),
459-
}
460-
for _, r := range expectedLines {
461-
if !r.MatchString(output.String()) {
462-
t.Errorf("output did not match regexp /%s/\n> output\n%s\n", r, output)
463-
return
464-
}
465-
}
447+
test.ExpectLines(t, output.String(),
448+
"Blueberries are a good fruit",
449+
"nobody wants to merge 8 commits into master from blueberries",
450+
"blueberries taste good",
451+
"View this pull request on GitHub: https://github.com/OWNER/REPO/pull/10")
466452
}
467453

468454
func TestPRView_previewCurrentBranchWithEmptyBody(t *testing.T) {
@@ -486,17 +472,10 @@ func TestPRView_previewCurrentBranchWithEmptyBody(t *testing.T) {
486472

487473
eq(t, output.Stderr(), "")
488474

489-
expectedLines := []*regexp.Regexp{
490-
regexp.MustCompile(`Blueberries are a good fruit`),
491-
regexp.MustCompile(`nobody wants to merge 8 commits into master from blueberries`),
492-
regexp.MustCompile(`View this pull request on GitHub: https://github.com/OWNER/REPO/pull/10`),
493-
}
494-
for _, r := range expectedLines {
495-
if !r.MatchString(output.String()) {
496-
t.Errorf("output did not match regexp /%s/\n> output\n%s\n", r, output)
497-
return
498-
}
499-
}
475+
test.ExpectLines(t, output.String(),
476+
"Blueberries are a good fruit",
477+
"nobody wants to merge 8 commits into master from blueberries",
478+
"View this pull request on GitHub: https://github.com/OWNER/REPO/pull/10")
500479
}
501480

502481
func TestPRView_currentBranch(t *testing.T) {

command/repo_test.go

Lines changed: 12 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -137,16 +137,9 @@ func TestRepoFork_in_parent_yes(t *testing.T) {
137137

138138
eq(t, output.Stderr(), "")
139139

140-
expectedLines := []*regexp.Regexp{
141-
regexp.MustCompile(`Created fork someone/REPO`),
142-
regexp.MustCompile(`Remote added at fork`),
143-
}
144-
for _, r := range expectedLines {
145-
if !r.MatchString(output.String()) {
146-
t.Errorf("output did not match regexp /%s/\n> output\n%s\n", r, output)
147-
return
148-
}
149-
}
140+
test.ExpectLines(t, output.String(),
141+
"Created fork someone/REPO",
142+
"Remote added at fork")
150143
}
151144

152145
func TestRepoFork_outside_yes(t *testing.T) {
@@ -169,16 +162,9 @@ func TestRepoFork_outside_yes(t *testing.T) {
169162

170163
eq(t, strings.Join(seenCmd.Args, " "), "git clone https://github.com/someone/repo.git")
171164

172-
expectedLines := []*regexp.Regexp{
173-
regexp.MustCompile(`Created fork someone/REPO`),
174-
regexp.MustCompile(`Cloned fork`),
175-
}
176-
for _, r := range expectedLines {
177-
if !r.MatchString(output.String()) {
178-
t.Errorf("output did not match regexp /%s/\n> output\n%s\n", r, output)
179-
return
180-
}
181-
}
165+
test.ExpectLines(t, output.String(),
166+
"Created fork someone/REPO",
167+
"Cloned fork")
182168
}
183169

184170
func TestRepoFork_outside_survey_yes(t *testing.T) {
@@ -208,16 +194,9 @@ func TestRepoFork_outside_survey_yes(t *testing.T) {
208194

209195
eq(t, strings.Join(seenCmd.Args, " "), "git clone https://github.com/someone/repo.git")
210196

211-
expectedLines := []*regexp.Regexp{
212-
regexp.MustCompile(`Created fork someone/REPO`),
213-
regexp.MustCompile(`Cloned fork`),
214-
}
215-
for _, r := range expectedLines {
216-
if !r.MatchString(output.String()) {
217-
t.Errorf("output did not match regexp /%s/\n> output\n%s\n", r, output)
218-
return
219-
}
220-
}
197+
test.ExpectLines(t, output.String(),
198+
"Created fork someone/REPO",
199+
"Cloned fork")
221200
}
222201

223202
func TestRepoFork_outside_survey_no(t *testing.T) {
@@ -290,16 +269,9 @@ func TestRepoFork_in_parent_survey_yes(t *testing.T) {
290269

291270
eq(t, output.Stderr(), "")
292271

293-
expectedLines := []*regexp.Regexp{
294-
regexp.MustCompile(`Created fork someone/REPO`),
295-
regexp.MustCompile(`Remote added at fork`),
296-
}
297-
for _, r := range expectedLines {
298-
if !r.MatchString(output.String()) {
299-
t.Errorf("output did not match regexp /%s/\n> output\n%s\n", r, output)
300-
return
301-
}
302-
}
272+
test.ExpectLines(t, output.String(),
273+
"Created fork someone/REPO",
274+
"Remote added at fork")
303275
}
304276

305277
func TestRepoFork_in_parent_survey_no(t *testing.T) {

test/helpers.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import (
66
"os"
77
"os/exec"
88
"path/filepath"
9+
"regexp"
10+
"testing"
911
)
1012

1113
// OutputStub implements a simple utils.Runnable
@@ -74,3 +76,14 @@ func UseTempGitRepo() *TempGitRepo {
7476

7577
return &TempGitRepo{Remote: remotePath, TearDown: tearDown}
7678
}
79+
80+
func ExpectLines(t *testing.T, output string, lines ...string) {
81+
var r *regexp.Regexp
82+
for _, l := range lines {
83+
r = regexp.MustCompile(l)
84+
if !r.MatchString(output) {
85+
t.Errorf("output did not match regexp /%s/\n> output\n%s\n", r, output)
86+
return
87+
}
88+
}
89+
}

0 commit comments

Comments
 (0)