mirror of
https://github.com/jimeh/go-render.git
synced 2026-02-19 11:26:39 +00:00
refactor: focus around Render/Compact/Pretty/NewWith functions
This is yet another drastic refactor of public API and concepts. Hopefully the last one, as I'm now fairly happy with things.
This commit is contained in:
108
xml_test.go
108
xml_test.go
@@ -32,7 +32,6 @@ func (mxm *mockXMLMarshaler) MarshalXML(
|
||||
func TestXML_Render(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
pretty bool
|
||||
prefix string
|
||||
indent string
|
||||
value any
|
||||
@@ -41,8 +40,7 @@ func TestXML_Render(t *testing.T) {
|
||||
wantErrIs []error
|
||||
}{
|
||||
{
|
||||
name: "simple object without pretty",
|
||||
pretty: false,
|
||||
name: "simple object",
|
||||
value: struct {
|
||||
XMLName xml.Name `xml:"user"`
|
||||
Age int `xml:"age"`
|
||||
@@ -50,28 +48,7 @@ func TestXML_Render(t *testing.T) {
|
||||
want: `<user><age>30</age></user>`,
|
||||
},
|
||||
{
|
||||
name: "simple object with pretty",
|
||||
pretty: true,
|
||||
value: struct {
|
||||
XMLName xml.Name `xml:"user"`
|
||||
Age int `xml:"age"`
|
||||
}{Age: 30},
|
||||
want: "<user>\n <age>30</age>\n</user>",
|
||||
},
|
||||
{
|
||||
name: "pretty with prefix and indent",
|
||||
pretty: true,
|
||||
prefix: "//",
|
||||
indent: "\t",
|
||||
value: struct {
|
||||
XMLName xml.Name `xml:"user"`
|
||||
Age int `xml:"age"`
|
||||
}{Age: 30},
|
||||
want: "//<user>\n//\t<age>30</age>\n//</user>",
|
||||
},
|
||||
{
|
||||
name: "prefix and indent without pretty",
|
||||
pretty: false,
|
||||
name: "ignores indent without pretty",
|
||||
prefix: "//",
|
||||
indent: "\t",
|
||||
value: struct {
|
||||
@@ -93,7 +70,6 @@ func TestXML_Render(t *testing.T) {
|
||||
},
|
||||
{
|
||||
name: "invalid value",
|
||||
pretty: false,
|
||||
value: make(chan int),
|
||||
wantErr: "render: failed: xml: unsupported type: chan int",
|
||||
wantErrIs: []error{Err, ErrFailed},
|
||||
@@ -103,7 +79,6 @@ func TestXML_Render(t *testing.T) {
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
x := &XML{
|
||||
Pretty: tt.pretty,
|
||||
Prefix: tt.prefix,
|
||||
Indent: tt.indent,
|
||||
}
|
||||
@@ -126,3 +101,82 @@ func TestXML_Render(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestXML_RenderPretty(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
prefix string
|
||||
indent string
|
||||
value any
|
||||
want string
|
||||
wantErr string
|
||||
wantErrIs []error
|
||||
}{
|
||||
{
|
||||
name: "simple object",
|
||||
value: struct {
|
||||
XMLName xml.Name `xml:"user"`
|
||||
Age int `xml:"age"`
|
||||
}{Age: 30},
|
||||
want: "<user>\n <age>30</age>\n</user>",
|
||||
},
|
||||
{
|
||||
name: "uses prefix and indent",
|
||||
prefix: "//",
|
||||
indent: "\t",
|
||||
value: struct {
|
||||
XMLName xml.Name `xml:"user"`
|
||||
Age int `xml:"age"`
|
||||
}{Age: 30},
|
||||
want: "//<user>\n//\t<age>30</age>\n//</user>",
|
||||
},
|
||||
{
|
||||
name: "implements xml.Marshaler",
|
||||
value: &mockXMLMarshaler{elm: "test string"},
|
||||
want: "<mockXMLMarshaler>test string</mockXMLMarshaler>",
|
||||
},
|
||||
{
|
||||
name: "error from xml.Marshaler",
|
||||
value: &mockXMLMarshaler{err: errors.New("mock error")},
|
||||
wantErr: "render: failed: mock error",
|
||||
wantErrIs: []error{Err, ErrFailed},
|
||||
},
|
||||
{
|
||||
name: "invalid value",
|
||||
value: make(chan int),
|
||||
wantErr: "render: failed: xml: unsupported type: chan int",
|
||||
wantErrIs: []error{Err, ErrFailed},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
x := &XML{
|
||||
Prefix: tt.prefix,
|
||||
Indent: tt.indent,
|
||||
}
|
||||
var buf bytes.Buffer
|
||||
|
||||
err := x.RenderPretty(&buf, tt.value)
|
||||
got := buf.String()
|
||||
|
||||
if tt.wantErr != "" {
|
||||
assert.EqualError(t, err, tt.wantErr)
|
||||
}
|
||||
for _, e := range tt.wantErrIs {
|
||||
assert.ErrorIs(t, err, e)
|
||||
}
|
||||
|
||||
if tt.wantErr == "" && len(tt.wantErrIs) == 0 {
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, tt.want, got)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestXML_Formats(t *testing.T) {
|
||||
h := &XML{}
|
||||
|
||||
assert.Equal(t, []string{"xml"}, h.Formats())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user