|
6 | 6 | "testing"
|
7 | 7 |
|
8 | 8 | "github.com/zclconf/go-cty/cty"
|
| 9 | + "github.com/zclconf/go-cty/cty/convert" |
9 | 10 | )
|
10 | 11 |
|
11 | 12 | func TestRoundTrip(t *testing.T) {
|
@@ -96,6 +97,10 @@ func TestRoundTrip(t *testing.T) {
|
96 | 97 | cty.MustParseNumberVal("9223372036854775809"),
|
97 | 98 | cty.Number,
|
98 | 99 | },
|
| 100 | + { |
| 101 | + cty.MustParseNumberVal("18446744073709551616"), |
| 102 | + cty.Number, |
| 103 | + }, |
99 | 104 | {
|
100 | 105 | awkwardFractionVal,
|
101 | 106 | cty.Number,
|
@@ -363,6 +368,119 @@ func TestRoundTrip(t *testing.T) {
|
363 | 368 | }
|
364 | 369 | }
|
365 | 370 |
|
| 371 | +func TestRoundTrip_fromString(t *testing.T) { |
| 372 | + tests := []struct { |
| 373 | + Value string |
| 374 | + Type cty.Type |
| 375 | + }{ |
| 376 | + { |
| 377 | + "0", |
| 378 | + cty.Number, |
| 379 | + }, |
| 380 | + { |
| 381 | + "1", |
| 382 | + cty.Number, |
| 383 | + }, |
| 384 | + { |
| 385 | + "-1", |
| 386 | + cty.Number, |
| 387 | + }, |
| 388 | + { |
| 389 | + "9223372036854775807", |
| 390 | + cty.Number, |
| 391 | + }, |
| 392 | + { |
| 393 | + "9223372036854775808", |
| 394 | + cty.Number, |
| 395 | + }, |
| 396 | + { |
| 397 | + "9223372036854775809", |
| 398 | + cty.Number, |
| 399 | + }, |
| 400 | + { |
| 401 | + "18446744073709551616", |
| 402 | + cty.Number, |
| 403 | + }, |
| 404 | + { |
| 405 | + "-9223372036854775807", |
| 406 | + cty.Number, |
| 407 | + }, |
| 408 | + { |
| 409 | + "-9223372036854775808", |
| 410 | + cty.Number, |
| 411 | + }, |
| 412 | + { |
| 413 | + "-9223372036854775809", |
| 414 | + cty.Number, |
| 415 | + }, |
| 416 | + { |
| 417 | + "-18446744073709551616", |
| 418 | + cty.Number, |
| 419 | + }, |
| 420 | + { |
| 421 | + "true", |
| 422 | + cty.Bool, |
| 423 | + }, |
| 424 | + { |
| 425 | + "false", |
| 426 | + cty.Bool, |
| 427 | + }, |
| 428 | + } |
| 429 | + for _, test := range tests { |
| 430 | + t.Run(fmt.Sprintf("%#v as %#v", test.Value, test.Type), func(t *testing.T) { |
| 431 | + stringVal := cty.StringVal(test.Value) |
| 432 | + |
| 433 | + original, err := convert.Convert(stringVal, test.Type) |
| 434 | + if err != nil { |
| 435 | + t.Fatalf("input type must be convertible from string: %s", err) |
| 436 | + } |
| 437 | + |
| 438 | + { |
| 439 | + // We'll first make sure that the conversion works even without |
| 440 | + // MessagePack involved, since otherwise we might falsely blame |
| 441 | + // the MessagePack encoding for bugs in package convert. |
| 442 | + stringGot, err := convert.Convert(original, cty.String) |
| 443 | + if err != nil { |
| 444 | + t.Fatalf("result must be convertible to string: %s", err) |
| 445 | + } |
| 446 | + |
| 447 | + if !stringGot.RawEquals(stringVal) { |
| 448 | + t.Fatalf("value did not round-trip to string even without msgpack\ninput: %#v\nresult: %#v", test.Value, stringGot) |
| 449 | + } |
| 450 | + } |
| 451 | + |
| 452 | + b, err := Marshal(original, test.Type) |
| 453 | + if err != nil { |
| 454 | + t.Fatal(err) |
| 455 | + } |
| 456 | + |
| 457 | + t.Logf("encoded as %x", b) |
| 458 | + |
| 459 | + got, err := Unmarshal(b, test.Type) |
| 460 | + if err != nil { |
| 461 | + t.Fatal(err) |
| 462 | + } |
| 463 | + |
| 464 | + if !got.RawEquals(original) { |
| 465 | + t.Errorf( |
| 466 | + "value did not round-trip\ninput: %#v\nresult: %#v", |
| 467 | + test.Value, got, |
| 468 | + ) |
| 469 | + } |
| 470 | + |
| 471 | + stringGot, err := convert.Convert(got, cty.String) |
| 472 | + if err != nil { |
| 473 | + t.Fatalf("result must be convertible to string: %s", err) |
| 474 | + } |
| 475 | + |
| 476 | + if !stringGot.RawEquals(stringVal) { |
| 477 | + t.Errorf("value did not round-trip to string\ninput: %#v\nresult: %#v", test.Value, stringGot) |
| 478 | + } |
| 479 | + |
| 480 | + }) |
| 481 | + } |
| 482 | +} |
| 483 | + |
366 | 484 | // Unknown values with very long string prefix refinements do not round-trip
|
367 | 485 | // losslessly. If the prefix is longer than 256 bytes it will be truncated to
|
368 | 486 | // a maximum of 256 bytes.
|
|
0 commit comments