|
7 | 7 | "path/filepath" |
8 | 8 | "regexp" |
9 | 9 | "slices" |
| 10 | + "strings" |
10 | 11 | "testing" |
11 | 12 |
|
12 | 13 | "github.com/hashicorp/hcl/v2" |
@@ -144,7 +145,8 @@ func Test_Extract(t *testing.T) { |
144 | 145 | unknownTags: []string{}, |
145 | 146 | input: preview.Input{}, |
146 | 147 | params: map[string]assertParam{ |
147 | | - "os": ap(). |
| 148 | + "os": apWithDiags(). |
| 149 | + errorDiagnostics("unique"). |
148 | 150 | value("0000000000000000000000000000000000000000000000000000000000000000"), |
149 | 151 | }, |
150 | 152 | }, |
@@ -222,7 +224,18 @@ func Test_Extract(t *testing.T) { |
222 | 224 | input: preview.Input{}, |
223 | 225 | unknownTags: []string{}, |
224 | 226 | params: map[string]assertParam{ |
225 | | - "word": ap(), |
| 227 | + "word": apWithDiags(). |
| 228 | + errorDiagnostics("Required"), |
| 229 | + }, |
| 230 | + }, |
| 231 | + { |
| 232 | + name: "required", |
| 233 | + dir: "required", |
| 234 | + expTags: map[string]string{}, |
| 235 | + input: preview.Input{}, |
| 236 | + unknownTags: []string{}, |
| 237 | + params: map[string]assertParam{ |
| 238 | + "region": apWithDiags().errorDiagnostics("Required"), |
226 | 239 | }, |
227 | 240 | }, |
228 | 241 | { |
@@ -487,9 +500,53 @@ func Test_Extract(t *testing.T) { |
487 | 500 | type assertParam func(t *testing.T, parameter types.Parameter) |
488 | 501 |
|
489 | 502 | func ap() assertParam { |
| 503 | + return func(t *testing.T, parameter types.Parameter) { |
| 504 | + t.Helper() |
| 505 | + assert.Empty(t, parameter.Diagnostics, "parameter should have no diagnostics") |
| 506 | + } |
| 507 | +} |
| 508 | + |
| 509 | +func apWithDiags() assertParam { |
490 | 510 | return func(t *testing.T, parameter types.Parameter) {} |
491 | 511 | } |
492 | 512 |
|
| 513 | +func (a assertParam) errorDiagnostics(patterns ...string) assertParam { |
| 514 | + return a.diagnostics(hcl.DiagError, patterns...) |
| 515 | +} |
| 516 | + |
| 517 | +func (a assertParam) warnDiagnostics(patterns ...string) assertParam { |
| 518 | + return a.diagnostics(hcl.DiagWarning, patterns...) |
| 519 | +} |
| 520 | + |
| 521 | +func (a assertParam) diagnostics(sev hcl.DiagnosticSeverity, patterns ...string) assertParam { |
| 522 | + shadow := patterns |
| 523 | + return a.extend(func(t *testing.T, parameter types.Parameter) { |
| 524 | + checks := make([]string, len(shadow)) |
| 525 | + copy(checks, shadow) |
| 526 | + |
| 527 | + DiagLoop: |
| 528 | + for _, diag := range parameter.Diagnostics { |
| 529 | + if diag.Severity != sev { |
| 530 | + continue |
| 531 | + } |
| 532 | + for i, pat := range checks { |
| 533 | + if strings.Contains(diag.Summary, pat) || strings.Contains(diag.Detail, pat) { |
| 534 | + checks = append(checks[:i], checks[i+1:]...) |
| 535 | + break DiagLoop |
| 536 | + } |
| 537 | + } |
| 538 | + } |
| 539 | + |
| 540 | + assert.Equal(t, []string{}, checks, "missing expected diagnostic errors") |
| 541 | + }) |
| 542 | +} |
| 543 | + |
| 544 | +func (a assertParam) noDiagnostics() assertParam { |
| 545 | + return a.extend(func(t *testing.T, parameter types.Parameter) { |
| 546 | + assert.Empty(t, parameter.Diagnostics, "parameter should have no diagnostics") |
| 547 | + }) |
| 548 | +} |
| 549 | + |
493 | 550 | func (a assertParam) formType(exp provider.ParameterFormType) assertParam { |
494 | 551 | return a.extend(func(t *testing.T, parameter types.Parameter) { |
495 | 552 | assert.Equal(t, exp, parameter.FormType, "parameter form type equality check") |
@@ -555,6 +612,7 @@ func (a assertParam) extend(f assertParam) assertParam { |
555 | 612 | } |
556 | 613 |
|
557 | 614 | return func(t *testing.T, parameter types.Parameter) { |
| 615 | + t.Helper() |
558 | 616 | (a)(t, parameter) |
559 | 617 | f(t, parameter) |
560 | 618 | } |
|
0 commit comments