...

Source file src/sigs.k8s.io/kustomize/kyaml/yaml/types_test.go

Documentation: sigs.k8s.io/kustomize/kyaml/yaml

     1  // Copyright 2019 The Kubernetes Authors.
     2  // SPDX-License-Identifier: Apache-2.0
     3  
     4  package yaml
     5  
     6  import (
     7  	"reflect"
     8  	"testing"
     9  )
    10  
    11  func TestCopyYNode(t *testing.T) {
    12  	ynSub1 := Node{
    13  		Kind: 100,
    14  	}
    15  	ynSub2 := Node{
    16  		Kind: 200,
    17  	}
    18  	ynSub3 := Node{
    19  		Kind: 300,
    20  	}
    21  	yn := Node{
    22  		Kind:        5000,
    23  		Style:       6000,
    24  		Tag:         "red",
    25  		Value:       "green",
    26  		Anchor:      "blue",
    27  		Alias:       &ynSub3,
    28  		Content:     []*Node{&ynSub1, &ynSub2},
    29  		HeadComment: "apple",
    30  		LineComment: "peach",
    31  		FootComment: "banana",
    32  		Line:        7000,
    33  		Column:      8000,
    34  	}
    35  	if ynAddr := &yn; !reflect.DeepEqual(&yn, ynAddr) {
    36  		t.Fatalf("truly %v should equal %v", &yn, ynAddr)
    37  	}
    38  	ynC := CopyYNode(&yn)
    39  	if !reflect.DeepEqual(yn.Content, ynC.Content) {
    40  		t.Fatalf("copy content %v is not deep equal to %v", ynC, yn)
    41  	}
    42  	if !reflect.DeepEqual(&yn, ynC) {
    43  		t.Fatalf("\noriginal: %v\n    copy: %v\nShould be equal.", yn, ynC)
    44  	}
    45  	tmp := yn.Content[0].Kind
    46  	yn.Content[0].Kind = 666
    47  	if reflect.DeepEqual(&yn, ynC) {
    48  		t.Fatalf("changing component should break equality")
    49  	}
    50  	yn.Content[0].Kind = tmp
    51  	if !reflect.DeepEqual(&yn, ynC) {
    52  		t.Fatalf("should be okay now")
    53  	}
    54  	yn.Tag = "Different"
    55  	if yn.Tag == ynC.Tag {
    56  		t.Fatalf("field aliased!")
    57  	}
    58  }
    59  
    60  func TestIsYNodeString(t *testing.T) {
    61  	if IsYNodeTaggedNull(nil) {
    62  		t.Fatalf("nil cannot be tagged null")
    63  	}
    64  	if IsYNodeTaggedNull(&Node{}) {
    65  		t.Fatalf("untagged node is not tagged")
    66  	}
    67  	if IsYNodeString(&Node{Tag: NodeTagString}) {
    68  		t.Fatalf("non-scalar node is not a string")
    69  	}
    70  	if IsYNodeString(&Node{Kind: ScalarNode, Tag: NodeTagFloat}) {
    71  		t.Fatalf("float tagged node is not tagged")
    72  	}
    73  	if !IsYNodeString(&Node{Kind: ScalarNode}) {
    74  		t.Fatalf("this looks like a string - no tag implies string")
    75  	}
    76  	if !IsYNodeString(&Node{Kind: ScalarNode, Tag: NodeTagString}) {
    77  		t.Fatalf("this looks like a string")
    78  	}
    79  }
    80  
    81  func TestIsYNodeTaggedNull(t *testing.T) {
    82  	if IsYNodeTaggedNull(nil) {
    83  		t.Fatalf("nil cannot be tagged null")
    84  	}
    85  	if IsYNodeTaggedNull(&Node{}) {
    86  		t.Fatalf("untagged node is not tagged")
    87  	}
    88  	if IsYNodeTaggedNull(&Node{Tag: NodeTagFloat}) {
    89  		t.Fatalf("float tagged node is not tagged")
    90  	}
    91  	if !IsYNodeTaggedNull(&Node{Tag: NodeTagNull}) {
    92  		t.Fatalf("tagged node is tagged")
    93  	}
    94  }
    95  
    96  func TestIsYNodeEmptyMap(t *testing.T) {
    97  	if IsYNodeEmptyMap(nil) {
    98  		t.Fatalf("nil cannot be a map")
    99  	}
   100  	if IsYNodeEmptyMap(&Node{}) {
   101  		t.Fatalf("raw node is not a map")
   102  	}
   103  	if IsYNodeEmptyMap(&Node{Kind: SequenceNode}) {
   104  		t.Fatalf("seq node is not a map")
   105  	}
   106  	n := &Node{Kind: MappingNode}
   107  	if !IsYNodeEmptyMap(n) {
   108  		t.Fatalf("empty mapping node is an empty mapping node")
   109  	}
   110  	n.Content = append(n.Content, &Node{Kind: SequenceNode})
   111  	if IsYNodeEmptyMap(n) {
   112  		t.Fatalf("a node with content isn't empty")
   113  	}
   114  }
   115  
   116  func TestIsYNodeEmptySeq(t *testing.T) {
   117  	if IsYNodeEmptySeq(nil) {
   118  		t.Fatalf("nil cannot be a map")
   119  	}
   120  	if IsYNodeEmptySeq(&Node{}) {
   121  		t.Fatalf("raw node is not a map")
   122  	}
   123  	if IsYNodeEmptySeq(&Node{Kind: MappingNode}) {
   124  		t.Fatalf("map node is not a sequence")
   125  	}
   126  	n := &Node{Kind: SequenceNode}
   127  	if !IsYNodeEmptySeq(n) {
   128  		t.Fatalf("empty sequence node is an empty sequence node")
   129  	}
   130  	n.Content = append(n.Content, &Node{Kind: MappingNode})
   131  	if IsYNodeEmptySeq(n) {
   132  		t.Fatalf("a node with content isn't empty")
   133  	}
   134  }
   135  
   136  func TestIsYNodeZero(t *testing.T) {
   137  	if IsYNodeZero(nil) {
   138  		t.Fatalf("nil node should not be zero")
   139  	}
   140  	if !IsYNodeZero(&Node{}) {
   141  		t.Fatalf("node is zero")
   142  	}
   143  	if IsYNodeZero(&Node{Kind: MappingNode}) {
   144  		t.Fatalf("node is not zero")
   145  	}
   146  }
   147  

View as plain text