|
| 1 | +// Copyright (c) HashiCorp, Inc. |
| 2 | +// SPDX-License-Identifier: MPL-2.0 |
| 3 | + |
| 4 | +package compare_test |
| 5 | + |
| 6 | +import ( |
| 7 | + "fmt" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/google/go-cmp/cmp" |
| 11 | + |
| 12 | + "github.com/hashicorp/terraform-plugin-testing/compare" |
| 13 | +) |
| 14 | + |
| 15 | +func TestValuesDiffer_CompareValues(t *testing.T) { |
| 16 | + t.Parallel() |
| 17 | + |
| 18 | + testCases := map[string]struct { |
| 19 | + in []any |
| 20 | + expectedError error |
| 21 | + }{ |
| 22 | + "nil": {}, |
| 23 | + "single-value": { |
| 24 | + in: []any{"str"}, |
| 25 | + }, |
| 26 | + "non-matching-sequential-values": { |
| 27 | + in: []any{"str", "other_str", "str"}, |
| 28 | + }, |
| 29 | + "matching-values-string": { |
| 30 | + in: []any{"str", "other_str", "other_str"}, |
| 31 | + expectedError: fmt.Errorf("expected values to differ, but they are the same: other_str == other_str"), |
| 32 | + }, |
| 33 | + "matching-values-slice": { |
| 34 | + in: []any{ |
| 35 | + []any{"other_str"}, |
| 36 | + []any{"other_str"}, |
| 37 | + }, |
| 38 | + expectedError: fmt.Errorf("expected values to differ, but they are the same: [other_str] == [other_str]"), |
| 39 | + }, |
| 40 | + "matching-values-map": { |
| 41 | + in: []any{ |
| 42 | + map[string]any{"a": "other_str"}, |
| 43 | + map[string]any{"a": "other_str"}, |
| 44 | + }, |
| 45 | + expectedError: fmt.Errorf("expected values to differ, but they are the same: map[a:other_str] == map[a:other_str]"), |
| 46 | + }, |
| 47 | + } |
| 48 | + |
| 49 | + for name, testCase := range testCases { |
| 50 | + name, testCase := name, testCase |
| 51 | + |
| 52 | + t.Run(name, func(t *testing.T) { |
| 53 | + t.Parallel() |
| 54 | + |
| 55 | + err := compare.ValuesDiffer().CompareValues(testCase.in...) |
| 56 | + |
| 57 | + if diff := cmp.Diff(err, testCase.expectedError, equateErrorMessage); diff != "" { |
| 58 | + t.Errorf("unexpected difference: %s", diff) |
| 59 | + } |
| 60 | + }) |
| 61 | + } |
| 62 | +} |
0 commit comments