Passed
Push — master ( 510e0f...ad64d3 )
by Serhii
02:43 queued 01:09
created

utils_test.go   A

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
cc 15
eloc 47
dl 0
loc 73
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A timeago.TestGetLastNumberDigit 0 24 4
A timeago.TestGetFileContent 0 9 3
A timeago.TestFileExists 0 14 5
A timeago.TestParseJsonIntoLang 0 6 3
1
package timeago
2
3
import (
4
	"encoding/json"
5
	"fmt"
6
	"testing"
7
)
8
9
func TestParseJsonIntoLang(t *testing.T) {
10
	t.Run("Function returns Lang model with needed values", func(test *testing.T) {
11
		result := parseJsonIntoLang("langs/ru.json")
12
13
		if result.Ago != "назад" {
14
			t.Errorf("Function needs to return model with value назад, but returned %v", result.Ago)
15
		}
16
	})
17
}
18
19
func TestFileExists(t *testing.T) {
20
	t.Run("fileExists return false if file doesn't exist", func(test *testing.T) {
21
		result, _ := fileExists("somerandompath")
22
23
		if result {
24
			t.Error("Function fileExists must return false, because filepath points to a file that doesn't exist")
25
		}
26
	})
27
28
	t.Run("fileExists return true if file exist", func(test *testing.T) {
29
		result, _ := fileExists("timeago.go")
30
31
		if result == false {
32
			t.Error("Function fileExists must return true, because filepath points to a file that exists")
33
		}
34
	})
35
}
36
37
func TestGetFileContent(t *testing.T) {
38
	t.Run("getFileContent returns content of the file", func(test *testing.T) {
39
		result := getFileContent("langs/en.json")
40
41
		var js json.RawMessage
42
		err := json.Unmarshal(result, &js)
43
44
		if err != nil {
45
			t.Errorf("Function getFileContent must return JSON object but %s returned", string(result))
46
		}
47
	})
48
}
49
50
func TestGetLastNumberDigit(t *testing.T) {
51
	cases := []struct {
52
		number int
53
		expect int
54
	}{
55
		{0, 0},
56
		{1, 1},
57
		{9, 9},
58
		{20, 0},
59
		{300, 0},
60
		{-1, 1},
61
		{253, 3},
62
		{23423252, 2},
63
		{22, 2},
64
		{4444, 4},
65
		{-24, 4},
66
		{23423521562348, 8},
67
		{23525, 5},
68
	}
69
70
	for _, tc := range cases {
71
		t.Run(fmt.Sprintf("Result must be %d", tc.expect), func(test *testing.T) {
72
			if res := getLastNumberDigit(tc.number); res != tc.expect {
73
				test.Errorf("Result must be %d, but got %d instead", tc.expect, res)
74
			}
75
		})
76
	}
77
}
78