2017-05-18 08:11:26 -07:00
|
|
|
package basecoin
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2017-07-04 03:46:57 -07:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
TxMapper.
|
|
|
|
RegisterImplementation(Demo{}, TypeDemo, ByteDemo).
|
|
|
|
RegisterImplementation(Fake{}, TypeFake, ByteFake)
|
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
|
|
|
ByteDemo = 0xF0
|
|
|
|
TypeDemo = "test/demo"
|
|
|
|
ByteFake = 0xF1
|
|
|
|
TypeFake = "test/fake"
|
2017-05-18 08:11:26 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
// define a Demo struct that implements TxLayer
|
|
|
|
type Demo struct{}
|
|
|
|
|
|
|
|
var _ TxLayer = Demo{}
|
|
|
|
|
2017-06-01 07:54:16 -07:00
|
|
|
func (d Demo) Next() Tx { return Tx{} }
|
|
|
|
func (d Demo) Wrap() Tx { return Tx{d} }
|
|
|
|
func (d Demo) ValidateBasic() error { return nil }
|
2017-05-18 08:11:26 -07:00
|
|
|
|
|
|
|
// define a Fake struct that doesn't implement TxLayer
|
|
|
|
type Fake struct{}
|
|
|
|
|
2017-06-01 07:54:16 -07:00
|
|
|
func (f Fake) Wrap() Tx { return Tx{f} }
|
|
|
|
func (f Fake) ValidateBasic() error { return nil }
|
2017-05-18 08:11:26 -07:00
|
|
|
|
|
|
|
// Make sure the layer
|
|
|
|
func TestLayer(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
// a fake tx, just don't use it...
|
|
|
|
nl := Fake{}.Wrap()
|
|
|
|
assert.False(nl.IsLayer())
|
|
|
|
assert.Nil(nl.GetLayer())
|
|
|
|
|
|
|
|
// a tx containing a TxLayer should respond properly
|
|
|
|
l := Demo{}.Wrap()
|
|
|
|
assert.True(l.IsLayer())
|
|
|
|
assert.NotNil(l.GetLayer())
|
|
|
|
}
|
2017-07-04 03:46:57 -07:00
|
|
|
|
|
|
|
func TestKind(t *testing.T) {
|
|
|
|
cases := []struct {
|
|
|
|
tx Tx
|
|
|
|
kind string
|
|
|
|
}{
|
|
|
|
{Demo{}.Wrap(), TypeDemo},
|
|
|
|
{Fake{}.Wrap(), TypeFake},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range cases {
|
|
|
|
kind, err := tc.tx.GetKind()
|
|
|
|
require.Nil(t, err, "%+v", err)
|
|
|
|
assert.Equal(t, tc.kind, kind)
|
|
|
|
}
|
|
|
|
}
|