Compare commits
1 Commits
6120ab7dea
...
scripts/v0
| Author | SHA1 | Date | |
|---|---|---|---|
| 99c54bfc0c |
@@ -4,7 +4,6 @@ package mime
|
|||||||
import (
|
import (
|
||||||
"encoding"
|
"encoding"
|
||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@@ -31,7 +30,7 @@ func (v Ext) MarshalText() ([]byte, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *Ext) UnmarshalText(text []byte) error {
|
func (s *Ext) UnmarshalText(text []byte) error {
|
||||||
ext := ExtFromString(strings.ToLower(string(text)))
|
ext := ExtFromString(string(text))
|
||||||
*s = ext
|
*s = ext
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
|||||||
41
extension_test.go
Normal file
41
extension_test.go
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
package mime_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
"gitoa.ru/go-4devs/mime"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestExtUnmarshalText(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
cases := []struct {
|
||||||
|
data string
|
||||||
|
expext mime.Ext
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
data: "txt",
|
||||||
|
expext: mime.ExtTxt,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
data: "TXT",
|
||||||
|
expext: mime.ExtUnrecognized,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
data: "C",
|
||||||
|
expext: mime.Ext_C,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
data: "c",
|
||||||
|
expext: mime.ExtC,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, cs := range cases {
|
||||||
|
var in mime.Ext
|
||||||
|
err := in.UnmarshalText([]byte(cs.data))
|
||||||
|
require.Equal(t, cs.expext, in)
|
||||||
|
require.NoError(t, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -44,7 +44,7 @@ func ExampleMime_jsonUnmarshal() {
|
|||||||
|
|
||||||
var res Expect
|
var res Expect
|
||||||
|
|
||||||
json.Unmarshal([]byte(`{"data":"text","mime":"text/html"}`), &res)
|
_ = json.Unmarshal([]byte(`{"data":"text","mime":"text/html"}`), &res)
|
||||||
|
|
||||||
fmt.Printf("%[1]v: %[1]T(%#[1]v)", res.Mime)
|
fmt.Printf("%[1]v: %[1]T(%#[1]v)", res.Mime)
|
||||||
|
|
||||||
@@ -63,10 +63,11 @@ func ExampleMime_jsonMarshal() {
|
|||||||
Mime: mime.TextMarkdown,
|
Mime: mime.TextMarkdown,
|
||||||
}
|
}
|
||||||
|
|
||||||
result, _ := json.Marshal(res)
|
result, err := json.Marshal(res)
|
||||||
|
|
||||||
fmt.Printf("%s", result)
|
fmt.Printf("%s\n%v", result, err)
|
||||||
|
|
||||||
// Output:
|
// Output:
|
||||||
// {"data":"some text","mime":"text/markdown"}
|
// {"data":"some text","mime":"text/markdown"}
|
||||||
|
// <nil>
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,12 +8,16 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func TestMimeMarshalText(t *testing.T) {
|
func TestMimeMarshalText(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
res, err := mime.ApplicationJavascript.MarshalText()
|
res, err := mime.ApplicationJavascript.MarshalText()
|
||||||
require.Equal(t, "application/javascript", string(res))
|
require.Equal(t, "application/javascript", string(res))
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestMimeUnmarshalText(t *testing.T) {
|
func TestMimeUnmarshalText(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
cases := []struct {
|
cases := []struct {
|
||||||
data string
|
data string
|
||||||
expext mime.Mime
|
expext mime.Mime
|
||||||
|
|||||||
@@ -4,7 +4,6 @@ package {{.ExtPackage}}
|
|||||||
import (
|
import (
|
||||||
"encoding"
|
"encoding"
|
||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@@ -31,7 +30,7 @@ func (v Ext) MarshalText() ([]byte, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *Ext) UnmarshalText(text []byte) error {
|
func (s *Ext) UnmarshalText(text []byte) error {
|
||||||
ext := ExtFromString(strings.ToLower(string(text)))
|
ext := ExtFromString(string(text))
|
||||||
*s = ext
|
*s = ext
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
|||||||
Reference in New Issue
Block a user