forked from emersion/go-imap
-
Notifications
You must be signed in to change notification settings - Fork 1
/
command_test.go
98 lines (82 loc) · 1.94 KB
/
command_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package imap_test
import (
"bytes"
"testing"
"github.com/emersion/go-imap"
)
func TestCommand_Command(t *testing.T) {
cmd := &imap.Command{
Tag: "A001",
Name: "NOOP",
}
if cmd.Command() != cmd {
t.Error("Command should return itself")
}
}
func TestCommand_WriteTo_NoArgs(t *testing.T) {
var b bytes.Buffer
w := imap.NewWriter(&b)
cmd := &imap.Command{
Tag: "A001",
Name: "NOOP",
}
if err := cmd.WriteTo(w); err != nil {
t.Fatal(err)
}
if b.String() != "A001 NOOP\r\n" {
t.Fatal("Not the expected command")
}
}
func TestCommand_WriteTo_WithArgs(t *testing.T) {
var b bytes.Buffer
w := imap.NewWriter(&b)
cmd := &imap.Command{
Tag: "A002",
Name: "LOGIN",
Arguments: []interface{}{"username", "password"},
}
if err := cmd.WriteTo(w); err != nil {
t.Fatal(err)
}
if b.String() != "A002 LOGIN username password\r\n" {
t.Fatal("Not the expected command")
}
}
func TestCommand_Parse_NoArgs(t *testing.T) {
fields := []interface{}{"a", "NOOP"}
cmd := &imap.Command{}
if err := cmd.Parse(fields); err != nil {
t.Fatal(err)
}
if cmd.Tag != "a" {
t.Error("Invalid tag:", cmd.Tag)
}
if cmd.Name != "NOOP" {
t.Error("Invalid name:", cmd.Name)
}
if len(cmd.Arguments) != 0 {
t.Error("Invalid arguments:", cmd.Arguments)
}
}
func TestCommand_Parse_WithArgs(t *testing.T) {
fields := []interface{}{"a", "LOGIN", "username", "password"}
cmd := &imap.Command{}
if err := cmd.Parse(fields); err != nil {
t.Fatal(err)
}
if cmd.Tag != "a" {
t.Error("Invalid tag:", cmd.Tag)
}
if cmd.Name != "LOGIN" {
t.Error("Invalid name:", cmd.Name)
}
if len(cmd.Arguments) != 2 {
t.Error("Invalid arguments:", cmd.Arguments)
}
if username, ok := cmd.Arguments[0].(string); !ok || username != "username" {
t.Error("Invalid first argument:", cmd.Arguments[0])
}
if password, ok := cmd.Arguments[1].(string); !ok || password != "password" {
t.Error("Invalid second argument:", cmd.Arguments[1])
}
}