|
| 1 | +// Copyright (c) HashiCorp, Inc. |
| 2 | +// SPDX-License-Identifier: MPL-2.0 |
| 3 | + |
| 4 | +package toproto_test |
| 5 | + |
| 6 | +import ( |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/google/go-cmp/cmp" |
| 10 | + "github.com/google/go-cmp/cmp/cmpopts" |
| 11 | + "github.com/hashicorp/terraform-plugin-go/tfprotov5" |
| 12 | + "github.com/hashicorp/terraform-plugin-go/tfprotov5/internal/tfplugin5" |
| 13 | + "github.com/hashicorp/terraform-plugin-go/tfprotov5/internal/toproto" |
| 14 | +) |
| 15 | + |
| 16 | +func TestGetMetadata_ListResourceMetadata(t *testing.T) { |
| 17 | + t.Parallel() |
| 18 | + |
| 19 | + testCases := map[string]struct { |
| 20 | + in *tfprotov5.ListResourceMetadata |
| 21 | + expected *tfplugin5.GetMetadata_ListResourceMetadata |
| 22 | + }{ |
| 23 | + "nil": { |
| 24 | + in: nil, |
| 25 | + expected: nil, |
| 26 | + }, |
| 27 | + "zero": { |
| 28 | + in: &tfprotov5.ListResourceMetadata{}, |
| 29 | + expected: &tfplugin5.GetMetadata_ListResourceMetadata{}, |
| 30 | + }, |
| 31 | + "TypeName": { |
| 32 | + in: &tfprotov5.ListResourceMetadata{ |
| 33 | + TypeName: "test", |
| 34 | + }, |
| 35 | + expected: &tfplugin5.GetMetadata_ListResourceMetadata{ |
| 36 | + TypeName: "test", |
| 37 | + }, |
| 38 | + }, |
| 39 | + } |
| 40 | + |
| 41 | + for name, testCase := range testCases { |
| 42 | + t.Run(name, func(t *testing.T) { |
| 43 | + t.Parallel() |
| 44 | + |
| 45 | + got := toproto.GetMetadata_ListResourceMetadata(testCase.in) |
| 46 | + |
| 47 | + // Protocol Buffers generated types must have unexported fields |
| 48 | + // ignored or cmp.Diff() will raise an error. This is easier than |
| 49 | + // writing a custom Comparer for each type, which would have no |
| 50 | + // benefits. |
| 51 | + diffOpts := cmpopts.IgnoreUnexported( |
| 52 | + tfplugin5.GetMetadata_ListResourceMetadata{}, |
| 53 | + ) |
| 54 | + |
| 55 | + if diff := cmp.Diff(got, testCase.expected, diffOpts); diff != "" { |
| 56 | + t.Errorf("unexpected difference: %s", diff) |
| 57 | + } |
| 58 | + }) |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +func TestValidateListResourceConfig_Response(t *testing.T) { |
| 63 | + t.Parallel() |
| 64 | + |
| 65 | + testCases := map[string]struct { |
| 66 | + in *tfprotov5.ValidateListResourceConfigResponse |
| 67 | + expected *tfplugin5.ValidateListResourceConfig_Response |
| 68 | + }{ |
| 69 | + "nil": { |
| 70 | + in: nil, |
| 71 | + expected: nil, |
| 72 | + }, |
| 73 | + "zero": { |
| 74 | + in: &tfprotov5.ValidateListResourceConfigResponse{}, |
| 75 | + expected: &tfplugin5.ValidateListResourceConfig_Response{ |
| 76 | + Diagnostics: []*tfplugin5.Diagnostic{}, |
| 77 | + }, |
| 78 | + }, |
| 79 | + "Diagnostics": { |
| 80 | + in: &tfprotov5.ValidateListResourceConfigResponse{ |
| 81 | + Diagnostics: []*tfprotov5.Diagnostic{ |
| 82 | + testTfprotov5Diagnostic, |
| 83 | + }, |
| 84 | + }, |
| 85 | + expected: &tfplugin5.ValidateListResourceConfig_Response{ |
| 86 | + Diagnostics: []*tfplugin5.Diagnostic{ |
| 87 | + testTfplugin5Diagnostic, |
| 88 | + }, |
| 89 | + }, |
| 90 | + }, |
| 91 | + } |
| 92 | + |
| 93 | + for name, testCase := range testCases { |
| 94 | + t.Run(name, func(t *testing.T) { |
| 95 | + t.Parallel() |
| 96 | + |
| 97 | + got := toproto.ValidateListResourceConfig_Response(testCase.in) |
| 98 | + |
| 99 | + // Protocol Buffers generated types must have unexported fields |
| 100 | + // ignored or cmp.Diff() will raise an error. This is easier than |
| 101 | + // writing a custom Comparer for each type, which would have no |
| 102 | + // benefits. |
| 103 | + diffOpts := cmpopts.IgnoreUnexported( |
| 104 | + tfplugin5.Diagnostic{}, |
| 105 | + tfplugin5.ValidateListResourceConfig_Response{}, |
| 106 | + ) |
| 107 | + |
| 108 | + if diff := cmp.Diff(got, testCase.expected, diffOpts); diff != "" { |
| 109 | + t.Errorf("unexpected difference: %s", diff) |
| 110 | + } |
| 111 | + }) |
| 112 | + } |
| 113 | +} |
0 commit comments