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

Add tests for config type http path

tags/v2.0.0-rc1
9seconds 5 лет назад
Родитель
Сommit
f7dc54b652
4 измененных файлов: 97 добавлений и 2 удалений
  1. 1
    1
      config/type_bytes_test.go
  2. 1
    1
      config/type_duration_test.go
  3. 4
    0
      config/type_http_path.go
  4. 91
    0
      config/type_http_path_test.go

+ 1
- 1
config/type_bytes_test.go Просмотреть файл

@@ -87,7 +87,7 @@ func (suite *TypeBytesTestSuite) TestMarshalOk() {
87 87
 
88 88
 			marshalled, err := testStruct.Value.MarshalText()
89 89
 			assert.NoError(t, err)
90
-			assert.Equal(t, string(marshalled), name)
90
+			assert.Equal(t, name, string(marshalled))
91 91
 		})
92 92
 	}
93 93
 }

+ 1
- 1
config/type_duration_test.go Просмотреть файл

@@ -85,7 +85,7 @@ func (suite *TypeDurationTestSuite) TestMarshalOk() {
85 85
 
86 86
 			marshalled, err := testStruct.Value.MarshalText()
87 87
 			assert.NoError(t, err)
88
-			assert.Equal(t, string(marshalled), name)
88
+			assert.Equal(t, name, string(marshalled))
89 89
 		})
90 90
 	}
91 91
 }

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

@@ -19,6 +19,10 @@ func (c TypeHTTPPath) MarshalText() ([]byte, error) {
19 19
 }
20 20
 
21 21
 func (c TypeHTTPPath) String() string {
22
+	if c.value == "" {
23
+		return "/"
24
+	}
25
+
22 26
 	return c.value
23 27
 }
24 28
 

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

@@ -0,0 +1,91 @@
1
+package config_test
2
+
3
+import (
4
+	"encoding/json"
5
+	"testing"
6
+
7
+	"github.com/9seconds/mtg/v2/config"
8
+	"github.com/stretchr/testify/assert"
9
+	"github.com/stretchr/testify/suite"
10
+)
11
+
12
+type typeHTTPPathTestStruct struct {
13
+	Value config.TypeHTTPPath `json:"value"`
14
+}
15
+
16
+type TypeHTTPPathTestSuite struct {
17
+	suite.Suite
18
+}
19
+
20
+func (suite *TypeHTTPPathTestSuite) TestUnmarshal() {
21
+	testData := []string{
22
+		"/hello",
23
+		"hello",
24
+		"hello/",
25
+		"/hello/",
26
+	}
27
+
28
+	for _, v := range testData {
29
+		data, err := json.Marshal(map[string]string{
30
+			"value": v,
31
+		})
32
+		suite.NoError(err)
33
+
34
+		suite.T().Run(v, func(t *testing.T) {
35
+			testStruct := &typeHTTPPathTestStruct{}
36
+
37
+			assert.NoError(t, json.Unmarshal(data, testStruct))
38
+			assert.Equal(t, "/hello", testStruct.Value.Value(""))
39
+		})
40
+	}
41
+}
42
+
43
+func (suite *TypeHTTPPathTestSuite) TestMarshalOk() {
44
+	testData := map[string]string{
45
+		"":        "/",
46
+		"/hello":  "/hello",
47
+		"/hello/": "/hello",
48
+		"hello/":  "/hello",
49
+		"hello":   "/hello",
50
+	}
51
+
52
+	for k, v := range testData {
53
+		toPass := k
54
+		compareWith := v
55
+
56
+		data, err := json.Marshal(map[string]string{
57
+			"value": toPass,
58
+		})
59
+		suite.NoError(err)
60
+
61
+		suite.T().Run(toPass, func(t *testing.T) {
62
+			testStruct := &typeHTTPPathTestStruct{}
63
+
64
+			assert.NoError(t, json.Unmarshal(data, testStruct))
65
+			assert.Equal(t, compareWith, testStruct.Value.String())
66
+
67
+			marshalled, err := testStruct.Value.MarshalText()
68
+			assert.NoError(t, err)
69
+			assert.Equal(t, compareWith, string(marshalled))
70
+		})
71
+	}
72
+}
73
+
74
+func (suite *TypeHTTPPathTestSuite) TestValue() {
75
+	testStruct := &typeHTTPPathTestStruct{}
76
+
77
+	suite.Equal("/hello", testStruct.Value.Value("/hello"))
78
+
79
+	data, err := json.Marshal(map[string]string{
80
+		"value": "/map",
81
+	})
82
+	suite.NoError(err)
83
+	suite.NoError(json.Unmarshal(data, testStruct))
84
+
85
+	suite.Equal("/map", testStruct.Value.Value("/hello"))
86
+}
87
+
88
+func TestTypeHTTPPath(t *testing.T) {
89
+	t.Parallel()
90
+	suite.Run(t, &TypeHTTPPathTestSuite{})
91
+}

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