| 1 |
package yaml_test |
| 2 |
|
| 3 |
import ( |
| 4 |
"errors" |
| 5 |
"io" |
| 6 |
"math" |
| 7 |
"reflect" |
| 8 |
"strings" |
| 9 |
"testing" |
| 10 |
"time" |
| 11 |
|
| 12 |
"go.yaml.in/yaml/v2" |
| 13 |
) |
| 14 |
|
| 15 |
var unmarshalIntTest = 123 |
| 16 |
|
| 17 |
var unmarshalTests = []struct { |
| 18 |
data string |
| 19 |
value interface{} |
| 20 |
}{ |
| 21 |
{ |
| 22 |
"", |
| 23 |
(*struct{})(nil), |
| 24 |
}, |
| 25 |
{ |
| 26 |
"{}", &struct{}{}, |
| 27 |
}, { |
| 28 |
"v: hi", |
| 29 |
map[string]string{"v": "hi"}, |
| 30 |
}, { |
| 31 |
"v: hi", map[string]interface{}{"v": "hi"}, |
| 32 |
}, { |
| 33 |
"v: true", |
| 34 |
map[string]string{"v": "true"}, |
| 35 |
}, { |
| 36 |
"v: true", |
| 37 |
map[string]interface{}{"v": true}, |
| 38 |
}, { |
| 39 |
"v: 10", |
| 40 |
map[string]interface{}{"v": 10}, |
| 41 |
}, { |
| 42 |
"v: 0b10", |
| 43 |
map[string]interface{}{"v": 2}, |
| 44 |
}, { |
| 45 |
"v: 0xA", |
| 46 |
map[string]interface{}{"v": 10}, |
| 47 |
}, { |
| 48 |
"v: 4294967296", |
| 49 |
map[string]int64{"v": 4294967296}, |
| 50 |
}, { |
| 51 |
"v: 0.1", |
| 52 |
map[string]interface{}{"v": 0.1}, |
| 53 |
}, { |
| 54 |
"v: .1", |
| 55 |
map[string]interface{}{"v": 0.1}, |
| 56 |
}, { |
| 57 |
"v: .Inf", |
| 58 |
map[string]interface{}{"v": math.Inf(+1)}, |
| 59 |
}, { |
| 60 |
"v: -.Inf", |
| 61 |
map[string]interface{}{"v": math.Inf(-1)}, |
| 62 |
}, { |
| 63 |
"v: -10", |
| 64 |
map[string]interface{}{"v": -10}, |
| 65 |
}, { |
| 66 |
"v: -.1", |
| 67 |
map[string]interface{}{"v": -0.1}, |
| 68 |
}, |
| 69 |
|
| 70 |
// Simple values. |
| 71 |
{ |
| 72 |
"123", |
| 73 |
&unmarshalIntTest, |
| 74 |
}, |
| 75 |
|
| 76 |
// Floats from spec |
| 77 |
{ |
| 78 |
"canonical: 6.8523e+5", |
| 79 |
map[string]interface{}{"canonical": 6.8523e+5}, |
| 80 |
}, { |
| 81 |
"expo: 685.230_15e+03", |
| 82 |
map[string]interface{}{"expo": 685.23015e+03}, |
| 83 |
}, { |
| 84 |
"fixed: 685_230.15", |
| 85 |
map[string]interface{}{"fixed": 685230.15}, |
| 86 |
}, { |
| 87 |
"neginf: -.inf", |
| 88 |
map[string]interface{}{"neginf": math.Inf(-1)}, |
| 89 |
}, { |
| 90 |
"fixed: 685_230.15", |
| 91 |
map[string]float64{"fixed": 685230.15}, |
| 92 |
}, |
| 93 |
//{"sexa: 190:20:30.15", map[string]interface{}{"sexa": 0}}, // Unsupported |
| 94 |
//{"notanum: .NaN", map[string]interface{}{"notanum": math.NaN()}}, // Equality of NaN fails. |
| 95 |
|
| 96 |
// Bools from spec |
| 97 |
{ |
| 98 |
"canonical: y", |
| 99 |
map[string]interface{}{"canonical": true}, |
| 100 |
}, { |
| 101 |
"answer: NO", |
| 102 |
map[string]interface{}{"answer": false}, |
| 103 |
}, { |
| 104 |
"logical: True", |
| 105 |
map[string]interface{}{"logical": true}, |
| 106 |
}, { |
| 107 |
"option: on", |
| 108 |
map[string]interface{}{"option": true}, |
| 109 |
}, { |
| 110 |
"option: on", |
| 111 |
map[string]bool{"option": true}, |
| 112 |
}, |
| 113 |
// Ints from spec |
| 114 |
{ |
| 115 |
"canonical: 685230", |
| 116 |
map[string]interface{}{"canonical": 685230}, |
| 117 |
}, { |
| 118 |
"decimal: +685_230", |
| 119 |
map[string]interface{}{"decimal": 685230}, |
| 120 |
}, { |
| 121 |
"octal: 02472256", |
| 122 |
map[string]interface{}{"octal": 685230}, |
| 123 |
}, { |
| 124 |
"hexa: 0x_0A_74_AE", |
| 125 |
map[string]interface{}{"hexa": 685230}, |
| 126 |
}, { |
| 127 |
"bin: 0b1010_0111_0100_1010_1110", |
| 128 |
map[string]interface{}{"bin": 685230}, |
| 129 |
}, { |
| 130 |
"bin: -0b101010", |
| 131 |
map[string]interface{}{"bin": -42}, |
| 132 |
}, { |
| 133 |
"decimal: +685_230", |
| 134 |
map[string]int{"decimal": 685230}, |
| 135 |
}, |
| 136 |
|
| 137 |
//{"sexa: 190:20:30", map[string]interface{}{"sexa": 0}}, // Unsupported |
| 138 |
|
| 139 |
// Nulls from spec |
| 140 |
{ |
| 141 |
"empty:", |
| 142 |
map[string]interface{}{"empty": nil}, |
| 143 |
}, { |
| 144 |
"canonical: ~", |
| 145 |
map[string]interface{}{"canonical": nil}, |
| 146 |
}, { |
| 147 |
"english: null", |
| 148 |
map[string]interface{}{"english": nil}, |
| 149 |
}, { |
| 150 |
"~: null key", |
| 151 |
map[interface{}]string{nil: "null key"}, |
| 152 |
}, { |
| 153 |
"empty:", |
| 154 |
map[string]*bool{"empty": nil}, |
| 155 |
}, |
| 156 |
|
| 157 |
// Flow sequence |
| 158 |
{ |
| 159 |
"seq: [A,B]", |
| 160 |
map[string]interface{}{"seq": []interface{}{"A", "B"}}, |
| 161 |
}, { |
| 162 |
"seq: [A,B,C,]", |
| 163 |
map[string][]string{"seq": []string{"A", "B", "C"}}, |
| 164 |
}, { |
| 165 |
"seq: [A,1,C]", |
| 166 |
map[string][]string{"seq": []string{"A", "1", "C"}}, |
| 167 |
}, { |
| 168 |
"seq: [A,1,C]", |
| 169 |
map[string][]int{"seq": []int{1}}, |
| 170 |
}, { |
| 171 |
"seq: [A,1,C]", |
| 172 |
map[string]interface{}{"seq": []interface{}{"A", 1, "C"}}, |
| 173 |
}, |
| 174 |
// Block sequence |
| 175 |
{ |
| 176 |
"seq:\n - A\n - B", |
| 177 |
map[string]interface{}{"seq": []interface{}{"A", "B"}}, |
| 178 |
}, { |
| 179 |
"seq:\n - A\n - B\n - C", |
| 180 |
map[string][]string{"seq": []string{"A", "B", "C"}}, |
| 181 |
}, { |
| 182 |
"seq:\n - A\n - 1\n - C", |
| 183 |
map[string][]string{"seq": []string{"A", "1", "C"}}, |
| 184 |
}, { |
| 185 |
"seq:\n - A\n - 1\n - C", |
| 186 |
map[string][]int{"seq": []int{1}}, |
| 187 |
}, { |
| 188 |
"seq:\n - A\n - 1\n - C", |
| 189 |
map[string]interface{}{"seq": []interface{}{"A", 1, "C"}}, |
| 190 |
}, |
| 191 |
|
| 192 |
// Literal block scalar |
| 193 |
{ |
| 194 |
"scalar: | # Comment\n\n literal\n\n \ttext\n\n", |
| 195 |
map[string]string{"scalar": "\nliteral\n\n\ttext\n"}, |
| 196 |
}, |
| 197 |
|
| 198 |
// Folded block scalar |
| 199 |
{ |
| 200 |
"scalar: > # Comment\n\n folded\n line\n \n next\n line\n * one\n * two\n\n last\n line\n\n", |
| 201 |
map[string]string{"scalar": "\nfolded line\nnext line\n * one\n * two\n\nlast line\n"}, |
| 202 |
}, |
| 203 |
|
| 204 |
// Map inside interface with no type hints. |
| 205 |
{ |
| 206 |
"a: {b: c}", |
| 207 |
map[interface{}]interface{}{"a": map[interface{}]interface{}{"b": "c"}}, |
| 208 |
}, |
| 209 |
|
| 210 |
// Structs and type conversions. |
| 211 |
{ |
| 212 |
"hello: world", |
| 213 |
&struct{ Hello string }{"world"}, |
| 214 |
}, { |
| 215 |
"a: {b: c}", |
| 216 |
&struct{ A struct{ B string } }{struct{ B string }{"c"}}, |
| 217 |
}, { |
| 218 |
"a: {b: c}", |
| 219 |
&struct{ A *struct{ B string } }{&struct{ B string }{"c"}}, |
| 220 |
}, { |
| 221 |
"a: {b: c}", |
| 222 |
&struct{ A map[string]string }{map[string]string{"b": "c"}}, |
| 223 |
}, { |
| 224 |
"a: {b: c}", |
| 225 |
&struct{ A *map[string]string }{&map[string]string{"b": "c"}}, |
| 226 |
}, { |
| 227 |
"a:", |
| 228 |
&struct{ A map[string]string }{}, |
| 229 |
}, { |
| 230 |
"a: 1", |
| 231 |
&struct{ A int }{1}, |
| 232 |
}, { |
| 233 |
"a: 1", |
| 234 |
&struct{ A float64 }{1}, |
| 235 |
}, { |
| 236 |
"a: 1.0", |
| 237 |
&struct{ A int }{1}, |
| 238 |
}, { |
| 239 |
"a: 1.0", |
| 240 |
&struct{ A uint }{1}, |
| 241 |
}, { |
| 242 |
"a: [1, 2]", |
| 243 |
&struct{ A []int }{[]int{1, 2}}, |
| 244 |
}, { |
| 245 |
"a: [1, 2]", |
| 246 |
&struct{ A [2]int }{[2]int{1, 2}}, |
| 247 |
}, { |
| 248 |
"a: 1", |
| 249 |
&struct{ B int }{0}, |
| 250 |
}, { |
| 251 |
"a: 1", |
| 252 |
&struct { |
| 253 |
B int "a" |
| 254 |
}{1}, |
| 255 |
}, { |
| 256 |
"a: y", |
| 257 |
&struct{ A bool }{true}, |
| 258 |
}, |
| 259 |
|
| 260 |
// Some cross type conversions |
| 261 |
{ |
| 262 |
"v: 42", |
| 263 |
map[string]uint{"v": 42}, |
| 264 |
}, { |
| 265 |
"v: -42", |
| 266 |
map[string]uint{}, |
| 267 |
}, { |
| 268 |
"v: 4294967296", |
| 269 |
map[string]uint64{"v": 4294967296}, |
| 270 |
}, { |
| 271 |
"v: -4294967296", |
| 272 |
map[string]uint64{}, |
| 273 |
}, |
| 274 |
|
| 275 |
// int |
| 276 |
{ |
| 277 |
"int_max: 2147483647", |
| 278 |
map[string]int{"int_max": math.MaxInt32}, |
| 279 |
}, |
| 280 |
{ |
| 281 |
"int_min: -2147483648", |
| 282 |
map[string]int{"int_min": math.MinInt32}, |
| 283 |
}, |
| 284 |
{ |
| 285 |
"int_overflow: 9223372036854775808", // math.MaxInt64 + 1 |
| 286 |
map[string]int{}, |
| 287 |
}, |
| 288 |
|
| 289 |
// int64 |
| 290 |
{ |
| 291 |
"int64_max: 9223372036854775807", |
| 292 |
map[string]int64{"int64_max": math.MaxInt64}, |
| 293 |
}, |
| 294 |
{ |
| 295 |
"int64_max_base2: 0b111111111111111111111111111111111111111111111111111111111111111", |
| 296 |
map[string]int64{"int64_max_base2": math.MaxInt64}, |
| 297 |
}, |
| 298 |
{ |
| 299 |
"int64_min: -9223372036854775808", |
| 300 |
map[string]int64{"int64_min": math.MinInt64}, |
| 301 |
}, |
| •302 |
{ |
| 303 |
"int64_min_base2: -0b1000000000000000000000000000000000000000000000000000000000000000", |
| 304 |
map[string]int64{"int64_min_base2": math.MinInt64}, |
| 305 |
}, |
| 306 |
{ |
| 307 |
"int64_neg_base2: -0b111111111111111111111111111111111111111111111111111111111111111", |
| 308 |
map[string]int64{"int64_neg_base2": -math.MaxInt64}, |
| 309 |
}, |
| 310 |
{ |
| 311 |
"int64_overflow: 9223372036854775808", // math.MaxInt64 + 1 |
| 312 |
map[string]int64{}, |
| 313 |
}, |
| 314 |
|
| 315 |
// uint |
| 316 |
{ |
| 317 |
"uint_min: 0", |
| 318 |
map[string]uint{"uint_min": 0}, |
| 319 |
}, |
| 320 |
{ |
| 321 |
"uint_max: 4294967295", |
| 322 |
map[string]uint{"uint_max": math.MaxUint32}, |
| 323 |
}, |
| 324 |
{ |
| 325 |
"uint_underflow: -1", |
| 326 |
map[string]uint{}, |
| 327 |
}, |
| 328 |
|
| 329 |
// uint64 |
| 330 |
{ |
| 331 |
"uint64_min: 0", |
| 332 |
map[string]uint{"uint64_min": 0}, |
| 333 |
}, |
| 334 |
{ |
| 335 |
"uint64_max: 18446744073709551615", |
| 336 |
map[string]uint64{"uint64_max": math.MaxUint64}, |
| 337 |
}, |
| 338 |
{ |
| 339 |
"uint64_max_base2: 0b1111111111111111111111111111111111111111111111111111111111111111", |
| 340 |
map[string]uint64{"uint64_max_base2": math.MaxUint64}, |
| 341 |
}, |
| 342 |
{ |
| 343 |
"uint64_maxint64: 9223372036854775807", |
| 344 |
map[string]uint64{"uint64_maxint64": math.MaxInt64}, |
| 345 |
}, |
| 346 |
{ |
| 347 |
"uint64_underflow: -1", |
| 348 |
map[string]uint64{}, |
| 349 |
}, |
| 350 |
|
| 351 |
// float32 |
| 352 |
{ |
| 353 |
"float32_max: 3.40282346638528859811704183484516925440e+38", |
| 354 |
map[string]float32{"float32_max": math.MaxFloat32}, |
| 355 |
}, |
| 356 |
{ |
| 357 |
"float32_nonzero: 1.401298464324817070923729583289916131280e-45", |
| 358 |
map[string]float32{"float32_nonzero": math.SmallestNonzeroFloat32}, |
| 359 |
}, |
| 360 |
{ |
| 361 |
"float32_maxuint64: 18446744073709551615", |
| 362 |
map[string]float32{"float32_maxuint64": float32(math.MaxUint64)}, |
| 363 |
}, |
| 364 |
{ |
| 365 |
"float32_maxuint64+1: 18446744073709551616", |
| 366 |
map[string]float32{"float32_maxuint64+1": float32(math.MaxUint64 + 1)}, |
| 367 |
}, |
| 368 |
|
| 369 |
// float64 |
| 370 |
{ |
| 371 |
"float64_max: 1.797693134862315708145274237317043567981e+308", |
| 372 |
map[string]float64{"float64_max": math.MaxFloat64}, |
| 373 |
}, |
| 374 |
{ |
| 375 |
"float64_nonzero: 4.940656458412465441765687928682213723651e-324", |
| 376 |
map[string]float64{"float64_nonzero": math.SmallestNonzeroFloat64}, |
| 377 |
}, |
| 378 |
{ |
| 379 |
"float64_maxuint64: 18446744073709551615", |
| 380 |
map[string]float64{"float64_maxuint64": float64(math.MaxUint64)}, |
| 381 |
}, |
| 382 |
{ |
| 383 |
"float64_maxuint64+1: 18446744073709551616", |
| 384 |
map[string]float64{"float64_maxuint64+1": float64(math.MaxUint64 + 1)}, |
| 385 |
}, |
| 386 |
|
| 387 |
// Overflow cases. |
| 388 |
{ |
| 389 |
"v: 4294967297", |
| 390 |
map[string]int32{}, |
| 391 |
}, { |
| 392 |
"v: 128", |
| 393 |
map[string]int8{}, |
| 394 |
}, |
| 395 |
|
| 396 |
// Quoted values. |
| 397 |
{ |
| 398 |
"'1': '\"2\"'", |
| 399 |
map[interface{}]interface{}{"1": "\"2\""}, |
| 400 |
}, { |
| 401 |
"v:\n- A\n- 'B\n\n C'\n", |
| 402 |
map[string][]string{"v": []string{"A", "B\nC"}}, |
| 403 |
}, |
| 404 |
|
| 405 |
// Explicit tags. |
| 406 |
{ |
| 407 |
"v: !!float '1.1'", |
| 408 |
map[string]interface{}{"v": 1.1}, |
| 409 |
}, { |
| 410 |
"v: !!float 0", |
| 411 |
map[string]interface{}{"v": float64(0)}, |
| 412 |
}, { |
| 413 |
"v: !!float -1", |
| 414 |
map[string]interface{}{"v": float64(-1)}, |
| 415 |
}, { |
| 416 |
"v: !!null ''", |
| 417 |
map[string]interface{}{"v": nil}, |
| 418 |
}, { |
| 419 |
"%TAG !y! tag:yaml.org,2002:\n---\nv: !y!int '1'", |
| 420 |
map[string]interface{}{"v": 1}, |
| 421 |
}, |
| 422 |
|
| 423 |
// Non-specific tag (Issue #75) |
| 424 |
{ |
| 425 |
"v: ! test", |
| 426 |
map[string]interface{}{"v": "test"}, |
| 427 |
}, |
| 428 |
|
| 429 |
// Anchors and aliases. |
| 430 |
{ |
| 431 |
"a: &x 1\nb: &y 2\nc: *x\nd: *y\n", |
| 432 |
&struct{ A, B, C, D int }{1, 2, 1, 2}, |
| 433 |
}, { |
| 434 |
"a: &a {c: 1}\nb: *a", |
| 435 |
&struct { |
| 436 |
A, B struct { |
| 437 |
C int |
| 438 |
} |
| 439 |
}{struct{ C int }{1}, struct{ C int }{1}}, |
| 440 |
}, { |
| 441 |
"a: &a [1, 2]\nb: *a", |
| 442 |
&struct{ B []int }{[]int{1, 2}}, |
| 443 |
}, |
| 444 |
|
| 445 |
// Bug #1133337 |
| 446 |
{ |
| 447 |
"foo: ''", |
| 448 |
map[string]*string{"foo": new(string)}, |
| 449 |
}, { |
| 450 |
"foo: null", |
| 451 |
map[string]*string{"foo": nil}, |
| 452 |
}, { |
| 453 |
"foo: null", |
| 454 |
map[string]string{"foo": ""}, |
| 455 |
}, { |
| 456 |
"foo: null", |
| 457 |
map[string]interface{}{"foo": nil}, |
| 458 |
}, |
| 459 |
|
| 460 |
// Support for ~ |
| 461 |
{ |
| 462 |
"foo: ~", |
| 463 |
map[string]*string{"foo": nil}, |
| 464 |
}, { |
| 465 |
"foo: ~", |
| 466 |
map[string]string{"foo": ""}, |
| 467 |
}, { |
| 468 |
"foo: ~", |
| 469 |
map[string]interface{}{"foo": nil}, |
| 470 |
}, |
| 471 |
|
| 472 |
// Ignored field |
| 473 |
{ |
| 474 |
"a: 1\nb: 2\n", |
| 475 |
&struct { |
| 476 |
A int |
| 477 |
B int "-" |
| 478 |
}{1, 0}, |
| 479 |
}, |
| 480 |
|
| 481 |
// Bug #1191981 |
| 482 |
{ |
| 483 |
"" + |
| 484 |
"%YAML 1.1\n" + |
| 485 |
"--- !!str\n" + |
| 486 |
`"Generic line break (no glyph)\n\` + "\n" + |
| 487 |
` Generic line break (glyphed)\n\` + "\n" + |
| 488 |
` Line separator\u2028\` + "\n" + |
| 489 |
` Paragraph separator\u2029"` + "\n", |
| 490 |
"" + |
| 491 |
"Generic line break (no glyph)\n" + |
| 492 |
"Generic line break (glyphed)\n" + |
| 493 |
"Line separator\u2028Paragraph separator\u2029", |
| 494 |
}, |
| 495 |
|
| 496 |
// Struct inlining |
| 497 |
{ |
| 498 |
"a: 1\nb: 2\nc: 3\n", |
| 499 |
&struct { |
| 500 |
A int |
| 501 |
C inlineB `yaml:",inline"` |
| 502 |
}{1, inlineB{2, inlineC{3}}}, |
| 503 |
}, |
| 504 |
|
| 505 |
// Map inlining |
| 506 |
{ |
| 507 |
"a: 1\nb: 2\nc: 3\n", |
| 508 |
&struct { |
| 509 |
A int |
| 510 |
C map[string]int `yaml:",inline"` |
| 511 |
}{1, map[string]int{"b": 2, "c": 3}}, |
| 512 |
}, |
| 513 |
|
| 514 |
// bug 1243827 |
| 515 |
{ |
| 516 |
"a: -b_c", |
| 517 |
map[string]interface{}{"a": "-b_c"}, |
| 518 |
}, |
| 519 |
{ |
| 520 |
"a: +b_c", |
| 521 |
map[string]interface{}{"a": "+b_c"}, |
| 522 |
}, |
| 523 |
{ |
| 524 |
"a: 50cent_of_dollar", |
| 525 |
map[string]interface{}{"a": "50cent_of_dollar"}, |
| 526 |
}, |
| 527 |
|
| 528 |
// issue #295 (allow scalars with colons in flow mappings and sequences) |
| 529 |
{ |
| 530 |
"a: {b: https://github.com/go-yaml/yaml}", |
| 531 |
map[string]interface{}{"a": map[interface{}]interface{}{ |
| 532 |
"b": "https://github.com/go-yaml/yaml", |
| 533 |
}}, |
| 534 |
}, |
| 535 |
{ |
| 536 |
"a: [https://github.com/go-yaml/yaml]", |
| 537 |
map[string]interface{}{"a": []interface{}{"https://github.com/go-yaml/yaml"}}, |
| 538 |
}, |
| 539 |
|
| 540 |
// Duration |
| 541 |
{ |
| 542 |
"a: 3s", |
| 543 |
map[string]time.Duration{"a": 3 * time.Second}, |
| 544 |
}, |
| 545 |
|
| 546 |
// Issue #24. |
| 547 |
{ |
| 548 |
"a: <foo>", |
| 549 |
map[string]string{"a": "<foo>"}, |
| 550 |
}, |
| 551 |
|
| 552 |
// Base 60 floats are obsolete and unsupported. |
| 553 |
{ |
| 554 |
"a: 1:1\n", |
| 555 |
map[string]string{"a": "1:1"}, |
| 556 |
}, |
| 557 |
|
| 558 |
// Binary data. |
| 559 |
{ |
| 560 |
"a: !!binary gIGC\n", |
| 561 |
map[string]string{"a": "\x80\x81\x82"}, |
| 562 |
}, { |
| 563 |
"a: !!binary |\n " + strings.Repeat("kJCQ", 17) + "kJ\n CQ\n", |
| 564 |
map[string]string{"a": strings.Repeat("\x90", 54)}, |
| 565 |
}, { |
| 566 |
"a: !!binary |\n " + strings.Repeat("A", 70) + "\n ==\n", |
| 567 |
map[string]string{"a": strings.Repeat("\x00", 52)}, |
| 568 |
}, |
| 569 |
|
| 570 |
// Ordered maps. |
| 571 |
{ |
| 572 |
"{b: 2, a: 1, d: 4, c: 3, sub: {e: 5}}", |
| 573 |
&yaml.MapSlice{{"b", 2}, {"a", 1}, {"d", 4}, {"c", 3}, {"sub", yaml.MapSlice{{"e", 5}}}}, |
| 574 |
}, |
| 575 |
|
| 576 |
// Issue #39. |
| 577 |
{ |
| 578 |
"a:\n b:\n c: d\n", |
| 579 |
map[string]struct{ B interface{} }{"a": {map[interface{}]interface{}{"c": "d"}}}, |
| 580 |
}, |
| 581 |
|
| 582 |
// Custom map type. |
| 583 |
{ |
| 584 |
"a: {b: c}", |
| 585 |
M{"a": M{"b": "c"}}, |
| 586 |
}, |
| 587 |
|
| 588 |
// Support encoding.TextUnmarshaler. |
| 589 |
{ |
| 590 |
"a: 1.2.3.4\n", |
| 591 |
map[string]textUnmarshaler{"a": textUnmarshaler{S: "1.2.3.4"}}, |
| 592 |
}, |
| 593 |
{ |
| 594 |
"a: 2015-02-24T18:19:39Z\n", |
| 595 |
map[string]textUnmarshaler{"a": textUnmarshaler{"2015-02-24T18:19:39Z"}}, |
| 596 |
}, |
| 597 |
|
| 598 |
// Timestamps |
| 599 |
{ |
| 600 |
// Date only. |
| 601 |
"a: 2015-01-01\n", |
| 602 |
map[string]time.Time{"a": time.Date(2015, 1, 1, 0, 0, 0, 0, time.UTC)}, |
| 603 |
}, |
| 604 |
{ |
| 605 |
// RFC3339 |
| 606 |
"a: 2015-02-24T18:19:39.12Z\n", |
| 607 |
map[string]time.Time{"a": time.Date(2015, 2, 24, 18, 19, 39, .12e9, time.UTC)}, |
| 608 |
}, |
| 609 |
{ |
| 610 |
// RFC3339 with short dates. |
| 611 |
"a: 2015-2-3T3:4:5Z", |
| 612 |
map[string]time.Time{"a": time.Date(2015, 2, 3, 3, 4, 5, 0, time.UTC)}, |
| 613 |
}, |
| 614 |
{ |
| 615 |
// ISO8601 lower case t |
| 616 |
"a: 2015-02-24t18:19:39Z\n", |
| 617 |
map[string]time.Time{"a": time.Date(2015, 2, 24, 18, 19, 39, 0, time.UTC)}, |
| 618 |
}, |
| 619 |
{ |
| 620 |
// space separate, no time zone |
| 621 |
"a: 2015-02-24 18:19:39\n", |
| 622 |
map[string]time.Time{"a": time.Date(2015, 2, 24, 18, 19, 39, 0, time.UTC)}, |
| 623 |
}, |
| 624 |
// Some cases not currently handled. Uncomment these when |
| 625 |
// the code is fixed. |
| 626 |
// { |
| 627 |
// // space separated with time zone |
| 628 |
// "a: 2001-12-14 21:59:43.10 -5", |
| 629 |
// map[string]interface{}{"a": time.Date(2001, 12, 14, 21, 59, 43, .1e9, time.UTC)}, |
| 630 |
// }, |
| 631 |
// { |
| 632 |
// // arbitrary whitespace between fields |
| 633 |
// "a: 2001-12-14 \t\t \t21:59:43.10 \t Z", |
| 634 |
// map[string]interface{}{"a": time.Date(2001, 12, 14, 21, 59, 43, .1e9, time.UTC)}, |
| 635 |
// }, |
| 636 |
{ |
| 637 |
// explicit string tag |
| 638 |
"a: !!str 2015-01-01", |
| 639 |
map[string]interface{}{"a": "2015-01-01"}, |
| 640 |
}, |
| 641 |
{ |
| 642 |
// explicit timestamp tag on quoted string |
| 643 |
"a: !!timestamp \"2015-01-01\"", |
| 644 |
map[string]time.Time{"a": time.Date(2015, 1, 1, 0, 0, 0, 0, time.UTC)}, |
| 645 |
}, |
| 646 |
{ |
| 647 |
// explicit timestamp tag on unquoted string |
| 648 |
"a: !!timestamp 2015-01-01", |
| 649 |
map[string]time.Time{"a": time.Date(2015, 1, 1, 0, 0, 0, 0, time.UTC)}, |
| 650 |
}, |
| 651 |
{ |
| 652 |
// quoted string that's a valid timestamp |
| 653 |
"a: \"2015-01-01\"", |
| 654 |
map[string]interface{}{"a": "2015-01-01"}, |
| 655 |
}, |
| 656 |
{ |
| 657 |
// explicit timestamp tag into interface. |
| 658 |
"a: !!timestamp \"2015-01-01\"", |
| 659 |
map[string]interface{}{"a": "2015-01-01"}, |
| 660 |
}, |
| 661 |
{ |
| 662 |
// implicit timestamp tag into interface. |
| 663 |
"a: 2015-01-01", |
| 664 |
map[string]interface{}{"a": "2015-01-01"}, |
| 665 |
}, |
| 666 |
|
| 667 |
// Encode empty lists as zero-length slices. |
| 668 |
{ |
| 669 |
"a: []", |
| 670 |
&struct{ A []int }{[]int{}}, |
| 671 |
}, |
| 672 |
|
| 673 |
// UTF-16-LE |
| 674 |
{ |
| 675 |
"\xff\xfe\xf1\x00o\x00\xf1\x00o\x00:\x00 \x00v\x00e\x00r\x00y\x00 \x00y\x00e\x00s\x00\n\x00", |
| 676 |
M{"ñoño": "very yes"}, |
| 677 |
}, |
| 678 |
// UTF-16-LE with surrogate. |
| 679 |
{ |
| 680 |
"\xff\xfe\xf1\x00o\x00\xf1\x00o\x00:\x00 \x00v\x00e\x00r\x00y\x00 \x00y\x00e\x00s\x00 \x00=\xd8\xd4\xdf\n\x00", |
| 681 |
M{"ñoño": "very yes 🟔"}, |
| 682 |
}, |
| 683 |
|
| 684 |
// UTF-16-BE |
| 685 |
{ |
| 686 |
"\xfe\xff\x00\xf1\x00o\x00\xf1\x00o\x00:\x00 \x00v\x00e\x00r\x00y\x00 \x00y\x00e\x00s\x00\n", |
| 687 |
M{"ñoño": "very yes"}, |
| 688 |
}, |
| 689 |
// UTF-16-BE with surrogate. |
| 690 |
{ |
| 691 |
"\xfe\xff\x00\xf1\x00o\x00\xf1\x00o\x00:\x00 \x00v\x00e\x00r\x00y\x00 \x00y\x00e\x00s\x00 \xd8=\xdf\xd4\x00\n", |
| 692 |
M{"ñoño": "very yes 🟔"}, |
| 693 |
}, |
| 694 |
|
| 695 |
// This *is* in fact a float number, per the spec. #171 was a mistake. |
| 696 |
{ |
| 697 |
"a: 123456e1\n", |
| 698 |
M{"a": 123456e1}, |
| 699 |
}, { |
| 700 |
"a: 123456E1\n", |
| 701 |
M{"a": 123456E1}, |
| 702 |
}, |
| 703 |
// yaml-test-suite 3GZX: Spec Example 7.1. Alias Nodes |
| 704 |
{ |
| 705 |
"First occurrence: &anchor Foo\nSecond occurrence: *anchor\nOverride anchor: &anchor Bar\nReuse anchor: *anchor\n", |
| 706 |
map[interface{}]interface{}{ |
| 707 |
"Reuse anchor": "Bar", |
| 708 |
"First occurrence": "Foo", |
| 709 |
"Second occurrence": "Foo", |
| 710 |
"Override anchor": "Bar", |
| 711 |
}, |
| 712 |
}, |
| 713 |
// Single document with garbage following it. |
| 714 |
{ |
| 715 |
"---\nhello\n...\n}not yaml", |
| 716 |
"hello", |
| 717 |
}, |
| 718 |
{ |
| 719 |
"a: 5\n", |
| 720 |
&struct{ A jsonNumberT }{"5"}, |
| 721 |
}, |
| 722 |
{ |
| 723 |
"a: 5.5\n", |
| 724 |
&struct{ A jsonNumberT }{"5.5"}, |
| 725 |
}, |
| 726 |
{ |
| 727 |
` |
| 728 |
a: |
| 729 |
b |
| 730 |
b: |
| 731 |
? a |
| 732 |
: a`, |
| 733 |
&M{"a": "b", |
| 734 |
"b": M{ |
| 735 |
"a": "a", |
| 736 |
}}, |
| 737 |
}, |
| 738 |
} |
| 739 |
|
| 740 |
type M map[interface{}]interface{} |
| 741 |
|
| 742 |
type inlineB struct { |
| 743 |
B int |
| 744 |
inlineC `yaml:",inline"` |
| 745 |
} |
| 746 |
|
| 747 |
type inlineC struct { |
| 748 |
C int |
| 749 |
} |
| 750 |
|
| 751 |
func TestUnmarshal(t *testing.T) { |
| 752 |
for i, item := range unmarshalTests { |
| 753 |
t.Logf("test %d: %q", i, item.data) |
| 754 |
typ := reflect.ValueOf(item.value).Type() |
| 755 |
value := reflect.New(typ) |
| 756 |
err := yaml.Unmarshal([]byte(item.data), value.Interface()) |
| 757 |
if _, ok := err.(*yaml.TypeError); !ok { |
| 758 |
if err != nil { |
| 759 |
t.Fatalf("Unmarshal() returned error: %v", err) |
| 760 |
} |
| 761 |
} |
| 762 |
if !reflect.DeepEqual(value.Elem().Interface(), item.value) { |
| 763 |
t.Fatalf("Unmarshal() returned %#v, want %#v (error: %v)", value.Elem().Interface(), item.value, err) |
| 764 |
} |
| 765 |
} |
| 766 |
} |
| 767 |
|
| 768 |
// TODO(v3): This test should also work when unmarshaling onto an interface{}. |
| 769 |
func TestUnmarshalFullTimestamp(t *testing.T) { |
| 770 |
// Full timestamp in same format as encoded. This is confirmed to be |
| 771 |
// properly decoded by Python as a timestamp as well. |
| 772 |
var str = "2015-02-24T18:19:39.123456789-03:00" |
| 773 |
var tm time.Time |
| 774 |
err := yaml.Unmarshal([]byte(str), &tm) |
| 775 |
if err != nil { |
| 776 |
t.Fatalf("Unmarshal() returned error: %v", err) |
| 777 |
} |
| 778 |
expected := time.Date(2015, 2, 24, 18, 19, 39, 123456789, tm.Location()) |
| 779 |
if !tm.Equal(expected) || tm.Location().String() != expected.Location().String() { |
| 780 |
t.Fatalf("Unmarshal() returned %v, want %v", tm, expected) |
| 781 |
} |
| 782 |
expectedUTC := time.Date(2015, 2, 24, 21, 19, 39, 123456789, time.UTC) |
| 783 |
if !tm.In(time.UTC).Equal(expectedUTC) { |
| 784 |
t.Fatalf("Unmarshal() in UTC returned %v, want %v", tm.In(time.UTC), expectedUTC) |
| 785 |
} |
| 786 |
} |
| 787 |
|
| 788 |
func TestDecoderSingleDocument(t *testing.T) { |
| 789 |
// Test that Decoder.Decode works as expected on |
| 790 |
// all the unmarshal tests. |
| 791 |
for i, item := range unmarshalTests { |
| 792 |
t.Logf("test %d: %q", i, item.data) |
| 793 |
if item.data == "" { |
| 794 |
// Behaviour differs when there's no YAML. |
| 795 |
continue |
| 796 |
} |
| 797 |
typ := reflect.ValueOf(item.value).Type() |
| 798 |
value := reflect.New(typ) |
| 799 |
err := yaml.NewDecoder(strings.NewReader(item.data)).Decode(value.Interface()) |
| 800 |
if _, ok := err.(*yaml.TypeError); !ok { |
| 801 |
if err != nil { |
| 802 |
t.Fatalf("Decode() returned error: %v", err) |
| 803 |
} |
| 804 |
} |
| 805 |
if !reflect.DeepEqual(value.Elem().Interface(), item.value) { |
| 806 |
t.Fatalf("Decode() returned %#v, want %#v", value.Elem().Interface(), item.value) |
| 807 |
} |
| 808 |
} |
| 809 |
} |
| 810 |
|
| 811 |
var decoderTests = []struct { |
| 812 |
data string |
| 813 |
values []interface{} |
| 814 |
}{{ |
| 815 |
"", |
| 816 |
nil, |
| 817 |
}, { |
| 818 |
"a: b", |
| 819 |
[]interface{}{ |
| 820 |
map[interface{}]interface{}{"a": "b"}, |
| 821 |
}, |
| 822 |
}, { |
| 823 |
"---\na: b\n...\n", |
| 824 |
[]interface{}{ |
| 825 |
map[interface{}]interface{}{"a": "b"}, |
| 826 |
}, |
| 827 |
}, { |
| 828 |
"---\n'hello'\n...\n---\ngoodbye\n...\n", |
| 829 |
[]interface{}{ |
| 830 |
"hello", |
| 831 |
"goodbye", |
| 832 |
}, |
| 833 |
}} |
| 834 |
|
| 835 |
func TestDecoder(t *testing.T) { |
| 836 |
for i, item := range decoderTests { |
| 837 |
t.Logf("test %d: %q", i, item.data) |
| 838 |
var values []interface{} |
| 839 |
dec := yaml.NewDecoder(strings.NewReader(item.data)) |
| 840 |
for { |
| 841 |
var value interface{} |
| 842 |
err := dec.Decode(&value) |
| 843 |
if err == io.EOF { |
| 844 |
break |
| 845 |
} |
| 846 |
if err != nil { |
| 847 |
t.Fatalf("Decode() returned error: %v", err) |
| 848 |
} |
| 849 |
values = append(values, value) |
| 850 |
} |
| 851 |
if !reflect.DeepEqual(values, item.values) { |
| 852 |
t.Fatalf("Decode() returned %#v, want %#v", values, item.values) |
| 853 |
} |
| 854 |
} |
| 855 |
} |
| 856 |
|
| 857 |
type errReader struct{} |
| 858 |
|
| 859 |
func (errReader) Read([]byte) (int, error) { |
| 860 |
return 0, errors.New("some read error") |
| 861 |
} |
| 862 |
|
| 863 |
func TestDecoderReadError(t *testing.T) { |
| 864 |
err := yaml.NewDecoder(errReader{}).Decode(&struct{}{}) |
| 865 |
if err == nil || !strings.Contains(err.Error(), `yaml: input error: some read error`) { |
| 866 |
t.Fatalf("Decode() returned %v, want error containing %q", err, `yaml: input error: some read error`) |
| 867 |
} |
| 868 |
} |
| 869 |
|
| 870 |
func TestUnmarshalNaN(t *testing.T) { |
| 871 |
value := map[string]interface{}{} |
| 872 |
err := yaml.Unmarshal([]byte("notanum: .NaN"), &value) |
| 873 |
if err != nil { |
| 874 |
t.Fatalf("Unmarshal() returned error: %v", err) |
| 875 |
} |
| 876 |
if !math.IsNaN(value["notanum"].(float64)) { |
| 877 |
t.Fatalf("Unmarshal() returned %v, want NaN", value["notanum"]) |
| 878 |
} |
| 879 |
} |
| 880 |
|
| 881 |
var unmarshalErrorTests = []struct { |
| 882 |
data, error string |
| 883 |
}{ |
| 884 |
{"v: !!float 'error'", "yaml: cannot decode !!str `error` as a !!float"}, |
| 885 |
{"v: [A,", "yaml: line 1: did not find expected node content"}, |
| 886 |
{"v:\n- [A,", "yaml: line 2: did not find expected node content"}, |
| 887 |
{"a:\n- b: *,", "yaml: line 2: did not find expected alphabetic or numeric character"}, |
| 888 |
{"a: *b\n", "yaml: unknown anchor 'b' referenced"}, |
| 889 |
{"a: &a\n b: *a\n", "yaml: anchor 'a' value contains itself"}, |
| 890 |
{"a: &x null\n<<:\n- *x\nb: &x {}\n", `yaml: map merge requires map or sequence of maps as the value`}, // Issue #529. |
| 891 |
{"value: -", "yaml: block sequence entries are not allowed in this context"}, |
| 892 |
{"a: !!binary ==", "yaml: !!binary value contains invalid base64 data"}, |
| 893 |
{"{[.]}", `yaml: invalid map key: []interface {}{"."}`}, |
| 894 |
{"{{.}}", `yaml: invalid map key: map[interface {}]interface {}{".":interface {}(nil)}`}, |
| 895 |
{"b: *a\na: &a {c: 1}", `yaml: unknown anchor 'a' referenced`}, |
| 896 |
{"%TAG !%79! tag:yaml.org,2002:\n---\nv: !%79!int '1'", "yaml: did not find expected whitespace"}, |
| 897 |
{"a:\n 1:\nb\n 2:", "could not find expected ':'"}, |
| 898 |
{ |
| 899 |
"a: &a [00,00,00,00,00,00,00,00,00]\n" + |
| 900 |
"b: &b [*a,*a,*a,*a,*a,*a,*a,*a,*a]\n" + |
| 901 |
"c: &c [*b,*b,*b,*b,*b,*b,*b,*b,*b]\n" + |
| 902 |
"d: &d [*c,*c,*c,*c,*c,*c,*c,*c,*c]\n" + |
| 903 |
"e: &e [*d,*d,*d,*d,*d,*d,*d,*d,*d]\n" + |
| 904 |
"f: &f [*e,*e,*e,*e,*e,*e,*e,*e,*e]\n" + |
| 905 |
"g: &g [*f,*f,*f,*f,*f,*f,*f,*f,*f]\n" + |
| 906 |
"h: &h [*g,*g,*g,*g,*g,*g,*g,*g,*g]\n" + |
| 907 |
"i: &i [*h,*h,*h,*h,*h,*h,*h,*h,*h]\n", |
| 908 |
"yaml: document contains excessive aliasing", |
| 909 |
}, |
| 910 |
} |
| 911 |
|
| 912 |
func TestUnmarshalErrors(t *testing.T) { |
| 913 |
for i, item := range unmarshalErrorTests { |
| 914 |
t.Logf("test %d: %q", i, item.data) |
| 915 |
var value interface{} |
| 916 |
err := yaml.Unmarshal([]byte(item.data), &value) |
| 917 |
if err == nil { |
| 918 |
t.Fatalf("Unmarshal() expected error, got none. Partial unmarshal: %#v", value) |
| 919 |
} |
| 920 |
if !strings.Contains(err.Error(), item.error) { |
| 921 |
t.Fatalf("Unmarshal() returned error %q, want containing %q. Partial unmarshal: %#v", err.Error(), item.error, value) |
| 922 |
} |
| 923 |
|
| 924 |
if strings.Contains(item.data, ":") { |
| 925 |
// Repeat test with typed value. |
| 926 |
var mapValue map[string]interface{} |
| 927 |
err := yaml.Unmarshal([]byte(item.data), &mapValue) |
| 928 |
if err == nil { |
| 929 |
t.Fatalf("Unmarshal() with map[string]interface{} expected error, got none. Partial unmarshal: %#v", mapValue) |
| 930 |
} |
| 931 |
if !strings.Contains(err.Error(), item.error) { |
| 932 |
t.Fatalf("Unmarshal() with map[string]interface{} returned error %q, want containing %q. Partial unmarshal: %#v", err.Error(), item.error, mapValue) |
| 933 |
} |
| 934 |
} |
| 935 |
} |
| 936 |
} |
| 937 |
|
| 938 |
func TestDecoderErrors(t *testing.T) { |
| 939 |
for _, item := range unmarshalErrorTests { |
| 940 |
var value interface{} |
| 941 |
err := yaml.NewDecoder(strings.NewReader(item.data)).Decode(&value) |
| 942 |
if err == nil { |
| 943 |
t.Fatalf("Decode() expected error, got none. Partial unmarshal: %#v", value) |
| 944 |
} |
| 945 |
if !strings.Contains(err.Error(), item.error) { |
| 946 |
t.Fatalf("Decode() returned error %q, want containing %q. Partial unmarshal: %#v", err.Error(), item.error, value) |
| 947 |
} |
| 948 |
} |
| 949 |
} |
| 950 |
|
| 951 |
var unmarshalerTests = []struct { |
| 952 |
data, tag string |
| 953 |
value interface{} |
| 954 |
}{ |
| 955 |
{"_: {hi: there}", "!!map", map[interface{}]interface{}{"hi": "there"}}, |
| 956 |
{"_: [1,A]", "!!seq", []interface{}{1, "A"}}, |
| 957 |
{"_: 10", "!!int", 10}, |
| 958 |
{"_: null", "!!null", nil}, |
| 959 |
{`_: BAR!`, "!!str", "BAR!"}, |
| 960 |
{`_: "BAR!"`, "!!str", "BAR!"}, |
| 961 |
{"_: !!foo 'BAR!'", "!!foo", "BAR!"}, |
| 962 |
{`_: ""`, "!!str", ""}, |
| 963 |
} |
| 964 |
|
| 965 |
var unmarshalerResult = map[int]error{} |
| 966 |
|
| 967 |
type unmarshalerType struct { |
| 968 |
value interface{} |
| 969 |
} |
| 970 |
|
| 971 |
func (o *unmarshalerType) UnmarshalYAML(unmarshal func(v interface{}) error) error { |
| 972 |
if err := unmarshal(&o.value); err != nil { |
| 973 |
return err |
| 974 |
} |
| 975 |
if i, ok := o.value.(int); ok { |
| 976 |
if result, ok := unmarshalerResult[i]; ok { |
| 977 |
return result |
| 978 |
} |
| 979 |
} |
| 980 |
return nil |
| 981 |
} |
| 982 |
|
| 983 |
type unmarshalerPointer struct { |
| 984 |
Field *unmarshalerType "_" |
| 985 |
} |
| 986 |
|
| 987 |
type unmarshalerValue struct { |
| 988 |
Field unmarshalerType "_" |
| 989 |
} |
| 990 |
|
| 991 |
func TestUnmarshalerPointerField(t *testing.T) { |
| 992 |
for _, item := range unmarshalerTests { |
| 993 |
obj := &unmarshalerPointer{} |
| 994 |
err := yaml.Unmarshal([]byte(item.data), obj) |
| 995 |
if err != nil { |
| 996 |
t.Fatalf("Unmarshal() returned error: %v", err) |
| 997 |
} |
| 998 |
if item.value == nil { |
| 999 |
if obj.Field != nil { |
| 1000 |
t.Fatalf("Unmarshal() returned %v, want nil", obj.Field) |
| 1001 |
} |
| 1002 |
} else { |
| 1003 |
if obj.Field == nil { |
| 1004 |
t.Fatalf("Unmarshal() returned nil, want non-nil. Value: %#v", item.value) |
| 1005 |
} |
| 1006 |
if !reflect.DeepEqual(obj.Field.value, item.value) { |
| 1007 |
t.Fatalf("Unmarshal() returned %#v, want %#v", obj.Field.value, item.value) |
| 1008 |
} |
| 1009 |
} |
| 1010 |
} |
| 1011 |
} |
| 1012 |
|
| 1013 |
func TestUnmarshalerValueField(t *testing.T) { |
| 1014 |
for _, item := range unmarshalerTests { |
| 1015 |
obj := &unmarshalerValue{} |
| 1016 |
err := yaml.Unmarshal([]byte(item.data), obj) |
| 1017 |
if err != nil { |
| 1018 |
t.Fatalf("Unmarshal() returned error: %v", err) |
| 1019 |
} |
| 1020 |
if !reflect.DeepEqual(obj.Field.value, item.value) { |
| 1021 |
t.Fatalf("Unmarshal() returned %#v, want %#v", obj.Field.value, item.value) |
| 1022 |
} |
| 1023 |
} |
| 1024 |
} |
| 1025 |
|
| 1026 |
func TestUnmarshalerWholeDocument(t *testing.T) { |
| 1027 |
obj := &unmarshalerType{} |
| 1028 |
err := yaml.Unmarshal([]byte(unmarshalerTests[0].data), obj) |
| 1029 |
if err != nil { |
| 1030 |
t.Fatalf("Unmarshal() returned error: %v", err) |
| 1031 |
} |
| 1032 |
value, ok := obj.value.(map[interface{}]interface{}) |
| 1033 |
if !ok { |
| 1034 |
t.Fatalf("obj.value is not a map[interface{}]interface{}. Value: %#v", obj.value) |
| 1035 |
} |
| 1036 |
if !reflect.DeepEqual(value["_"], unmarshalerTests[0].value) { |
| 1037 |
t.Fatalf("Unmarshal() returned %#v but expected %#v", value["_"], unmarshalerTests[0].value) |
| 1038 |
} |
| 1039 |
} |
| 1040 |
|
| 1041 |
func TestUnmarshalerTypeError(t *testing.T) { |
| 1042 |
unmarshalerResult[2] = &yaml.TypeError{[]string{"foo"}} |
| 1043 |
unmarshalerResult[4] = &yaml.TypeError{[]string{"bar"}} |
| 1044 |
defer func() { |
| 1045 |
delete(unmarshalerResult, 2) |
| 1046 |
delete(unmarshalerResult, 4) |
| 1047 |
}() |
| 1048 |
|
| 1049 |
type T struct { |
| 1050 |
Before int |
| 1051 |
After int |
| 1052 |
M map[string]*unmarshalerType |
| 1053 |
} |
| 1054 |
var v T |
| 1055 |
data := `{before: A, m: {abc: 1, def: 2, ghi: 3, jkl: 4}, after: B}` |
| 1056 |
err := yaml.Unmarshal([]byte(data), &v) |
| 1057 |
expectedError := "" + |
| 1058 |
"yaml: unmarshal errors:\n" + |
| 1059 |
" line 1: cannot unmarshal !!str `A` into int\n" + |
| 1060 |
" foo\n" + |
| 1061 |
" bar\n" + |
| 1062 |
" line 1: cannot unmarshal !!str `B` into int" |
| 1063 |
if err == nil { |
| 1064 |
t.Fatalf("Unmarshal() expected error, got none.") |
| 1065 |
} |
| 1066 |
if err.Error() != expectedError { |
| 1067 |
t.Fatalf("Unmarshal() returned error %q, want %q", err, expectedError) |
| 1068 |
} |
| 1069 |
if v.M["abc"] == nil { |
| 1070 |
t.Fatalf("v.M[\"abc\"] is nil, want non-nil") |
| 1071 |
} |
| 1072 |
if v.M["def"] != nil { |
| 1073 |
t.Fatalf("v.M[\"def\"] is not nil, want nil") |
| 1074 |
} |
| 1075 |
if v.M["ghi"] == nil { |
| 1076 |
t.Fatalf("v.M[\"ghi\"] is nil, want non-nil") |
| 1077 |
} |
| 1078 |
if v.M["jkl"] != nil { |
| 1079 |
t.Fatalf("v.M[\"jkl\"] is not nil, want nil") |
| 1080 |
} |
| 1081 |
|
| 1082 |
if !reflect.DeepEqual(v.M["abc"].value, 1) { |
| 1083 |
t.Fatalf("v.M[\"abc\"].value is %v, want %v", v.M["abc"].value, 1) |
| 1084 |
} |
| 1085 |
if !reflect.DeepEqual(v.M["ghi"].value, 3) { |
| 1086 |
t.Fatalf("v.M[\"ghi\"].value is %v, want %v", v.M["ghi"].value, 3) |
| 1087 |
} |
| 1088 |
} |
| 1089 |
|
| 1090 |
type proxyTypeError struct{} |
| 1091 |
|
| 1092 |
func (v *proxyTypeError) UnmarshalYAML(unmarshal func(interface{}) error) error { |
| 1093 |
var s string |
| 1094 |
var a int32 |
| 1095 |
var b int64 |
| 1096 |
if err := unmarshal(&s); err != nil { |
| 1097 |
panic(err) |
| 1098 |
} |
| 1099 |
if s == "a" { |
| 1100 |
if err := unmarshal(&b); err == nil { |
| 1101 |
panic("should have failed") |
| 1102 |
} |
| 1103 |
return unmarshal(&a) |
| 1104 |
} |
| 1105 |
if err := unmarshal(&a); err == nil { |
| 1106 |
panic("should have failed") |
| 1107 |
} |
| 1108 |
return unmarshal(&b) |
| 1109 |
} |
| 1110 |
|
| 1111 |
func TestUnmarshalerTypeErrorProxying(t *testing.T) { |
| 1112 |
type T struct { |
| 1113 |
Before int |
| 1114 |
After int |
| 1115 |
M map[string]*proxyTypeError |
| 1116 |
} |
| 1117 |
var v T |
| 1118 |
data := `{before: A, m: {abc: a, def: b}, after: B}` |
| 1119 |
err := yaml.Unmarshal([]byte(data), &v) |
| 1120 |
expectedError := "" + |
| 1121 |
"yaml: unmarshal errors:\n" + |
| 1122 |
" line 1: cannot unmarshal !!str `A` into int\n" + |
| 1123 |
" line 1: cannot unmarshal !!str `a` into int32\n" + |
| 1124 |
" line 1: cannot unmarshal !!str `b` into int64\n" + |
| 1125 |
" line 1: cannot unmarshal !!str `B` into int" |
| 1126 |
if err == nil { |
| 1127 |
t.Fatalf("Unmarshal() expected error, got none.") |
| 1128 |
} |
| 1129 |
if err.Error() != expectedError { |
| 1130 |
t.Fatalf("Unmarshal() returned error %q, want %q", err, expectedError) |
| 1131 |
} |
| 1132 |
} |
| 1133 |
|
| 1134 |
type failingUnmarshaler struct{} |
| 1135 |
|
| 1136 |
var failingErr = errors.New("failingErr") |
| 1137 |
|
| 1138 |
func (ft *failingUnmarshaler) UnmarshalYAML(unmarshal func(interface{}) error) error { |
| 1139 |
return failingErr |
| 1140 |
} |
| 1141 |
|
| 1142 |
func TestUnmarshalerError(t *testing.T) { |
| 1143 |
err := yaml.Unmarshal([]byte("a: b"), &failingUnmarshaler{}) |
| 1144 |
if err != failingErr { |
| 1145 |
t.Fatalf("Unmarshal() returned %v, want %v", err, failingErr) |
| 1146 |
} |
| 1147 |
} |
| 1148 |
|
| 1149 |
type sliceUnmarshaler []int |
| 1150 |
|
| 1151 |
func (su *sliceUnmarshaler) UnmarshalYAML(unmarshal func(interface{}) error) error { |
| 1152 |
var slice []int |
| 1153 |
err := unmarshal(&slice) |
| 1154 |
if err == nil { |
| 1155 |
*su = slice |
| 1156 |
return nil |
| 1157 |
} |
| 1158 |
|
| 1159 |
var intVal int |
| 1160 |
err = unmarshal(&intVal) |
| 1161 |
if err == nil { |
| 1162 |
*su = []int{intVal} |
| 1163 |
return nil |
| 1164 |
} |
| 1165 |
|
| 1166 |
return err |
| 1167 |
} |
| 1168 |
|
| 1169 |
func TestUnmarshalerRetry(t *testing.T) { |
| 1170 |
var su sliceUnmarshaler |
| 1171 |
err := yaml.Unmarshal([]byte("[1, 2, 3]"), &su) |
| 1172 |
if err != nil { |
| 1173 |
t.Fatalf("Unmarshal() returned error: %v", err) |
| 1174 |
} |
| 1175 |
if !reflect.DeepEqual(su, sliceUnmarshaler([]int{1, 2, 3})) { |
| 1176 |
t.Fatalf("Unmarshal() returned %v, want %v", su, sliceUnmarshaler([]int{1, 2, 3})) |
| 1177 |
} |
| 1178 |
|
| 1179 |
err = yaml.Unmarshal([]byte("1"), &su) |
| 1180 |
if err != nil { |
| 1181 |
t.Fatalf("Unmarshal() returned error: %v", err) |
| 1182 |
} |
| 1183 |
if !reflect.DeepEqual(su, sliceUnmarshaler([]int{1})) { |
| 1184 |
t.Fatalf("Unmarshal() returned %v, want %v", su, sliceUnmarshaler([]int{1})) |
| 1185 |
} |
| 1186 |
} |
| 1187 |
|
| 1188 |
// From http://yaml.org/type/merge.html |
| 1189 |
var mergeTests = ` |
| 1190 |
anchors: |
| 1191 |
list: |
| 1192 |
- &CENTER { "x": 1, "y": 2 } |
| 1193 |
- &LEFT { "x": 0, "y": 2 } |
| 1194 |
- &BIG { "r": 10 } |
| 1195 |
- &SMALL { "r": 1 } |
| 1196 |
|
| 1197 |
# All the following maps are equal: |
| 1198 |
|
| 1199 |
plain: |
| 1200 |
# Explicit keys |
| 1201 |
"x": 1 |
| 1202 |
"y": 2 |
| 1203 |
"r": 10 |
| 1204 |
label: center/big |
| 1205 |
|
| 1206 |
mergeOne: |
| 1207 |
# Merge one map |
| 1208 |
<< : *CENTER |
| 1209 |
"r": 10 |
| 1210 |
label: center/big |
| 1211 |
|
| 1212 |
mergeMultiple: |
| 1213 |
# Merge multiple maps |
| 1214 |
<< : [ *CENTER, *BIG ] |
| 1215 |
label: center/big |
| 1216 |
|
| 1217 |
override: |
| 1218 |
# Override |
| 1219 |
<< : [ *BIG, *LEFT, *SMALL ] |
| 1220 |
"x": 1 |
| 1221 |
label: center/big |
| 1222 |
|
| 1223 |
shortTag: |
| 1224 |
# Explicit short merge tag |
| 1225 |
!!merge "<<" : [ *CENTER, *BIG ] |
| 1226 |
label: center/big |
| 1227 |
|
| 1228 |
longTag: |
| 1229 |
# Explicit merge long tag |
| 1230 |
!<tag:yaml.org,2002:merge> "<<" : [ *CENTER, *BIG ] |
| 1231 |
label: center/big |
| 1232 |
|
| 1233 |
inlineMap: |
| 1234 |
# Inlined map |
| 1235 |
<< : {"x": 1, "y": 2, "r": 10} |
| 1236 |
label: center/big |
| 1237 |
|
| 1238 |
inlineSequenceMap: |
| 1239 |
# Inlined map in sequence |
| 1240 |
<< : [ *CENTER, {"r": 10} ] |
| 1241 |
label: center/big |
| 1242 |
` |
| 1243 |
|
| 1244 |
func TestMerge(t *testing.T) { |
| 1245 |
var want = map[interface{}]interface{}{ |
| 1246 |
"x": 1, |
| 1247 |
"y": 2, |
| 1248 |
"r": 10, |
| 1249 |
"label": "center/big", |
| 1250 |
} |
| 1251 |
|
| 1252 |
var m map[interface{}]interface{} |
| 1253 |
err := yaml.Unmarshal([]byte(mergeTests), &m) |
| 1254 |
if err != nil { |
| 1255 |
t.Fatalf("Unmarshal() returned error: %v", err) |
| 1256 |
} |
| 1257 |
for name, test := range m { |
| 1258 |
if name == "anchors" { |
| 1259 |
continue |
| 1260 |
} |
| 1261 |
if !reflect.DeepEqual(test, want) { |
| 1262 |
t.Errorf("test %q failed: got %#v, want %#v", name, test, want) |
| 1263 |
} |
| 1264 |
} |
| 1265 |
} |
| 1266 |
|
| 1267 |
func TestMergeStruct(t *testing.T) { |
| 1268 |
type Data struct { |
| 1269 |
X, Y, R int |
| 1270 |
Label string |
| 1271 |
} |
| 1272 |
want := Data{1, 2, 10, "center/big"} |
| 1273 |
|
| 1274 |
var m map[string]Data |
| 1275 |
err := yaml.Unmarshal([]byte(mergeTests), &m) |
| 1276 |
if err != nil { |
| 1277 |
t.Fatalf("Unmarshal() returned error: %v", err) |
| 1278 |
} |
| 1279 |
for name, test := range m { |
| 1280 |
if name == "anchors" { |
| 1281 |
continue |
| 1282 |
} |
| 1283 |
if !reflect.DeepEqual(test, want) { |
| 1284 |
t.Errorf("test %q failed: got %#v, want %#v", name, test, want) |
| 1285 |
} |
| 1286 |
} |
| 1287 |
} |
| 1288 |
|
| 1289 |
var unmarshalNullTests = []func() interface{}{ |
| 1290 |
func() interface{} { var v interface{}; v = "v"; return &v }, |
| 1291 |
func() interface{} { var s = "s"; return &s }, |
| 1292 |
func() interface{} { var s = "s"; sptr := &s; return &sptr }, |
| 1293 |
func() interface{} { var i = 1; return &i }, |
| 1294 |
func() interface{} { var i = 1; iptr := &i; return &iptr }, |
| 1295 |
func() interface{} { m := map[string]int{"s": 1}; return &m }, |
| 1296 |
func() interface{} { m := map[string]int{"s": 1}; return m }, |
| 1297 |
} |
| 1298 |
|
| 1299 |
func TestUnmarshalNull(t *testing.T) { |
| 1300 |
for _, test := range unmarshalNullTests { |
| 1301 |
item := test() |
| 1302 |
zero := reflect.Zero(reflect.TypeOf(item).Elem()).Interface() |
| 1303 |
err := yaml.Unmarshal([]byte("null"), item) |
| 1304 |
if err != nil { |
| 1305 |
t.Fatalf("Unmarshal() returned error: %v", err) |
| 1306 |
} |
| 1307 |
if reflect.TypeOf(item).Kind() == reflect.Map { |
| 1308 |
if !reflect.DeepEqual(reflect.ValueOf(item).Interface(), reflect.MakeMap(reflect.TypeOf(item)).Interface()) { |
| 1309 |
t.Fatalf("Unmarshal() returned %v, want empty map", reflect.ValueOf(item).Interface()) |
| 1310 |
} |
| 1311 |
} else { |
| 1312 |
if !reflect.DeepEqual(reflect.ValueOf(item).Elem().Interface(), zero) { |
| 1313 |
t.Fatalf("Unmarshal() returned %v, want %v", reflect.ValueOf(item).Elem().Interface(), zero) |
| 1314 |
} |
| 1315 |
} |
| 1316 |
} |
| 1317 |
} |
| 1318 |
|
| 1319 |
func TestUnmarshalSliceOnPreset(t *testing.T) { |
| 1320 |
// Issue #48. |
| 1321 |
v := struct{ A []int }{[]int{1}} |
| 1322 |
err := yaml.Unmarshal([]byte("a: [2]"), &v) |
| 1323 |
if err != nil { |
| 1324 |
t.Fatalf("Unmarshal() returned error: %v", err) |
| 1325 |
} |
| 1326 |
if !reflect.DeepEqual(v.A, []int{2}) { |
| 1327 |
t.Fatalf("v.A is %v, want %v", v.A, []int{2}) |
| 1328 |
} |
| 1329 |
} |
| 1330 |
|
| 1331 |
var unmarshalStrictTests = []struct { |
| 1332 |
data string |
| 1333 |
value interface{} |
| 1334 |
error string |
| 1335 |
}{{ |
| 1336 |
data: "a: 1\nc: 2\n", |
| 1337 |
value: struct{ A, B int }{A: 1}, |
| 1338 |
error: "yaml: unmarshal errors:\n line 2: field c not found in type struct { A int; B int }", |
| 1339 |
}, { |
| 1340 |
data: "a: 1\nb: 2\na: 3\n", |
| 1341 |
value: struct{ A, B int }{A: 3, B: 2}, |
| 1342 |
error: "yaml: unmarshal errors:\n line 3: field a already set in type struct { A int; B int }", |
| 1343 |
}, { |
| 1344 |
data: "c: 3\na: 1\nb: 2\nc: 4\n", |
| 1345 |
value: struct { |
| 1346 |
A int |
| 1347 |
inlineB `yaml:",inline"` |
| 1348 |
}{ |
| 1349 |
A: 1, |
| 1350 |
inlineB: inlineB{ |
| 1351 |
B: 2, |
| 1352 |
inlineC: inlineC{ |
| 1353 |
C: 4, |
| 1354 |
}, |
| 1355 |
}, |
| 1356 |
}, |
| 1357 |
error: "yaml: unmarshal errors:\n line 4: field c already set in type struct { A int; yaml_test.inlineB \"yaml:\\\",inline\\\"\" }", |
| 1358 |
}, { |
| 1359 |
data: "c: 0\na: 1\nb: 2\nc: 1\n", |
| 1360 |
value: struct { |
| 1361 |
A int |
| 1362 |
inlineB `yaml:",inline"` |
| 1363 |
}{ |
| 1364 |
A: 1, |
| 1365 |
inlineB: inlineB{ |
| 1366 |
B: 2, |
| 1367 |
inlineC: inlineC{ |
| 1368 |
C: 1, |
| 1369 |
}, |
| 1370 |
}, |
| 1371 |
}, |
| 1372 |
error: "yaml: unmarshal errors:\n line 4: field c already set in type struct { A int; yaml_test.inlineB \"yaml:\\\",inline\\\"\" }", |
| 1373 |
}, { |
| 1374 |
data: "c: 1\na: 1\nb: 2\nc: 3\n", |
| 1375 |
value: struct { |
| 1376 |
A int |
| 1377 |
M map[string]interface{} `yaml:",inline"` |
| 1378 |
}{ |
| 1379 |
A: 1, |
| 1380 |
M: map[string]interface{}{ |
| 1381 |
"b": 2, |
| 1382 |
"c": 3, |
| 1383 |
}, |
| 1384 |
}, |
| 1385 |
error: "yaml: unmarshal errors:\n line 4: key \"c\" already set in map", |
| 1386 |
}, { |
| 1387 |
data: "a: 1\n9: 2\nnull: 3\n9: 4", |
| 1388 |
value: map[interface{}]interface{}{ |
| 1389 |
"a": 1, |
| 1390 |
nil: 3, |
| 1391 |
9: 4, |
| 1392 |
}, |
| 1393 |
error: "yaml: unmarshal errors:\n line 4: key 9 already set in map", |
| 1394 |
}} |
| 1395 |
|
| 1396 |
func TestUnmarshalStrict(t *testing.T) { |
| 1397 |
for i, item := range unmarshalStrictTests { |
| 1398 |
t.Logf("test %d: %q", i, item.data) |
| 1399 |
// First test that normal Unmarshal unmarshals to the expected value. |
| 1400 |
typ := reflect.ValueOf(item.value).Type() |
| 1401 |
value := reflect.New(typ) |
| 1402 |
err := yaml.Unmarshal([]byte(item.data), value.Interface()) |
| 1403 |
if err != nil { |
| 1404 |
t.Fatalf("Unmarshal() returned error: %v", err) |
| 1405 |
} |
| 1406 |
if !reflect.DeepEqual(value.Elem().Interface(), item.value) { |
| 1407 |
t.Fatalf("Unmarshal() returned %v, want %v", value.Elem().Interface(), item.value) |
| 1408 |
} |
| 1409 |
|
| 1410 |
// Then test that UnmarshalStrict fails on the same thing. |
| 1411 |
typ = reflect.ValueOf(item.value).Type() |
| 1412 |
value = reflect.New(typ) |
| 1413 |
err = yaml.UnmarshalStrict([]byte(item.data), value.Interface()) |
| 1414 |
if err == nil { |
| 1415 |
t.Fatalf("UnmarshalStrict() returned no error, want %q", item.error) |
| 1416 |
} |
| 1417 |
if err.Error() != item.error { |
| 1418 |
t.Fatalf("UnmarshalStrict() returned error %q, want %q", err, item.error) |
| 1419 |
} |
| 1420 |
} |
| 1421 |
} |
| 1422 |
|
| 1423 |
type textUnmarshaler struct { |
| 1424 |
S string |
| 1425 |
} |
| 1426 |
|
| 1427 |
func (t *textUnmarshaler) UnmarshalText(s []byte) error { |
| 1428 |
t.S = string(s) |
| 1429 |
return nil |
| 1430 |
} |
| 1431 |
|
| 1432 |
func TestFuzzCrashers(t *testing.T) { |
| 1433 |
cases := []string{ |
| 1434 |
// runtime error: index out of range |
| 1435 |
"\"\\0\\\r\n", |
| 1436 |
|
| 1437 |
// should not happen |
| 1438 |
" 0: [\n] 0", |
| 1439 |
"? ? \"\n\" 0", |
| 1440 |
" - {\n000}0", |
| 1441 |
"0:\n 0: [0\n] 0", |
| 1442 |
" - \"\n000\"0", |
| 1443 |
" - \"\n000\"\"", |
| 1444 |
"0:\n - {\n000}0", |
| 1445 |
"0:\n - \"\n000\"0", |
| 1446 |
"0:\n - \"\n000\"\"", |
| 1447 |
|
| 1448 |
// runtime error: index out of range |
| 1449 |
" \ufeff\n", |
| 1450 |
"? \ufeff\n", |
| 1451 |
"? \ufeff:\n", |
| 1452 |
"0: \ufeff\n", |
| 1453 |
"? \ufeff: \ufeff\n", |
| 1454 |
} |
| 1455 |
for _, data := range cases { |
| 1456 |
var v interface{} |
| 1457 |
_ = yaml.Unmarshal([]byte(data), &v) |
| 1458 |
} |
| 1459 |
} |
| 1460 |
|
| 1461 |
//var data []byte |
| 1462 |
//func init() { |
| 1463 |
// var err error |
| 1464 |
// data, err = ioutil.ReadFile("/tmp/file.yaml") |
| 1465 |
// if err != nil { |
| 1466 |
// panic(err) |
| 1467 |
// } |
| 1468 |
//} |
| 1469 |
// |
| 1470 |
//func (s *S) BenchmarkUnmarshal(c *C) { |
| 1471 |
// var err error |
| 1472 |
// for i := 0; i < c.N; i++ { |
| 1473 |
// var v map[string]interface{} |
| 1474 |
// err = yaml.Unmarshal(data, &v) |
| 1475 |
// } |
| 1476 |
// if err != nil { |
| 1477 |
// panic(err) |
| 1478 |
// } |
| 1479 |
//} |
| 1480 |
// |
| 1481 |
//func (s *S) BenchmarkMarshal(c *C) { |
| 1482 |
// var v map[string]interface{} |
| 1483 |
// yaml.Unmarshal(data, &v) |
| 1484 |
// c.ResetTimer() |
| 1485 |
// for i := 0; i < c.N; i++ { |
| 1486 |
// yaml.Marshal(&v) |
| 1487 |
// } |
| 1488 |
//} |