File: status_test.go

package info (click to toggle)
golang-github-emersion-go-imap 1.2.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 840 kB
  • sloc: makefile: 2
file content (94 lines) | stat: -rw-r--r-- 2,161 bytes parent folder | download | duplicates (3)
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
package imap_test

import (
	"bytes"
	"testing"

	"github.com/emersion/go-imap"
)

func TestStatusResp_WriteTo(t *testing.T) {
	tests := []struct {
		input    *imap.StatusResp
		expected string
	}{
		{
			input: &imap.StatusResp{
				Tag:  "*",
				Type: imap.StatusRespOk,
			},
			expected: "* OK \r\n",
		},
		{
			input: &imap.StatusResp{
				Tag:  "*",
				Type: imap.StatusRespOk,
				Info: "LOGIN completed",
			},
			expected: "* OK LOGIN completed\r\n",
		},
		{
			input: &imap.StatusResp{
				Tag:  "42",
				Type: imap.StatusRespBad,
				Info: "Invalid arguments",
			},
			expected: "42 BAD Invalid arguments\r\n",
		},
		{
			input: &imap.StatusResp{
				Tag:  "a001",
				Type: imap.StatusRespOk,
				Code: "READ-ONLY",
				Info: "EXAMINE completed",
			},
			expected: "a001 OK [READ-ONLY] EXAMINE completed\r\n",
		},
		{
			input: &imap.StatusResp{
				Tag:       "*",
				Type:      imap.StatusRespOk,
				Code:      "CAPABILITY",
				Arguments: []interface{}{imap.RawString("IMAP4rev1")},
				Info:      "IMAP4rev1 service ready",
			},
			expected: "* OK [CAPABILITY IMAP4rev1] IMAP4rev1 service ready\r\n",
		},
	}

	for i, test := range tests {
		b := &bytes.Buffer{}
		w := imap.NewWriter(b)

		if err := test.input.WriteTo(w); err != nil {
			t.Errorf("Cannot write status #%v, got error: %v", i, err)
			continue
		}

		o := b.String()
		if o != test.expected {
			t.Errorf("Invalid output for status #%v: %v", i, o)
		}
	}
}

func TestStatus_Err(t *testing.T) {
	status := &imap.StatusResp{Type: imap.StatusRespOk, Info: "All green"}
	if err := status.Err(); err != nil {
		t.Error("OK status returned error:", err)
	}

	status = &imap.StatusResp{Type: imap.StatusRespBad, Info: "BAD!"}
	if err := status.Err(); err == nil {
		t.Error("BAD status didn't returned error:", err)
	} else if err.Error() != "BAD!" {
		t.Error("BAD status returned incorrect error message:", err)
	}

	status = &imap.StatusResp{Type: imap.StatusRespNo, Info: "NO!"}
	if err := status.Err(); err == nil {
		t.Error("NO status didn't returned error:", err)
	} else if err.Error() != "NO!" {
		t.Error("NO status returned incorrect error message:", err)
	}
}