(t *testing.T)
| 2489 | } |
| 2490 | |
| 2491 | func TestExpressionErrorMessages(t *testing.T) { |
| 2492 | tests := []struct { |
| 2493 | input string |
| 2494 | ctx *hcl.EvalContext |
| 2495 | wantSummary string |
| 2496 | wantDetail string |
| 2497 | }{ |
| 2498 | // Error messages describing inconsistent result types for conditional expressions. |
| 2499 | { |
| 2500 | "true ? 1 : true", |
| 2501 | nil, |
| 2502 | "Inconsistent conditional result types", |
| 2503 | "The true and false result expressions must have consistent types. The 'true' value is number, but the 'false' value is bool.", |
| 2504 | }, |
| 2505 | { |
| 2506 | "true ? [1] : [true]", |
| 2507 | nil, |
| 2508 | "Inconsistent conditional result types", |
| 2509 | "The true and false result expressions must have consistent types. Type mismatch for tuple element 0: The 'true' value is number, but the 'false' value is bool.", |
| 2510 | }, |
| 2511 | { |
| 2512 | "true ? [1] : [1, true]", |
| 2513 | nil, |
| 2514 | "Inconsistent conditional result types", |
| 2515 | "The true and false result expressions must have consistent types. The 'true' tuple has length 1, but the 'false' tuple has length 2.", |
| 2516 | }, |
| 2517 | { |
| 2518 | "true ? { a = 1 } : { a = true }", |
| 2519 | nil, |
| 2520 | "Inconsistent conditional result types", |
| 2521 | "The true and false result expressions must have consistent types. Type mismatch for object attribute \"a\": The 'true' value is number, but the 'false' value is bool.", |
| 2522 | }, |
| 2523 | { |
| 2524 | "true ? { a = true, b = 1 } : { a = true }", |
| 2525 | nil, |
| 2526 | "Inconsistent conditional result types", |
| 2527 | "The true and false result expressions must have consistent types. The 'true' value includes object attribute \"b\", which is absent in the 'false' value.", |
| 2528 | }, |
| 2529 | { |
| 2530 | "true ? { a = true } : { a = true, b = 1 }", |
| 2531 | nil, |
| 2532 | "Inconsistent conditional result types", |
| 2533 | "The true and false result expressions must have consistent types. The 'false' value includes object attribute \"b\", which is absent in the 'true' value.", |
| 2534 | }, |
| 2535 | { |
| 2536 | // Failing cases for automatic collection conversions. HCL and cty |
| 2537 | // will attempt to unify tuples into lists. We have to make sure |
| 2538 | // the tuple inner types have no common base type, so we mix and |
| 2539 | // match booleans and numbers and validate the error messages. |
| 2540 | "true ? listOf2Tuple : listOf1Tuple", |
| 2541 | &hcl.EvalContext{ |
| 2542 | Variables: map[string]cty.Value{ |
| 2543 | "listOf2Tuple": cty.ListVal([]cty.Value{cty.TupleVal([]cty.Value{cty.True, cty.Zero})}), |
| 2544 | "listOf1Tuple": cty.ListVal([]cty.Value{cty.TupleVal([]cty.Value{cty.True})}), |
| 2545 | }, |
| 2546 | }, |
| 2547 | "Inconsistent conditional result types", |
| 2548 | "The true and false result expressions must have consistent types. Mismatched list element types: The 'true' tuple has length 2, but the 'false' tuple has length 1.", |
nothing calls this directly
no test coverage detected