From c6b6dec91fb160e13c68b55408f81051d78b6097 Mon Sep 17 00:00:00 2001 From: Donny Xia Date: Mon, 13 Jul 2020 14:15:55 -0700 Subject: [PATCH] Add tests for IsEmpty and IsMissingorNull --- kyaml/yaml/types_test.go | 80 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) diff --git a/kyaml/yaml/types_test.go b/kyaml/yaml/types_test.go index b963d5b28..68f26c001 100644 --- a/kyaml/yaml/types_test.go +++ b/kyaml/yaml/types_test.go @@ -8,6 +8,7 @@ import ( "testing" "github.com/stretchr/testify/assert" + "gopkg.in/yaml.v3" ) // Test that non-UTF8 characters in comments don't cause failures @@ -166,3 +167,82 @@ type: string } assert.Equal(t, expected, actual) } + +func TestIsMissingOrNull(t *testing.T) { + if IsMissingOrNull(nil) != true { + t.Fatalf("input: nil") + } + // missing value or null value + node := &RNode{value: nil} + if IsMissingOrNull(node) != true { + t.Fatalf("input: nil value") + } + + node.value = &yaml.Node{} + if IsMissingOrNull(node) != false { + t.Fatalf("input: valid node") + } + // node with NullNodeTag + node.value.Tag = NullNodeTag + if IsMissingOrNull(node) != true { + t.Fatalf("input: with NullNodeTag") + } + + node.value = &yaml.Node{} + if IsMissingOrNull(node) != false { + t.Fatalf("input: valid node") + } +} + +func TestIsEmpty(t *testing.T) { + if IsEmpty(nil) != true { + t.Fatalf("input: nil") + } + + // missing value or null value + node := &RNode{value: nil} + if IsEmpty(node) != true { + t.Fatalf("input: nil value") + } + // not array or map + node.value = &yaml.Node{} + if IsEmpty(node) != false { + t.Fatalf("input: not array or map") + } + + // === array tests === + node.value.Kind = yaml.SequenceNode + // empty array. empty array is not expected as empty + if IsEmpty(node) != false { + t.Fatalf("input: empty array") + } + // array with 1 item + node.value.Content = append(node.value.Content, &yaml.Node{}) + if IsEmpty(node) != false { + t.Fatalf("input: array with 1 item") + } + // delete the item in array + node.value.Content = node.value.Content[:len(node.value.Content)-1] + if IsEmpty(node) != false { + t.Fatalf("input: empty array") + } + + // === map tests === + node.value = &yaml.Node{ + Kind: yaml.MappingNode, + } + // empty map + if IsEmpty(node) != true { + t.Fatalf("input: empty map") + } + // map with 1 item + node.value.Content = append(node.value.Content, &yaml.Node{}) + if IsEmpty(node) != false { + t.Fatalf("input: map with 1 item") + } + // delete the item in map + node.value.Content = node.value.Content[:len(node.value.Content)-1] + if IsEmpty(node) != true { + t.Fatalf("input: empty map") + } +}