|
| 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 TestFloat32Func_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 | + "nil": { |
| 25 | + self: knownvalue.Float32Func(func(float32) error { return nil }), |
| 26 | + expectedError: fmt.Errorf("expected json.Number value for Float32Func check, got: <nil>"), |
| 27 | + }, |
| 28 | + "wrong-type": { |
| 29 | + self: knownvalue.Float32Func(func(float32) error { return nil }), |
| 30 | + other: "wrongtype", |
| 31 | + expectedError: fmt.Errorf("expected json.Number value for Float32Func check, got: string"), |
| 32 | + }, |
| 33 | + "no-digits": { |
| 34 | + self: knownvalue.Float32Func(func(float32) error { return nil }), |
| 35 | + other: json.Number("str"), |
| 36 | + expectedError: fmt.Errorf("expected json.Number to be parseable as float32 value for Float32Func check: strconv.ParseFloat: parsing \"str\": invalid syntax"), |
| 37 | + }, |
| 38 | + "failure": { |
| 39 | + self: knownvalue.Float32Func(func(f float32) error { |
| 40 | + if f != 1.1 { |
| 41 | + return fmt.Errorf("%f was not 1.1", f) |
| 42 | + } |
| 43 | + return nil |
| 44 | + }), |
| 45 | + other: json.Number("1.2"), |
| 46 | + expectedError: fmt.Errorf("%f was not 1.1", 1.2), |
| 47 | + }, |
| 48 | + "success": { |
| 49 | + self: knownvalue.Float32Func(func(f float32) error { |
| 50 | + if f != 1.1 { |
| 51 | + return fmt.Errorf("%f was not 1.1", f) |
| 52 | + } |
| 53 | + return nil |
| 54 | + }), |
| 55 | + other: json.Number("1.1"), |
| 56 | + }, |
| 57 | + } |
| 58 | + |
| 59 | + for name, testCase := range testCases { |
| 60 | + t.Run(name, func(t *testing.T) { |
| 61 | + t.Parallel() |
| 62 | + |
| 63 | + got := testCase.self.CheckValue(testCase.other) |
| 64 | + |
| 65 | + if diff := cmp.Diff(got, testCase.expectedError, equateErrorMessage); diff != "" { |
| 66 | + t.Errorf("unexpected difference: %s", diff) |
| 67 | + } |
| 68 | + }) |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +func TestFloat32Func_String(t *testing.T) { |
| 73 | + t.Parallel() |
| 74 | + |
| 75 | + got := knownvalue.Float32Func(func(float32) error { return nil }).String() |
| 76 | + |
| 77 | + if diff := cmp.Diff(got, "Float32Func"); diff != "" { |
| 78 | + t.Errorf("unexpected difference: %s", diff) |
| 79 | + } |
| 80 | +} |
0 commit comments