Просмотр исходного кода

Add tests for config type ip

tags/v2.0.0-rc1
9seconds 5 лет назад
Родитель
Сommit
113b5ecbe0
1 измененных файлов: 114 добавлений и 0 удалений
  1. 114
    0
      config/type_ip_test.go

+ 114
- 0
config/type_ip_test.go Просмотреть файл

@@ -0,0 +1,114 @@
1
+package config_test
2
+
3
+import (
4
+	"encoding/json"
5
+	"net"
6
+	"testing"
7
+
8
+	"github.com/9seconds/mtg/v2/config"
9
+	"github.com/stretchr/testify/assert"
10
+	"github.com/stretchr/testify/suite"
11
+)
12
+
13
+type typeIPTestStruct struct {
14
+	Value config.TypeIP `json:"value"`
15
+}
16
+
17
+type TypeIPTestSuite struct {
18
+	suite.Suite
19
+}
20
+
21
+func (suite *TypeIPTestSuite) TestUnmarshalFail() {
22
+	testData := []string{
23
+		"0.0.10",
24
+		"10.0.0.10:",
25
+		"xxx:80",
26
+		"2001:0db8:85a3:0000:0000:8a2e:4",
27
+	}
28
+
29
+	for _, v := range testData {
30
+		data, err := json.Marshal(map[string]string{
31
+			"value": v,
32
+		})
33
+		suite.NoError(err)
34
+
35
+		suite.T().Run(v, func(t *testing.T) {
36
+			assert.Error(t, json.Unmarshal(data, &typeIPTestStruct{}))
37
+		})
38
+	}
39
+}
40
+
41
+func (suite *TypeIPTestSuite) TestUnmarshalOk() {
42
+	testData := []string{
43
+		"0.0.0.0",
44
+		"10.0.0.10",
45
+		"2001:0db8:85a3:0000:0000:8a2e:0370:7334",
46
+	}
47
+
48
+	for _, v := range testData {
49
+		value := v
50
+
51
+		data, err := json.Marshal(map[string]string{
52
+			"value": v,
53
+		})
54
+		suite.NoError(err)
55
+
56
+		suite.T().Run(v, func(t *testing.T) {
57
+			testStruct := &typeIPTestStruct{}
58
+
59
+			assert.NoError(t, json.Unmarshal(data, testStruct))
60
+			assert.Equal(t,
61
+				net.ParseIP(value).String(),
62
+				testStruct.Value.Value(nil).String())
63
+		})
64
+	}
65
+}
66
+
67
+func (suite *TypeIPTestSuite) TestMarshalOk() {
68
+	testData := []string{
69
+		"0.0.0.0",
70
+		"10.0.0.10",
71
+		"2001:0db8:85a3:0000:0000:8a2e:0370:7334",
72
+	}
73
+
74
+	for _, v := range testData {
75
+		value := net.ParseIP(v).String()
76
+
77
+		data, err := json.Marshal(map[string]string{
78
+			"value": v,
79
+		})
80
+		suite.NoError(err)
81
+
82
+		suite.T().Run(v, func(t *testing.T) {
83
+			testStruct := &typeIPTestStruct{}
84
+
85
+			assert.NoError(t, json.Unmarshal(data, testStruct))
86
+			assert.Equal(t, value, testStruct.Value.String())
87
+
88
+			marshalled, err := testStruct.Value.MarshalText()
89
+			assert.NoError(t, err)
90
+			assert.Equal(t, value, string(marshalled))
91
+		})
92
+	}
93
+}
94
+
95
+func (suite *TypeIPTestSuite) TestValue() {
96
+	testStruct := &typeIPTestStruct{}
97
+
98
+	suite.Nil(testStruct.Value.Value(nil))
99
+	suite.Equal("127.1.0.1", testStruct.Value.Value(net.ParseIP("127.1.0.1")).String())
100
+
101
+	data, err := json.Marshal(map[string]string{
102
+		"value": "127.0.0.1",
103
+	})
104
+	suite.NoError(err)
105
+	suite.NoError(json.Unmarshal(data, testStruct))
106
+
107
+	suite.Equal("127.0.0.1", testStruct.Value.Value(nil).String())
108
+	suite.Equal("127.0.0.1", testStruct.Value.Value(net.ParseIP("10.0.0.10")).String())
109
+}
110
+
111
+func TestTypeIP(t *testing.T) {
112
+	t.Parallel()
113
+	suite.Run(t, &TypeIPTestSuite{})
114
+}

Загрузка…
Отмена
Сохранить