1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332
|
package yaml
import (
"testing"
"github.com/zclconf/go-cty/cty"
)
func TestImpliedType(t *testing.T) {
tests := map[string]struct {
converter *Converter
src string
want cty.Type
wantErr string
}{
"single string doublequote": {
Standard,
`"hello"`,
cty.String,
``,
},
"single string singlequote": {
Standard,
`'hello'`,
cty.String,
``,
},
"single string implied": {
Standard,
`hello`,
cty.String,
``,
},
"single string implied not merge": {
Standard,
`<<`,
cty.String,
``,
},
"single string short tag": {
Standard,
`!!str true`,
cty.String,
``,
},
"single string long tag": {
Standard,
`!<tag:yaml.org,2002:str> true`,
cty.String,
``,
},
"single bool implied true": {
Standard,
`true`,
cty.Bool,
``,
},
"single bool implied false": {
Standard,
`false`,
cty.Bool,
``,
},
"single bool short tag": {
Standard,
`!!bool true`,
cty.Bool,
``,
},
"single bool long tag": {
Standard,
`!<tag:yaml.org,2002:bool> true`,
cty.Bool,
``,
},
"single bool short tag invalid": {
Standard,
`!!bool bananas`,
cty.NilType,
`cannot parse "bananas" as tag:yaml.org,2002:bool`,
},
"single float implied by prefix": {
Standard,
`.2`,
cty.Number,
``,
},
"single float implied by parsability": {
Standard,
`1.2`,
cty.Number,
``,
},
"single float short tag": {
Standard,
`!!float 1.2`,
cty.Number,
``,
},
"single int implied by parsability": {
Standard,
`12`,
cty.Number,
``,
},
"single int negative implied by parsability": {
Standard,
`-12`,
cty.Number,
``,
},
"single int short tag": {
Standard,
`!!int 1`,
cty.Number,
``,
},
"single positive infinity implied": {
Standard,
`+.Inf`,
cty.Number,
``,
},
"single negative infinity implied": {
Standard,
`-.Inf`,
cty.Number,
``,
},
"single NaN implied": {
Standard,
`.NaN`,
cty.NilType,
`floating point NaN is not supported`,
},
"single timestamp implied": {
Standard,
`2006-1-2`,
cty.String,
``,
},
"single timestamp short tag": {
Standard,
`!!timestamp 2006-1-2`,
cty.String,
``,
},
"single binary short tag": {
Standard,
`!!binary 'aGVsbG8='`,
cty.String,
``,
},
"single binary short tag invalid base64": {
Standard,
`!!binary '>>>>>>>>>'`,
cty.NilType,
`cannot parse ">>>>>>>>>" as tag:yaml.org,2002:binary: not valid base64`,
},
"single null implied": {
Standard,
`null`,
cty.DynamicPseudoType,
``,
},
"single scalar invalid tag": {
Standard,
`!!nope foo`,
cty.NilType,
`unsupported tag "tag:yaml.org,2002:nope"`,
},
"mapping empty flow mode": {
Standard,
`{}`,
cty.EmptyObject,
``,
},
"mapping flow mode": {
Standard,
`{a: 1, b: true}`,
cty.Object(map[string]cty.Type{
"a": cty.Number,
"b": cty.Bool,
}),
``,
},
"mapping multi-line mode": {
Standard,
`
a: 1
b: true
`,
cty.Object(map[string]cty.Type{
"a": cty.Number,
"b": cty.Bool,
}),
``,
},
"mapping with sequence multi-line mode": {
Standard,
`
a: 1
b:
- foo
- bar
- baz
`,
cty.Object(map[string]cty.Type{
"a": cty.Number,
"b": cty.Tuple([]cty.Type{
cty.String,
cty.String,
cty.String,
}),
}),
``,
},
"sequence empty flow mode": {
Standard,
`[]`,
cty.EmptyTuple,
``,
},
"sequence flow mode": {
Standard,
`[a, b, true]`,
cty.Tuple([]cty.Type{
cty.String,
cty.String,
cty.Bool,
}),
``,
},
"sequence multi-line mode": {
Standard,
`
- a
- <<
- true
`,
cty.Tuple([]cty.Type{
cty.String,
cty.String,
cty.Bool,
}),
``,
},
"alias": {
Standard,
`
foo: &bar
- x
bar: *bar
`,
cty.Object(map[string]cty.Type{
"foo": cty.Tuple([]cty.Type{cty.String}),
"bar": cty.Tuple([]cty.Type{cty.String}),
}),
``,
},
"alias cyclic": {
Standard,
`
foo: &bar
- x
- *bar
`,
cty.NilType,
`on line 3, column 5: cannot refer to anchor "bar" from inside its own definition`,
},
"alias merge": {
Standard,
`
foo: &bar
a: b
bar:
<<: *bar
c: d
`,
cty.Object(map[string]cty.Type{
"foo": cty.Object(map[string]cty.Type{
"a": cty.String,
}),
"bar": cty.Object(map[string]cty.Type{
"a": cty.String,
"c": cty.String,
}),
}),
``,
},
"alias scalar": {
Standard,
`
- &foo a
- b
- *foo
`,
cty.Tuple([]cty.Type{
cty.String,
cty.String,
cty.String,
}),
``,
},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
got, gotErr := test.converter.ImpliedType([]byte(test.src))
if gotErr != nil {
if test.wantErr == "" {
t.Fatalf("wrong error\ngot: %s\nwant: (no error)", gotErr.Error())
}
if got, want := gotErr.Error(), test.wantErr; got != want {
t.Fatalf("wrong error\ngot: %s\nwant: %s", got, want)
}
return
}
if test.wantErr != "" {
t.Fatalf("wrong error\ngot: (no error)\nwant: %s", test.wantErr)
}
if !test.want.Equals(got) {
t.Fatalf("wrong result\ngot: %#v\nwant: %#v", got, test.want)
}
})
}
}
|