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. 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 // TestReadResultCmd tests that readResultCmd can properly unmarshall the
// returned []byte that contains a json reply for both known and unknown // returned []byte that contains a json reply for both known and unknown
// messages. // messages.
func TestReadResultCmd(t *testing.T) { func TestReadResultCmd(t *testing.T) {
// Generate a fake message to make sure we can encode and decode it and for i, tt := range resulttests {
// get the same thing back. result, err := readResultCmd(tt.cmd, tt.msg)
msg := []byte(`{"result":226790,"error":{"code":1,"message":"No Error"},"id":"btcd"}`) if tt.pass {
result, err := readResultCmd("getblockcount", msg) if err != nil {
if err != nil { t.Errorf("Should read result: %d %v", i, err)
t.Errorf("Reading json reply to struct failed. %v", err) }
} // Due to the pointer for the Error and other structs,
msg2, err := json.Marshal(result) // we can't always guarantee byte for byte comparison.
if err != nil { if tt.comp {
t.Errorf("Converting struct back to json bytes failed. %v", err) msg2, err := json.Marshal(result)
} if err != nil {
if bytes.Compare(msg, msg2) != 0 { t.Errorf("Should unmarshal result: %d %v", i, err)
t.Errorf("json byte arrays differ.") }
} if bytes.Compare(tt.msg, msg2) != 0 {
// Generate a fake message to make sure we don't make a command from it. t.Errorf("json byte arrays differ. %d %v %v", i, tt.msg, msg2)
msg = []byte(`{"result":"test","id":1}`) }
_, err = readResultCmd("anycommand", msg) }
if err == nil {
t.Errorf("Incorrect json accepted.") } else {
if err == nil {
t.Errorf("Should fail: %d, %s", i, tt.msg)
}
}
} }
return 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/jsonfxns.go GetRaw 100.00% (6/6)
github.com/conformal/btcjson/jsonapi.go jsonWithArgs 100.00% (5/5) 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 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 RpcCommand 66.67% (18/27)
github.com/conformal/btcjson/jsonapi.go readResultCmd 40.00% (20/50) github.com/conformal/btcjson --------------- 96.74% (416/430)
github.com/conformal/btcjson --------------- 90.93% (391/430)