-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
initial (working) scaffold for be_struct.HavingField matcher
- Loading branch information
1 parent
0409e18
commit fce45c0
Showing
15 changed files
with
203 additions
and
40 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
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
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
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
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,20 @@ | ||
# be_struct | ||
-- | ||
import "." | ||
|
||
|
||
## Usage | ||
|
||
#### func HavingField | ||
|
||
```go | ||
func HavingField[StructT any](fieldName string, expectedValue ...any) types.BeMatcher | ||
``` | ||
HavingField succeeds if the actual value is a struct and it has a field with the | ||
given name. If an expected value is provided, it also succeeds if the actual | ||
value's field has the same value. | ||
|
||
Example: | ||
|
||
Expect(result).To(be_structs.HavingField[TestStruct]("Field1", "hello1")) | ||
Expect(result).To(be_structs.HavingField[TestStruct]("Field2")) |
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,13 @@ | ||
package be_struct_test | ||
|
||
import ( | ||
"testing" | ||
|
||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
func TestBeStructs(t *testing.T) { | ||
RegisterFailHandler(Fail) | ||
RunSpecs(t, "BeStructs Suite") | ||
} |
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,37 @@ | ||
package be_struct_test | ||
|
||
import ( | ||
"github.com/expectto/be/be_struct" | ||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
var _ = Describe("BeStructs", func() { | ||
It("should match a struct field", func() { | ||
type TestStruct struct { | ||
Field1 string | ||
Field2 int | ||
} | ||
|
||
var result = TestStruct{ | ||
Field1: "hello1", | ||
} | ||
|
||
Expect(result).To( | ||
And( | ||
be_struct.HavingField[TestStruct]("Field1", "hello1"), | ||
be_struct.HavingField[TestStruct]("Field2"), // just ensure it exists | ||
)) | ||
|
||
Expect(result).NotTo( | ||
be_struct.HavingField[TestStruct]("Field3"), | ||
) | ||
|
||
type WrongStruct struct { | ||
Field1 string | ||
} | ||
Expect(result).NotTo( | ||
be_struct.HavingField[WrongStruct]("Field1"), | ||
) | ||
}) | ||
}) |
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 @@ | ||
package be_struct | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"reflect" | ||
|
||
. "github.com/expectto/be/internal/psi" | ||
"github.com/expectto/be/types" | ||
"github.com/onsi/gomega/gcustom" | ||
) | ||
|
||
// HavingField succeeds if the actual value is a struct and it has a field with the given name. | ||
// If an expected value is provided, it also succeeds if the actual value's field has the same value. | ||
// | ||
// Example: | ||
// | ||
// Expect(result).To(be_structs.HavingField[TestStruct]("Field1", "hello1")) | ||
// Expect(result).To(be_structs.HavingField[TestStruct]("Field2")) | ||
func HavingField[StructT any](fieldName string, expectedValue ...any) types.BeMatcher { | ||
message := "have field " + fieldName | ||
if len(expectedValue) > 0 { | ||
message += fmt.Sprintf(" with value\n\t<%T>: %v", expectedValue[0], expectedValue[0]) | ||
} | ||
|
||
structType := reflect.TypeFor[StructT]() | ||
|
||
return Psi(gcustom.MakeMatcher(func(actual any) (bool, error) { | ||
val := reflect.ValueOf(actual) | ||
|
||
// Dereference if it's a pointer to a struct | ||
if val.Kind() == reflect.Ptr { | ||
val = val.Elem() | ||
} | ||
if val.Kind() != reflect.Struct { | ||
return false, errors.New("actual value is not a struct") | ||
} | ||
if val.Type() != structType { | ||
// TODO: it doesn't work | ||
message = fmt.Sprintf("be type of %s", structType.String()) | ||
return false, nil | ||
} | ||
|
||
// Check if the field exists | ||
field := val.FieldByName(fieldName) | ||
if !field.IsValid() { | ||
return false, nil | ||
} | ||
|
||
// If an expected value is provided, compare the field's value with it | ||
if len(expectedValue) > 0 { | ||
expected := reflect.ValueOf(expectedValue[0]) | ||
if !reflect.DeepEqual(field.Interface(), expected.Interface()) { | ||
return false, nil | ||
} | ||
} | ||
|
||
// If no value to compare, return true if the field exists | ||
return true, nil | ||
}), message) | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
# be_url | ||
-- | ||
import "github.com/expectto/be/be_url" | ||
import "." | ||
|
||
Package be_url provides Be matchers on url.URL | ||
|
||
|
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