From 01b34c8ea0f8ee9bbe0267f458fc5a134a5acfac Mon Sep 17 00:00:00 2001 From: jregan Date: Sun, 9 Aug 2020 11:27:25 -0700 Subject: [PATCH] Add tests for IsNil --- kyaml/yaml/types.go | 14 +++++++------- kyaml/yaml/types_test.go | 24 ++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 7 deletions(-) diff --git a/kyaml/yaml/types.go b/kyaml/yaml/types.go index f44aa65e2..46c4c92d7 100644 --- a/kyaml/yaml/types.go +++ b/kyaml/yaml/types.go @@ -36,7 +36,7 @@ func MakeNullNode() *RNode { // IsMissingOrNull is true if the RNode is nil or explicitly tagged null. // TODO: make this a method on RNode. func IsMissingOrNull(node *RNode) bool { - return IsNil(node) || node.YNode().Tag == NodeTagNull + return node.IsNil() || node.YNode().Tag == NodeTagNull } // Deprecated. Use IsMissingOrNull instead. @@ -49,13 +49,8 @@ func IsEmptyMap(node *RNode) bool { return IsMissingOrNull(node) || IsYNodeEmptyMap(node.YNode()) } -// IsNil return true if the node is nil, or its underlying YNode is nil. -func IsNil(node *RNode) bool { - return node == nil || node.YNode() == nil -} - func IsNull(node *RNode) bool { - return !IsNil(node) && node.YNode().Tag == NodeTagNull + return !node.IsNil() && node.YNode().Tag == NodeTagNull } func IsFieldEmpty(node *MapNode) bool { @@ -377,6 +372,11 @@ const ( LabelsField = "labels" ) +// IsNil is true if the node is nil, or its underlying YNode is nil. +func (rn *RNode) IsNil() bool { + return rn == nil || rn.YNode() == nil +} + // GetMeta returns the ResourceMeta for an RNode func (rn *RNode) GetMeta() (ResourceMeta, error) { if IsMissingOrNull(rn) { diff --git a/kyaml/yaml/types_test.go b/kyaml/yaml/types_test.go index 53b687e9a..27c60d4b8 100644 --- a/kyaml/yaml/types_test.go +++ b/kyaml/yaml/types_test.go @@ -220,3 +220,27 @@ func TestIsEmptyMap(t *testing.T) { t.Fatalf("input: empty map") } } + +func TestIsNil(t *testing.T) { + var rn *RNode + + if !rn.IsNil() { + t.Fatalf("uninitialized RNode should be nil") + } + + if !NewRNode(nil).IsNil() { + t.Fatalf("missing value YNode should be nil") + } + + if MakeNullNode().IsNil() { + t.Fatalf("value tagged null is not nil") + } + + if NewMapRNode(nil).IsNil() { + t.Fatalf("empty map not nil") + } + + if NewListRNode().IsNil() { + t.Fatalf("empty list not nil") + } +}