-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Modifying ExpectKnown<Value|OutputValue|OutputValueAtPath> to allow f…
…or checking of null values (#266)
- Loading branch information
1 parent
d93aa82
commit 598dac8
Showing
8 changed files
with
110 additions
and
356 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// Copyright (c) HashiCorp, Inc. | ||
// SPDX-License-Identifier: MPL-2.0 | ||
|
||
package knownvalue | ||
|
||
import ( | ||
"fmt" | ||
) | ||
|
||
var _ Check = NullValue{} | ||
|
||
// NullValue is a Check for asserting equality between the value supplied | ||
// to NullValueExact and the value passed to the CheckValue method. | ||
type NullValue struct{} | ||
|
||
// CheckValue determines whether the passed value is of nil. | ||
func (v NullValue) CheckValue(other any) error { | ||
if other != nil { | ||
return fmt.Errorf("expected value nil for NullValue check, got: %T", other) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// String returns the string representation of nil. | ||
func (v NullValue) String() string { | ||
return "nil" | ||
} | ||
|
||
// NullValueExact returns a Check for asserting equality nil | ||
// and the value passed to the CheckValue method. | ||
func NullValueExact() NullValue { | ||
return NullValue{} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
// Copyright (c) HashiCorp, Inc. | ||
// SPDX-License-Identifier: MPL-2.0 | ||
|
||
package knownvalue_test | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
|
||
"github.com/hashicorp/terraform-plugin-testing/knownvalue" | ||
) | ||
|
||
func TestNullValue_CheckValue(t *testing.T) { | ||
t.Parallel() | ||
|
||
testCases := map[string]struct { | ||
self knownvalue.NullValue | ||
other any | ||
expectedError error | ||
}{ | ||
"zero-nil": {}, | ||
"zero-other": { | ||
other: nil, // checking against the underlying value field zero-value | ||
}, | ||
"not-nil": { | ||
self: knownvalue.NullValueExact(), | ||
other: false, | ||
expectedError: fmt.Errorf("expected value nil for NullValue check, got: bool"), | ||
}, | ||
"equal": { | ||
self: knownvalue.NullValueExact(), | ||
other: nil, | ||
}, | ||
} | ||
|
||
for name, testCase := range testCases { | ||
name, testCase := name, testCase | ||
|
||
t.Run(name, func(t *testing.T) { | ||
t.Parallel() | ||
|
||
got := testCase.self.CheckValue(testCase.other) | ||
|
||
if diff := cmp.Diff(got, testCase.expectedError, equateErrorMessage); diff != "" { | ||
t.Errorf("unexpected difference: %s", diff) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestNullValue_String(t *testing.T) { | ||
t.Parallel() | ||
|
||
got := knownvalue.NullValueExact().String() | ||
|
||
if diff := cmp.Diff(got, "nil"); diff != "" { | ||
t.Errorf("unexpected difference: %s", diff) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.