|
| 1 | +// Copyright (c) HashiCorp, Inc. |
| 2 | +// SPDX-License-Identifier: MPL-2.0 |
| 3 | + |
| 4 | +package knownvalue_test |
| 5 | + |
| 6 | +import ( |
| 7 | + "encoding/json" |
| 8 | + "fmt" |
| 9 | + "testing" |
| 10 | + |
| 11 | + "github.com/google/go-cmp/cmp" |
| 12 | + |
| 13 | + "github.com/hashicorp/terraform-plugin-testing/knownvalue" |
| 14 | +) |
| 15 | + |
| 16 | +func TestFloat32Value_CheckValue(t *testing.T) { |
| 17 | + t.Parallel() |
| 18 | + |
| 19 | + testCases := map[string]struct { |
| 20 | + self knownvalue.Check |
| 21 | + other any |
| 22 | + expectedError error |
| 23 | + }{ |
| 24 | + "zero-nil": { |
| 25 | + self: knownvalue.Float32Exact(0), |
| 26 | + expectedError: fmt.Errorf("expected json.Number value for Float32Exact check, got: <nil>"), |
| 27 | + }, |
| 28 | + "zero-other": { |
| 29 | + self: knownvalue.Float32Exact(0), |
| 30 | + other: json.Number("0.0"), // checking against the underlying value field zero-value |
| 31 | + }, |
| 32 | + "nil": { |
| 33 | + self: knownvalue.Float32Exact(1.234), |
| 34 | + expectedError: fmt.Errorf("expected json.Number value for Float32Exact check, got: <nil>"), |
| 35 | + }, |
| 36 | + "wrong-type": { |
| 37 | + self: knownvalue.Float32Exact(1.234), |
| 38 | + other: json.Number("str"), |
| 39 | + expectedError: fmt.Errorf("expected json.Number to be parseable as float32 value for Float32Exact check: strconv.ParseFloat: parsing \"str\": invalid syntax"), |
| 40 | + }, |
| 41 | + "not-equal": { |
| 42 | + self: knownvalue.Float32Exact(1.234), |
| 43 | + other: json.Number("4.321"), |
| 44 | + expectedError: fmt.Errorf("expected value 1.234 for Float32Exact check, got: 4.321"), |
| 45 | + }, |
| 46 | + "equal": { |
| 47 | + self: knownvalue.Float32Exact(1.234), |
| 48 | + other: json.Number("1.234"), |
| 49 | + }, |
| 50 | + } |
| 51 | + |
| 52 | + for name, testCase := range testCases { |
| 53 | + name, testCase := name, testCase |
| 54 | + |
| 55 | + t.Run(name, func(t *testing.T) { |
| 56 | + t.Parallel() |
| 57 | + |
| 58 | + got := testCase.self.CheckValue(testCase.other) |
| 59 | + |
| 60 | + if diff := cmp.Diff(got, testCase.expectedError, equateErrorMessage); diff != "" { |
| 61 | + t.Errorf("unexpected difference: %s", diff) |
| 62 | + } |
| 63 | + }) |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +func TestFloat32Value_String(t *testing.T) { |
| 68 | + t.Parallel() |
| 69 | + |
| 70 | + got := knownvalue.Float32Exact(1.234567890123e+03).String() |
| 71 | + |
| 72 | + if diff := cmp.Diff(got, "1234.5679"); diff != "" { |
| 73 | + t.Errorf("unexpected difference: %s", diff) |
| 74 | + } |
| 75 | +} |
0 commit comments