aboutsummaryrefslogtreecommitdiff
path: root/internal/feed/template/funcs_test.go
blob: a5c25c3f234c2d7031654ce9a02508be956f1d71 (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
package template

import (
	"testing"

	"github.com/google/go-cmp/cmp"
)

func TestByteCount(t *testing.T) {
	tests := map[string]struct {
		inp string
		out string
	}{
		"Empty":        {"", "0 B"},
		"Byte":         {"123", "123 B"},
		"KByte":        {"2048", "2.0 KB"},
		"KByte slight": {"2049", "2.0 KB"},
		"KByte round":  {"2560", "2.5 KB"},
		"MByte":        {"2097152", "2.0 MB"},
	}

	for name, tt := range tests {
		t.Run(name, func(tst *testing.T) {
			out := byteCount(tt.inp)

			if diff := cmp.Diff(tt.out, out); diff != "" {
				tst.Error(diff)
			}
		})
	}
}

func TestDict(t *testing.T) {
	type i []any
	type o map[string]any

	tests := map[string]struct {
		inp i
		out o
	}{
		"Empty": {i{}, o{}},
		"One":   {i{"1"}, o{"1": ""}},
		"Two":   {i{"1", 1}, o{"1": 1}},
		"Three": {i{"1", "2", "3"}, o{"1": "2", "3": ""}},
		"Four":  {i{"1", 2, "3", '4'}, o{"1": 2, "3": '4'}},
	}

	for name, tt := range tests {
		t.Run(name, func(tst *testing.T) {
			out := dict(tt.inp...)

			if diff := cmp.Diff(tt.out, o(out)); diff != "" {
				tst.Error(diff)
			}
		})
	}
}