File: flags_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 (86 lines) | stat: -rw-r--r-- 2,170 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
package backendutil

import (
	"reflect"
	"testing"

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

var updateFlagsTests = []struct {
	op    imap.FlagsOp
	flags []string
	res   []string
}{
	{
		op:    imap.AddFlags,
		flags: []string{"d", "e"},
		res:   []string{"a", "b", "c", "d", "e"},
	},
	{
		op:    imap.AddFlags,
		flags: []string{"a", "d", "b"},
		res:   []string{"a", "b", "c", "d"},
	},
	{
		op:    imap.RemoveFlags,
		flags: []string{"b", "v", "e", "a"},
		res:   []string{"c"},
	},
	{
		op:    imap.SetFlags,
		flags: []string{"a", "d", "e"},
		res:   []string{"a", "d", "e"},
	},
	// Test unknown op for code coverage.
	{
		op:    imap.FlagsOp("TestUnknownOp"),
		flags: []string{"a", "d", "e"},
		res:   []string{"a", "b", "c"},
	},
}

func TestUpdateFlags(t *testing.T) {
	flagsList := []string{"a", "b", "c"}
	for _, test := range updateFlagsTests {
		// Make a backup copy of 'test.flags'
		origFlags := append(test.flags[:0:0], test.flags...)
		// Copy flags
		current := append(flagsList[:0:0], flagsList...)
		got := UpdateFlags(current, test.op, test.flags)

		if !reflect.DeepEqual(got, test.res) {
			t.Errorf("Expected result to be \n%v\n but got \n%v", test.res, got)
		}
		// Verify that 'test.flags' wasn't modified
		if !reflect.DeepEqual(origFlags, test.flags) {
			t.Errorf("Unexpected change to operation flags list changed \nbefore %v\n after \n%v",
				origFlags, test.flags)
		}
	}
}

func TestUpdateFlags_Recent(t *testing.T) {
	current := []string{}

	current = UpdateFlags(current, imap.SetFlags, []string{imap.RecentFlag})

	res := []string{imap.RecentFlag}
	if !reflect.DeepEqual(current, res) {
		t.Errorf("Expected result to be \n%v\n but got \n%v", res, current)
	}

	current = UpdateFlags(current, imap.SetFlags, []string{"something"})

	res = []string{imap.RecentFlag, "something"}
	if !reflect.DeepEqual(current, res) {
		t.Errorf("Expected result to be \n%v\n but got \n%v", res, current)
	}

	current = UpdateFlags(current, imap.SetFlags, []string{"another", imap.RecentFlag})

	res = []string{imap.RecentFlag, "another"}
	if !reflect.DeepEqual(current, res) {
		t.Errorf("Expected result to be \n%v\n but got \n%v", res, current)
	}
}