Passed
Push — v3.1.0 ( 23510f...abc924 )
by Serhii
01:21
created

config_test.go   A

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 55
dl 0
loc 85
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A timeago.TestCustomTranslations 0 48 4
A timeago.TestIsLocationProvided 0 25 4
1
package timeago
2
3
import (
4
	"testing"
5
	"time"
6
)
7
8
func TestIsLocationProvided(t *testing.T) {
9
	cases := []struct {
10
		name   string
11
		loc    string
12
		expect bool
13
	}{
14
		{
15
			name:   "Location is set",
16
			loc:    "Russia/Moscow",
17
			expect: true,
18
		},
19
		{
20
			name:   "Location is not set",
21
			loc:    "",
22
			expect: false,
23
		},
24
	}
25
26
	for _, tc := range cases {
27
		t.Run(tc.name, func(t *testing.T) {
28
			c := defaultConfig()
29
			actual := c.isLocationProvided()
30
31
			if actual != tc.expect {
32
				t.Fatalf("Expected %v, but got %v", tc.expect, actual)
33
			}
34
		})
35
	}
36
}
37
38
func TestCustomTranslations(t *testing.T) {
39
	cases := []struct {
40
		expect  string
41
		time    time.Duration
42
		langSet LangSet
43
	}{
44
		{
45
			expect: "10 h a",
46
			time:   time.Hour * 10,
47
			langSet: LangSet{
48
				Lang: "en",
49
				Ago:  "a",
50
				Hour: LangForms{"other": "h"},
51
			},
52
		},
53
		{
54
			expect: "1м н",
55
			time:   time.Minute,
56
			langSet: LangSet{
57
				Format: "{num}{timeUnit} {ago}",
58
				Lang:   "ru",
59
				Ago:    "н",
60
				Minute: LangForms{"one": "м"},
61
			},
62
		},
63
		{
64
			expect: "2 d ago",
65
			time:   time.Hour * 24 * 2,
66
			langSet: LangSet{
67
				Lang: "en",
68
				Day:  LangForms{"other": "d"},
69
			},
70
		},
71
	}
72
73
	for _, tc := range cases {
74
		name := tc.langSet.Lang + " " + tc.expect
75
76
		t.Run(name, func(t *testing.T) {
77
			Reconfigure(Config{
78
				Language:     tc.langSet.Lang,
79
				Translations: []LangSet{tc.langSet},
80
			})
81
82
			date := timestampFromPastDate(tc.time)
83
84
			if res, _ := Parse(date); res != tc.expect {
85
				t.Errorf("Result must be %q, but got %q instead", tc.expect, res)
86
			}
87
		})
88
	}
89
}
90