github.com/solo-io/cue@v0.4.7/encoding/yaml/yaml_test.go (about) 1 // Copyright 2019 CUE Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package yaml 16 17 import ( 18 "strings" 19 "testing" 20 21 "github.com/solo-io/cue/cue" 22 "github.com/solo-io/cue/cue/ast" 23 "github.com/solo-io/cue/cue/format" 24 ) 25 26 func TestYAML(t *testing.T) { 27 testCases := []struct { 28 name string 29 yaml string 30 yamlOut string 31 want string 32 isStream bool 33 }{{ 34 name: "empty", 35 yaml: "", 36 yamlOut: "null", 37 want: "null", 38 }, { 39 name: "empty stream", 40 want: "null", 41 isStream: true, 42 }, { 43 name: "string literal", 44 yaml: `foo`, 45 want: `"foo"`, 46 }, { 47 name: "struct", 48 yaml: `a: foo 49 b: bar`, 50 want: `a: "foo" 51 b: "bar"`, 52 }, { 53 name: "stream", 54 yaml: `a: foo 55 --- 56 b: bar 57 c: baz 58 `, 59 want: `[{ 60 a: "foo" 61 }, { 62 b: "bar" 63 c: "baz" 64 }]`, 65 isStream: true, 66 }, { 67 name: "stream with null", 68 yaml: ` 69 --- 70 a: foo 71 --- 72 --- 73 b: bar 74 c: baz 75 --- 76 `, 77 // Not sure if a leading document separator should be gobbled, but the 78 // YAML parser seems to think so. This could have something to do with 79 // the fact that the document separator is really an "end of directives" 80 // marker, while ... means "end of document". YAML is hard! 81 yamlOut: `a: foo 82 --- 83 null 84 --- 85 b: bar 86 c: baz 87 --- 88 null 89 `, 90 // TODO(bug): seems like bug in yaml parser. Try moving to yaml.v3, 91 // or validate that this is indeed a correct interpretation. 92 want: `[{ 93 a: "foo" 94 }, null, { 95 b: "bar" 96 c: "baz" 97 }, null]`, 98 isStream: true, 99 }} 100 r := &cue.Runtime{} 101 for _, tc := range testCases { 102 t.Run(tc.name, func(t *testing.T) { 103 f, err := Extract(tc.name, tc.yaml) 104 if err != nil { 105 t.Fatal(err) 106 } 107 b, _ := format.Node(f) 108 if got := strings.TrimSpace(string(b)); got != tc.want { 109 t.Errorf("Extract:\ngot %q\nwant %q", got, tc.want) 110 } 111 112 inst, err := Decode(r, tc.name, tc.yaml) 113 if err != nil { 114 t.Fatal(err) 115 } 116 n := inst.Value().Syntax() 117 if s, ok := n.(*ast.StructLit); ok { 118 n = &ast.File{Decls: s.Elts} 119 } 120 b, _ = format.Node(n) 121 if got := strings.TrimSpace(string(b)); got != tc.want { 122 t.Errorf("Decode:\ngot %q\nwant %q", got, tc.want) 123 } 124 125 yamlOut := tc.yaml 126 if tc.yamlOut != "" { 127 yamlOut = tc.yamlOut 128 } 129 130 inst, _ = r.Compile(tc.name, tc.want) 131 if !tc.isStream { 132 b, err = Encode(inst.Value()) 133 if err != nil { 134 t.Error(err) 135 } 136 if got := strings.TrimSpace(string(b)); got != yamlOut { 137 t.Errorf("Encode:\ngot %q\nwant %q", got, yamlOut) 138 } 139 } else { 140 iter, _ := inst.Value().List() 141 b, err := EncodeStream(iter) 142 if err != nil { 143 t.Error(err) 144 } 145 if got := string(b); got != yamlOut { 146 t.Errorf("EncodeStream:\ngot %q\nwant %q", got, yamlOut) 147 } 148 } 149 }) 150 } 151 }