Skip to content

Commit

Permalink
Add Type() method to node.Node
Browse files Browse the repository at this point in the history
  • Loading branch information
qdm12 committed Dec 7, 2021
1 parent b95987e commit 8b28e30
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 0 deletions.
12 changes: 12 additions & 0 deletions lib/trie/branch/type.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package branch

import "github.com/ChainSafe/gossamer/lib/trie/node"

// Type returns node.BranchType if the branch value
// is nil, and node.BranchWithValueType otherwise.
func (b *Branch) Type() node.Type {
if b.Value == nil {
return node.BranchType
}
return node.BranchWithValueType
}
46 changes: 46 additions & 0 deletions lib/trie/branch/type_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright 2021 ChainSafe Systems (ON)
// SPDX-License-Identifier: LGPL-3.0-only

package branch

import (
"testing"

"github.com/ChainSafe/gossamer/lib/trie/node"
"github.com/stretchr/testify/assert"
)

func Test_Branch_Type(t *testing.T) {
testCases := map[string]struct {
branch *Branch
Type node.Type
}{
"nil value": {
branch: &Branch{},
Type: node.BranchType,
},
"empty value": {
branch: &Branch{
Value: []byte{},
},
Type: node.BranchWithValueType,
},
"non empty value": {
branch: &Branch{
Value: []byte{1},
},
Type: node.BranchWithValueType,
},
}

for name, testCase := range testCases {
testCase := testCase
t.Run(name, func(t *testing.T) {
t.Parallel()

Type := testCase.branch.Type()

assert.Equal(t, testCase.Type, Type)
})
}
}
8 changes: 8 additions & 0 deletions lib/trie/leaf/type.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package leaf

import "github.com/ChainSafe/gossamer/lib/trie/node"

// Type returns node.LeafType.
func (l *Leaf) Type() node.Type {
return node.LeafType
}
18 changes: 18 additions & 0 deletions lib/trie/leaf/type_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package leaf

import (
"testing"

"github.com/ChainSafe/gossamer/lib/trie/node"
"github.com/stretchr/testify/assert"
)

func Test_Leaf_Type(t *testing.T) {
t.Parallel()

leaf := new(Leaf)

Type := leaf.Type()

assert.Equal(t, node.LeafType, Type)
}

0 comments on commit 8b28e30

Please sign in to comment.