Skip to content

Commit

Permalink
Test command output
Browse files Browse the repository at this point in the history
  • Loading branch information
antham committed Mar 14, 2023
1 parent cf19f46 commit 773089b
Show file tree
Hide file tree
Showing 4 changed files with 177 additions and 7 deletions.
48 changes: 46 additions & 2 deletions cmd/inbox_delete_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cmd

import (
"bytes"
"errors"
"testing"

Expand All @@ -15,12 +16,15 @@ func TestInboxDelete(t *testing.T) {
args []string
errExpected error
inboxBuilder inboxBuilder
output string
outputErr string
}

scenarios := []scenario{
{
name: "No mails found",
args: []string{"test", "1"},
name: "No mails found",
args: []string{"test", "1"},
errExpected: errors.New("inbox is empty"),
inboxBuilder: func(name string) (Inbox, error) {
mock := &InboxMock{}
mock.mails = []inbox.Mail{}
Expand Down Expand Up @@ -51,18 +55,58 @@ func TestInboxDelete(t *testing.T) {
errExpected: errors.New("delete message error"),
inboxBuilder: func(name string) (Inbox, error) {
mock := &InboxMock{deleteError: errors.New("delete message error")}
mock.count = 1
mock.mails = []inbox.Mail{
{
ID: "abcdefg",
Title: "title",
Body: "body",
Sender: &inbox.Sender{
Mail: "test123",
Name: "name123",
},
},
}
return mock, nil
},
},
{
name: "Email deleted successfully",
args: []string{"test", "1"},
inboxBuilder: func(name string) (Inbox, error) {
mock := &InboxMock{}
mock.count = 1
mock.mails = []inbox.Mail{
{
ID: "abcdefg",
Title: "title",
Body: "body",
Sender: &inbox.Sender{
Mail: "test123",
Name: "name123",
},
},
}
return mock, nil
},
output: `Email "1" successfully deleted
`,
},
}

for _, scenario := range scenarios {
scenario := scenario
t.Run(scenario.name, func(t *testing.T) {
t.Parallel()
var output bytes.Buffer
var outputErr bytes.Buffer
cmd := &cobra.Command{}
cmd.SetOut(&output)
cmd.SetErr(&outputErr)
err := inboxDelete(scenario.inboxBuilder)(cmd, scenario.args)
assert.Equal(t, scenario.errExpected, err)
assert.Equal(t, scenario.output, output.String())
assert.Equal(t, scenario.outputErr, outputErr.String())
})
}
}
45 changes: 45 additions & 0 deletions cmd/inbox_flush_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cmd

import (
"bytes"
"errors"
"testing"

Expand All @@ -15,6 +16,8 @@ func TestInboxFlush(t *testing.T) {
args []string
errExpected error
inboxBuilder inboxBuilder
output string
outputErr string
}

scenarios := []scenario{
Expand All @@ -26,6 +29,8 @@ func TestInboxFlush(t *testing.T) {
mock.mails = []inbox.Mail{}
return mock, nil
},
output: `Inbox "test" successfully flushed
`,
},
{
name: "An error is thrown in inbox builder",
Expand All @@ -51,18 +56,58 @@ func TestInboxFlush(t *testing.T) {
errExpected: errors.New("flush inbox error"),
inboxBuilder: func(name string) (Inbox, error) {
mock := &InboxMock{flushError: errors.New("flush inbox error")}
mock.count = 1
mock.mails = []inbox.Mail{
{
ID: "abcdefg",
Title: "title",
Body: "body",
Sender: &inbox.Sender{
Mail: "test123",
Name: "name123",
},
},
}
return mock, nil
},
},
{
name: "Inbox flushed successfully",
args: []string{"test", "1"},
inboxBuilder: func(name string) (Inbox, error) {
mock := &InboxMock{}
mock.count = 1
mock.mails = []inbox.Mail{
{
ID: "abcdefg",
Title: "title",
Body: "body",
Sender: &inbox.Sender{
Mail: "test123",
Name: "name123",
},
},
}
return mock, nil
},
output: `Inbox "test" successfully flushed
`,
},
}

for _, scenario := range scenarios {
scenario := scenario
t.Run(scenario.name, func(t *testing.T) {
t.Parallel()
var output bytes.Buffer
var outputErr bytes.Buffer
cmd := &cobra.Command{}
cmd.SetOut(&output)
cmd.SetErr(&outputErr)
err := inboxFlush(scenario.inboxBuilder)(cmd, scenario.args)
assert.Equal(t, scenario.errExpected, err)
assert.Equal(t, scenario.output, output.String())
assert.Equal(t, scenario.outputErr, outputErr.String())
})
}
}
14 changes: 14 additions & 0 deletions cmd/inbox_list_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cmd

import (
"bytes"
"errors"
"testing"

Expand Down Expand Up @@ -62,6 +63,8 @@ func TestInboxList(t *testing.T) {
args []string
errExpected error
inboxBuilder inboxBuilder
output string
outputErr string
}

scenarios := []scenario{
Expand Down Expand Up @@ -112,16 +115,27 @@ func TestInboxList(t *testing.T) {
}
return mock, nil
},
output: ` 1 test123name123
title
`,
},
}

for _, scenario := range scenarios {
scenario := scenario
t.Run(scenario.name, func(t *testing.T) {
t.Parallel()
var output bytes.Buffer
var outputErr bytes.Buffer
cmd := &cobra.Command{}
cmd.SetOut(&output)
cmd.SetErr(&outputErr)
err := inboxList(scenario.inboxBuilder)(cmd, scenario.args)
assert.Equal(t, scenario.errExpected, err)
assert.Equal(t, scenario.output, output.String())
assert.Equal(t, scenario.outputErr, outputErr.String())
})
}
}
77 changes: 72 additions & 5 deletions cmd/inbox_show_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package cmd

import (
"bytes"
"errors"
"testing"
"time"

"github.com/antham/yogo/inbox"
"github.com/spf13/cobra"
"github.com/stretchr/testify/assert"
)

Expand All @@ -14,12 +17,15 @@ func TestInboxShow(t *testing.T) {
args []string
errExpected error
inboxBuilder inboxBuilder
output string
outputErr string
}

scenarios := []scenario{
{
name: "No mails found",
args: []string{"test", "1"},
name: "No mails found",
args: []string{"test", "1"},
errExpected: errors.New("inbox is empty"),
inboxBuilder: func(name string) (Inbox, error) {
mock := &InboxMock{}
mock.mails = []inbox.Mail{}
Expand Down Expand Up @@ -50,25 +56,86 @@ func TestInboxShow(t *testing.T) {
errExpected: errors.New("parse email error"),
inboxBuilder: func(name string) (Inbox, error) {
mock := &InboxMock{parseError: errors.New("parse email error")}
mock.count = 1
mock.mails = []inbox.Mail{
{
ID: "abcdefg",
Title: "title",
Body: "body",
Sender: &inbox.Sender{
Mail: "test123",
Name: "name123",
},
},
}
return mock, nil
},
},
{
name: "No mail found",
args: []string{"test", "1"},
name: "No mail found",
args: []string{"test", "1"},
errExpected: errors.New("inbox is empty"),
inboxBuilder: func(name string) (Inbox, error) {
mock := &InboxMock{getMail: nil}
return mock, nil
},
},
{
name: "Output the mail",
args: []string{"test", "1"},
output: `---
From : name123 <test123>
Title : title
Date : 2001-01-01 00:00
---
body
---
`,
inboxBuilder: func(name string) (Inbox, error) {
now, _ := time.Parse("2006-01-02", "2001-01-01")
mock := &InboxMock{}
mock.count = 1
mock.mails = []inbox.Mail{
{
ID: "abcdefg",
Title: "title",
Body: "body",
Sender: &inbox.Sender{
Mail: "test123",
Name: "name123",
},
Date: &now,
},
}
mock.getMail = &inbox.Mail{
ID: "abcdefg",
Title: "title",
Body: "body",
Sender: &inbox.Sender{
Mail: "test123",
Name: "name123",
},
Date: &now,
}
return mock, nil
},
},
}

for _, scenario := range scenarios {
scenario := scenario
t.Run(scenario.name, func(t *testing.T) {
t.Parallel()
err := inboxShow(scenario.inboxBuilder)(nil, scenario.args)
var output bytes.Buffer
var outputErr bytes.Buffer
cmd := &cobra.Command{}
cmd.SetOut(&output)
cmd.SetErr(&outputErr)
err := inboxShow(scenario.inboxBuilder)(cmd, scenario.args)
assert.Equal(t, scenario.errExpected, err)
assert.Equal(t, scenario.output, output.String())
assert.Equal(t, scenario.outputErr, outputErr.String())
})
}
}

0 comments on commit 773089b

Please sign in to comment.