aboutsummaryrefslogtreecommitdiffhomepage
path: root/reader/media/media_test.go
blob: b0d2842da903c00f4b8a6c3d5a5ecb9c7a8c3782 (plain)
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
// Copyright 2019 Frédéric Guillot. All rights reserved.
// Use of this source code is governed by the Apache 2.0
// license that can be found in the LICENSE file.

package media // import "miniflux.app/reader/media"

import "testing"

func TestContentMimeType(t *testing.T) {
	scenarios := []struct {
		inputType, inputMedium, expectedMimeType string
	}{
		{"image/png", "image", "image/png"},
		{"", "image", "image/*"},
		{"", "video", "video/*"},
		{"", "audio", "audio/*"},
		{"", "", "application/octet-stream"},
	}

	for _, scenario := range scenarios {
		content := &Content{Type: scenario.inputType, Medium: scenario.inputMedium}
		result := content.MimeType()
		if result != scenario.expectedMimeType {
			t.Errorf(`Unexpected mime type, got %q instead of %q for type=%q medium=%q`,
				result,
				scenario.expectedMimeType,
				scenario.inputType,
				scenario.inputMedium,
			)
		}
	}
}

func TestContentSize(t *testing.T) {
	scenarios := []struct {
		inputSize    string
		expectedSize int64
	}{
		{"", 0},
		{"123", int64(123)},
	}

	for _, scenario := range scenarios {
		content := &Content{FileSize: scenario.inputSize}
		result := content.Size()
		if result != scenario.expectedSize {
			t.Errorf(`Unexpected size, got %d instead of %d for %q`,
				result,
				scenario.expectedSize,
				scenario.inputSize,
			)
		}
	}
}

func TestPeerLinkType(t *testing.T) {
	scenarios := []struct {
		inputType        string
		expectedMimeType string
	}{
		{"", "application/octet-stream"},
		{"application/x-bittorrent", "application/x-bittorrent"},
	}

	for _, scenario := range scenarios {
		peerLink := &PeerLink{Type: scenario.inputType}
		result := peerLink.MimeType()
		if result != scenario.expectedMimeType {
			t.Errorf(`Unexpected mime type, got %q instead of %q for %q`,
				result,
				scenario.expectedMimeType,
				scenario.inputType,
			)
		}
	}
}

func TestDescription(t *testing.T) {
	scenarios := []struct {
		inputType           string
		inputContent        string
		expectedDescription string
	}{
		{"", "", ""},
		{"html", "a <b>c</b>", "a <b>c</b>"},
		{"plain", "a\nhttp://www.example.org/", `a<br><a href="http://www.example.org/">http://www.example.org/</a>`},
	}

	for _, scenario := range scenarios {
		desc := &Description{Type: scenario.inputType, Description: scenario.inputContent}
		result := desc.HTML()
		if result != scenario.expectedDescription {
			t.Errorf(`Unexpected description, got %q instead of %q for %q`,
				result,
				scenario.expectedDescription,
				scenario.inputType,
			)
		}
	}
}

func TestFirstDescription(t *testing.T) {
	var descList DescriptionList
	descList = append(descList, Description{})
	descList = append(descList, Description{Description: "Something"})

	if descList.First() != "Something" {
		t.Errorf(`Unexpected description`)
	}
}