Passed
Push — master ( 4dcc72...1f1bc8 )
by Serhii
03:39 queued 01:59
created

timeago.TestTrans   D

Complexity

Conditions 12

Size

Total Lines 22
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

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

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 "testing"
4
5
func TestTrans(t *testing.T) {
6
	cases := []struct {
7
		lang   string
8
		trans  func() string
9
		result string
10
	}{
11
		{"ru", func() string { return trans().Online }, "В сети"},
12
		{"ru", func() string { return trans().Second }, "секунда"},
13
		{"ru", func() string { return trans().Hour }, "час"},
14
		{"ru", func() string { return trans().Day }, "день"},
15
		{"en", func() string { return trans().Online }, "Online"},
16
		{"en", func() string { return trans().Second }, "second"},
17
		{"en", func() string { return trans().Hour }, "hour"},
18
		{"en", func() string { return trans().Day }, "day"},
19
	}
20
21
	for _, tc := range cases {
22
		t.Run("returns "+tc.lang+" language", func(test *testing.T) {
23
			Set("language", tc.lang)
24
25
			if result := tc.trans(); result != tc.result {
26
				test.Errorf("Result must be %s but got %s", tc.result, result)
27
			}
28
		})
29
	}
30
}
31
32
func TestSet_for_language(t *testing.T) {
33
	cases := []struct {
34
		name  string
35
		value string
36
		err   string
37
	}{
38
		{"sets language to ru", "ru", "Set must set the `language` variable to `ru` but it didn't"},
39
		{"sets language to en", "en", "Set must set the `language` variable to `en` but it didn't"},
40
	}
41
42
	for _, tc := range cases {
43
		t.Run(tc.name, func(test *testing.T) {
44
			Set("language", tc.value)
45
46
			if language != tc.value {
47
				test.Error(tc.err)
48
			}
49
		})
50
	}
51
}
52
53
func TestSet_for_location(t *testing.T) {
54
	cases := []struct {
55
		name  string
56
		value string
57
		err   string
58
	}{
59
		{"sets location to India Delhi", "India/Delhi", "Set must set the `location` variable to `India/Delhi` but it didn't"},
60
		{"sets language to Europe/Kiev", "Europe/Kiev", "Set must set the `location` variable to `Europe/Kiev` but it didn't"},
61
	}
62
63
	for _, tc := range cases {
64
		t.Run(tc.name, func(test *testing.T) {
65
			Set("location", tc.value)
66
67
			if location != tc.value {
68
				test.Error(tc.err)
69
			}
70
		})
71
	}
72
}
73