Explorar el Código

Add tests for config type http path

tags/v2.0.0-rc1
9seconds hace 5 años
padre
commit
f7dc54b652

+ 1
- 1
config/type_bytes_test.go Ver fichero

87
 
87
 
88
 			marshalled, err := testStruct.Value.MarshalText()
88
 			marshalled, err := testStruct.Value.MarshalText()
89
 			assert.NoError(t, err)
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 Ver fichero

85
 
85
 
86
 			marshalled, err := testStruct.Value.MarshalText()
86
 			marshalled, err := testStruct.Value.MarshalText()
87
 			assert.NoError(t, err)
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 Ver fichero

19
 }
19
 }
20
 
20
 
21
 func (c TypeHTTPPath) String() string {
21
 func (c TypeHTTPPath) String() string {
22
+	if c.value == "" {
23
+		return "/"
24
+	}
25
+
22
 	return c.value
26
 	return c.value
23
 }
27
 }
24
 
28
 

+ 91
- 0
config/type_http_path_test.go Ver fichero

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
+}

Loading…
Cancelar
Guardar