config_test.go   A
last analyzed

Size/Duplication

Total Lines 97
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 62
dl 0
loc 97
rs 10
c 0
b 0
f 0

2 Methods

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