Commit 428368d2 authored by Jakub Sztandera's avatar Jakub Sztandera Committed by GitHub

Merge pull request #20 from Stebalien/add-tests

Add tests
parents 864b7f9d f718bf7e
package format
import (
"errors"
"testing"
blocks "github.com/ipfs/go-block-format"
cid "github.com/ipfs/go-cid"
mh "github.com/multiformats/go-multihash"
)
func init() {
DefaultBlockDecoder[cid.Raw] = func(b blocks.Block) (Node, error) {
node := &EmptyNode{}
if b.RawData() != nil || !b.Cid().Equals(node.Cid()) {
return nil, errors.New("can only decode empty blocks")
}
return node, nil
}
}
func TestDecode(t *testing.T) {
id, err := cid.Prefix{
Version: 1,
Codec: cid.Raw,
MhType: mh.ID,
MhLength: 0,
}.Sum(nil)
if err != nil {
t.Fatalf("failed to create cid: %s", err)
}
block, err := blocks.NewBlockWithCid(nil, id)
if err != nil {
t.Fatalf("failed to create empty block: %s", err)
}
node, err := Decode(block)
if err != nil {
t.Fatalf("failed to decode empty node: %s", err)
}
if !node.Cid().Equals(id) {
t.Fatalf("empty node doesn't have the right cid")
}
if _, ok := node.(*EmptyNode); !ok {
t.Fatalf("empty node doesn't have the right type")
}
}
package format
import (
"errors"
"testing"
cid "github.com/ipfs/go-cid"
mh "github.com/multiformats/go-multihash"
)
type EmptyNode struct{}
var EmptyNodeError error = errors.New("dummy node")
func (n *EmptyNode) Resolve([]string) (interface{}, []string, error) {
return nil, nil, EmptyNodeError
}
func (n *EmptyNode) Tree(string, int) []string {
return nil
}
func (n *EmptyNode) ResolveLink([]string) (*Link, []string, error) {
return nil, nil, EmptyNodeError
}
func (n *EmptyNode) Copy() Node {
return &EmptyNode{}
}
func (n *EmptyNode) Cid() *cid.Cid {
id, err := cid.Prefix{
Version: 1,
Codec: cid.Raw,
MhType: mh.ID,
MhLength: 0,
}.Sum(nil)
if err != nil {
panic("failed to create an empty cid!")
}
return id
}
func (n *EmptyNode) Links() []*Link {
return nil
}
func (n *EmptyNode) Loggable() map[string]interface{} {
return nil
}
func (n *EmptyNode) String() string {
return "[]"
}
func (n *EmptyNode) RawData() []byte {
return nil
}
func (n *EmptyNode) Size() (uint64, error) {
return 0, nil
}
func (n *EmptyNode) Stat() (*NodeStat, error) {
return &NodeStat{}, nil
}
func TestNodeType(t *testing.T) {
// Type assertion.
var _ Node = &EmptyNode{}
}
......@@ -9,15 +9,21 @@
"gxDependencies": [
{
"author": "whyrusleeping",
"hash": "Qma4RJSuh7mMeJQYCqMbKzekn6EwBo7HEs5AQYjVRMQATB",
"hash": "QmTprEaAA2A9bst5XH7exuyi5KzNMK3SEDNN8rBDnKWcUS",
"name": "go-cid",
"version": "0.7.16"
"version": "0.7.17"
},
{
"author": "stebalien",
"hash": "QmXxGS5QsUxpR3iqL5DjmsYPHR1Yz74siRQ4ChJqWFosMh",
"hash": "QmVA4mafxbfH5aEvNz8fyoxC6J1xhAtw88B4GerPznSZBg",
"name": "go-block-format",
"version": "0.1.2"
"version": "0.1.3"
},
{
"author": "multiformats",
"hash": "QmU9a9NV9RdPNwZQDYd5uKsm6N6LJLSvLbywDDYFbaaC6P",
"name": "go-multihash",
"version": "1.0.5"
}
],
"gxVersion": "0.10.0",
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment