| 1 | package yaml_test |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "fmt" |
| 6 | "math" |
| 7 | "net" |
| 8 | "os" |
| 9 | "strconv" |
| 10 | "strings" |
| 11 | "testing" |
| 12 | "time" |
| 13 | |
| 14 | "go.yaml.in/yaml/v2" |
| 15 | ) |
| 16 | |
| 17 | type jsonNumberT string |
| 18 | |
| 19 | func (j jsonNumberT) Int64() (int64, error) { |
| 20 | val, err := strconv.Atoi(string(j)) |
| 21 | if err != nil { |
| 22 | return 0, err |
| 23 | } |
| 24 | return int64(val), nil |
| 25 | } |
| 26 | |
| 27 | func (j jsonNumberT) Float64() (float64, error) { |
| 28 | return strconv.ParseFloat(string(j), 64) |
| 29 | } |
| 30 | |
| 31 | func (j jsonNumberT) String() string { |
| 32 | return string(j) |
| 33 | } |
| 34 | |
| 35 | var marshalIntTest = 123 |
| 36 | |
| 37 | var marshalTests = []struct { |
| 38 | value interface{} |
| 39 | data string |
| 40 | }{ |
| 41 | { |
| 42 | nil, |
| 43 | "null\n", |
| 44 | }, { |
| 45 | (*marshalerType)(nil), |
| 46 | "null\n", |
| 47 | }, { |
| 48 | &struct{}{}, |
| 49 | "{}\n", |
| 50 | }, { |
| 51 | map[string]string{"v": "hi"}, |
| 52 | "v: hi\n", |
| 53 | }, { |
| 54 | map[string]interface{}{"v": "hi"}, |
| 55 | "v: hi\n", |
| 56 | }, { |
| 57 | map[string]string{"v": "true"}, |
| 58 | "v: \"true\"\n", |
| 59 | }, { |
| 60 | map[string]string{"v": "false"}, |
| 61 | "v: \"false\"\n", |
| 62 | }, { |
| 63 | map[string]interface{}{"v": true}, |
| 64 | "v: true\n", |
| 65 | }, { |
| 66 | map[string]interface{}{"v": false}, |
| 67 | "v: false\n", |
| 68 | }, { |
| 69 | map[string]interface{}{"v": 10}, |
| 70 | "v: 10\n", |
| 71 | }, { |
| 72 | map[string]interface{}{"v": -10}, |
| 73 | "v: -10\n", |
| 74 | }, { |
| 75 | map[string]uint{"v": 42}, |
| 76 | "v: 42\n", |
| 77 | }, { |
| 78 | map[string]interface{}{"v": int64(4294967296)}, |
| 79 | "v: 4294967296\n", |
| 80 | }, { |
| 81 | map[string]int64{"v": int64(4294967296)}, |
| 82 | "v: 4294967296\n", |
| 83 | }, { |
| 84 | map[string]uint64{"v": 4294967296}, |
| 85 | "v: 4294967296\n", |
| 86 | }, { |
| 87 | map[string]interface{}{"v": "10"}, |
| 88 | "v: \"10\"\n", |
| 89 | }, { |
| 90 | map[string]interface{}{"v": 0.1}, |
| 91 | "v: 0.1\n", |
| 92 | }, { |
| 93 | map[string]interface{}{"v": float64(0.1)}, |
| 94 | "v: 0.1\n", |
| 95 | }, { |
| 96 | map[string]interface{}{"v": float32(0.99)}, |
| 97 | "v: 0.99\n", |
| 98 | }, { |
| 99 | map[string]interface{}{"v": -0.1}, |
| 100 | "v: -0.1\n", |
| 101 | }, { |
| 102 | map[string]interface{}{"v": math.Inf(+1)}, |
| 103 | "v: .inf\n", |
| 104 | }, { |
| 105 | map[string]interface{}{"v": math.Inf(-1)}, |
| 106 | "v: -.inf\n", |
| 107 | }, { |
| 108 | map[string]interface{}{"v": math.NaN()}, |
| 109 | "v: .nan\n", |
| 110 | }, { |
| 111 | map[string]interface{}{"v": nil}, |
| 112 | "v: null\n", |
| 113 | }, { |
| 114 | map[string]interface{}{"v": ""}, |
| 115 | "v: \"\"\n", |
| 116 | }, { |
| 117 | map[string][]string{"v": []string{"A", "B"}}, |
| 118 | "v:\n- A\n- B\n", |
| 119 | }, { |
| 120 | map[string][]string{"v": []string{"A", "B\nC"}}, |
| 121 | "v:\n- A\n- |-\n B\n C\n", |
| 122 | }, { |
| 123 | map[string][]interface{}{"v": []interface{}{"A", 1, map[string][]int{"B": []int{2, 3}}}}, |
| 124 | "v:\n- A\n- 1\n- B:\n - 2\n - 3\n", |
| 125 | }, { |
| 126 | map[string]interface{}{"a": map[interface{}]interface{}{"b": "c"}}, |
| 127 | "a:\n b: c\n", |
| 128 | }, { |
| 129 | map[string]interface{}{"a": "-"}, |
| 130 | "a: '-'\n", |
| 131 | }, |
| 132 | |
| 133 | // Simple values. |
| 134 | { |
| 135 | &marshalIntTest, |
| 136 | "123\n", |
| 137 | }, |
| 138 | |
| 139 | // Structures |
| 140 | { |
| 141 | &struct{ Hello string }{"world"}, |
| 142 | "hello: world\n", |
| 143 | }, { |
| 144 | &struct { |
| 145 | A struct { |
| 146 | B string |
| 147 | } |
| 148 | }{struct{ B string }{"c"}}, |
| 149 | "a:\n b: c\n", |
| 150 | }, { |
| 151 | &struct { |
| 152 | A *struct { |
| 153 | B string |
| 154 | } |
| 155 | }{&struct{ B string }{"c"}}, |
| 156 | "a:\n b: c\n", |
| 157 | }, { |
| 158 | &struct { |
| 159 | A *struct { |
| 160 | B string |
| 161 | } |
| 162 | }{}, |
| 163 | "a: null\n", |
| 164 | }, { |
| 165 | &struct{ A int }{1}, |
| 166 | "a: 1\n", |
| 167 | }, { |
| 168 | &struct{ A []int }{[]int{1, 2}}, |
| 169 | "a:\n- 1\n- 2\n", |
| 170 | }, { |
| 171 | &struct{ A [2]int }{[2]int{1, 2}}, |
| 172 | "a:\n- 1\n- 2\n", |
| 173 | }, { |
| 174 | &struct { |
| 175 | B int "a" |
| 176 | }{1}, |
| 177 | "a: 1\n", |
| 178 | }, { |
| 179 | &struct{ A bool }{true}, |
| 180 | "a: true\n", |
| 181 | }, |
| 182 | |
| 183 | // Conditional flag |
| 184 | { |
| 185 | &struct { |
| 186 | A int "a,omitempty" |
| 187 | B int "b,omitempty" |
| 188 | }{1, 0}, |
| 189 | "a: 1\n", |
| 190 | }, { |
| 191 | &struct { |
| 192 | A int "a,omitempty" |
| 193 | B int "b,omitempty" |
| 194 | }{0, 0}, |
| 195 | "{}\n", |
| 196 | }, { |
| 197 | &struct { |
| 198 | A *struct{ X, y int } "a,omitempty,flow" |
| 199 | }{&struct{ X, y int }{1, 2}}, |
| 200 | "a: {x: 1}\n", |
| 201 | }, { |
| 202 | &struct { |
| 203 | A *struct{ X, y int } "a,omitempty,flow" |
| 204 | }{nil}, |
| 205 | "{}\n", |
| 206 | }, { |
| 207 | &struct { |
| 208 | A *struct{ X, y int } "a,omitempty,flow" |
| 209 | }{&struct{ X, y int }{}}, |
| 210 | "a: {x: 0}\n", |
| 211 | }, { |
| 212 | &struct { |
| 213 | A struct{ X, y int } "a,omitempty,flow" |
| 214 | }{struct{ X, y int }{1, 2}}, |
| 215 | "a: {x: 1}\n", |
| 216 | }, { |
| 217 | &struct { |
| 218 | A struct{ X, y int } "a,omitempty,flow" |
| 219 | }{struct{ X, y int }{0, 1}}, |
| 220 | "{}\n", |
| 221 | }, { |
| 222 | &struct { |
| 223 | A float64 "a,omitempty" |
| 224 | B float64 "b,omitempty" |
| 225 | }{1, 0}, |
| 226 | "a: 1\n", |
| 227 | }, |
| 228 | { |
| 229 | &struct { |
| 230 | T1 time.Time "t1,omitempty" |
| 231 | T2 time.Time "t2,omitempty" |
| 232 | T3 *time.Time "t3,omitempty" |
| 233 | T4 *time.Time "t4,omitempty" |
| 234 | }{ |
| 235 | T2: time.Date(2018, 1, 9, 10, 40, 47, 0, time.UTC), |
| 236 | T4: newTime(time.Date(2098, 1, 9, 10, 40, 47, 0, time.UTC)), |
| 237 | }, |
| 238 | "t2: 2018-01-09T10:40:47Z\nt4: 2098-01-09T10:40:47Z\n", |
| 239 | }, |
| 240 | // Nil interface that implements Marshaler. |
| 241 | { |
| 242 | map[string]yaml.Marshaler{ |
| 243 | "a": nil, |
| 244 | }, |
| 245 | "a: null\n", |
| 246 | }, |
| 247 | |
| 248 | // Flow flag |
| 249 | { |
| 250 | &struct { |
| 251 | A []int "a,flow" |
| 252 | }{[]int{1, 2}}, |
| 253 | "a: [1, 2]\n", |
| 254 | }, { |
| 255 | &struct { |
| 256 | A map[string]string "a,flow" |
| 257 | }{map[string]string{"b": "c", "d": "e"}}, |
| 258 | "a: {b: c, d: e}\n", |
| 259 | }, { |
| 260 | &struct { |
| 261 | A struct { |
| 262 | B, D string |
| 263 | } "a,flow" |
| 264 | }{struct{ B, D string }{"c", "e"}}, |
| 265 | "a: {b: c, d: e}\n", |
| 266 | }, |
| 267 | |
| 268 | // Unexported field |
| 269 | { |
| 270 | &struct { |
| 271 | u int |
| 272 | A int |
| 273 | }{0, 1}, |
| 274 | "a: 1\n", |
| 275 | }, |
| 276 | |
| 277 | // Ignored field |
| 278 | { |
| 279 | &struct { |
| 280 | A int |
| 281 | B int "-" |
| 282 | }{1, 2}, |
| 283 | "a: 1\n", |
| 284 | }, |
| 285 | |
| 286 | // Struct inlining |
| 287 | { |
| 288 | &struct { |
| 289 | A int |
| 290 | C inlineB `yaml:",inline"` |
| 291 | }{1, inlineB{2, inlineC{3}}}, |
| 292 | "a: 1\nb: 2\nc: 3\n", |
| 293 | }, |
| 294 | |
| 295 | // Map inlining |
| 296 | { |
| 297 | &struct { |
| 298 | A int |
| 299 | C map[string]int `yaml:",inline"` |
| 300 | }{1, map[string]int{"b": 2, "c": 3}}, |
| 301 | "a: 1\nb: 2\nc: 3\n", |
| 302 | }, |
| 303 | |
| 304 | // Duration |
| 305 | { |
| 306 | map[string]time.Duration{"a": 3 * time.Second}, |
| 307 | "a: 3s\n", |
| 308 | }, |
| 309 | |
| 310 | // Issue #24: bug in map merging logic. |
| 311 | { |
| 312 | map[string]string{"a": "<foo>"}, |
| 313 | "a: <foo>\n", |
| 314 | }, |
| 315 | |
| 316 | // Issue #34: marshal unsupported base 60 floats quoted for compatibility |
| 317 | // with old YAML 1.1 parsers. |
| 318 | { |
| 319 | map[string]string{"a": "1:1"}, |
| 320 | "a: \"1:1\"\n", |
| 321 | }, |
| 322 | |
| 323 | // Binary data. |
| 324 | { |
| 325 | map[string]string{"a": "\x00"}, |
| 326 | "a: \"\\0\"\n", |
| 327 | }, { |
| 328 | map[string]string{"a": "\x80\x81\x82"}, |
| 329 | "a: !!binary gIGC\n", |
| 330 | }, { |
| 331 | map[string]string{"a": strings.Repeat("\x90", 54)}, |
| 332 | "a: !!binary |\n " + strings.Repeat("kJCQ", 17) + "kJ\n CQ\n", |
| 333 | }, |
| 334 | |
| 335 | // Ordered maps. |
| 336 | { |
| 337 | &yaml.MapSlice{{"b", 2}, {"a", 1}, {"d", 4}, {"c", 3}, {"sub", yaml.MapSlice{{"e", 5}}}}, |
| 338 | "b: 2\na: 1\nd: 4\nc: 3\nsub:\n e: 5\n", |
| 339 | }, |
| 340 | |
| 341 | // Encode unicode as utf-8 rather than in escaped form. |
| 342 | { |
| 343 | map[string]string{"a": "ä½ å¥½"}, |
| 344 | "a: ä½ å¥½\n", |
| 345 | }, |
| 346 | |
| 347 | // Support encoding.TextMarshaler. |
| 348 | { |
| 349 | map[string]net.IP{"a": net.IPv4(1, 2, 3, 4)}, |
| 350 | "a: 1.2.3.4\n", |
| 351 | }, |
| 352 | // time.Time gets a timestamp tag. |
| 353 | { |
| 354 | map[string]time.Time{"a": time.Date(2015, 2, 24, 18, 19, 39, 0, time.UTC)}, |
| 355 | "a: 2015-02-24T18:19:39Z\n", |
| 356 | }, |
| 357 | { |
| 358 | map[string]*time.Time{"a": newTime(time.Date(2015, 2, 24, 18, 19, 39, 0, time.UTC))}, |
| 359 | "a: 2015-02-24T18:19:39Z\n", |
| 360 | }, |
| 361 | { |
| 362 | // This is confirmed to be properly decoded in Python (libyaml) without a timestamp tag. |
| 363 | map[string]time.Time{"a": time.Date(2015, 2, 24, 18, 19, 39, 123456789, time.FixedZone("FOO", -3*60*60))}, |
| 364 | "a: 2015-02-24T18:19:39.123456789-03:00\n", |
| 365 | }, |
| 366 | // Ensure timestamp-like strings are quoted. |
| 367 | { |
| 368 | map[string]string{"a": "2015-02-24T18:19:39Z"}, |
| 369 | "a: \"2015-02-24T18:19:39Z\"\n", |
| 370 | }, |
| 371 | |
| 372 | // Ensure strings containing ": " are quoted (reported as PR #43, but not reproducible). |
| 373 | { |
| 374 | map[string]string{"a": "b: c"}, |
| 375 | "a: 'b: c'\n", |
| 376 | }, |
| 377 | |
| 378 | // Containing hash mark ('#') in string should be quoted |
| 379 | { |
| 380 | map[string]string{"a": "Hello #comment"}, |
| 381 | "a: 'Hello #comment'\n", |
| 382 | }, |
| 383 | { |
| 384 | map[string]string{"a": "ä½ å¥½ #comment"}, |
| 385 | "a: 'ä½ å¥½ #comment'\n", |
| 386 | }, |
| 387 | { |
| 388 | map[string]interface{}{"a": jsonNumberT("5")}, |
| 389 | "a: 5\n", |
| 390 | }, |
| 391 | { |
| 392 | map[string]interface{}{"a": jsonNumberT("100.5")}, |
| 393 | "a: 100.5\n", |
| 394 | }, |
| 395 | { |
| 396 | map[string]interface{}{"a": jsonNumberT("bogus")}, |
| 397 | "a: bogus\n", |
| 398 | }, |
| 399 | } |
| 400 | |
| 401 | func TestLineWrapping(t *testing.T) { |
| 402 | var v = map[string]string{ |
| 403 | "a": "abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ 1234567890 abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ 1234567890 ", |
| 404 | } |
| 405 | data, err := yaml.Marshal(v) |
| 406 | if err != nil { |
| 407 | t.Fatalf("Marshal() returned error: %v", err) |
| 408 | } |
| 409 | expected := "a: 'abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ 1234567890 abcdefghijklmnopqrstuvwxyz\n" + |
| 410 | " ABCDEFGHIJKLMNOPQRSTUVWXYZ 1234567890 '\n" |
| 411 | if string(data) != expected { |
| 412 | t.Fatalf("Marshal() returned %q, want %q", string(data), expected) |
| 413 | } |
| 414 | |
| 415 | // The API does not allow this process to be reversed as it's intended |
| 416 | // for migration only. v3 drops this method and instead offers more |
| 417 | // control on a per encoding basis. |
| 418 | yaml.FutureLineWrap() |
| 419 | |
| 420 | data, err = yaml.Marshal(v) |
| 421 | if err != nil { |
| 422 | t.Fatalf("Marshal() returned error: %v", err) |
| 423 | } |
| 424 | expected = "a: 'abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ 1234567890 abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ 1234567890 '\n" |
| 425 | if string(data) != expected { |
| 426 | t.Fatalf("Marshal() returned %q, want %q", string(data), expected) |
| 427 | } |
| 428 | } |
| 429 | |
| 430 | func TestMarshal(t *testing.T) { |
| 431 | defer os.Setenv("TZ", os.Getenv("TZ")) |
| 432 | os.Setenv("TZ", "UTC") |
| 433 | for i, item := range marshalTests { |
| 434 | t.Logf("test %d: %q", i, item.data) |
| 435 | data, err := yaml.Marshal(item.value) |
| 436 | if err != nil { |
| 437 | t.Fatalf("Marshal() returned error: %v", err) |
| 438 | } |
| 439 | if string(data) != item.data { |
| 440 | t.Fatalf("Marshal() returned %q, want %q", string(data), item.data) |
| 441 | } |
| 442 | } |
| 443 | } |
| 444 | |
| 445 | func TestEncoderSingleDocument(t *testing.T) { |
| 446 | for i, item := range marshalTests { |
| 447 | t.Logf("test %d. %q", i, item.data) |
| 448 | var buf bytes.Buffer |
| 449 | enc := yaml.NewEncoder(&buf) |
| 450 | err := enc.Encode(item.value) |
| 451 | if err != nil { |
| 452 | t.Fatalf("Encode() returned error: %v", err) |
| 453 | } |
| 454 | err = enc.Close() |
| 455 | if err != nil { |
| 456 | t.Fatalf("Close() returned error: %v", err) |
| 457 | } |
| 458 | if buf.String() != item.data { |
| 459 | t.Fatalf("Encode() returned %q, want %q", buf.String(), item.data) |
| 460 | } |
| 461 | } |
| 462 | } |
| 463 | |
| 464 | func TestEncoderMultipleDocuments(t *testing.T) { |
| 465 | var buf bytes.Buffer |
| 466 | enc := yaml.NewEncoder(&buf) |
| 467 | err := enc.Encode(map[string]string{"a": "b"}) |
| 468 | if err != nil { |
| 469 | t.Fatalf("Encode() returned error: %v", err) |
| 470 | } |
| 471 | err = enc.Encode(map[string]string{"c": "d"}) |
| 472 | if err != nil { |
| 473 | t.Fatalf("Encode() returned error: %v", err) |
| 474 | } |
| 475 | err = enc.Close() |
| 476 | if err != nil { |
| 477 | t.Fatalf("Close() returned error: %v", err) |
| 478 | } |
| 479 | if buf.String() != "a: b\n---\nc: d\n" { |
| 480 | t.Fatalf("Encode() returned %q, want %q", buf.String(), "a: b\n---\nc: d\n") |
| 481 | } |
| 482 | } |
| 483 | |
| 484 | func TestEncoderWriteError(t *testing.T) { |
| 485 | enc := yaml.NewEncoder(errorWriter{}) |
| 486 | err := enc.Encode(map[string]string{"a": "b"}) |
| 487 | if err == nil || !strings.Contains(err.Error(), `yaml: write error: some write error`) { |
| 488 | t.Fatalf("Encode() returned %v, want error containing %q", err, `yaml: write error: some write error`) |
| 489 | } |
| 490 | } |
| 491 | |
| 492 | type errorWriter struct{} |
| 493 | |
| 494 | func (errorWriter) Write([]byte) (int, error) { |
| 495 | return 0, fmt.Errorf("some write error") |
| 496 | } |
| 497 | |
| 498 | var marshalErrorTests = []struct { |
| 499 | value interface{} |
| 500 | error string |
| 501 | panic string |
| 502 | }{{ |
| 503 | value: &struct { |
| 504 | B int |
| 505 | inlineB ",inline" |
| 506 | }{1, inlineB{2, inlineC{3}}}, |
| 507 | panic: `Duplicated key 'b' in struct struct { B int; yaml_test.inlineB ",inline" }`, |
| 508 | }, { |
| 509 | value: &struct { |
| 510 | A int |
| 511 | B map[string]int ",inline" |
| 512 | }{1, map[string]int{"a": 2}}, |
| 513 | panic: `Can't have key "a" in inlined map; conflicts with struct field`, |
| 514 | }} |
| 515 | |
| 516 | func TestMarshalErrors(t *testing.T) { |
| 517 | for _, item := range marshalErrorTests { |
| 518 | if item.panic != "" { |
| 519 | func() { |
| 520 | defer func() { |
| 521 | r := recover() |
| 522 | if r == nil { |
| 523 | t.Fatalf("expected panic %q", item.panic) |
| 524 | } |
| 525 | panicText := fmt.Sprintf("%v", r) |
| 526 | if panicText != item.panic { |
| 527 | t.Fatalf("panic value %q does not match %q", panicText, item.panic) |
| 528 | } |
| 529 | }() |
| 530 | yaml.Marshal(item.value) |
| 531 | }() |
| 532 | } else { |
| 533 | _, err := yaml.Marshal(item.value) |
| 534 | if err == nil || err.Error() != item.error { |
| 535 | t.Fatalf("Marshal() returned %v, want error %q", err, item.error) |
| 536 | } |
| 537 | } |
| 538 | } |
| 539 | } |
| 540 | |
| 541 | func TestMarshalTypeCache(t *testing.T) { |
| 542 | var data []byte |
| 543 | var err error |
| 544 | func() { |
| 545 | type T struct{ A int } |
| 546 | data, err = yaml.Marshal(&T{}) |
| 547 | if err != nil { |
| 548 | t.Fatalf("Marshal() returned error: %v", err) |
| 549 | } |
| 550 | }() |
| 551 | func() { |
| 552 | type T struct{ B int } |
| 553 | data, err = yaml.Marshal(&T{}) |
| 554 | if err != nil { |
| 555 | t.Fatalf("Marshal() returned error: %v", err) |
| 556 | } |
| 557 | }() |
| 558 | if string(data) != "b: 0\n" { |
| 559 | t.Fatalf("Marshal() returned %q, want %q", string(data), "b: 0\n") |
| 560 | } |
| 561 | } |
| 562 | |
| 563 | var marshalerTests = []struct { |
| 564 | data string |
| 565 | value interface{} |
| 566 | }{ |
| 567 | {"_:\n hi: there\n", map[interface{}]interface{}{"hi": "there"}}, |
| 568 | {"_:\n- 1\n- A\n", []interface{}{1, "A"}}, |
| 569 | {"_: 10\n", 10}, |
| 570 | {"_: null\n", nil}, |
| 571 | {"_: BAR!\n", "BAR!"}, |
| 572 | } |
| 573 | |
| 574 | type marshalerType struct { |
| 575 | value interface{} |
| 576 | } |
| 577 | |
| 578 | func (o marshalerType) MarshalText() ([]byte, error) { |
| 579 | panic("MarshalText called on type with MarshalYAML") |
| 580 | } |
| 581 | |
| 582 | func (o marshalerType) MarshalYAML() (interface{}, error) { |
| 583 | return o.value, nil |
| 584 | } |
| 585 | |
| 586 | type marshalerValue struct { |
| 587 | Field marshalerType "_" |
| 588 | } |
| 589 | |
| 590 | func TestMarshaler(t *testing.T) { |
| 591 | for _, item := range marshalerTests { |
| 592 | obj := &marshalerValue{} |
| 593 | obj.Field.value = item.value |
| 594 | data, err := yaml.Marshal(obj) |
| 595 | if err != nil { |
| 596 | t.Fatalf("Marshal() returned error: %v", err) |
| 597 | } |
| 598 | if string(data) != string(item.data) { |
| 599 | t.Fatalf("Marshal() returned %q, want %q", string(data), string(item.data)) |
| 600 | } |
| 601 | } |
| 602 | } |
| 603 | |
| 604 | func TestMarshalerWholeDocument(t *testing.T) { |
| 605 | obj := &marshalerType{} |
| 606 | obj.value = map[string]string{"hello": "world!"} |
| 607 | data, err := yaml.Marshal(obj) |
| 608 | if err != nil { |
| 609 | t.Fatalf("Marshal() returned error: %v", err) |
| 610 | } |
| 611 | if string(data) != "hello: world!\n" { |
| 612 | t.Fatalf("Marshal() returned %q, want %q", string(data), "hello: world!\n") |
| 613 | } |
| 614 | } |
| 615 | |
| 616 | type failingMarshaler struct{} |
| 617 | |
| 618 | func (ft *failingMarshaler) MarshalYAML() (interface{}, error) { |
| 619 | return nil, failingErr |
| 620 | } |
| 621 | |
| 622 | func TestMarshalerError(t *testing.T) { |
| 623 | _, err := yaml.Marshal(&failingMarshaler{}) |
| 624 | if err != failingErr { |
| 625 | t.Fatalf("Marshal() returned %v, want %v", err, failingErr) |
| 626 | } |
| 627 | } |
| 628 | |
| 629 | func TestSortedOutput(t *testing.T) { |
| 630 | order := []interface{}{ |
| 631 | false, |
| 632 | true, |
| 633 | 1, |
| 634 | uint(1), |
| 635 | 1.0, |
| 636 | 1.1, |
| 637 | 1.2, |
| 638 | 2, |
| 639 | uint(2), |
| 640 | 2.0, |
| 641 | 2.1, |
| 642 | "", |
| 643 | ".1", |
| 644 | ".2", |
| 645 | ".a", |
| 646 | "1", |
| 647 | "2", |
| 648 | "a!10", |
| 649 | "a/0001", |
| 650 | "a/002", |
| 651 | "a/3", |
| 652 | "a/10", |
| 653 | "a/11", |
| 654 | "a/0012", |
| 655 | "a/100", |
| 656 | "a~10", |
| 657 | "ab/1", |
| 658 | "b/1", |
| 659 | "b/01", |
| 660 | "b/2", |
| 661 | "b/02", |
| 662 | "b/3", |
| 663 | "b/03", |
| 664 | "b1", |
| 665 | "b01", |
| 666 | "b3", |
| 667 | "c2.10", |
| 668 | "c10.2", |
| 669 | "d1", |
| 670 | "d7", |
| 671 | "d7abc", |
| 672 | "d12", |
| 673 | "d12a", |
| 674 | } |
| 675 | m := make(map[interface{}]int) |
| 676 | for _, k := range order { |
| 677 | m[k] = 1 |
| 678 | } |
| 679 | data, err := yaml.Marshal(m) |
| 680 | if err != nil { |
| 681 | t.Fatalf("Marshal() returned error: %v", err) |
| 682 | } |
| 683 | out := "\n" + string(data) |
| 684 | last := 0 |
| 685 | for i, k := range order { |
| 686 | repr := fmt.Sprint(k) |
| 687 | if s, ok := k.(string); ok { |
| 688 | if _, err = strconv.ParseFloat(repr, 32); s == "" || err == nil { |
| 689 | repr = `"` + repr + `"` |
| 690 | } |
| 691 | } |
| 692 | index := strings.Index(out, "\n"+repr+":") |
| 693 | if index == -1 { |
| 694 | t.Fatalf("%#v is not in the output: %#v", k, out) |
| 695 | } |
| 696 | if index < last { |
| 697 | t.Fatalf("%#v was generated before %#v: %q", k, order[i-1], out) |
| 698 | } |
| 699 | last = index |
| 700 | } |
| 701 | } |
| 702 | |
| 703 | func newTime(t time.Time) *time.Time { |
| 704 | return &t |
| 705 | } |