Skip to content

Commit

Permalink
Refactor Response#ToXML()
Browse files Browse the repository at this point in the history
  • Loading branch information
ruedap committed Apr 2, 2015
1 parent 719b8ab commit 99f02c8
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 11 deletions.
10 changes: 7 additions & 3 deletions command.go
Expand Up @@ -34,12 +34,16 @@ func (cmd *Command) Find(terms []string) int {
})
}

xml := r.ToXML()
_, err := fmt.Fprint(cmd.outStream, xml)
xml, err := r.ToXML()
if err != nil {
return ExitCodeError
}

if xml == "" || err != nil {
_, err = fmt.Fprint(cmd.outStream, xml)
if err != nil {
return ExitCodeError
}

return ExitCodeOK
}

Expand Down
8 changes: 2 additions & 6 deletions response.go
Expand Up @@ -40,11 +40,7 @@ func (r *Response) AddItem(item *ResponseItem) *Response {
return r
}

func (r *Response) ToXML() string {
func (r *Response) ToXML() (string, error) {
var x, err = xml.Marshal(r)
if err != nil {
panic(err) // FIXME
}

return xml.Header + string(x)
return xml.Header + string(x), err
}
14 changes: 12 additions & 2 deletions response_test.go
Expand Up @@ -40,7 +40,12 @@ func TestResponse_ToXML(t *testing.T) {
}
r.AddItem(&item)

actual := r.ToXML()
actual, err := r.ToXML()
if err != nil {
t.Error("failed to convert to XML")
return
}

expected := `<?xml version="1.0" encoding="UTF-8"?>
<items><item valid="true" arg="arg-foo" uid="f000-uid" unicode="f000-unicode"><title>title-foo</title><subtitle>Subtitle foo.</subtitle><icon>./icons/title-foo.png</icon></item></items>`
if actual != expected {
Expand All @@ -51,7 +56,12 @@ func TestResponse_ToXML(t *testing.T) {
func TestResponse_ToXML_Blank(t *testing.T) {
r := NewResponse([]string{})

actual := r.ToXML()
actual, err := r.ToXML()
if err != nil {
t.Error("failed to convert to XML")
return
}

expected := `<?xml version="1.0" encoding="UTF-8"?>
<items></items>`
if actual != expected {
Expand Down

0 comments on commit 99f02c8

Please sign in to comment.