Passed
Push — v3.3.0 ( 1f57ca )
by Serhii
01:46
created

timeago.TestParse   B

Complexity

Conditions 5

Size

Total Lines 64
Code Lines 53

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 53
dl 0
loc 64
rs 8.0715
c 0
b 0
f 0
nop 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
package timeago
2
3
import (
4
	"strconv"
5
	"testing"
6
	"time"
7
8
	"github.com/SerhiiCho/timeago/v3/internal/utils"
9
)
10
11
func TestParse(t *testing.T) {
12
	cases := []struct {
13
		date interface{}
14
		res  string
15
	}{
16
		// Integer timestamp input parsing
17
		{utils.UnixFromPastDate(time.Minute), "1 minute ago"},
18
		{utils.UnixFromPastDate(time.Minute * 5), "5 minutes ago"},
19
		{utils.UnixFromPastDate(time.Hour), "1 hour ago"},
20
		{utils.UnixFromPastDate(time.Hour * 3), "3 hours ago"},
21
		{utils.UnixFromPastDate(time.Hour * 5), "5 hours ago"},
22
		{utils.UnixFromPastDate(time.Hour * 24), "1 day ago"},
23
		{utils.UnixFromPastDate(time.Hour * 24 * 2), "2 days ago"},
24
		{utils.UnixFromPastDate(time.Hour * 24 * 3), "3 days ago"},
25
		{utils.UnixFromPastDate(time.Hour * 24 * 4), "4 days ago"},
26
		{utils.UnixFromPastDate(time.Hour * 24 * 5), "5 days ago"},
27
		{utils.UnixFromPastDate(time.Hour * 24 * 6), "6 days ago"},
28
		{utils.UnixFromPastDate(time.Hour * 24 * 7), "1 week ago"},
29
		// time.Time input parsing
30
		{utils.SubMinutes(1), "1 minute ago"},
31
		{utils.SubMinutes(2), "2 minutes ago"},
32
		{utils.SubMinutes(3), "3 minutes ago"},
33
		{utils.SubMinutes(4), "4 minutes ago"},
34
		{utils.SubMinutes(5), "5 minutes ago"},
35
		{utils.SubMinutes(6), "6 minutes ago"},
36
		{utils.SubHours(7), "7 hours ago"},
37
		{utils.SubHours(8), "8 hours ago"},
38
		{utils.SubHours(9), "9 hours ago"},
39
		{utils.SubHours(10), "10 hours ago"},
40
		{utils.SubHours(11), "11 hours ago"},
41
		// time.Time future date parsing
42
		{utils.AddMinutes(2), "2 minutes"},
43
		{utils.AddMinutes(5), "5 minutes"},
44
		{utils.AddMinutes(10), "10 minutes"},
45
		{utils.AddHours(1), "1 hour"},
46
		{utils.AddHours(24), "1 day"},
47
		{utils.AddHours(48), "2 days"},
48
		// Timestamp string input parsing
49
		{strconv.Itoa(utils.UnixFromPastDate(time.Minute)), "1 minute ago"},
50
		{strconv.Itoa(utils.UnixFromPastDate(time.Minute * 5)), "5 minutes ago"},
51
		{strconv.Itoa(utils.UnixFromPastDate(time.Hour)), "1 hour ago"},
52
		{strconv.Itoa(utils.UnixFromPastDate(time.Hour * 3)), "3 hours ago"},
53
		{strconv.Itoa(utils.UnixFromPastDate(time.Hour * 5)), "5 hours ago"},
54
		{strconv.Itoa(utils.UnixFromPastDate(time.Hour * 24)), "1 day ago"},
55
		{strconv.Itoa(utils.UnixFromPastDate(time.Hour * 24 * 2)), "2 days ago"},
56
		{strconv.Itoa(utils.UnixFromPastDate(time.Hour * 24 * 3)), "3 days ago"},
57
		{strconv.Itoa(utils.UnixFromPastDate(time.Hour * 24 * 4)), "4 days ago"},
58
		{strconv.Itoa(utils.UnixFromPastDate(time.Hour * 24 * 5)), "5 days ago"},
59
		{strconv.Itoa(utils.UnixFromPastDate(time.Hour * 24 * 6)), "6 days ago"},
60
		{strconv.Itoa(utils.UnixFromPastDate(time.Hour * 24 * 7)), "1 week ago"},
61
	}
62
63
	Reconfigure(Config{Language: LangEn})
64
65
	for _, tc := range cases {
66
		t.Run(tc.res, func(t *testing.T) {
67
			res, err := Parse(tc.date)
68
69
			if err != nil {
70
				t.Errorf("Error must be nil, but got %q instead", err)
71
			}
72
73
			if res != tc.res {
74
				t.Errorf("Result must be %q, but got %q instead", tc.res, res)
75
			}
76
		})
77
	}
78
}
79