File: utils_test.go

package info (click to toggle)
golang-github-jhillyerd-enmime 0.9.3-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,156 kB
  • sloc: makefile: 25; sh: 16
file content (163 lines) | stat: -rw-r--r-- 3,300 bytes parent folder | download | duplicates (2)
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package cmd

import (
	"bufio"
	"bytes"
	"testing"

	"github.com/jhillyerd/enmime"
)

func TestMarkdownH1(t *testing.T) {
	buf := new(bytes.Buffer)
	bw := bufio.NewWriter(buf)
	md := &markdown{bw}
	md.H1("Big Header")
	bw.Flush()

	want := "Big Header\n==========\n\n"
	got := buf.String()
	if got != want {
		t.Errorf("got: %q, wanted: %q", got, want)
	}
}

func TestMarkdownH2(t *testing.T) {
	buf := new(bytes.Buffer)
	bw := bufio.NewWriter(buf)
	md := &markdown{bw}
	md.H2("Big Header")
	bw.Flush()

	want := "## Big Header\n"
	got := buf.String()
	if got != want {
		t.Errorf("got: %q, wanted: %q", got, want)
	}
}

func TestMarkdownH3(t *testing.T) {
	buf := new(bytes.Buffer)
	bw := bufio.NewWriter(buf)
	md := &markdown{bw}
	md.H3("Big Header")
	bw.Flush()

	want := "### Big Header\n"
	got := buf.String()
	if got != want {
		t.Errorf("got: %q, wanted: %q", got, want)
	}
}

func TestMarkdownPrintf(t *testing.T) {
	buf := new(bytes.Buffer)
	bw := bufio.NewWriter(buf)
	md := &markdown{bw}
	md.Printf("%v %q", 123, "quoted")
	bw.Flush()

	want := "123 \"quoted\""
	got := buf.String()
	if got != want {
		t.Errorf("got: %q, wanted: %q", got, want)
	}
}

func TestMarkdownPrintln(t *testing.T) {
	buf := new(bytes.Buffer)
	bw := bufio.NewWriter(buf)
	md := &markdown{bw}
	md.Println("words")
	bw.Flush()

	want := "words\n"
	got := buf.String()
	if got != want {
		t.Errorf("got: %q, wanted: %q", got, want)
	}
}

func TestFormatPartNil(t *testing.T) {
	buf := new(bytes.Buffer)
	FormatPart(buf, nil, "")
	got := buf.String()
	want := ""
	if got != want {
		t.Errorf("FormatPart(nil) == %q, want: %q", got, want)
	}
}

func TestFormatPartEmpty(t *testing.T) {
	buf := new(bytes.Buffer)
	FormatPart(buf, &enmime.Part{}, "")
	got := buf.String()
	want := "MISSING TYPE\n"
	if got != want {
		t.Errorf("FormatPart(nil) == %q, want: %q", got, want)
	}
}

func TestFormatPartMulti(t *testing.T) {
	buf := new(bytes.Buffer)

	// Build part tree
	root := &enmime.Part{
		ContentType: "multipart/alternative",
	}
	lev1 := &enmime.Part{
		ContentType: "text/plain",
		Parent:      root,
		NextSibling: &enmime.Part{
			ContentType: "text/html",
			Parent:      root,
			NextSibling: &enmime.Part{
				ContentType: "multipart/mixed",
				Parent:      root,
			},
		},
	}
	root.FirstChild = lev1
	lev2 := &enmime.Part{
		ContentType: "image/png",
		Disposition: "inline",
		FileName:    "test.png",
		Parent:      lev1,
		NextSibling: &enmime.Part{
			ContentType: "image/jpeg",
			Disposition: "attachment",
			FileName:    "test.jpg",
			Parent:      lev1,
		},
	}
	lev1.NextSibling.NextSibling.FirstChild = lev2

	// Setup an error
	lev1.Errors = []*enmime.Error{
		{Name: "Test Error", Detail: "None", Severe: false},
	}

	// Desired output lines
	lines := []string{
		"multipart/alternative",
		"|-- text/plain (errors: 1)",
		"|-- text/html",
		"`-- multipart/mixed",
		"    |-- image/png, disposition: inline, filename: \"test.png\"",
		"    `-- image/jpeg, disposition: attachment, filename: \"test.jpg\"",
	}

	FormatPart(buf, root, "")

	for i, want := range lines {
		got, err := buf.ReadString('\n')
		if err != nil {
			t.Fatalf("Error on line %v: %v", i+1, err)
		}
		// Drop \n
		got = got[:len(got)-1]
		if got != want {
			t.Errorf("Line %v got: %q, want: %q", i+1, got, want)
		}
	}
}