Passed
Pull Request — main (#40)
by Serhii
01:18
created

timeago.TestTrans   D

Complexity

Conditions 12

Size

Total Lines 26
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 12
eloc 20
dl 0
loc 26
rs 4.8
c 0
b 0
f 0
nop 1

1 Method

Rating   Name   Duplication   Size   Complexity  
A timeago.TestLocationIsSet 0 25 4

How to fix   Complexity   

Complexity

Complex classes like timeago.TestTrans often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
package timeago
2
3
import (
4
	"testing"
5
	"time"
6
)
7
8
func TestLocationIsSet(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 := NewConfig("ru", tc.loc, []LangSet{})
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