Passed
Push — v3.0.0 ( 5b9f70...671bfc )
by Serhii
01:19
created

timeago.TestOptionIsEnabled   B

Complexity

Conditions 7

Size

Total Lines 24
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 14
dl 0
loc 24
rs 8
c 0
b 0
f 0
nop 1
1
package timeago
2
3
import (
4
	"testing"
5
	"time"
6
7
	"github.com/SerhiiCho/timeago/v3/config"
8
)
9
10
func TestParseFunctionCanExceptTimestamp(t *testing.T) {
11
	cases := []struct {
12
		timestamp int
13
		result    string
14
	}{
15
		{getTimestampOfPastDate(time.Minute), "1 minute ago"},
16
		{getTimestampOfPastDate(time.Minute * 5), "5 minutes ago"},
17
		{getTimestampOfPastDate(time.Hour), "1 hour ago"},
18
		{getTimestampOfPastDate(time.Hour * 3), "3 hours ago"},
19
		{getTimestampOfPastDate(time.Hour * 5), "5 hours ago"},
20
		{getTimestampOfPastDate(time.Hour * 24), "1 day ago"},
21
		{getTimestampOfPastDate(time.Hour * 24 * 2), "2 days ago"},
22
		{getTimestampOfPastDate(time.Hour * 24 * 3), "3 days ago"},
23
		{getTimestampOfPastDate(time.Hour * 24 * 4), "4 days ago"},
24
		{getTimestampOfPastDate(time.Hour * 24 * 5), "5 days ago"},
25
		{getTimestampOfPastDate(time.Hour * 24 * 6), "6 days ago"},
26
		{getTimestampOfPastDate(time.Hour * 24 * 7), "1 week ago"},
27
	}
28
29
	Configure(&config.Config{Language: "en"})
30
31
	for _, tc := range cases {
32
		t.Run(tc.result, func(t *testing.T) {
33
			if res := Parse(tc.timestamp); res != tc.result {
34
				t.Errorf("Result must be %v, but got %v instead", tc.result, res)
35
			}
36
		})
37
	}
38
}
39
40
func TestParseFunctionCanExceptTimePackage(t *testing.T) {
41
	cases := []struct {
42
		time   time.Time
43
		result string
44
	}{
45
		{time.Now().Add(-time.Minute), "1 minute ago"},
46
		{time.Now().Add(-time.Minute * 2), "2 minutes ago"},
47
		{time.Now().Add(-time.Minute * 3), "3 minutes ago"},
48
		{time.Now().Add(-time.Minute * 4), "4 minutes ago"},
49
		{time.Now().Add(-time.Minute * 5), "5 minutes ago"},
50
		{time.Now().Add(-time.Minute * 6), "6 minutes ago"},
51
		{time.Now().Add(-time.Hour * 7), "7 hours ago"},
52
		{time.Now().Add(-time.Hour * 8), "8 hours ago"},
53
		{time.Now().Add(-time.Hour * 9), "9 hours ago"},
54
		{time.Now().Add(-time.Hour * 10), "10 hours ago"},
55
		{time.Now().Add(-time.Hour * 11), "11 hours ago"},
56
	}
57
58
	Configure(&config.Config{Language: "en"})
59
60
	for _, tc := range cases {
61
		t.Run("Test for date "+tc.time.String(), func(t *testing.T) {
62
			if res := Parse(tc.time); res != tc.result {
63
				t.Errorf("Result must be %v, but got %v instead", tc.result, res)
64
			}
65
		})
66
	}
67
}
68
69
func TestParseFuncWillCalculateIntervalToFutureDate(t *testing.T) {
70
	testCases := []struct {
71
		time   time.Time
72
		result string
73
	}{
74
		{time.Now().Add(time.Minute * 2), "2 minutes"},
75
		{time.Now().Add(time.Minute * 5), "5 minutes"},
76
		{time.Now().Add(time.Minute * 10), "10 minutes"},
77
		{time.Now().Add(time.Hour), "1 hour"},
78
		{time.Now().Add(time.Hour * 24), "1 day"},
79
		{time.Now().Add(time.Hour * 48), "2 days"},
80
	}
81
82
	Configure(&config.Config{Language: "en"})
83
84
	for _, tc := range testCases {
85
		t.Run("Test for date: "+tc.time.String(), func(t *testing.T) {
86
			if res := Parse(tc.time); res != tc.result {
87
				t.Errorf("Result must be %v, but got %v instead", tc.result, res)
88
			}
89
		})
90
	}
91
}
92
93
func TestOptionIsEnabled(t *testing.T) {
94
	t.Run("returns true if option is enabled", func(test *testing.T) {
95
		options = []Option{"noSuffix"}
96
97
		if res := optionIsEnabled("noSuffix"); res == false {
98
			test.Error("Result must be true, but got false instead")
99
		}
100
101
		options = []Option{}
102
	})
103
104
	t.Run("returns true if option is enabled with other option", func(test *testing.T) {
105
		options = []Option{"noSuffix", "upcoming"}
106
107
		if res := optionIsEnabled("upcoming"); res == false {
108
			test.Error("Result must be true, but got false instead")
109
		}
110
111
		options = []Option{}
112
	})
113
114
	t.Run("returns false if option is disabled", func(test *testing.T) {
115
		if res := optionIsEnabled("noSuffix"); res == true {
116
			test.Error("Result must be true, but got false instead")
117
		}
118
	})
119
}
120
121
func TestCustomTranslations(t *testing.T) {
122
	t.Run("can overwrite translations", func(t *testing.T) {
123
		Configure(&config.Config{
124
			Language: "en",
125
			Translations: []config.Translation{
126
				{
127
					Language: "en",
128
					Translations: map[string]string{
129
						"hours": "h",
130
					},
131
				},
132
			},
133
		})
134
135
		date := getTimestampOfPastDate(time.Hour * 10)
136
		expect := "10 h ago"
137
138
		if res := Parse(date); res != expect {
139
			t.Errorf("Result must be %v, but got %v instead", expect, res)
140
		}
141
	})
142
}
143