vendor: Mega update all dependencies

GitHub-Pull-Request: https://github.com/syncthing/syncthing/pull/4080
This commit is contained in:
Jakob Borg
2017-04-05 14:34:41 +00:00
parent 49c1527724
commit a1bcc15458
1354 changed files with 55066 additions and 797850 deletions

View File

@@ -1,103 +0,0 @@
package matchers_test
import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
. "github.com/onsi/gomega/matchers"
"github.com/onsi/gomega/types"
)
// sample data
var (
// example input
input = "hi"
// some matchers that succeed against the input
true1 = HaveLen(2)
true2 = Equal("hi")
true3 = MatchRegexp("hi")
// some matchers that fail against the input.
false1 = HaveLen(1)
false2 = Equal("hip")
false3 = MatchRegexp("hope")
)
// verifyFailureMessage expects the matcher to fail with the given input, and verifies the failure message.
func verifyFailureMessage(m types.GomegaMatcher, input string, expectedFailureMsgFragment string) {
Expect(m.Match(input)).To(BeFalse())
Expect(m.FailureMessage(input)).To(Equal(
"Expected\n <string>: " + input + "\n" + expectedFailureMsgFragment))
}
var _ = Describe("AndMatcher", func() {
It("works with positive cases", func() {
Expect(input).To(And())
Expect(input).To(And(true1))
Expect(input).To(And(true1, true2))
Expect(input).To(And(true1, true2, true3))
// use alias
Expect(input).To(SatisfyAll(true1, true2, true3))
})
It("works with negative cases", func() {
Expect(input).ToNot(And(false1, false2))
Expect(input).ToNot(And(true1, true2, false3))
Expect(input).ToNot(And(true1, false2, false3))
Expect(input).ToNot(And(false1, true1, true2))
})
Context("failure messages", func() {
Context("when match fails", func() {
It("gives a descriptive message", func() {
verifyFailureMessage(And(false1, true1), input, "to have length 1")
verifyFailureMessage(And(true1, false2), input, "to equal\n <string>: hip")
verifyFailureMessage(And(true1, true2, false3), input, "to match regular expression\n <string>: hope")
})
})
Context("when match succeeds, but expected it to fail", func() {
It("gives a descriptive message", func() {
verifyFailureMessage(Not(And(true1, true2)), input,
`To not satisfy all of these matchers: [%!s(*matchers.HaveLenMatcher=&{2}) %!s(*matchers.EqualMatcher=&{hi})]`)
})
})
})
Context("MatchMayChangeInTheFuture", func() {
Context("Match returned false", func() {
Context("returns value of the failed matcher", func() {
It("false if failed matcher not going to change", func() {
// 3 matchers: 1st returns true, 2nd returns false and is not going to change, 3rd is never called
m := And(Not(BeNil()), Or(), Equal(1))
Expect(m.Match("hi")).To(BeFalse())
Expect(m.(*AndMatcher).MatchMayChangeInTheFuture("hi")).To(BeFalse()) // empty Or() indicates not going to change
})
It("true if failed matcher indicates it might change", func() {
// 3 matchers: 1st returns true, 2nd returns false and "might" change, 3rd is never called
m := And(Not(BeNil()), Equal(5), Equal(1))
Expect(m.Match("hi")).To(BeFalse())
Expect(m.(*AndMatcher).MatchMayChangeInTheFuture("hi")).To(BeTrue()) // Equal(5) indicates it might change
})
})
})
Context("Match returned true", func() {
It("returns true if any of the matchers could change", func() {
// 3 matchers, all return true, and all could change
m := And(Not(BeNil()), Equal("hi"), HaveLen(2))
Expect(m.Match("hi")).To(BeTrue())
Expect(m.(*AndMatcher).MatchMayChangeInTheFuture("hi")).To(BeTrue()) // all 3 of these matchers default to 'true'
})
It("returns false if none of the matchers could change", func() {
// empty And() has the property of always matching, and never can change since there are no sub-matchers that could change
m := And()
Expect(m.Match("anything")).To(BeTrue())
Expect(m.(*AndMatcher).MatchMayChangeInTheFuture("anything")).To(BeFalse())
// And() with 3 sub-matchers that return true, and can't change
m = And(And(), And(), And())
Expect(m.Match("hi")).To(BeTrue())
Expect(m.(*AndMatcher).MatchMayChangeInTheFuture("hi")).To(BeFalse()) // the 3 empty And()'s won't change
})
})
})
})

View File

@@ -1,30 +0,0 @@
package matchers_test
import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
. "github.com/onsi/gomega/matchers"
)
var _ = Describe("AssignableToTypeOf", func() {
Context("When asserting assignability between types", func() {
It("should do the right thing", func() {
Ω(0).Should(BeAssignableToTypeOf(0))
Ω(5).Should(BeAssignableToTypeOf(-1))
Ω("foo").Should(BeAssignableToTypeOf("bar"))
Ω(struct{ Foo string }{}).Should(BeAssignableToTypeOf(struct{ Foo string }{}))
Ω(0).ShouldNot(BeAssignableToTypeOf("bar"))
Ω(5).ShouldNot(BeAssignableToTypeOf(struct{ Foo string }{}))
Ω("foo").ShouldNot(BeAssignableToTypeOf(42))
})
})
Context("When asserting nil values", func() {
It("should error", func() {
success, err := (&AssignableToTypeOfMatcher{Expected: nil}).Match(nil)
Ω(success).Should(BeFalse())
Ω(err).Should(HaveOccurred())
})
})
})

View File

@@ -1,40 +0,0 @@
package matchers_test
import (
"io/ioutil"
"os"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
. "github.com/onsi/gomega/matchers"
)
var _ = Describe("BeADirectoryMatcher", func() {
Context("when passed a string", func() {
It("should do the right thing", func() {
Ω("/dne/test").ShouldNot(BeADirectory())
tmpFile, err := ioutil.TempFile("", "gomega-test-tempfile")
Ω(err).ShouldNot(HaveOccurred())
defer os.Remove(tmpFile.Name())
Ω(tmpFile.Name()).ShouldNot(BeADirectory())
tmpDir, err := ioutil.TempDir("", "gomega-test-tempdir")
Ω(err).ShouldNot(HaveOccurred())
defer os.Remove(tmpDir)
Ω(tmpDir).Should(BeADirectory())
})
})
Context("when passed something else", func() {
It("should error", func() {
success, err := (&BeADirectoryMatcher{}).Match(nil)
Ω(success).Should(BeFalse())
Ω(err).Should(HaveOccurred())
success, err = (&BeADirectoryMatcher{}).Match(true)
Ω(success).Should(BeFalse())
Ω(err).Should(HaveOccurred())
})
})
})

View File

@@ -1,40 +0,0 @@
package matchers_test
import (
"io/ioutil"
"os"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
. "github.com/onsi/gomega/matchers"
)
var _ = Describe("BeARegularFileMatcher", func() {
Context("when passed a string", func() {
It("should do the right thing", func() {
Ω("/dne/test").ShouldNot(BeARegularFile())
tmpFile, err := ioutil.TempFile("", "gomega-test-tempfile")
Ω(err).ShouldNot(HaveOccurred())
defer os.Remove(tmpFile.Name())
Ω(tmpFile.Name()).Should(BeARegularFile())
tmpDir, err := ioutil.TempDir("", "gomega-test-tempdir")
Ω(err).ShouldNot(HaveOccurred())
defer os.Remove(tmpDir)
Ω(tmpDir).ShouldNot(BeARegularFile())
})
})
Context("when passed something else", func() {
It("should error", func() {
success, err := (&BeARegularFileMatcher{}).Match(nil)
Ω(success).Should(BeFalse())
Ω(err).Should(HaveOccurred())
success, err = (&BeARegularFileMatcher{}).Match(true)
Ω(success).Should(BeFalse())
Ω(err).Should(HaveOccurred())
})
})
})

View File

@@ -1,40 +0,0 @@
package matchers_test
import (
"io/ioutil"
"os"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
. "github.com/onsi/gomega/matchers"
)
var _ = Describe("BeAnExistingFileMatcher", func() {
Context("when passed a string", func() {
It("should do the right thing", func() {
Ω("/dne/test").ShouldNot(BeAnExistingFile())
tmpFile, err := ioutil.TempFile("", "gomega-test-tempfile")
Ω(err).ShouldNot(HaveOccurred())
defer os.Remove(tmpFile.Name())
Ω(tmpFile.Name()).Should(BeAnExistingFile())
tmpDir, err := ioutil.TempDir("", "gomega-test-tempdir")
Ω(err).ShouldNot(HaveOccurred())
defer os.Remove(tmpDir)
Ω(tmpDir).Should(BeAnExistingFile())
})
})
Context("when passed something else", func() {
It("should error", func() {
success, err := (&BeAnExistingFileMatcher{}).Match(nil)
Ω(success).Should(BeFalse())
Ω(err).Should(HaveOccurred())
success, err = (&BeAnExistingFileMatcher{}).Match(true)
Ω(success).Should(BeFalse())
Ω(err).Should(HaveOccurred())
})
})
})

View File

@@ -1,70 +0,0 @@
package matchers_test
import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
. "github.com/onsi/gomega/matchers"
)
var _ = Describe("BeClosedMatcher", func() {
Context("when passed a channel", func() {
It("should do the right thing", func() {
openChannel := make(chan bool)
Ω(openChannel).ShouldNot(BeClosed())
var openReaderChannel <-chan bool
openReaderChannel = openChannel
Ω(openReaderChannel).ShouldNot(BeClosed())
closedChannel := make(chan bool)
close(closedChannel)
Ω(closedChannel).Should(BeClosed())
var closedReaderChannel <-chan bool
closedReaderChannel = closedChannel
Ω(closedReaderChannel).Should(BeClosed())
})
})
Context("when passed a send-only channel", func() {
It("should error", func() {
openChannel := make(chan bool)
var openWriterChannel chan<- bool
openWriterChannel = openChannel
success, err := (&BeClosedMatcher{}).Match(openWriterChannel)
Ω(success).Should(BeFalse())
Ω(err).Should(HaveOccurred())
closedChannel := make(chan bool)
close(closedChannel)
var closedWriterChannel chan<- bool
closedWriterChannel = closedChannel
success, err = (&BeClosedMatcher{}).Match(closedWriterChannel)
Ω(success).Should(BeFalse())
Ω(err).Should(HaveOccurred())
})
})
Context("when passed something else", func() {
It("should error", func() {
var nilChannel chan bool
success, err := (&BeClosedMatcher{}).Match(nilChannel)
Ω(success).Should(BeFalse())
Ω(err).Should(HaveOccurred())
success, err = (&BeClosedMatcher{}).Match(nil)
Ω(success).Should(BeFalse())
Ω(err).Should(HaveOccurred())
success, err = (&BeClosedMatcher{}).Match(7)
Ω(success).Should(BeFalse())
Ω(err).Should(HaveOccurred())
})
})
})

View File

@@ -1,52 +0,0 @@
package matchers_test
import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
. "github.com/onsi/gomega/matchers"
)
var _ = Describe("BeEmpty", func() {
Context("when passed a supported type", func() {
It("should do the right thing", func() {
Ω("").Should(BeEmpty())
Ω(" ").ShouldNot(BeEmpty())
Ω([0]int{}).Should(BeEmpty())
Ω([1]int{1}).ShouldNot(BeEmpty())
Ω([]int{}).Should(BeEmpty())
Ω([]int{1}).ShouldNot(BeEmpty())
Ω(map[string]int{}).Should(BeEmpty())
Ω(map[string]int{"a": 1}).ShouldNot(BeEmpty())
c := make(chan bool, 1)
Ω(c).Should(BeEmpty())
c <- true
Ω(c).ShouldNot(BeEmpty())
})
})
Context("when passed a correctly typed nil", func() {
It("should be true", func() {
var nilSlice []int
Ω(nilSlice).Should(BeEmpty())
var nilMap map[int]string
Ω(nilMap).Should(BeEmpty())
})
})
Context("when passed an unsupported type", func() {
It("should error", func() {
success, err := (&BeEmptyMatcher{}).Match(0)
Ω(success).Should(BeFalse())
Ω(err).Should(HaveOccurred())
success, err = (&BeEmptyMatcher{}).Match(nil)
Ω(success).Should(BeFalse())
Ω(err).Should(HaveOccurred())
})
})
})

View File

@@ -1,50 +0,0 @@
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(HaveOccurred())
})
})
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))
})
})
})

View File

@@ -1,20 +0,0 @@
package matchers_test
import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
. "github.com/onsi/gomega/matchers"
)
var _ = Describe("BeFalse", func() {
It("should handle true and false correctly", func() {
Ω(true).ShouldNot(BeFalse())
Ω(false).Should(BeFalse())
})
It("should only support booleans", func() {
success, err := (&BeFalseMatcher{}).Match("foo")
Ω(success).Should(BeFalse())
Ω(err).Should(HaveOccurred())
})
})

View File

@@ -1,61 +0,0 @@
package matchers_test
import (
"errors"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
. "github.com/onsi/gomega/matchers"
)
var _ = Describe("BeIdenticalTo", func() {
Context("when asserting that nil equals nil", func() {
It("should error", func() {
success, err := (&BeIdenticalToMatcher{Expected: nil}).Match(nil)
Ω(success).Should(BeFalse())
Ω(err).Should(HaveOccurred())
})
})
It("should treat the same pointer to a struct as identical", func() {
mySpecialStruct := myCustomType{}
Ω(&mySpecialStruct).Should(BeIdenticalTo(&mySpecialStruct))
Ω(&myCustomType{}).ShouldNot(BeIdenticalTo(&mySpecialStruct))
})
It("should be strict about types", func() {
Ω(5).ShouldNot(BeIdenticalTo("5"))
Ω(5).ShouldNot(BeIdenticalTo(5.0))
Ω(5).ShouldNot(BeIdenticalTo(3))
})
It("should treat primtives as identical", func() {
Ω("5").Should(BeIdenticalTo("5"))
Ω("5").ShouldNot(BeIdenticalTo("55"))
Ω(5.55).Should(BeIdenticalTo(5.55))
Ω(5.55).ShouldNot(BeIdenticalTo(6.66))
Ω(5).Should(BeIdenticalTo(5))
Ω(5).ShouldNot(BeIdenticalTo(55))
})
It("should treat the same pointers to a slice as identical", func() {
mySlice := []int{1, 2}
Ω(&mySlice).Should(BeIdenticalTo(&mySlice))
Ω(&mySlice).ShouldNot(BeIdenticalTo(&[]int{1, 2}))
})
It("should treat the same pointers to a map as identical", func() {
myMap := map[string]string{"a": "b", "c": "d"}
Ω(&myMap).Should(BeIdenticalTo(&myMap))
Ω(myMap).ShouldNot(BeIdenticalTo(map[string]string{"a": "b", "c": "d"}))
})
It("should treat the same pointers to an error as identical", func() {
myError := errors.New("foo")
Ω(&myError).Should(BeIdenticalTo(&myError))
Ω(errors.New("foo")).ShouldNot(BeIdenticalTo(errors.New("bar")))
})
})

View File

@@ -1,28 +0,0 @@
package matchers_test
import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
var _ = Describe("BeNil", func() {
It("should succeed when passed nil", func() {
Ω(nil).Should(BeNil())
})
It("should succeed when passed a typed nil", func() {
var a []int
Ω(a).Should(BeNil())
})
It("should succeed when passing nil pointer", func() {
var f *struct{}
Ω(f).Should(BeNil())
})
It("should not succeed when not passed nil", func() {
Ω(0).ShouldNot(BeNil())
Ω(false).ShouldNot(BeNil())
Ω("").ShouldNot(BeNil())
})
})

View File

@@ -2,8 +2,9 @@ package matchers
import (
"fmt"
"github.com/onsi/gomega/format"
"math"
"github.com/onsi/gomega/format"
)
type BeNumericallyMatcher struct {

View File

@@ -1,148 +0,0 @@
package matchers_test
import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
. "github.com/onsi/gomega/matchers"
)
var _ = Describe("BeNumerically", func() {
Context("when passed a number", func() {
It("should support ==", func() {
Ω(uint32(5)).Should(BeNumerically("==", 5))
Ω(float64(5.0)).Should(BeNumerically("==", 5))
Ω(int8(5)).Should(BeNumerically("==", 5))
})
It("should not have false positives", func() {
Ω(5.1).ShouldNot(BeNumerically("==", 5))
Ω(5).ShouldNot(BeNumerically("==", 5.1))
})
It("should support >", func() {
Ω(uint32(5)).Should(BeNumerically(">", 4))
Ω(float64(5.0)).Should(BeNumerically(">", 4.9))
Ω(int8(5)).Should(BeNumerically(">", 4))
Ω(uint32(5)).ShouldNot(BeNumerically(">", 5))
Ω(float64(5.0)).ShouldNot(BeNumerically(">", 5.0))
Ω(int8(5)).ShouldNot(BeNumerically(">", 5))
})
It("should support <", func() {
Ω(uint32(5)).Should(BeNumerically("<", 6))
Ω(float64(5.0)).Should(BeNumerically("<", 5.1))
Ω(int8(5)).Should(BeNumerically("<", 6))
Ω(uint32(5)).ShouldNot(BeNumerically("<", 5))
Ω(float64(5.0)).ShouldNot(BeNumerically("<", 5.0))
Ω(int8(5)).ShouldNot(BeNumerically("<", 5))
})
It("should support >=", func() {
Ω(uint32(5)).Should(BeNumerically(">=", 4))
Ω(float64(5.0)).Should(BeNumerically(">=", 4.9))
Ω(int8(5)).Should(BeNumerically(">=", 4))
Ω(uint32(5)).Should(BeNumerically(">=", 5))
Ω(float64(5.0)).Should(BeNumerically(">=", 5.0))
Ω(int8(5)).Should(BeNumerically(">=", 5))
Ω(uint32(5)).ShouldNot(BeNumerically(">=", 6))
Ω(float64(5.0)).ShouldNot(BeNumerically(">=", 5.1))
Ω(int8(5)).ShouldNot(BeNumerically(">=", 6))
})
It("should support <=", func() {
Ω(uint32(5)).Should(BeNumerically("<=", 6))
Ω(float64(5.0)).Should(BeNumerically("<=", 5.1))
Ω(int8(5)).Should(BeNumerically("<=", 6))
Ω(uint32(5)).Should(BeNumerically("<=", 5))
Ω(float64(5.0)).Should(BeNumerically("<=", 5.0))
Ω(int8(5)).Should(BeNumerically("<=", 5))
Ω(uint32(5)).ShouldNot(BeNumerically("<=", 4))
Ω(float64(5.0)).ShouldNot(BeNumerically("<=", 4.9))
Ω(int8(5)).Should(BeNumerically("<=", 5))
})
Context("when passed ~", func() {
Context("when passed a float", func() {
Context("and there is no precision parameter", func() {
It("should default to 1e-8", func() {
Ω(5.00000001).Should(BeNumerically("~", 5.00000002))
Ω(5.00000001).ShouldNot(BeNumerically("~", 5.0000001))
})
})
Context("and there is a precision parameter", func() {
It("should use the precision parameter", func() {
Ω(5.1).Should(BeNumerically("~", 5.19, 0.1))
Ω(5.1).Should(BeNumerically("~", 5.01, 0.1))
Ω(5.1).ShouldNot(BeNumerically("~", 5.22, 0.1))
Ω(5.1).ShouldNot(BeNumerically("~", 4.98, 0.1))
})
})
})
Context("when passed an int/uint", func() {
Context("and there is no precision parameter", func() {
It("should just do strict equality", func() {
Ω(5).Should(BeNumerically("~", 5))
Ω(5).ShouldNot(BeNumerically("~", 6))
Ω(uint(5)).ShouldNot(BeNumerically("~", 6))
})
})
Context("and there is a precision parameter", func() {
It("should use precision paramter", func() {
Ω(5).Should(BeNumerically("~", 6, 2))
Ω(5).ShouldNot(BeNumerically("~", 8, 2))
Ω(uint(5)).Should(BeNumerically("~", 6, 1))
})
})
})
})
})
Context("when passed a non-number", func() {
It("should error", func() {
success, err := (&BeNumericallyMatcher{Comparator: "==", CompareTo: []interface{}{5}}).Match("foo")
Ω(success).Should(BeFalse())
Ω(err).Should(HaveOccurred())
success, err = (&BeNumericallyMatcher{Comparator: "=="}).Match(5)
Ω(success).Should(BeFalse())
Ω(err).Should(HaveOccurred())
success, err = (&BeNumericallyMatcher{Comparator: "~", CompareTo: []interface{}{3.0, "foo"}}).Match(5.0)
Ω(success).Should(BeFalse())
Ω(err).Should(HaveOccurred())
success, err = (&BeNumericallyMatcher{Comparator: "==", CompareTo: []interface{}{"bar"}}).Match(5)
Ω(success).Should(BeFalse())
Ω(err).Should(HaveOccurred())
success, err = (&BeNumericallyMatcher{Comparator: "==", CompareTo: []interface{}{"bar"}}).Match("foo")
Ω(success).Should(BeFalse())
Ω(err).Should(HaveOccurred())
success, err = (&BeNumericallyMatcher{Comparator: "==", CompareTo: []interface{}{nil}}).Match(0)
Ω(success).Should(BeFalse())
Ω(err).Should(HaveOccurred())
success, err = (&BeNumericallyMatcher{Comparator: "==", CompareTo: []interface{}{0}}).Match(nil)
Ω(success).Should(BeFalse())
Ω(err).Should(HaveOccurred())
})
})
Context("when passed an unsupported comparator", func() {
It("should error", func() {
success, err := (&BeNumericallyMatcher{Comparator: "!=", CompareTo: []interface{}{5}}).Match(4)
Ω(success).Should(BeFalse())
Ω(err).Should(HaveOccurred())
})
})
})

View File

@@ -1,106 +0,0 @@
package matchers_test
import (
. "github.com/onsi/gomega/matchers"
"time"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
var _ = Describe("BeSent", func() {
Context("when passed a channel and a matching type", func() {
Context("when the channel is ready to receive", func() {
It("should succeed and send the value down the channel", func() {
c := make(chan string)
d := make(chan string)
go func() {
val := <-c
d <- val
}()
time.Sleep(10 * time.Millisecond)
Ω(c).Should(BeSent("foo"))
Eventually(d).Should(Receive(Equal("foo")))
})
It("should succeed (with a buffered channel)", func() {
c := make(chan string, 1)
Ω(c).Should(BeSent("foo"))
Ω(<-c).Should(Equal("foo"))
})
})
Context("when the channel is not ready to receive", func() {
It("should fail and not send down the channel", func() {
c := make(chan string)
Ω(c).ShouldNot(BeSent("foo"))
Consistently(c).ShouldNot(Receive())
})
})
Context("when the channel is eventually ready to receive", func() {
It("should succeed", func() {
c := make(chan string)
d := make(chan string)
go func() {
time.Sleep(30 * time.Millisecond)
val := <-c
d <- val
}()
Eventually(c).Should(BeSent("foo"))
Eventually(d).Should(Receive(Equal("foo")))
})
})
Context("when the channel is closed", func() {
It("should error", func() {
c := make(chan string)
close(c)
success, err := (&BeSentMatcher{Arg: "foo"}).Match(c)
Ω(success).Should(BeFalse())
Ω(err).Should(HaveOccurred())
})
It("should short-circuit Eventually", func() {
c := make(chan string)
close(c)
t := time.Now()
failures := InterceptGomegaFailures(func() {
Eventually(c, 10.0).Should(BeSent("foo"))
})
Ω(failures).Should(HaveLen(1))
Ω(time.Since(t)).Should(BeNumerically("<", time.Second))
})
})
})
Context("when passed a channel and a non-matching type", func() {
It("should error", func() {
success, err := (&BeSentMatcher{Arg: "foo"}).Match(make(chan int, 1))
Ω(success).Should(BeFalse())
Ω(err).Should(HaveOccurred())
})
})
Context("when passed a receive-only channel", func() {
It("should error", func() {
var c <-chan string
c = make(chan string, 1)
success, err := (&BeSentMatcher{Arg: "foo"}).Match(c)
Ω(success).Should(BeFalse())
Ω(err).Should(HaveOccurred())
})
})
Context("when passed a nonchannel", func() {
It("should error", func() {
success, err := (&BeSentMatcher{Arg: "foo"}).Match("bar")
Ω(success).Should(BeFalse())
Ω(err).Should(HaveOccurred())
})
})
})

View File

@@ -1,98 +0,0 @@
package matchers_test
import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
. "github.com/onsi/gomega/matchers"
"time"
)
var _ = Describe("BeTemporally", func() {
var t0, t1, t2 time.Time
BeforeEach(func() {
t0 = time.Now()
t1 = t0.Add(time.Second)
t2 = t0.Add(-time.Second)
})
Context("When comparing times", func() {
It("should support ==", func() {
Ω(t0).Should(BeTemporally("==", t0))
Ω(t1).ShouldNot(BeTemporally("==", t0))
Ω(t0).ShouldNot(BeTemporally("==", t1))
Ω(t0).ShouldNot(BeTemporally("==", time.Time{}))
})
It("should support >", func() {
Ω(t0).Should(BeTemporally(">", t2))
Ω(t0).ShouldNot(BeTemporally(">", t0))
Ω(t2).ShouldNot(BeTemporally(">", t0))
})
It("should support <", func() {
Ω(t0).Should(BeTemporally("<", t1))
Ω(t0).ShouldNot(BeTemporally("<", t0))
Ω(t1).ShouldNot(BeTemporally("<", t0))
})
It("should support >=", func() {
Ω(t0).Should(BeTemporally(">=", t2))
Ω(t0).Should(BeTemporally(">=", t0))
Ω(t0).ShouldNot(BeTemporally(">=", t1))
})
It("should support <=", func() {
Ω(t0).Should(BeTemporally("<=", t1))
Ω(t0).Should(BeTemporally("<=", t0))
Ω(t0).ShouldNot(BeTemporally("<=", t2))
})
Context("when passed ~", func() {
Context("and there is no precision parameter", func() {
BeforeEach(func() {
t1 = t0.Add(time.Millisecond / 2)
t2 = t0.Add(-2 * time.Millisecond)
})
It("should approximate", func() {
Ω(t0).Should(BeTemporally("~", t0))
Ω(t0).Should(BeTemporally("~", t1))
Ω(t0).ShouldNot(BeTemporally("~", t2))
})
})
Context("and there is a precision parameter", func() {
BeforeEach(func() {
t2 = t0.Add(3 * time.Second)
})
It("should use precision paramter", func() {
d := 2 * time.Second
Ω(t0).Should(BeTemporally("~", t0, d))
Ω(t0).Should(BeTemporally("~", t1, d))
Ω(t0).ShouldNot(BeTemporally("~", t2, d))
})
})
})
})
Context("when passed a non-time", func() {
It("should error", func() {
success, err := (&BeTemporallyMatcher{Comparator: "==", CompareTo: t0}).Match("foo")
Ω(success).Should(BeFalse())
Ω(err).Should(HaveOccurred())
success, err = (&BeTemporallyMatcher{Comparator: "=="}).Match(nil)
Ω(success).Should(BeFalse())
Ω(err).Should(HaveOccurred())
})
})
Context("when passed an unsupported comparator", func() {
It("should error", func() {
success, err := (&BeTemporallyMatcher{Comparator: "!=", CompareTo: t0}).Match(t2)
Ω(success).Should(BeFalse())
Ω(err).Should(HaveOccurred())
})
})
})

View File

@@ -1,20 +0,0 @@
package matchers_test
import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
. "github.com/onsi/gomega/matchers"
)
var _ = Describe("BeTrue", func() {
It("should handle true and false correctly", func() {
Ω(true).Should(BeTrue())
Ω(false).ShouldNot(BeTrue())
})
It("should only support booleans", func() {
success, err := (&BeTrueMatcher{}).Match("foo")
Ω(success).Should(BeFalse())
Ω(err).Should(HaveOccurred())
})
})

View File

@@ -1,30 +0,0 @@
package matchers_test
import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
var _ = Describe("BeZero", func() {
It("should succeed if the passed in object is the zero value for its type", func() {
Ω(nil).Should(BeZero())
Ω("").Should(BeZero())
Ω(" ").ShouldNot(BeZero())
Ω(0).Should(BeZero())
Ω(1).ShouldNot(BeZero())
Ω(0.0).Should(BeZero())
Ω(0.1).ShouldNot(BeZero())
// Ω([]int{}).Should(BeZero())
Ω([]int{1}).ShouldNot(BeZero())
// Ω(map[string]int{}).Should(BeZero())
Ω(map[string]int{"a": 1}).ShouldNot(BeZero())
Ω(myCustomType{}).Should(BeZero())
Ω(myCustomType{s: "a"}).ShouldNot(BeZero())
})
})

View File

@@ -1,75 +0,0 @@
package matchers_test
import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
var _ = Describe("ConsistOf", func() {
Context("with a slice", func() {
It("should do the right thing", func() {
Ω([]string{"foo", "bar", "baz"}).Should(ConsistOf("foo", "bar", "baz"))
Ω([]string{"foo", "bar", "baz"}).Should(ConsistOf("foo", "bar", "baz"))
Ω([]string{"foo", "bar", "baz"}).Should(ConsistOf("baz", "bar", "foo"))
Ω([]string{"foo", "bar", "baz"}).ShouldNot(ConsistOf("baz", "bar", "foo", "foo"))
Ω([]string{"foo", "bar", "baz"}).ShouldNot(ConsistOf("baz", "foo"))
})
})
Context("with an array", func() {
It("should do the right thing", func() {
Ω([3]string{"foo", "bar", "baz"}).Should(ConsistOf("foo", "bar", "baz"))
Ω([3]string{"foo", "bar", "baz"}).Should(ConsistOf("baz", "bar", "foo"))
Ω([3]string{"foo", "bar", "baz"}).ShouldNot(ConsistOf("baz", "bar", "foo", "foo"))
Ω([3]string{"foo", "bar", "baz"}).ShouldNot(ConsistOf("baz", "foo"))
})
})
Context("with a map", func() {
It("should apply to the values", func() {
Ω(map[int]string{1: "foo", 2: "bar", 3: "baz"}).Should(ConsistOf("foo", "bar", "baz"))
Ω(map[int]string{1: "foo", 2: "bar", 3: "baz"}).Should(ConsistOf("baz", "bar", "foo"))
Ω(map[int]string{1: "foo", 2: "bar", 3: "baz"}).ShouldNot(ConsistOf("baz", "bar", "foo", "foo"))
Ω(map[int]string{1: "foo", 2: "bar", 3: "baz"}).ShouldNot(ConsistOf("baz", "foo"))
})
})
Context("with anything else", func() {
It("should error", func() {
failures := InterceptGomegaFailures(func() {
Ω("foo").Should(ConsistOf("f", "o", "o"))
})
Ω(failures).Should(HaveLen(1))
})
})
Context("when passed matchers", func() {
It("should pass if the matchers pass", func() {
Ω([]string{"foo", "bar", "baz"}).Should(ConsistOf("foo", MatchRegexp("^ba"), "baz"))
Ω([]string{"foo", "bar", "baz"}).ShouldNot(ConsistOf("foo", MatchRegexp("^ba")))
Ω([]string{"foo", "bar", "baz"}).ShouldNot(ConsistOf("foo", MatchRegexp("^ba"), MatchRegexp("foo")))
Ω([]string{"foo", "bar", "baz"}).Should(ConsistOf("foo", MatchRegexp("^ba"), MatchRegexp("^ba")))
Ω([]string{"foo", "bar", "baz"}).ShouldNot(ConsistOf("foo", MatchRegexp("^ba"), MatchRegexp("turducken")))
})
It("should not depend on the order of the matchers", func() {
Ω([][]int{[]int{1, 2}, []int{2}}).Should(ConsistOf(ContainElement(1), ContainElement(2)))
Ω([][]int{[]int{1, 2}, []int{2}}).Should(ConsistOf(ContainElement(2), ContainElement(1)))
})
Context("when a matcher errors", func() {
It("should soldier on", func() {
Ω([]string{"foo", "bar", "baz"}).ShouldNot(ConsistOf(BeFalse(), "foo", "bar"))
Ω([]interface{}{"foo", "bar", false}).Should(ConsistOf(BeFalse(), ContainSubstring("foo"), "bar"))
})
})
})
Context("when passed exactly one argument, and that argument is a slice", func() {
It("should match against the elements of that argument", func() {
Ω([]string{"foo", "bar", "baz"}).Should(ConsistOf([]string{"foo", "bar", "baz"}))
})
})
})

View File

@@ -1,76 +0,0 @@
package matchers_test
import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
. "github.com/onsi/gomega/matchers"
)
var _ = Describe("ContainElement", func() {
Context("when passed a supported type", func() {
Context("and expecting a non-matcher", func() {
It("should do the right thing", func() {
Ω([2]int{1, 2}).Should(ContainElement(2))
Ω([2]int{1, 2}).ShouldNot(ContainElement(3))
Ω([]int{1, 2}).Should(ContainElement(2))
Ω([]int{1, 2}).ShouldNot(ContainElement(3))
Ω(map[string]int{"foo": 1, "bar": 2}).Should(ContainElement(2))
Ω(map[int]int{3: 1, 4: 2}).ShouldNot(ContainElement(3))
arr := make([]myCustomType, 2)
arr[0] = myCustomType{s: "foo", n: 3, f: 2.0, arr: []string{"a", "b"}}
arr[1] = myCustomType{s: "foo", n: 3, f: 2.0, arr: []string{"a", "c"}}
Ω(arr).Should(ContainElement(myCustomType{s: "foo", n: 3, f: 2.0, arr: []string{"a", "b"}}))
Ω(arr).ShouldNot(ContainElement(myCustomType{s: "foo", n: 3, f: 2.0, arr: []string{"b", "c"}}))
})
})
Context("and expecting a matcher", func() {
It("should pass each element through the matcher", func() {
Ω([]int{1, 2, 3}).Should(ContainElement(BeNumerically(">=", 3)))
Ω([]int{1, 2, 3}).ShouldNot(ContainElement(BeNumerically(">", 3)))
Ω(map[string]int{"foo": 1, "bar": 2}).Should(ContainElement(BeNumerically(">=", 2)))
Ω(map[string]int{"foo": 1, "bar": 2}).ShouldNot(ContainElement(BeNumerically(">", 2)))
})
It("should power through even if the matcher ever fails", func() {
Ω([]interface{}{1, 2, "3", 4}).Should(ContainElement(BeNumerically(">=", 3)))
})
It("should fail if the matcher fails", func() {
actual := []interface{}{1, 2, "3", "4"}
success, err := (&ContainElementMatcher{Element: BeNumerically(">=", 3)}).Match(actual)
Ω(success).Should(BeFalse())
Ω(err).Should(HaveOccurred())
})
})
})
Context("when passed a correctly typed nil", func() {
It("should operate succesfully on the passed in value", func() {
var nilSlice []int
Ω(nilSlice).ShouldNot(ContainElement(1))
var nilMap map[int]string
Ω(nilMap).ShouldNot(ContainElement("foo"))
})
})
Context("when passed an unsupported type", func() {
It("should error", func() {
success, err := (&ContainElementMatcher{Element: 0}).Match(0)
Ω(success).Should(BeFalse())
Ω(err).Should(HaveOccurred())
success, err = (&ContainElementMatcher{Element: 0}).Match("abc")
Ω(success).Should(BeFalse())
Ω(err).Should(HaveOccurred())
success, err = (&ContainElementMatcher{Element: 0}).Match(nil)
Ω(success).Should(BeFalse())
Ω(err).Should(HaveOccurred())
})
})
})

View File

@@ -1,36 +0,0 @@
package matchers_test
import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
. "github.com/onsi/gomega/matchers"
)
var _ = Describe("ContainSubstringMatcher", func() {
Context("when actual is a string", func() {
It("should match against the string", func() {
Ω("Marvelous").Should(ContainSubstring("rve"))
Ω("Marvelous").ShouldNot(ContainSubstring("boo"))
})
})
Context("when the matcher is called with multiple arguments", func() {
It("should pass the string and arguments to sprintf", func() {
Ω("Marvelous3").Should(ContainSubstring("velous%d", 3))
})
})
Context("when actual is a stringer", func() {
It("should call the stringer and match agains the returned string", func() {
Ω(&myStringer{a: "Abc3"}).Should(ContainSubstring("bc3"))
})
})
Context("when actual is neither a string nor a stringer", func() {
It("should error", func() {
success, err := (&ContainSubstringMatcher{Substr: "2"}).Match(2)
Ω(success).Should(BeFalse())
Ω(err).Should(HaveOccurred())
})
})
})

View File

@@ -19,6 +19,12 @@ func (matcher *EqualMatcher) Match(actual interface{}) (success bool, err error)
}
func (matcher *EqualMatcher) FailureMessage(actual interface{}) (message string) {
actualString, actualOK := actual.(string)
expectedString, expectedOK := matcher.Expected.(string)
if actualOK && expectedOK {
return format.MessageWithDiff(actualString, "to equal", expectedString)
}
return format.Message(actual, "to equal", matcher.Expected)
}

View File

@@ -1,44 +0,0 @@
package matchers_test
import (
"errors"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
. "github.com/onsi/gomega/matchers"
)
var _ = Describe("Equal", func() {
Context("when asserting that nil equals nil", func() {
It("should error", func() {
success, err := (&EqualMatcher{Expected: nil}).Match(nil)
Ω(success).Should(BeFalse())
Ω(err).Should(HaveOccurred())
})
})
Context("When asserting equality between objects", func() {
It("should do the right thing", func() {
Ω(5).Should(Equal(5))
Ω(5.0).Should(Equal(5.0))
Ω(5).ShouldNot(Equal("5"))
Ω(5).ShouldNot(Equal(5.0))
Ω(5).ShouldNot(Equal(3))
Ω("5").Should(Equal("5"))
Ω([]int{1, 2}).Should(Equal([]int{1, 2}))
Ω([]int{1, 2}).ShouldNot(Equal([]int{2, 1}))
Ω(map[string]string{"a": "b", "c": "d"}).Should(Equal(map[string]string{"a": "b", "c": "d"}))
Ω(map[string]string{"a": "b", "c": "d"}).ShouldNot(Equal(map[string]string{"a": "b", "c": "e"}))
Ω(errors.New("foo")).Should(Equal(errors.New("foo")))
Ω(errors.New("foo")).ShouldNot(Equal(errors.New("bar")))
Ω(myCustomType{s: "foo", n: 3, f: 2.0, arr: []string{"a", "b"}}).Should(Equal(myCustomType{s: "foo", n: 3, f: 2.0, arr: []string{"a", "b"}}))
Ω(myCustomType{s: "foo", n: 3, f: 2.0, arr: []string{"a", "b"}}).ShouldNot(Equal(myCustomType{s: "bar", n: 3, f: 2.0, arr: []string{"a", "b"}}))
Ω(myCustomType{s: "foo", n: 3, f: 2.0, arr: []string{"a", "b"}}).ShouldNot(Equal(myCustomType{s: "foo", n: 2, f: 2.0, arr: []string{"a", "b"}}))
Ω(myCustomType{s: "foo", n: 3, f: 2.0, arr: []string{"a", "b"}}).ShouldNot(Equal(myCustomType{s: "foo", n: 3, f: 3.0, arr: []string{"a", "b"}}))
Ω(myCustomType{s: "foo", n: 3, f: 2.0, arr: []string{"a", "b"}}).ShouldNot(Equal(myCustomType{s: "foo", n: 3, f: 2.0, arr: []string{"a", "b", "c"}}))
})
})
})

View File

@@ -1,50 +0,0 @@
package matchers_test
import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
. "github.com/onsi/gomega/matchers"
)
var _ = Describe("HaveCap", func() {
Context("when passed a supported type", func() {
It("should do the right thing", func() {
Ω([0]int{}).Should(HaveCap(0))
Ω([2]int{1}).Should(HaveCap(2))
Ω([]int{}).Should(HaveCap(0))
Ω([]int{1, 2, 3, 4, 5}[:2]).Should(HaveCap(5))
Ω(make([]int, 0, 5)).Should(HaveCap(5))
c := make(chan bool, 3)
Ω(c).Should(HaveCap(3))
c <- true
c <- true
Ω(c).Should(HaveCap(3))
Ω(make(chan bool)).Should(HaveCap(0))
})
})
Context("when passed a correctly typed nil", func() {
It("should operate succesfully on the passed in value", func() {
var nilSlice []int
Ω(nilSlice).Should(HaveCap(0))
var nilChan chan int
Ω(nilChan).Should(HaveCap(0))
})
})
Context("when passed an unsupported type", func() {
It("should error", func() {
success, err := (&HaveCapMatcher{Count: 0}).Match(0)
Ω(success).Should(BeFalse())
Ω(err).Should(HaveOccurred())
success, err = (&HaveCapMatcher{Count: 0}).Match(nil)
Ω(success).Should(BeFalse())
Ω(err).Should(HaveOccurred())
})
})
})

View File

@@ -1,73 +0,0 @@
package matchers_test
import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
. "github.com/onsi/gomega/matchers"
)
var _ = Describe("HaveKey", func() {
var (
stringKeys map[string]int
intKeys map[int]string
objKeys map[*myCustomType]string
customA *myCustomType
customB *myCustomType
)
BeforeEach(func() {
stringKeys = map[string]int{"foo": 2, "bar": 3}
intKeys = map[int]string{2: "foo", 3: "bar"}
customA = &myCustomType{s: "a", n: 2, f: 2.3, arr: []string{"ice", "cream"}}
customB = &myCustomType{s: "b", n: 4, f: 3.1, arr: []string{"cake"}}
objKeys = map[*myCustomType]string{customA: "aardvark", customB: "kangaroo"}
})
Context("when passed a map", func() {
It("should do the right thing", func() {
Ω(stringKeys).Should(HaveKey("foo"))
Ω(stringKeys).ShouldNot(HaveKey("baz"))
Ω(intKeys).Should(HaveKey(2))
Ω(intKeys).ShouldNot(HaveKey(4))
Ω(objKeys).Should(HaveKey(customA))
Ω(objKeys).Should(HaveKey(&myCustomType{s: "b", n: 4, f: 3.1, arr: []string{"cake"}}))
Ω(objKeys).ShouldNot(HaveKey(&myCustomType{s: "b", n: 4, f: 3.1, arr: []string{"apple", "pie"}}))
})
})
Context("when passed a correctly typed nil", func() {
It("should operate succesfully on the passed in value", func() {
var nilMap map[int]string
Ω(nilMap).ShouldNot(HaveKey("foo"))
})
})
Context("when the passed in key is actually a matcher", func() {
It("should pass each element through the matcher", func() {
Ω(stringKeys).Should(HaveKey(ContainSubstring("oo")))
Ω(stringKeys).ShouldNot(HaveKey(ContainSubstring("foobar")))
})
It("should fail if the matcher ever fails", func() {
actual := map[int]string{1: "a", 3: "b", 2: "c"}
success, err := (&HaveKeyMatcher{Key: ContainSubstring("ar")}).Match(actual)
Ω(success).Should(BeFalse())
Ω(err).Should(HaveOccurred())
})
})
Context("when passed something that is not a map", func() {
It("should error", func() {
success, err := (&HaveKeyMatcher{Key: "foo"}).Match([]string{"foo"})
Ω(success).Should(BeFalse())
Ω(err).Should(HaveOccurred())
success, err = (&HaveKeyMatcher{Key: "foo"}).Match(nil)
Ω(success).Should(BeFalse())
Ω(err).Should(HaveOccurred())
})
})
})

View File

@@ -1,82 +0,0 @@
package matchers_test
import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
. "github.com/onsi/gomega/matchers"
)
var _ = Describe("HaveKeyWithValue", func() {
var (
stringKeys map[string]int
intKeys map[int]string
objKeys map[*myCustomType]*myCustomType
customA *myCustomType
customB *myCustomType
)
BeforeEach(func() {
stringKeys = map[string]int{"foo": 2, "bar": 3}
intKeys = map[int]string{2: "foo", 3: "bar"}
customA = &myCustomType{s: "a", n: 2, f: 2.3, arr: []string{"ice", "cream"}}
customB = &myCustomType{s: "b", n: 4, f: 3.1, arr: []string{"cake"}}
objKeys = map[*myCustomType]*myCustomType{customA: customA, customB: customA}
})
Context("when passed a map", func() {
It("should do the right thing", func() {
Ω(stringKeys).Should(HaveKeyWithValue("foo", 2))
Ω(stringKeys).ShouldNot(HaveKeyWithValue("foo", 1))
Ω(stringKeys).ShouldNot(HaveKeyWithValue("baz", 2))
Ω(stringKeys).ShouldNot(HaveKeyWithValue("baz", 1))
Ω(intKeys).Should(HaveKeyWithValue(2, "foo"))
Ω(intKeys).ShouldNot(HaveKeyWithValue(4, "foo"))
Ω(intKeys).ShouldNot(HaveKeyWithValue(2, "baz"))
Ω(objKeys).Should(HaveKeyWithValue(customA, customA))
Ω(objKeys).Should(HaveKeyWithValue(&myCustomType{s: "b", n: 4, f: 3.1, arr: []string{"cake"}}, &myCustomType{s: "a", n: 2, f: 2.3, arr: []string{"ice", "cream"}}))
Ω(objKeys).ShouldNot(HaveKeyWithValue(&myCustomType{s: "b", n: 4, f: 3.1, arr: []string{"apple", "pie"}}, customA))
})
})
Context("when passed a correctly typed nil", func() {
It("should operate succesfully on the passed in value", func() {
var nilMap map[int]string
Ω(nilMap).ShouldNot(HaveKeyWithValue("foo", "bar"))
})
})
Context("when the passed in key or value is actually a matcher", func() {
It("should pass each element through the matcher", func() {
Ω(stringKeys).Should(HaveKeyWithValue(ContainSubstring("oo"), 2))
Ω(intKeys).Should(HaveKeyWithValue(2, ContainSubstring("oo")))
Ω(stringKeys).ShouldNot(HaveKeyWithValue(ContainSubstring("foobar"), 2))
})
It("should fail if the matcher ever fails", func() {
actual := map[int]string{1: "a", 3: "b", 2: "c"}
success, err := (&HaveKeyWithValueMatcher{Key: ContainSubstring("ar"), Value: 2}).Match(actual)
Ω(success).Should(BeFalse())
Ω(err).Should(HaveOccurred())
otherActual := map[string]int{"a": 1, "b": 2, "c": 3}
success, err = (&HaveKeyWithValueMatcher{Key: "a", Value: ContainSubstring("1")}).Match(otherActual)
Ω(success).Should(BeFalse())
Ω(err).Should(HaveOccurred())
})
})
Context("when passed something that is not a map", func() {
It("should error", func() {
success, err := (&HaveKeyWithValueMatcher{Key: "foo", Value: "bar"}).Match([]string{"foo"})
Ω(success).Should(BeFalse())
Ω(err).Should(HaveOccurred())
success, err = (&HaveKeyWithValueMatcher{Key: "foo", Value: "bar"}).Match(nil)
Ω(success).Should(BeFalse())
Ω(err).Should(HaveOccurred())
})
})
})

View File

@@ -1,53 +0,0 @@
package matchers_test
import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
. "github.com/onsi/gomega/matchers"
)
var _ = Describe("HaveLen", func() {
Context("when passed a supported type", func() {
It("should do the right thing", func() {
Ω("").Should(HaveLen(0))
Ω("AA").Should(HaveLen(2))
Ω([0]int{}).Should(HaveLen(0))
Ω([2]int{1, 2}).Should(HaveLen(2))
Ω([]int{}).Should(HaveLen(0))
Ω([]int{1, 2, 3}).Should(HaveLen(3))
Ω(map[string]int{}).Should(HaveLen(0))
Ω(map[string]int{"a": 1, "b": 2, "c": 3, "d": 4}).Should(HaveLen(4))
c := make(chan bool, 3)
Ω(c).Should(HaveLen(0))
c <- true
c <- true
Ω(c).Should(HaveLen(2))
})
})
Context("when passed a correctly typed nil", func() {
It("should operate succesfully on the passed in value", func() {
var nilSlice []int
Ω(nilSlice).Should(HaveLen(0))
var nilMap map[int]string
Ω(nilMap).Should(HaveLen(0))
})
})
Context("when passed an unsupported type", func() {
It("should error", func() {
success, err := (&HaveLenMatcher{Count: 0}).Match(0)
Ω(success).Should(BeFalse())
Ω(err).Should(HaveOccurred())
success, err = (&HaveLenMatcher{Count: 0}).Match(nil)
Ω(success).Should(BeFalse())
Ω(err).Should(HaveOccurred())
})
})
})

View File

@@ -1,58 +0,0 @@
package matchers_test
import (
"errors"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
. "github.com/onsi/gomega/matchers"
)
type CustomErr struct {
msg string
}
func (e *CustomErr) Error() string {
return e.msg
}
var _ = Describe("HaveOccurred", func() {
It("should succeed if matching an error", func() {
Ω(errors.New("Foo")).Should(HaveOccurred())
})
It("should not succeed with nil", func() {
Ω(nil).ShouldNot(HaveOccurred())
})
It("should only support errors and nil", func() {
success, err := (&HaveOccurredMatcher{}).Match("foo")
Ω(success).Should(BeFalse())
Ω(err).Should(HaveOccurred())
success, err = (&HaveOccurredMatcher{}).Match("")
Ω(success).Should(BeFalse())
Ω(err).Should(HaveOccurred())
})
It("doesn't support non-error type", func() {
success, err := (&HaveOccurredMatcher{}).Match(AnyType{})
Ω(success).Should(BeFalse())
Ω(err).Should(MatchError("Expected an error-type. Got:\n <matchers_test.AnyType>: {}"))
})
It("doesn't support non-error pointer type", func() {
success, err := (&HaveOccurredMatcher{}).Match(&AnyType{})
Ω(success).Should(BeFalse())
Ω(err).Should(MatchError(MatchRegexp(`Expected an error-type. Got:\n <*matchers_test.AnyType | 0x[[:xdigit:]]+>: {}`)))
})
It("should succeed with pointer types that conform to error interface", func() {
err := &CustomErr{"ohai"}
Ω(err).Should(HaveOccurred())
})
It("should not succeed with nil pointers to types that conform to error interface", func() {
var err *CustomErr = nil
Ω(err).ShouldNot(HaveOccurred())
})
})

View File

@@ -1,36 +0,0 @@
package matchers_test
import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
. "github.com/onsi/gomega/matchers"
)
var _ = Describe("HavePrefixMatcher", func() {
Context("when actual is a string", func() {
It("should match a string prefix", func() {
Ω("Ab").Should(HavePrefix("A"))
Ω("A").ShouldNot(HavePrefix("Ab"))
})
})
Context("when the matcher is called with multiple arguments", func() {
It("should pass the string and arguments to sprintf", func() {
Ω("C3PO").Should(HavePrefix("C%dP", 3))
})
})
Context("when actual is a stringer", func() {
It("should call the stringer and match against the returned string", func() {
Ω(&myStringer{a: "Ab"}).Should(HavePrefix("A"))
})
})
Context("when actual is neither a string nor a stringer", func() {
It("should error", func() {
success, err := (&HavePrefixMatcher{Prefix: "2"}).Match(2)
Ω(success).Should(BeFalse())
Ω(err).Should(HaveOccurred())
})
})
})

View File

@@ -1,36 +0,0 @@
package matchers_test
import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
. "github.com/onsi/gomega/matchers"
)
var _ = Describe("HaveSuffixMatcher", func() {
Context("when actual is a string", func() {
It("should match a string suffix", func() {
Ω("Ab").Should(HaveSuffix("b"))
Ω("A").ShouldNot(HaveSuffix("Ab"))
})
})
Context("when the matcher is called with multiple arguments", func() {
It("should pass the string and arguments to sprintf", func() {
Ω("C3PO").Should(HaveSuffix("%dPO", 3))
})
})
Context("when actual is a stringer", func() {
It("should call the stringer and match against the returned string", func() {
Ω(&myStringer{a: "Ab"}).Should(HaveSuffix("b"))
})
})
Context("when actual is neither a string nor a stringer", func() {
It("should error", func() {
success, err := (&HaveSuffixMatcher{Suffix: "2"}).Match(2)
Ω(success).Should(BeFalse())
Ω(err).Should(HaveOccurred())
})
})
})

View File

@@ -1,93 +0,0 @@
package matchers_test
import (
"errors"
"fmt"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
. "github.com/onsi/gomega/matchers"
)
type CustomError struct {
}
func (c CustomError) Error() string {
return "an error"
}
var _ = Describe("MatchErrorMatcher", func() {
Context("When asserting against an error", func() {
It("should succeed when matching with an error", func() {
err := errors.New("an error")
fmtErr := fmt.Errorf("an error")
customErr := CustomError{}
Ω(err).Should(MatchError(errors.New("an error")))
Ω(err).ShouldNot(MatchError(errors.New("another error")))
Ω(fmtErr).Should(MatchError(errors.New("an error")))
Ω(customErr).Should(MatchError(CustomError{}))
})
It("should succeed when matching with a string", func() {
err := errors.New("an error")
fmtErr := fmt.Errorf("an error")
customErr := CustomError{}
Ω(err).Should(MatchError("an error"))
Ω(err).ShouldNot(MatchError("another error"))
Ω(fmtErr).Should(MatchError("an error"))
Ω(customErr).Should(MatchError("an error"))
})
Context("when passed a matcher", func() {
It("should pass if the matcher passes against the error string", func() {
err := errors.New("error 123 abc")
Ω(err).Should(MatchError(MatchRegexp(`\d{3}`)))
})
It("should fail if the matcher fails against the error string", func() {
err := errors.New("no digits")
Ω(err).ShouldNot(MatchError(MatchRegexp(`\d`)))
})
})
It("should fail when passed anything else", func() {
actualErr := errors.New("an error")
_, err := (&MatchErrorMatcher{
Expected: []byte("an error"),
}).Match(actualErr)
Ω(err).Should(HaveOccurred())
_, err = (&MatchErrorMatcher{
Expected: 3,
}).Match(actualErr)
Ω(err).Should(HaveOccurred())
})
})
Context("when passed nil", func() {
It("should fail", func() {
_, err := (&MatchErrorMatcher{
Expected: "an error",
}).Match(nil)
Ω(err).Should(HaveOccurred())
})
})
Context("when passed a non-error", func() {
It("should fail", func() {
_, err := (&MatchErrorMatcher{
Expected: "an error",
}).Match("an error")
Ω(err).Should(HaveOccurred())
_, err = (&MatchErrorMatcher{
Expected: "an error",
}).Match(3)
Ω(err).Should(HaveOccurred())
})
})
})

View File

@@ -40,11 +40,13 @@ func (matcher *MatchJSONMatcher) NegatedFailureMessage(actual interface{}) (mess
}
func (matcher *MatchJSONMatcher) prettyPrint(actual interface{}) (actualFormatted, expectedFormatted string, err error) {
actualString, aok := toString(actual)
expectedString, eok := toString(matcher.JSONToMatch)
if !(aok && eok) {
return "", "", fmt.Errorf("MatchJSONMatcher matcher requires a string or stringer. Got:\n%s", format.Object(actual, 1))
actualString, ok := toString(actual)
if !ok {
return "", "", fmt.Errorf("MatchJSONMatcher matcher requires a string, stringer, or []byte. Got actual:\n%s", format.Object(actual, 1))
}
expectedString, ok := toString(matcher.JSONToMatch)
if !ok {
return "", "", fmt.Errorf("MatchJSONMatcher matcher requires a string, stringer, or []byte. Got expected:\n%s", format.Object(matcher.JSONToMatch, 1))
}
abuf := new(bytes.Buffer)

View File

@@ -1,65 +0,0 @@
package matchers_test
import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
. "github.com/onsi/gomega/matchers"
)
var _ = Describe("MatchJSONMatcher", func() {
Context("When passed stringifiables", func() {
It("should succeed if the JSON matches", func() {
Ω("{}").Should(MatchJSON("{}"))
Ω(`{"a":1}`).Should(MatchJSON(`{"a":1}`))
Ω(`{
"a":1
}`).Should(MatchJSON(`{"a":1}`))
Ω(`{"a":1, "b":2}`).Should(MatchJSON(`{"b":2, "a":1}`))
Ω(`{"a":1}`).ShouldNot(MatchJSON(`{"b":2, "a":1}`))
})
It("should work with byte arrays", func() {
Ω([]byte("{}")).Should(MatchJSON([]byte("{}")))
Ω("{}").Should(MatchJSON([]byte("{}")))
Ω([]byte("{}")).Should(MatchJSON("{}"))
})
})
Context("when the expected is not valid JSON", func() {
It("should error and explain why", func() {
success, err := (&MatchJSONMatcher{JSONToMatch: `{}`}).Match(`oops`)
Ω(success).Should(BeFalse())
Ω(err).Should(HaveOccurred())
Ω(err.Error()).Should(ContainSubstring("Actual 'oops' should be valid JSON"))
})
})
Context("when the actual is not valid JSON", func() {
It("should error and explain why", func() {
success, err := (&MatchJSONMatcher{JSONToMatch: `oops`}).Match(`{}`)
Ω(success).Should(BeFalse())
Ω(err).Should(HaveOccurred())
Ω(err.Error()).Should(ContainSubstring("Expected 'oops' should be valid JSON"))
})
})
Context("when either side is neither a string nor a stringer", func() {
It("should error", func() {
success, err := (&MatchJSONMatcher{JSONToMatch: "{}"}).Match(2)
Ω(success).Should(BeFalse())
Ω(err).Should(HaveOccurred())
success, err = (&MatchJSONMatcher{JSONToMatch: 2}).Match("{}")
Ω(success).Should(BeFalse())
Ω(err).Should(HaveOccurred())
success, err = (&MatchJSONMatcher{JSONToMatch: nil}).Match("{}")
Ω(success).Should(BeFalse())
Ω(err).Should(HaveOccurred())
success, err = (&MatchJSONMatcher{JSONToMatch: 2}).Match(nil)
Ω(success).Should(BeFalse())
Ω(err).Should(HaveOccurred())
})
})
})

View File

@@ -1,44 +0,0 @@
package matchers_test
import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
. "github.com/onsi/gomega/matchers"
)
var _ = Describe("MatchRegexp", func() {
Context("when actual is a string", func() {
It("should match against the string", func() {
Ω(" a2!bla").Should(MatchRegexp(`\d!`))
Ω(" a2!bla").ShouldNot(MatchRegexp(`[A-Z]`))
})
})
Context("when actual is a stringer", func() {
It("should call the stringer and match agains the returned string", func() {
Ω(&myStringer{a: "Abc3"}).Should(MatchRegexp(`[A-Z][a-z]+\d`))
})
})
Context("when the matcher is called with multiple arguments", func() {
It("should pass the string and arguments to sprintf", func() {
Ω(" a23!bla").Should(MatchRegexp(`\d%d!`, 3))
})
})
Context("when actual is neither a string nor a stringer", func() {
It("should error", func() {
success, err := (&MatchRegexpMatcher{Regexp: `\d`}).Match(2)
Ω(success).Should(BeFalse())
Ω(err).Should(HaveOccurred())
})
})
Context("when the passed in regexp fails to compile", func() {
It("should error", func() {
success, err := (&MatchRegexpMatcher{Regexp: "("}).Match("Foo")
Ω(success).Should(BeFalse())
Ω(err).Should(HaveOccurred())
})
})
})

View File

@@ -0,0 +1,74 @@
package matchers
import (
"fmt"
"reflect"
"strings"
"github.com/onsi/gomega/format"
"gopkg.in/yaml.v2"
)
type MatchYAMLMatcher struct {
YAMLToMatch interface{}
}
func (matcher *MatchYAMLMatcher) Match(actual interface{}) (success bool, err error) {
actualString, expectedString, err := matcher.toStrings(actual)
if err != nil {
return false, err
}
var aval interface{}
var eval interface{}
if err := yaml.Unmarshal([]byte(actualString), &aval); err != nil {
return false, fmt.Errorf("Actual '%s' should be valid YAML, but it is not.\nUnderlying error:%s", actualString, err)
}
if err := yaml.Unmarshal([]byte(expectedString), &eval); err != nil {
return false, fmt.Errorf("Expected '%s' should be valid YAML, but it is not.\nUnderlying error:%s", expectedString, err)
}
return reflect.DeepEqual(aval, eval), nil
}
func (matcher *MatchYAMLMatcher) FailureMessage(actual interface{}) (message string) {
actualString, expectedString, _ := matcher.toNormalisedStrings(actual)
return format.Message(actualString, "to match YAML of", expectedString)
}
func (matcher *MatchYAMLMatcher) NegatedFailureMessage(actual interface{}) (message string) {
actualString, expectedString, _ := matcher.toNormalisedStrings(actual)
return format.Message(actualString, "not to match YAML of", expectedString)
}
func (matcher *MatchYAMLMatcher) toNormalisedStrings(actual interface{}) (actualFormatted, expectedFormatted string, err error) {
actualString, expectedString, err := matcher.toStrings(actual)
return normalise(actualString), normalise(expectedString), err
}
func normalise(input string) string {
var val interface{}
err := yaml.Unmarshal([]byte(input), &val)
if err != nil {
panic(err) // guarded by Match
}
output, err := yaml.Marshal(val)
if err != nil {
panic(err) // guarded by Unmarshal
}
return strings.TrimSpace(string(output))
}
func (matcher *MatchYAMLMatcher) toStrings(actual interface{}) (actualFormatted, expectedFormatted string, err error) {
actualString, ok := toString(actual)
if !ok {
return "", "", fmt.Errorf("MatchYAMLMatcher matcher requires a string, stringer, or []byte. Got actual:\n%s", format.Object(actual, 1))
}
expectedString, ok := toString(matcher.YAMLToMatch)
if !ok {
return "", "", fmt.Errorf("MatchYAMLMatcher matcher requires a string, stringer, or []byte. Got expected:\n%s", format.Object(matcher.YAMLToMatch, 1))
}
return actualString, expectedString, nil
}

View File

@@ -1,30 +0,0 @@
package matchers_test
import (
"testing"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
type myStringer struct {
a string
}
func (s *myStringer) String() string {
return s.a
}
type StringAlias string
type myCustomType struct {
s string
n int
f float32
arr []string
}
func Test(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Gomega Matchers")
}

View File

@@ -1,57 +0,0 @@
package matchers_test
import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
. "github.com/onsi/gomega/matchers"
)
var _ = Describe("NotMatcher", func() {
Context("basic examples", func() {
It("works", func() {
Expect(input).To(Not(false1))
Expect(input).To(Not(Not(true2)))
Expect(input).ToNot(Not(true3))
Expect(input).ToNot(Not(Not(false1)))
Expect(input).To(Not(Not(Not(false2))))
})
})
Context("De Morgan's laws", func() {
It("~(A && B) == ~A || ~B", func() {
Expect(input).To(Not(And(false1, false2)))
Expect(input).To(Or(Not(false1), Not(false2)))
})
It("~(A || B) == ~A && ~B", func() {
Expect(input).To(Not(Or(false1, false2)))
Expect(input).To(And(Not(false1), Not(false2)))
})
})
Context("failure messages are opposite of original matchers' failure messages", func() {
Context("when match fails", func() {
It("gives a descriptive message", func() {
verifyFailureMessage(Not(HaveLen(2)), input, "not to have length 2")
})
})
Context("when match succeeds, but expected it to fail", func() {
It("gives a descriptive message", func() {
verifyFailureMessage(Not(Not(HaveLen(3))), input, "to have length 3")
})
})
})
Context("MatchMayChangeInTheFuture()", func() {
It("Propagates value from wrapped matcher", func() {
m := Not(Or()) // an empty Or() always returns false, and indicates it cannot change
Expect(m.Match("anything")).To(BeTrue())
Expect(m.(*NotMatcher).MatchMayChangeInTheFuture("anything")).To(BeFalse())
})
It("Defaults to true", func() {
m := Not(Equal(1)) // Equal does not have this method
Expect(m.Match(2)).To(BeTrue())
Expect(m.(*NotMatcher).MatchMayChangeInTheFuture(2)).To(BeTrue()) // defaults to true
})
})
})

View File

@@ -1,85 +0,0 @@
package matchers_test
import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
. "github.com/onsi/gomega/matchers"
)
var _ = Describe("OrMatcher", func() {
It("works with positive cases", func() {
Expect(input).To(Or(true1))
Expect(input).To(Or(true1, true2))
Expect(input).To(Or(true1, false1))
Expect(input).To(Or(false1, true2))
Expect(input).To(Or(true1, true2, true3))
Expect(input).To(Or(true1, true2, false3))
Expect(input).To(Or(true1, false2, true3))
Expect(input).To(Or(false1, true2, true3))
Expect(input).To(Or(true1, false2, false3))
Expect(input).To(Or(false1, false2, true3))
// use alias
Expect(input).To(SatisfyAny(false1, false2, true3))
})
It("works with negative cases", func() {
Expect(input).ToNot(Or())
Expect(input).ToNot(Or(false1))
Expect(input).ToNot(Or(false1, false2))
Expect(input).ToNot(Or(false1, false2, false3))
})
Context("failure messages", func() {
Context("when match fails", func() {
It("gives a descriptive message", func() {
verifyFailureMessage(Or(false1, false2), input,
"To satisfy at least one of these matchers: [%!s(*matchers.HaveLenMatcher=&{1}) %!s(*matchers.EqualMatcher=&{hip})]")
})
})
Context("when match succeeds, but expected it to fail", func() {
It("gives a descriptive message", func() {
verifyFailureMessage(Not(Or(true1, true2)), input, `not to have length 2`)
})
})
})
Context("MatchMayChangeInTheFuture", func() {
Context("Match returned false", func() {
It("returns true if any of the matchers could change", func() {
// 3 matchers, all return false, and all could change
m := Or(BeNil(), Equal("hip"), HaveLen(1))
Expect(m.Match("hi")).To(BeFalse())
Expect(m.(*OrMatcher).MatchMayChangeInTheFuture("hi")).To(BeTrue()) // all 3 of these matchers default to 'true'
})
It("returns false if none of the matchers could change", func() {
// empty Or() has the property of never matching, and never can change since there are no sub-matchers that could change
m := Or()
Expect(m.Match("anything")).To(BeFalse())
Expect(m.(*OrMatcher).MatchMayChangeInTheFuture("anything")).To(BeFalse())
// Or() with 3 sub-matchers that return false, and can't change
m = Or(Or(), Or(), Or())
Expect(m.Match("hi")).To(BeFalse())
Expect(m.(*OrMatcher).MatchMayChangeInTheFuture("hi")).To(BeFalse()) // the 3 empty Or()'s won't change
})
})
Context("Match returned true", func() {
Context("returns value of the successful matcher", func() {
It("false if successful matcher not going to change", func() {
// 3 matchers: 1st returns false, 2nd returns true and is not going to change, 3rd is never called
m := Or(BeNil(), And(), Equal(1))
Expect(m.Match("hi")).To(BeTrue())
Expect(m.(*OrMatcher).MatchMayChangeInTheFuture("hi")).To(BeFalse())
})
It("true if successful matcher indicates it might change", func() {
// 3 matchers: 1st returns false, 2nd returns true and "might" change, 3rd is never called
m := Or(Not(BeNil()), Equal("hi"), Equal(1))
Expect(m.Match("hi")).To(BeTrue())
Expect(m.(*OrMatcher).MatchMayChangeInTheFuture("hi")).To(BeTrue()) // Equal("hi") indicates it might change
})
})
})
})
})

View File

@@ -2,11 +2,14 @@ package matchers
import (
"fmt"
"github.com/onsi/gomega/format"
"reflect"
"github.com/onsi/gomega/format"
)
type PanicMatcher struct{}
type PanicMatcher struct {
object interface{}
}
func (matcher *PanicMatcher) Match(actual interface{}) (success bool, err error) {
if actual == nil {
@@ -24,6 +27,7 @@ func (matcher *PanicMatcher) Match(actual interface{}) (success bool, err error)
success = false
defer func() {
if e := recover(); e != nil {
matcher.object = e
success = true
}
}()
@@ -38,5 +42,5 @@ func (matcher *PanicMatcher) FailureMessage(actual interface{}) (message string)
}
func (matcher *PanicMatcher) NegatedFailureMessage(actual interface{}) (message string) {
return format.Message(actual, "not to panic")
return format.Message(actual, fmt.Sprintf("not to panic, but panicked with\n%s", format.Object(matcher.object, 1)))
}

View File

@@ -1,36 +0,0 @@
package matchers_test
import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
. "github.com/onsi/gomega/matchers"
)
var _ = Describe("Panic", func() {
Context("when passed something that's not a function that takes zero arguments and returns nothing", func() {
It("should error", func() {
success, err := (&PanicMatcher{}).Match("foo")
Ω(success).Should(BeFalse())
Ω(err).Should(HaveOccurred())
success, err = (&PanicMatcher{}).Match(nil)
Ω(success).Should(BeFalse())
Ω(err).Should(HaveOccurred())
success, err = (&PanicMatcher{}).Match(func(foo string) {})
Ω(success).Should(BeFalse())
Ω(err).Should(HaveOccurred())
success, err = (&PanicMatcher{}).Match(func() string { return "bar" })
Ω(success).Should(BeFalse())
Ω(err).Should(HaveOccurred())
})
})
Context("when passed a function of the correct type", func() {
It("should call the function and pass if the function panics", func() {
Ω(func() { panic("ack!") }).Should(Panic())
Ω(func() {}).ShouldNot(Panic())
})
})
})

View File

@@ -1,280 +0,0 @@
package matchers_test
import (
"time"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
. "github.com/onsi/gomega/matchers"
)
type kungFuActor interface {
DrunkenMaster() bool
}
type jackie struct {
name string
}
func (j *jackie) DrunkenMaster() bool {
return true
}
var _ = Describe("ReceiveMatcher", func() {
Context("with no argument", func() {
Context("for a buffered channel", func() {
It("should succeed", func() {
channel := make(chan bool, 1)
Ω(channel).ShouldNot(Receive())
channel <- true
Ω(channel).Should(Receive())
})
})
Context("for an unbuffered channel", func() {
It("should succeed (eventually)", func() {
channel := make(chan bool)
Ω(channel).ShouldNot(Receive())
go func() {
time.Sleep(10 * time.Millisecond)
channel <- true
}()
Eventually(channel).Should(Receive())
})
})
})
Context("with a pointer argument", func() {
Context("of the correct type", func() {
It("should write the value received on the channel to the pointer", func() {
channel := make(chan int, 1)
var value int
Ω(channel).ShouldNot(Receive(&value))
Ω(value).Should(BeZero())
channel <- 17
Ω(channel).Should(Receive(&value))
Ω(value).Should(Equal(17))
})
})
Context("to various types of objects", func() {
It("should work", func() {
//channels of strings
stringChan := make(chan string, 1)
stringChan <- "foo"
var s string
Ω(stringChan).Should(Receive(&s))
Ω(s).Should(Equal("foo"))
//channels of slices
sliceChan := make(chan []bool, 1)
sliceChan <- []bool{true, true, false}
var sl []bool
Ω(sliceChan).Should(Receive(&sl))
Ω(sl).Should(Equal([]bool{true, true, false}))
//channels of channels
chanChan := make(chan chan bool, 1)
c := make(chan bool)
chanChan <- c
var receivedC chan bool
Ω(chanChan).Should(Receive(&receivedC))
Ω(receivedC).Should(Equal(c))
//channels of interfaces
jackieChan := make(chan kungFuActor, 1)
aJackie := &jackie{name: "Jackie Chan"}
jackieChan <- aJackie
var theJackie kungFuActor
Ω(jackieChan).Should(Receive(&theJackie))
Ω(theJackie).Should(Equal(aJackie))
})
})
Context("of the wrong type", func() {
It("should error", func() {
channel := make(chan int)
var incorrectType bool
success, err := (&ReceiveMatcher{Arg: &incorrectType}).Match(channel)
Ω(success).Should(BeFalse())
Ω(err).Should(HaveOccurred())
var notAPointer int
success, err = (&ReceiveMatcher{Arg: notAPointer}).Match(channel)
Ω(success).Should(BeFalse())
Ω(err).Should(HaveOccurred())
})
})
})
Context("with a matcher", func() {
It("should defer to the underlying matcher", func() {
intChannel := make(chan int, 1)
intChannel <- 3
Ω(intChannel).Should(Receive(Equal(3)))
intChannel <- 2
Ω(intChannel).ShouldNot(Receive(Equal(3)))
stringChannel := make(chan []string, 1)
stringChannel <- []string{"foo", "bar", "baz"}
Ω(stringChannel).Should(Receive(ContainElement(ContainSubstring("fo"))))
stringChannel <- []string{"foo", "bar", "baz"}
Ω(stringChannel).ShouldNot(Receive(ContainElement(ContainSubstring("archipelago"))))
})
It("should defer to the underlying matcher for the message", func() {
matcher := Receive(Equal(3))
channel := make(chan int, 1)
channel <- 2
matcher.Match(channel)
Ω(matcher.FailureMessage(channel)).Should(MatchRegexp(`Expected\s+<int>: 2\s+to equal\s+<int>: 3`))
channel <- 3
matcher.Match(channel)
Ω(matcher.NegatedFailureMessage(channel)).Should(MatchRegexp(`Expected\s+<int>: 3\s+not to equal\s+<int>: 3`))
})
It("should work just fine with Eventually", func() {
stringChannel := make(chan string)
go func() {
time.Sleep(5 * time.Millisecond)
stringChannel <- "A"
time.Sleep(5 * time.Millisecond)
stringChannel <- "B"
}()
Eventually(stringChannel).Should(Receive(Equal("B")))
})
Context("if the matcher errors", func() {
It("should error", func() {
channel := make(chan int, 1)
channel <- 3
success, err := (&ReceiveMatcher{Arg: ContainSubstring("three")}).Match(channel)
Ω(success).Should(BeFalse())
Ω(err).Should(HaveOccurred())
})
})
Context("if nothing is received", func() {
It("should fail", func() {
channel := make(chan int, 1)
success, err := (&ReceiveMatcher{Arg: Equal(1)}).Match(channel)
Ω(success).Should(BeFalse())
Ω(err).ShouldNot(HaveOccurred())
})
})
})
Context("When actual is a *closed* channel", func() {
Context("for a buffered channel", func() {
It("should work until it hits the end of the buffer", func() {
channel := make(chan bool, 1)
channel <- true
close(channel)
Ω(channel).Should(Receive())
Ω(channel).ShouldNot(Receive())
})
})
Context("for an unbuffered channel", func() {
It("should always fail", func() {
channel := make(chan bool)
close(channel)
Ω(channel).ShouldNot(Receive())
})
})
})
Context("When actual is a send-only channel", func() {
It("should error", func() {
channel := make(chan bool)
var writerChannel chan<- bool
writerChannel = channel
success, err := (&ReceiveMatcher{}).Match(writerChannel)
Ω(success).Should(BeFalse())
Ω(err).Should(HaveOccurred())
})
})
Context("when acutal is a non-channel", func() {
It("should error", func() {
var nilChannel chan bool
success, err := (&ReceiveMatcher{}).Match(nilChannel)
Ω(success).Should(BeFalse())
Ω(err).Should(HaveOccurred())
success, err = (&ReceiveMatcher{}).Match(nil)
Ω(success).Should(BeFalse())
Ω(err).Should(HaveOccurred())
success, err = (&ReceiveMatcher{}).Match(3)
Ω(success).Should(BeFalse())
Ω(err).Should(HaveOccurred())
})
})
Describe("when used with eventually and a custom matcher", func() {
It("should return the matcher's error when a failing value is received on the channel, instead of the must receive something failure", func() {
failures := InterceptGomegaFailures(func() {
c := make(chan string, 0)
Eventually(c, 0.01).Should(Receive(Equal("hello")))
})
Ω(failures[0]).Should(ContainSubstring("When passed a matcher, ReceiveMatcher's channel *must* receive something."))
failures = InterceptGomegaFailures(func() {
c := make(chan string, 1)
c <- "hi"
Eventually(c, 0.01).Should(Receive(Equal("hello")))
})
Ω(failures[0]).Should(ContainSubstring("<string>: hello"))
})
})
Describe("Bailing early", func() {
It("should bail early when passed a closed channel", func() {
c := make(chan bool)
close(c)
t := time.Now()
failures := InterceptGomegaFailures(func() {
Eventually(c).Should(Receive())
})
Ω(time.Since(t)).Should(BeNumerically("<", 500*time.Millisecond))
Ω(failures).Should(HaveLen(1))
})
It("should bail early when passed a non-channel", func() {
t := time.Now()
failures := InterceptGomegaFailures(func() {
Eventually(3).Should(Receive())
})
Ω(time.Since(t)).Should(BeNumerically("<", 500*time.Millisecond))
Ω(failures).Should(HaveLen(1))
})
})
})

View File

@@ -1,62 +0,0 @@
package matchers_test
import (
"errors"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
. "github.com/onsi/gomega/matchers"
)
func Erroring() error {
return errors.New("bam")
}
func NotErroring() error {
return nil
}
type AnyType struct{}
func Invalid() *AnyType {
return nil
}
var _ = Describe("Succeed", func() {
It("should succeed if the function succeeds", func() {
Ω(NotErroring()).Should(Succeed())
})
It("should succeed (in the negated) if the function errored", func() {
Ω(Erroring()).ShouldNot(Succeed())
})
It("should not if passed a non-error", func() {
success, err := (&SucceedMatcher{}).Match(Invalid())
Ω(success).Should(BeFalse())
Ω(err).Should(MatchError("Expected an error-type. Got:\n <*matchers_test.AnyType | 0x0>: nil"))
})
It("doesn't support non-error type", func() {
success, err := (&SucceedMatcher{}).Match(AnyType{})
Ω(success).Should(BeFalse())
Ω(err).Should(MatchError("Expected an error-type. Got:\n <matchers_test.AnyType>: {}"))
})
It("doesn't support non-error pointer type", func() {
success, err := (&SucceedMatcher{}).Match(&AnyType{})
Ω(success).Should(BeFalse())
Ω(err).Should(MatchError(MatchRegexp(`Expected an error-type. Got:\n <*matchers_test.AnyType | 0x[[:xdigit:]]+>: {}`)))
})
It("should not succeed with pointer types that conform to error interface", func() {
err := &CustomErr{"ohai"}
Ω(err).ShouldNot(Succeed())
})
It("should succeed with nil pointers to types that conform to error interface", func() {
var err *CustomErr = nil
Ω(err).Should(Succeed())
})
})

View File

@@ -1,20 +0,0 @@
Copyright (c) 2014 Amit Kumar Gupta
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

View File

@@ -1,102 +0,0 @@
package matchers_test
import (
"errors"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
. "github.com/onsi/gomega/matchers"
)
var _ = Describe("WithTransformMatcher", func() {
var plus1 = func(i int) int { return i + 1 }
Context("Panic if transform function invalid", func() {
panicsWithTransformer := func(transform interface{}) {
ExpectWithOffset(1, func() { WithTransform(transform, nil) }).To(Panic())
}
It("nil", func() {
panicsWithTransformer(nil)
})
Context("Invalid number of args, but correct return value count", func() {
It("zero", func() {
panicsWithTransformer(func() int { return 5 })
})
It("two", func() {
panicsWithTransformer(func(i, j int) int { return 5 })
})
})
Context("Invalid number of return values, but correct number of arguments", func() {
It("zero", func() {
panicsWithTransformer(func(i int) {})
})
It("two", func() {
panicsWithTransformer(func(i int) (int, int) { return 5, 6 })
})
})
})
It("works with positive cases", func() {
Expect(1).To(WithTransform(plus1, Equal(2)))
Expect(1).To(WithTransform(plus1, WithTransform(plus1, Equal(3))))
Expect(1).To(WithTransform(plus1, And(Equal(2), BeNumerically(">", 1))))
// transform expects custom type
type S struct {
A int
B string
}
transformer := func(s S) string { return s.B }
Expect(S{1, "hi"}).To(WithTransform(transformer, Equal("hi")))
// transform expects interface
errString := func(e error) string { return e.Error() }
Expect(errors.New("abc")).To(WithTransform(errString, Equal("abc")))
})
It("works with negative cases", func() {
Expect(1).ToNot(WithTransform(plus1, Equal(3)))
Expect(1).ToNot(WithTransform(plus1, WithTransform(plus1, Equal(2))))
})
Context("failure messages", func() {
Context("when match fails", func() {
It("gives a descriptive message", func() {
m := WithTransform(plus1, Equal(3))
Expect(m.Match(1)).To(BeFalse())
Expect(m.FailureMessage(1)).To(Equal("Expected\n <int>: 2\nto equal\n <int>: 3"))
})
})
Context("when match succeeds, but expected it to fail", func() {
It("gives a descriptive message", func() {
m := Not(WithTransform(plus1, Equal(3)))
Expect(m.Match(2)).To(BeFalse())
Expect(m.FailureMessage(2)).To(Equal("Expected\n <int>: 3\nnot to equal\n <int>: 3"))
})
})
Context("actual value is incompatible with transform function's argument type", func() {
It("gracefully fails if transform cannot be performed", func() {
m := WithTransform(plus1, Equal(3))
result, err := m.Match("hi") // give it a string but transform expects int; doesn't panic
Expect(result).To(BeFalse())
Expect(err).To(MatchError("Transform function expects 'int' but we have 'string'"))
})
})
})
Context("MatchMayChangeInTheFuture()", func() {
It("Propagates value from wrapped matcher on the transformed value", func() {
m := WithTransform(plus1, Or()) // empty Or() always returns false, and indicates it cannot change
Expect(m.Match(1)).To(BeFalse())
Expect(m.(*WithTransformMatcher).MatchMayChangeInTheFuture(1)).To(BeFalse()) // empty Or() indicates cannot change
})
It("Defaults to true", func() {
m := WithTransform(plus1, Equal(2)) // Equal does not have this method
Expect(m.Match(1)).To(BeTrue())
Expect(m.(*WithTransformMatcher).MatchMayChangeInTheFuture(1)).To(BeTrue()) // defaults to true
})
})
})