Skip to content

Commit c4d7606

Browse files
committed
better tests
1 parent 662d0e8 commit c4d7606

File tree

2 files changed

+24
-24
lines changed

2 files changed

+24
-24
lines changed

command_test.go

+5
Original file line numberDiff line numberDiff line change
@@ -367,5 +367,10 @@ func TestPostRun(t *testing.T) {
367367
t.Fatal("too few values in next")
368368
}
369369
}
370+
371+
_, err = res.Next()
372+
if err != io.EOF {
373+
t.Fatal("expected EOF, got", err)
374+
}
370375
}
371376
}

response_test.go

+19-24
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,54 @@
11
package cmds
22

33
import (
4-
// "bytes"
5-
// "fmt"
4+
"bytes"
5+
"fmt"
66
"strings"
7-
// "testing"
7+
"testing"
8+
9+
"github.com/ipfs/go-ipfs-cmds/cmdsutil"
810
)
911

1012
type TestOutput struct {
1113
Foo, Bar string
1214
Baz int
1315
}
1416

15-
/*
1617
func TestMarshalling(t *testing.T) {
1718
cmd := &Command{}
1819
opts, _ := cmd.GetOptions(nil)
1920

2021
req, _ := NewRequest(nil, nil, nil, nil, nil, opts)
2122

22-
res := NewResponse(req)
23-
res.SetOutput(TestOutput{"beep", "boop", 1337})
24-
25-
_, err := res.Marshal()
26-
if err == nil {
27-
t.Error("Should have failed (no encoding type specified in request)")
28-
}
23+
buf := bytes.NewBuffer(nil)
24+
wc := writecloser{Writer: buf, Closer: nopCloser{}}
25+
re := NewWriterResponseEmitter(wc, req, Encoders[JSON])
2926

30-
req.SetOption(EncShort, JSON)
31-
32-
reader, err := res.Marshal()
27+
err := re.Emit(TestOutput{"beep", "boop", 1337})
3328
if err != nil {
3429
t.Error(err, "Should have passed")
3530
}
36-
buf := new(bytes.Buffer)
37-
buf.ReadFrom(reader)
31+
32+
req.SetOption(cmdsutil.EncShort, JSON)
33+
3834
output := buf.String()
3935
if removeWhitespace(output) != "{\"Foo\":\"beep\",\"Bar\":\"boop\",\"Baz\":1337}" {
36+
t.Log("expected: {\"Foo\":\"beep\",\"Bar\":\"boop\",\"Baz\":1337}")
37+
t.Log("got:", removeWhitespace(buf.String()))
4038
t.Error("Incorrect JSON output")
4139
}
4240

43-
res.SetError(fmt.Errorf("Oops!"), ErrClient)
44-
reader, err = res.Marshal()
45-
if err != nil {
46-
t.Error("Should have passed")
47-
}
4841
buf.Reset()
49-
buf.ReadFrom(reader)
42+
43+
re.SetError(fmt.Errorf("Oops!"), cmdsutil.ErrClient)
44+
5045
output = buf.String()
51-
fmt.Println(removeWhitespace(output))
5246
if removeWhitespace(output) != "{\"Message\":\"Oops!\",\"Code\":1}" {
47+
t.Log("expected: {\"Message\":\"Oops!\",\"Code\":1}")
48+
t.Log("got:", removeWhitespace(buf.String()))
5349
t.Error("Incorrect JSON output")
5450
}
5551
}
56-
*/
5752

5853
func removeWhitespace(input string) string {
5954
input = strings.Replace(input, " ", "", -1)

0 commit comments

Comments
 (0)