diff --git a/testing/gomega/factorial5/factorial.go b/testing/gomega/factorial5/factorial.go new file mode 100644 index 0000000..34c3ba6 --- /dev/null +++ b/testing/gomega/factorial5/factorial.go @@ -0,0 +1,18 @@ +package main + +import "errors" + +func Factorial(n int64) (int64, error) { + switch { + case n < 0: + return 0, errors.New("math: factorial of negative number?!?") + case n == 0: + return 1, nil + default: + ret, err := Factorial(n - 2) + if err != nil { + return 0, err + } + return n * ret, nil + } +} diff --git a/testing/gomega/factorial5/factorial_test.go b/testing/gomega/factorial5/factorial_test.go new file mode 100644 index 0000000..a708d06 --- /dev/null +++ b/testing/gomega/factorial5/factorial_test.go @@ -0,0 +1,36 @@ +package main + +import ( + . "github.com/onsi/gomega" + "testing" +) + +func TestFactorialForNegativeValue(t *testing.T) { + g := NewGomegaWithT(t) + _, err := Factorial(-1) + g.Expect(err).Should(HaveOccurred()) +} + +func TestFactorialForZero(t *testing.T) { + g := NewGomegaWithT(t) + result, err := Factorial(0) + g.Expect(err).ShouldNot(HaveOccurred()) + g.Expect(result).To(BeNumerically("==", 1)) +} + +func TestFactorialForOne(t *testing.T) { + g := NewGomegaWithT(t) + result, err := Factorial(0) + g.Expect(err).Should(Succeed()) + g.Expect(result).To(BeNumerically("==", 1)) +} + +func TestFactorialForTen(t *testing.T) { + g := NewGomegaWithT(t) + g.Expect(Factorial(10)).To(BeNumerically("==", 3628800)) +} + +func TestFactorialForSmallNumber(t *testing.T) { + g := NewGomegaWithT(t) + g.Expect(Factorial(5)).To(SatisfyAll(BeNumerically(">=", 10), BeNumerically("<=", 10000))) +} diff --git a/testing/gomega/factorial5/go.mod b/testing/gomega/factorial5/go.mod new file mode 100644 index 0000000..f649187 --- /dev/null +++ b/testing/gomega/factorial5/go.mod @@ -0,0 +1,3 @@ +module factorial.go + +require github.com/onsi/gomega v1.7.1 diff --git a/testing/gomega/factorial5/go.sum b/testing/gomega/factorial5/go.sum new file mode 100644 index 0000000..5516e25 --- /dev/null +++ b/testing/gomega/factorial5/go.sum @@ -0,0 +1,17 @@ +github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= +github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= +github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU= +github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= +github.com/onsi/gomega v1.7.1 h1:K0jcRCwNQM3vFGh1ppMtDh/+7ApJrjldlX8fA0jDTLQ= +github.com/onsi/gomega v1.7.1/go.mod h1:XdKZgCCFLUoM/7CFJVPcG8C1xQ1AJ0vpAezJrB7JYyY= +golang.org/x/net v0.0.0-20180906233101-161cd47e91fd h1:nTDtHvHSdCn1m6ITfMRqtOd/9+7a3s8RBNOZ3eYZzJA= +golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys= +gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw= +gopkg.in/yaml.v2 v2.2.4 h1:/eiJrUcujPVeJ3xlSWaiNi3uSVmDGBK1pDHUHAnao1I= +gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=