Add tests for config type http path

This commit is contained in:
9seconds
2021-03-11 22:07:01 +03:00
parent c0ab254acf
commit f7dc54b652
4 changed files with 97 additions and 2 deletions
+1 -1
View File
@@ -87,7 +87,7 @@ func (suite *TypeBytesTestSuite) TestMarshalOk() {
marshalled, err := testStruct.Value.MarshalText()
assert.NoError(t, err)
assert.Equal(t, string(marshalled), name)
assert.Equal(t, name, string(marshalled))
})
}
}
+1 -1
View File
@@ -85,7 +85,7 @@ func (suite *TypeDurationTestSuite) TestMarshalOk() {
marshalled, err := testStruct.Value.MarshalText()
assert.NoError(t, err)
assert.Equal(t, string(marshalled), name)
assert.Equal(t, name, string(marshalled))
})
}
}
+4
View File
@@ -19,6 +19,10 @@ func (c TypeHTTPPath) MarshalText() ([]byte, error) {
}
func (c TypeHTTPPath) String() string {
if c.value == "" {
return "/"
}
return c.value
}
+91
View File
@@ -0,0 +1,91 @@
package config_test
import (
"encoding/json"
"testing"
"github.com/9seconds/mtg/v2/config"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
)
type typeHTTPPathTestStruct struct {
Value config.TypeHTTPPath `json:"value"`
}
type TypeHTTPPathTestSuite struct {
suite.Suite
}
func (suite *TypeHTTPPathTestSuite) TestUnmarshal() {
testData := []string{
"/hello",
"hello",
"hello/",
"/hello/",
}
for _, v := range testData {
data, err := json.Marshal(map[string]string{
"value": v,
})
suite.NoError(err)
suite.T().Run(v, func(t *testing.T) {
testStruct := &typeHTTPPathTestStruct{}
assert.NoError(t, json.Unmarshal(data, testStruct))
assert.Equal(t, "/hello", testStruct.Value.Value(""))
})
}
}
func (suite *TypeHTTPPathTestSuite) TestMarshalOk() {
testData := map[string]string{
"": "/",
"/hello": "/hello",
"/hello/": "/hello",
"hello/": "/hello",
"hello": "/hello",
}
for k, v := range testData {
toPass := k
compareWith := v
data, err := json.Marshal(map[string]string{
"value": toPass,
})
suite.NoError(err)
suite.T().Run(toPass, func(t *testing.T) {
testStruct := &typeHTTPPathTestStruct{}
assert.NoError(t, json.Unmarshal(data, testStruct))
assert.Equal(t, compareWith, testStruct.Value.String())
marshalled, err := testStruct.Value.MarshalText()
assert.NoError(t, err)
assert.Equal(t, compareWith, string(marshalled))
})
}
}
func (suite *TypeHTTPPathTestSuite) TestValue() {
testStruct := &typeHTTPPathTestStruct{}
suite.Equal("/hello", testStruct.Value.Value("/hello"))
data, err := json.Marshal(map[string]string{
"value": "/map",
})
suite.NoError(err)
suite.NoError(json.Unmarshal(data, testStruct))
suite.Equal("/map", testStruct.Value.Value("/hello"))
}
func TestTypeHTTPPath(t *testing.T) {
t.Parallel()
suite.Run(t, &TypeHTTPPathTestSuite{})
}