-
-
Notifications
You must be signed in to change notification settings - Fork 282
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Onsi Fakhouri
committed
Oct 27, 2013
1 parent
ce22a0b
commit 27c7621
Showing
8 changed files
with
105 additions
and
9 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
File renamed without changes.
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,28 @@ | ||
package matchers | ||
|
||
import ( | ||
"fmt" | ||
"reflect" | ||
) | ||
|
||
type BeEquivalentToMatcher struct { | ||
Expected interface{} | ||
} | ||
|
||
func (matcher *BeEquivalentToMatcher) Match(actual interface{}) (success bool, message string, err error) { | ||
if actual == nil && matcher.Expected == nil { | ||
return false, "", fmt.Errorf("Both actual and expected must not be nil.") | ||
} | ||
|
||
convertedActual := actual | ||
|
||
if actual != nil && matcher.Expected != nil && reflect.TypeOf(actual).ConvertibleTo(reflect.TypeOf(matcher.Expected)) { | ||
convertedActual = reflect.ValueOf(actual).Convert(reflect.TypeOf(matcher.Expected)).Interface() | ||
} | ||
|
||
if reflect.DeepEqual(convertedActual, matcher.Expected) { | ||
return true, formatMessage(actual, "not to equal", matcher.Expected), nil | ||
} else { | ||
return false, formatMessage(actual, "to equal", matcher.Expected), nil | ||
} | ||
} |
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,50 @@ | ||
package matchers_test | ||
|
||
import ( | ||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
. "github.com/onsi/gomega/matchers" | ||
) | ||
|
||
var _ = Describe("BeEquivalentTo", func() { | ||
Context("when asserting that nil is equivalent to nil", func() { | ||
It("should error", func() { | ||
success, _, err := (&BeEquivalentToMatcher{Expected: nil}).Match(nil) | ||
|
||
Ω(success).Should(BeFalse()) | ||
Ω(err).Should(HaveOccured()) | ||
}) | ||
}) | ||
|
||
Context("When asserting on nil", func() { | ||
It("should do the right thing", func() { | ||
Ω("foo").ShouldNot(BeEquivalentTo(nil)) | ||
Ω(nil).ShouldNot(BeEquivalentTo(3)) | ||
Ω([]int{1, 2}).ShouldNot(BeEquivalentTo(nil)) | ||
}) | ||
}) | ||
|
||
Context("When asserting on type aliases", func() { | ||
It("should the right thing", func() { | ||
Ω(StringAlias("foo")).Should(BeEquivalentTo("foo")) | ||
Ω("foo").Should(BeEquivalentTo(StringAlias("foo"))) | ||
Ω(StringAlias("foo")).ShouldNot(BeEquivalentTo("bar")) | ||
Ω("foo").ShouldNot(BeEquivalentTo(StringAlias("bar"))) | ||
}) | ||
}) | ||
|
||
Context("When asserting on numbers", func() { | ||
It("should convert actual to expected and do the right thing", func() { | ||
Ω(5).Should(BeEquivalentTo(5)) | ||
Ω(5.0).Should(BeEquivalentTo(5.0)) | ||
Ω(5).Should(BeEquivalentTo(5.0)) | ||
|
||
Ω(5).ShouldNot(BeEquivalentTo("5")) | ||
Ω(5).ShouldNot(BeEquivalentTo(3)) | ||
|
||
//Here be dragons! | ||
Ω(5.1).Should(BeEquivalentTo(5)) | ||
Ω(5).ShouldNot(BeEquivalentTo(5.1)) | ||
}) | ||
}) | ||
}) |
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