Skip to content

Commit

Permalink
#35 added tests for Index and Key methods
Browse files Browse the repository at this point in the history
  • Loading branch information
spyzhov committed Nov 6, 2021
1 parent 185b057 commit 0fa60cc
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
10 changes: 8 additions & 2 deletions node.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ func (n *Node) Source() []byte {
if n == nil {
return nil
}
if n.ready() && !n.dirty {
if n.ready() && !n.dirty && n.data != nil {
return (*n.data)[n.borders[0]:n.borders[1]]
}
return nil
Expand Down Expand Up @@ -233,7 +233,10 @@ func (n *Node) Type() NodeType {

// Key will return key of current node, please check, that parent of this node has an Object type
func (n *Node) Key() string {
if n == nil || n.key == nil {
if n == nil {
return ""
}
if n.key == nil {
return ""
}
return *n.key
Expand All @@ -244,6 +247,9 @@ func (n *Node) Index() int {
if n == nil {
return -1
}
if n.index == nil {
return -1
}
return *n.index
}

Expand Down
9 changes: 9 additions & 0 deletions node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,12 @@ func TestNode_Index(t *testing.T) {
if (*Node)(nil).Index() != -1 {
t.Errorf("Wrong value for (*Node)(nil).Index()")
}
if NullNode("").Index() != -1 {
t.Errorf("Wrong value for Null.Index()")
}
if ObjectNode("", nil).Index() != -1 {
t.Errorf("Wrong value for Null.Index()")
}
}

func TestNode_Key(t *testing.T) {
Expand All @@ -493,6 +499,9 @@ func TestNode_Key(t *testing.T) {
if (*Node)(nil).Key() != "" {
t.Errorf("Wrong value for (*Node)(nil).Key()")
}
if root.MustKey("foo").Clone().Key() != "" {
t.Errorf("Wrong value for Cloned.Key()")
}
}

func TestNode_IsArray(t *testing.T) {
Expand Down

0 comments on commit 0fa60cc

Please sign in to comment.