Passed
Push — master ( 3df1d0...111a66 )
by Serhii
02:56 queued 01:25
created

timeago.smallSubTime   A

Complexity

Conditions 1

Size

Total Lines 2
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nop 1
dl 0
loc 2
rs 10
c 0
b 0
f 0
1
package timeago
2
3
import (
4
	"testing"
5
)
6
7
func TestGetOption(test *testing.T) {
8
	cases := []struct {
9
		name            string
10
		date            string
11
		optionMustBe    string
12
		hasOptionMustBe bool
13
	}{
14
		{"case has online option", "2017-02-01 00:00:00|online", "online", true},
15
		{"case has random option", "2017-02-01 00:00:00|random", "random", true},
16
		{"case has online option", "2017-02-01 00:00:00|korotchaeva", "korotchaeva", true},
17
		{"case don't have option", "2017-02-01 00:00:00", "", false},
18
	}
19
20
	for _, tc := range cases {
21
		test.Run(tc.name, func(t *testing.T) {
22
			option, hasOption := getOption(&tc.date)
23
24
			if hasOption != tc.hasOptionMustBe || option != tc.optionMustBe {
25
				t.Errorf("Result of getOption func must return `online` string and `true`, but `%s` string and `%v` returned returned", tc.optionMustBe, tc.hasOptionMustBe)
26
			}
27
		})
28
	}
29
}
30
31
func TestGetWords(t *testing.T) {
32
	cases := []struct {
33
		timeKind string
34
		num      int
35
		result   string
36
		lang     string
37
	}{
38
		// english
39
		{"days", 11, "11 days ago", "en"},
40
		{"days", 21, "21 days ago", "en"},
41
		{"seconds", 30, "30 seconds ago", "en"},
42
		{"seconds", 31, "31 seconds ago", "en"},
43
		{"hours", 10, "10 hours ago", "en"},
44
		{"years", 2, "2 years ago", "en"},
45
		// russian
46
		{"hours", 5, "5 часов назад", "ru"},
47
		{"days", 11, "11 дней назад", "ru"},
48
		{"years", 21, "21 год назад", "ru"},
49
		{"minutes", 59, "59 минут назад", "ru"},
50
	}
51
52
	for _, tc := range cases {
53
		t.Run(tc.result, func(test *testing.T) {
54
			Set("language", tc.lang)
55
			Set("location", "Europe/Kiev")
56
57
			if res := getWords(tc.timeKind, tc.num); res != tc.result {
58
				test.Errorf("Result must be `%s` but got `%s` instead", tc.result, res)
59
			}
60
		})
61
	}
62
}
63
64
func TestGetLastNumber(t *testing.T) {
65
	cases := []struct {
66
		number int
67
		result int
68
		name   string
69
	}{
70
		{0, 0, "must return 0"},
71
		{1, 1, "must return 1"},
72
		{9, 9, "must return 9"},
73
		{20, 0, "must return 0"},
74
		{253, 3, "must return 3"},
75
		{23423252, 2, "must return 2"},
76
		{223424342325, 5, "must return 5"},
77
		{23423521562348, 8, "must return 8"},
78
	}
79
80
	for _, tc := range cases {
81
		t.Run(tc.name, func(test *testing.T) {
82
			if res := getLastNumber(tc.number); res != tc.result {
83
				test.Errorf("Result must be %d, but got %d instead", tc.result, res)
84
			}
85
		})
86
	}
87
}
88