Increase test coverage.

This commit is contained in:
John C. Vernaleo 2013-05-15 14:20:17 -04:00
parent 86f848ddd4
commit 39a1be8682
2 changed files with 48 additions and 21 deletions

View file

@ -17,30 +17,57 @@ cases which are either not possible or can't reliably be tested via the public
interface. The functions are only exported while the tests are being run.
*/
var resulttests = []struct {
cmd string
msg []byte
comp bool
pass bool
}{
// Generate a fake message to make sure we can encode and decode it and
// get the same thing back.
{"getblockcount",
[]byte(`{"result":226790,"error":{"code":1,"message":"No Error"},"id":"btcd"}`),
true, true},
// Generate a fake message to make sure we don't make a command from it.
{"anycommand", []byte(`{"result":"test","id":1}`), false, false},
{"anycommand", []byte(`{some junk}`), false, false},
{"getinfo", []byte(`{"error":null,"result":null,"id":"test"}`), false, true},
{"getinfo", []byte(`{"error":null,"result":null}`), false, false},
{"getinfo", []byte(`{"error":null,"id":1,"result":[{"a":"b"}]}`), false, false},
{"getblock", []byte(`{"error":null,"id":1,"result":[{"a":"b"}]}`), false, false},
{"getrawtransaction", []byte(`{"error":null,"id":1,"result":[{"a":"b"}]}`), false, false},
{"decoderawtransaction", []byte(`{"error":null,"id":1,"result":[{"a":"b"}]}`), false, false},
}
// TestReadResultCmd tests that readResultCmd can properly unmarshall the
// returned []byte that contains a json reply for both known and unknown
// messages.
func TestReadResultCmd(t *testing.T) {
// Generate a fake message to make sure we can encode and decode it and
// get the same thing back.
msg := []byte(`{"result":226790,"error":{"code":1,"message":"No Error"},"id":"btcd"}`)
result, err := readResultCmd("getblockcount", msg)
for i, tt := range resulttests {
result, err := readResultCmd(tt.cmd, tt.msg)
if tt.pass {
if err != nil {
t.Errorf("Reading json reply to struct failed. %v", err)
t.Errorf("Should read result: %d %v", i, err)
}
// Due to the pointer for the Error and other structs,
// we can't always guarantee byte for byte comparison.
if tt.comp {
msg2, err := json.Marshal(result)
if err != nil {
t.Errorf("Converting struct back to json bytes failed. %v", err)
t.Errorf("Should unmarshal result: %d %v", i, err)
}
if bytes.Compare(msg, msg2) != 0 {
t.Errorf("json byte arrays differ.")
if bytes.Compare(tt.msg, msg2) != 0 {
t.Errorf("json byte arrays differ. %d %v %v", i, tt.msg, msg2)
}
// Generate a fake message to make sure we don't make a command from it.
msg = []byte(`{"result":"test","id":1}`)
_, err = readResultCmd("anycommand", msg)
}
} else {
if err == nil {
t.Errorf("Incorrect json accepted.")
t.Errorf("Should fail: %d, %s", i, tt.msg)
}
}
}
return
}

View file

@ -6,7 +6,7 @@ github.com/conformal/btcjson/jsonfxns.go MarshallAndSend 100.00% (7/7)
github.com/conformal/btcjson/jsonfxns.go GetRaw 100.00% (6/6)
github.com/conformal/btcjson/jsonapi.go jsonWithArgs 100.00% (5/5)
github.com/conformal/btcjson/jsonapi.go IsValidIdType 100.00% (3/3)
github.com/conformal/btcjson/jsonapi.go readResultCmd 90.00% (45/50)
github.com/conformal/btcjson/jsonapi.go RpcCommand 66.67% (18/27)
github.com/conformal/btcjson/jsonapi.go readResultCmd 40.00% (20/50)
github.com/conformal/btcjson --------------- 90.93% (391/430)
github.com/conformal/btcjson --------------- 96.74% (416/430)