Commit b8ccb823 by bergquist

Merge branch 'alerting'

parents 90388710 7095a9d7
......@@ -24,7 +24,6 @@
* **Graphite**: Fixed issue with mixed data sources and Graphite, fixes [#5617](https://github.com/grafana/grafana/issues/5617)
* **Templating**: Fixed issue with template variable query was issued multiple times during dashboard load, fixes [#5637](https://github.com/grafana/grafana/issues/5637)
* **Zoom**: Fixed issues with zoom in and out on embedded (iframed) panel, fixes [#4489](https://github.com/grafana/grafana/issues/4489), [#5666](https://github.com/grafana/grafana/issues/5666)
* **Templating**: Row/Panel repeat issue when saving dashboard caused dupes to appear, fixes [#5591](https://github.com/grafana/grafana/issues/5591)
# 3.1.0 stable (2016-07-12)
......
......@@ -131,6 +131,10 @@
"Rev": "f80e7d0182a463dff0c0da6bbed57f21369d4346"
},
{
"ImportPath": "github.com/benbjohnson/clock",
"Rev": "a620c1cc9866f84a2550ad53f4f353ec030fa26b"
},
{
"ImportPath": "github.com/bmizerany/assert",
"Comment": "release.r60-6-ge17e998",
"Rev": "e17e99893cb6509f428e1728281c2ad60a6b31e3"
......@@ -273,6 +277,14 @@
"Rev": "e6ac2fc51e89a3249e82157fa0bb7a18ef9dd5bb"
},
{
"ImportPath": "github.com/kr/s3",
"Rev": "c070c8f9a8f0032d48f0d2a77d4e382788bd8a1d"
},
{
"ImportPath": "github.com/kr/s3/s3util",
"Rev": "c070c8f9a8f0032d48f0d2a77d4e382788bd8a1d"
},
{
"ImportPath": "github.com/kr/text",
"Rev": "bb797dc4fb8320488f47bf11de07a733d7233e1f"
},
......
The MIT License (MIT)
Copyright (c) 2014 Ben Johnson
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.
clock [![Build Status](https://drone.io/github.com/benbjohnson/clock/status.png)](https://drone.io/github.com/benbjohnson/clock/latest) [![Coverage Status](https://coveralls.io/repos/benbjohnson/clock/badge.png?branch=master)](https://coveralls.io/r/benbjohnson/clock?branch=master) [![GoDoc](https://godoc.org/github.com/benbjohnson/clock?status.png)](https://godoc.org/github.com/benbjohnson/clock) ![Project status](http://img.shields.io/status/experimental.png?color=red)
=====
Clock is a small library for mocking time in Go. It provides an interface
around the standard library's [`time`][time] package so that the application
can use the realtime clock while tests can use the mock clock.
[time]: http://golang.org/pkg/time/
## Usage
### Realtime Clock
Your application can maintain a `Clock` variable that will allow realtime and
mock clocks to be interchangable. For example, if you had an `Application` type:
```go
import "github.com/benbjohnson/clock"
type Application struct {
Clock clock.Clock
}
```
You could initialize it to use the realtime clock like this:
```go
var app Application
app.Clock = clock.New()
...
```
Then all timers and time-related functionality should be performed from the
`Clock` variable.
### Mocking time
In your tests, you will want to use a `Mock` clock:
```go
import (
"testing"
"github.com/benbjohnson/clock"
)
func TestApplication_DoSomething(t *testing.T) {
mock := clock.NewMock()
app := Application{Clock: mock}
...
}
```
Now that you've initialized your application to use the mock clock, you can
adjust the time programmatically. The mock clock always starts from the Unix
epoch (midnight, Jan 1, 1970 UTC).
### Controlling time
The mock clock provides the same functions that the standard library's `time`
package provides. For example, to find the current time, you use the `Now()`
function:
```go
mock := clock.NewMock()
// Find the current time.
mock.Now().UTC() // 1970-01-01 00:00:00 +0000 UTC
// Move the clock forward.
mock.Add(2 * time.Hour)
// Check the time again. It's 2 hours later!
mock.Now().UTC() // 1970-01-01 02:00:00 +0000 UTC
```
Timers and Tickers are also controlled by this same mock clock. They will only
execute when the clock is moved forward:
```
mock := clock.NewMock()
count := 0
// Kick off a timer to increment every 1 mock second.
go func() {
ticker := clock.Ticker(1 * time.Second)
for {
<-ticker.C
count++
}
}()
runtime.Gosched()
// Move the clock forward 10 second.
mock.Add(10 * time.Second)
// This prints 10.
fmt.Println(count)
```
package clock
import (
"sort"
"sync"
"time"
)
// Clock represents an interface to the functions in the standard library time
// package. Two implementations are available in the clock package. The first
// is a real-time clock which simply wraps the time package's functions. The
// second is a mock clock which will only make forward progress when
// programmatically adjusted.
type Clock interface {
After(d time.Duration) <-chan time.Time
AfterFunc(d time.Duration, f func()) *Timer
Now() time.Time
Sleep(d time.Duration)
Tick(d time.Duration) <-chan time.Time
Ticker(d time.Duration) *Ticker
Timer(d time.Duration) *Timer
}
// New returns an instance of a real-time clock.
func New() Clock {
return &clock{}
}
// clock implements a real-time clock by simply wrapping the time package functions.
type clock struct{}
func (c *clock) After(d time.Duration) <-chan time.Time { return time.After(d) }
func (c *clock) AfterFunc(d time.Duration, f func()) *Timer {
return &Timer{timer: time.AfterFunc(d, f)}
}
func (c *clock) Now() time.Time { return time.Now() }
func (c *clock) Sleep(d time.Duration) { time.Sleep(d) }
func (c *clock) Tick(d time.Duration) <-chan time.Time { return time.Tick(d) }
func (c *clock) Ticker(d time.Duration) *Ticker {
t := time.NewTicker(d)
return &Ticker{C: t.C, ticker: t}
}
func (c *clock) Timer(d time.Duration) *Timer {
t := time.NewTimer(d)
return &Timer{C: t.C, timer: t}
}
// Mock represents a mock clock that only moves forward programmically.
// It can be preferable to a real-time clock when testing time-based functionality.
type Mock struct {
mu sync.Mutex
now time.Time // current time
timers clockTimers // tickers & timers
}
// NewMock returns an instance of a mock clock.
// The current time of the mock clock on initialization is the Unix epoch.
func NewMock() *Mock {
return &Mock{now: time.Unix(0, 0)}
}
// Add moves the current time of the mock clock forward by the duration.
// This should only be called from a single goroutine at a time.
func (m *Mock) Add(d time.Duration) {
// Calculate the final current time.
t := m.now.Add(d)
// Continue to execute timers until there are no more before the new time.
for {
if !m.runNextTimer(t) {
break
}
}
// Ensure that we end with the new time.
m.mu.Lock()
m.now = t
m.mu.Unlock()
// Give a small buffer to make sure the other goroutines get handled.
gosched()
}
// Sets the current time of the mock clock to a specific one.
// This should only be called from a single goroutine at a time.
func (m *Mock) Set(t time.Time) {
// Continue to execute timers until there are no more before the new time.
for {
if !m.runNextTimer(t) {
break
}
}
// Ensure that we end with the new time.
m.mu.Lock()
m.now = t
m.mu.Unlock()
// Give a small buffer to make sure the other goroutines get handled.
gosched()
}
// runNextTimer executes the next timer in chronological order and moves the
// current time to the timer's next tick time. The next time is not executed if
// it's next time if after the max time. Returns true if a timer is executed.
func (m *Mock) runNextTimer(max time.Time) bool {
m.mu.Lock()
// Sort timers by time.
sort.Sort(m.timers)
// If we have no more timers then exit.
if len(m.timers) == 0 {
m.mu.Unlock()
return false
}
// Retrieve next timer. Exit if next tick is after new time.
t := m.timers[0]
if t.Next().After(max) {
m.mu.Unlock()
return false
}
// Move "now" forward and unlock clock.
m.now = t.Next()
m.mu.Unlock()
// Execute timer.
t.Tick(m.now)
return true
}
// After waits for the duration to elapse and then sends the current time on the returned channel.
func (m *Mock) After(d time.Duration) <-chan time.Time {
return m.Timer(d).C
}
// AfterFunc waits for the duration to elapse and then executes a function.
// A Timer is returned that can be stopped.
func (m *Mock) AfterFunc(d time.Duration, f func()) *Timer {
t := m.Timer(d)
t.C = nil
t.fn = f
return t
}
// Now returns the current wall time on the mock clock.
func (m *Mock) Now() time.Time {
m.mu.Lock()
defer m.mu.Unlock()
return m.now
}
// Sleep pauses the goroutine for the given duration on the mock clock.
// The clock must be moved forward in a separate goroutine.
func (m *Mock) Sleep(d time.Duration) {
<-m.After(d)
}
// Tick is a convenience function for Ticker().
// It will return a ticker channel that cannot be stopped.
func (m *Mock) Tick(d time.Duration) <-chan time.Time {
return m.Ticker(d).C
}
// Ticker creates a new instance of Ticker.
func (m *Mock) Ticker(d time.Duration) *Ticker {
m.mu.Lock()
defer m.mu.Unlock()
ch := make(chan time.Time, 1)
t := &Ticker{
C: ch,
c: ch,
mock: m,
d: d,
next: m.now.Add(d),
}
m.timers = append(m.timers, (*internalTicker)(t))
return t
}
// Timer creates a new instance of Timer.
func (m *Mock) Timer(d time.Duration) *Timer {
m.mu.Lock()
defer m.mu.Unlock()
ch := make(chan time.Time, 1)
t := &Timer{
C: ch,
c: ch,
mock: m,
next: m.now.Add(d),
stopped: false,
}
m.timers = append(m.timers, (*internalTimer)(t))
return t
}
func (m *Mock) removeClockTimer(t clockTimer) {
m.mu.Lock()
defer m.mu.Unlock()
for i, timer := range m.timers {
if timer == t {
copy(m.timers[i:], m.timers[i+1:])
m.timers[len(m.timers)-1] = nil
m.timers = m.timers[:len(m.timers)-1]
break
}
}
sort.Sort(m.timers)
}
// clockTimer represents an object with an associated start time.
type clockTimer interface {
Next() time.Time
Tick(time.Time)
}
// clockTimers represents a list of sortable timers.
type clockTimers []clockTimer
func (a clockTimers) Len() int { return len(a) }
func (a clockTimers) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a clockTimers) Less(i, j int) bool { return a[i].Next().Before(a[j].Next()) }
// Timer represents a single event.
// The current time will be sent on C, unless the timer was created by AfterFunc.
type Timer struct {
C <-chan time.Time
c chan time.Time
timer *time.Timer // realtime impl, if set
next time.Time // next tick time
mock *Mock // mock clock, if set
fn func() // AfterFunc function, if set
stopped bool // True if stopped, false if running
}
// Stop turns off the ticker.
func (t *Timer) Stop() bool {
if t.timer != nil {
return t.timer.Stop()
}
registered := !t.stopped
t.mock.removeClockTimer((*internalTimer)(t))
t.stopped = true
return registered
}
// Reset changes the expiry time of the timer
func (t *Timer) Reset(d time.Duration) bool {
if t.timer != nil {
return t.timer.Reset(d)
}
t.next = t.mock.now.Add(d)
registered := !t.stopped
if t.stopped {
t.mock.mu.Lock()
t.mock.timers = append(t.mock.timers, (*internalTimer)(t))
t.mock.mu.Unlock()
}
t.stopped = false
return registered
}
type internalTimer Timer
func (t *internalTimer) Next() time.Time { return t.next }
func (t *internalTimer) Tick(now time.Time) {
if t.fn != nil {
t.fn()
} else {
t.c <- now
}
t.mock.removeClockTimer((*internalTimer)(t))
t.stopped = true
gosched()
}
// Ticker holds a channel that receives "ticks" at regular intervals.
type Ticker struct {
C <-chan time.Time
c chan time.Time
ticker *time.Ticker // realtime impl, if set
next time.Time // next tick time
mock *Mock // mock clock, if set
d time.Duration // time between ticks
}
// Stop turns off the ticker.
func (t *Ticker) Stop() {
if t.ticker != nil {
t.ticker.Stop()
} else {
t.mock.removeClockTimer((*internalTicker)(t))
}
}
type internalTicker Ticker
func (t *internalTicker) Next() time.Time { return t.next }
func (t *internalTicker) Tick(now time.Time) {
select {
case t.c <- now:
default:
}
t.next = now.Add(t.d)
gosched()
}
// Sleep momentarily so that other goroutines can process.
func gosched() { time.Sleep(1 * time.Millisecond) }
Copyright (c) 2012 Keith Rarick.
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.
Package s3 signs HTTP requests for use with Amazon’s S3 API.
Documentation:
http://godoc.org/github.com/kr/s3
Package s3util provides streaming transfers to and from Amazon S3.
Full documentation:
http://godoc.org/github.com/kr/s3/s3util
// Package s3util provides streaming transfers to and from Amazon S3.
//
// To use it, open or create an S3 object, read or write data,
// and close the object.
//
// You must assign valid credentials to DefaultConfig.Keys before using
// DefaultConfig. Be sure to close an io.WriteCloser returned by this package,
// to flush buffers and complete the multipart upload process.
package s3util
// TODO(kr): parse error responses; return structured data
import (
"github.com/kr/s3"
"net/http"
)
var DefaultConfig = &Config{
Service: s3.DefaultService,
Keys: new(s3.Keys),
}
type Config struct {
*s3.Service
*s3.Keys
*http.Client // if nil, uses http.DefaultClient
}
package s3util
import (
"bytes"
"fmt"
"io"
"net/http"
)
type respError struct {
r *http.Response
b bytes.Buffer
}
func newRespError(r *http.Response) *respError {
e := new(respError)
e.r = r
io.Copy(&e.b, r.Body)
r.Body.Close()
return e
}
func (e *respError) Error() string {
return fmt.Sprintf(
"unwanted http status %d: %q",
e.r.StatusCode,
e.b.String(),
)
}
package s3util
import (
"io"
"net/http"
"time"
)
// Open requests the S3 object at url. An HTTP status other than 200 is
// considered an error.
//
// If c is nil, Open uses DefaultConfig.
func Open(url string, c *Config) (io.ReadCloser, error) {
if c == nil {
c = DefaultConfig
}
// TODO(kr): maybe parallel range fetching
r, _ := http.NewRequest("GET", url, nil)
r.Header.Set("Date", time.Now().UTC().Format(http.TimeFormat))
c.Sign(r, *c.Keys)
client := c.Client
if client == nil {
client = http.DefaultClient
}
resp, err := client.Do(r)
if err != nil {
return nil, err
}
if resp.StatusCode != 200 {
return nil, newRespError(resp)
}
return resp.Body, nil
}
package s3util
import (
"bytes"
"encoding/xml"
"errors"
"io"
"net/http"
"net/url"
"os"
"strconv"
"strings"
"time"
)
// File represents an S3 object or directory.
type File struct {
url string
prefix string
config *Config
result *listObjectsResult
}
type fileInfo struct {
name string
size int64
dir bool
modTime time.Time
sys *Stat
}
// Stat contains information about an S3 object or directory.
// It is the "underlying data source" returned by method Sys
// for each FileInfo produced by this package.
// fi.Sys().(*s3util.Stat)
// For the meaning of these fields, see
// http://docs.aws.amazon.com/AmazonS3/latest/API/RESTBucketGET.html.
type Stat struct {
Key string
LastModified string
ETag string // ETag value, without double quotes.
Size string
StorageClass string
OwnerID string `xml:"Owner>ID"`
OwnerName string `xml:"Owner>DisplayName"`
}
type listObjectsResult struct {
IsTruncated bool
Contents []Stat
Directories []string `xml:"CommonPrefixes>Prefix"` // Suffix "/" trimmed
}
func (f *fileInfo) Name() string { return f.name }
func (f *fileInfo) Size() int64 { return f.size }
func (f *fileInfo) Mode() os.FileMode {
if f.dir {
return 0755 | os.ModeDir
}
return 0644
}
func (f *fileInfo) ModTime() time.Time {
if f.modTime.IsZero() && f.sys != nil {
// we return the zero value if a parse error ever happens.
f.modTime, _ = time.Parse(time.RFC3339Nano, f.sys.LastModified)
}
return f.modTime
}
func (f *fileInfo) IsDir() bool { return f.dir }
func (f *fileInfo) Sys() interface{} { return f.sys }
// NewFile returns a new File with the given URL and config.
//
// Set rawurl to a directory on S3, such as
// https://mybucket.s3.amazonaws.com/myfolder.
// The URL cannot have query parameters or a fragment.
// If c is nil, DefaultConfig will be used.
func NewFile(rawurl string, c *Config) (*File, error) {
u, err := url.Parse(rawurl)
if err != nil {
return nil, err
}
if u.RawQuery != "" {
return nil, errors.New("url cannot have raw query parameters.")
}
if u.Fragment != "" {
return nil, errors.New("url cannot have a fragment.")
}
prefix := strings.TrimLeft(u.Path, "/")
if prefix != "" && !strings.HasSuffix(prefix, "/") {
prefix += "/"
}
u.Path = ""
return &File{u.String(), prefix, c, nil}, nil
}
// Readdir requests a list of entries in the S3 directory
// represented by f and returns a slice of up to n FileInfo
// values, in alphabetical order. Subsequent calls
// on the same File will yield further FileInfos.
// Only direct children are returned, not deeper descendants.
func (f *File) Readdir(n int) ([]os.FileInfo, error) {
if f.result != nil && !f.result.IsTruncated {
return make([]os.FileInfo, 0), io.EOF
}
reader, err := f.sendRequest(n)
if err != nil {
return nil, err
}
defer reader.Close()
return f.parseResponse(reader)
}
func (f *File) sendRequest(count int) (io.ReadCloser, error) {
c := f.config
if c == nil {
c = DefaultConfig
}
var buf bytes.Buffer
buf.WriteString(f.url)
buf.WriteString("?delimiter=%2F")
if f.prefix != "" {
buf.WriteString("&prefix=")
buf.WriteString(url.QueryEscape(f.prefix))
}
if count > 0 {
buf.WriteString("&max-keys=")
buf.WriteString(strconv.Itoa(count))
}
if f.result != nil && f.result.IsTruncated {
var lastDir, lastKey, marker string
if len(f.result.Directories) > 0 {
lastDir = f.result.Directories[len(f.result.Directories)-1]
}
if len(f.result.Contents) > 0 {
lastKey = f.result.Contents[len(f.result.Contents)-1].Key
}
if lastKey > lastDir {
marker = lastKey
} else {
marker = lastDir
}
if marker != "" {
buf.WriteString("&marker=")
buf.WriteString(url.QueryEscape(marker))
}
}
u := buf.String()
r, _ := http.NewRequest("GET", u, nil)
r.Header.Set("Date", time.Now().UTC().Format(http.TimeFormat))
c.Sign(r, *c.Keys)
resp, err := http.DefaultClient.Do(r)
if err != nil {
return nil, err
}
if resp.StatusCode != 200 {
return nil, newRespError(resp)
}
return resp.Body, nil
}
func (f *File) parseResponse(reader io.Reader) ([]os.FileInfo, error) {
decoder := xml.NewDecoder(reader)
result := listObjectsResult{}
var err error
err = decoder.Decode(&result)
if err != nil {
return nil, err
}
infos := make([]os.FileInfo, len(result.Contents)+len(result.Directories))
var size int64
var name string
var is_dir bool
for i, content := range result.Contents {
c := content
c.ETag = strings.Trim(c.ETag, `"`)
size, _ = strconv.ParseInt(c.Size, 10, 0)
if size == 0 && strings.HasSuffix(c.Key, "/") {
name = strings.TrimRight(c.Key, "/")
is_dir = true
} else {
name = c.Key
is_dir = false
}
infos[i] = &fileInfo{
name: name,
size: size,
dir: is_dir,
sys: &c,
}
}
for i, dir := range result.Directories {
infos[len(result.Contents)+i] = &fileInfo{
name: strings.TrimRight(dir, "/"),
size: 0,
dir: true,
}
}
f.result = &result
return infos, nil
}
package s3util
import (
"bytes"
"encoding/xml"
"github.com/kr/s3"
"fmt"
"io"
"net/http"
"net/url"
"strconv"
"sync"
"syscall"
"time"
)
// defined by amazon
const (
minPartSize = 5 * 1024 * 1024
maxPartSize = 1<<31 - 1 // for 32-bit use; amz max is 5GiB
maxObjSize = 5 * 1024 * 1024 * 1024 * 1024
maxNPart = 10000
)
const (
concurrency = 5
nTry = 2
)
type part struct {
r io.ReadSeeker
len int64
// read by xml encoder
PartNumber int
ETag string
}
type uploader struct {
s3 s3.Service
keys s3.Keys
url string
client *http.Client
UploadId string // written by xml decoder
bufsz int64
buf []byte
off int
ch chan *part
part int
closed bool
err error
wg sync.WaitGroup
xml struct {
XMLName string `xml:"CompleteMultipartUpload"`
Part []*part
}
}
// Create creates an S3 object at url and sends multipart upload requests as
// data is written.
//
// If h is not nil, each of its entries is added to the HTTP request header.
// If c is nil, Create uses DefaultConfig.
func Create(url string, h http.Header, c *Config) (io.WriteCloser, error) {
if c == nil {
c = DefaultConfig
}
return newUploader(url, h, c)
}
// Sends an S3 multipart upload initiation request.
// See http://docs.amazonwebservices.com/AmazonS3/latest/dev/mpuoverview.html.
// This initial request returns an UploadId that we use to identify
// subsequent PUT requests.
func newUploader(url string, h http.Header, c *Config) (u *uploader, err error) {
u = new(uploader)
u.s3 = *c.Service
u.url = url
u.keys = *c.Keys
u.client = c.Client
if u.client == nil {
u.client = http.DefaultClient
}
u.bufsz = minPartSize
r, err := http.NewRequest("POST", url+"?uploads", nil)
if err != nil {
return nil, err
}
r.Header.Set("Date", time.Now().UTC().Format(http.TimeFormat))
for k := range h {
for _, v := range h[k] {
r.Header.Add(k, v)
}
}
u.s3.Sign(r, u.keys)
resp, err := u.client.Do(r)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return nil, newRespError(resp)
}
err = xml.NewDecoder(resp.Body).Decode(u)
if err != nil {
return nil, err
}
u.ch = make(chan *part)
for i := 0; i < concurrency; i++ {
go u.worker()
}
return u, nil
}
func (u *uploader) Write(p []byte) (n int, err error) {
if u.closed {
return 0, syscall.EINVAL
}
if u.err != nil {
return 0, u.err
}
for n < len(p) {
if cap(u.buf) == 0 {
u.buf = make([]byte, int(u.bufsz))
// Increase part size (1.001x).
// This lets us reach the max object size (5TiB) while
// still doing minimal buffering for small objects.
u.bufsz = min(u.bufsz+u.bufsz/1000, maxPartSize)
}
r := copy(u.buf[u.off:], p[n:])
u.off += r
n += r
if u.off == len(u.buf) {
u.flush()
}
}
return n, nil
}
func (u *uploader) flush() {
u.wg.Add(1)
u.part++
p := &part{bytes.NewReader(u.buf[:u.off]), int64(u.off), u.part, ""}
u.xml.Part = append(u.xml.Part, p)
u.ch <- p
u.buf, u.off = nil, 0
}
func (u *uploader) worker() {
for p := range u.ch {
u.retryUploadPart(p)
}
}
// Calls putPart up to nTry times to recover from transient errors.
func (u *uploader) retryUploadPart(p *part) {
defer u.wg.Done()
defer func() { p.r = nil }() // free the large buffer
var err error
for i := 0; i < nTry; i++ {
p.r.Seek(0, 0)
err = u.putPart(p)
if err == nil {
return
}
}
u.err = err
}
// Uploads part p, reading its contents from p.r.
// Stores the ETag in p.ETag.
func (u *uploader) putPart(p *part) error {
v := url.Values{}
v.Set("partNumber", strconv.Itoa(p.PartNumber))
v.Set("uploadId", u.UploadId)
req, err := http.NewRequest("PUT", u.url+"?"+v.Encode(), p.r)
if err != nil {
return err
}
req.ContentLength = p.len
req.Header.Set("Date", time.Now().UTC().Format(http.TimeFormat))
u.s3.Sign(req, u.keys)
resp, err := u.client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return newRespError(resp)
}
s := resp.Header.Get("etag") // includes quote chars for some reason
if len(s) < 2 {
return fmt.Errorf("received invalid etag %q", s)
}
p.ETag = s[1 : len(s)-1]
return nil
}
func (u *uploader) Close() error {
if u.closed {
return syscall.EINVAL
}
if cap(u.buf) > 0 {
u.flush()
}
u.wg.Wait()
close(u.ch)
u.closed = true
if u.err != nil {
u.abort()
return u.err
}
body, err := xml.Marshal(u.xml)
if err != nil {
return err
}
b := bytes.NewBuffer(body)
v := url.Values{}
v.Set("uploadId", u.UploadId)
req, err := http.NewRequest("POST", u.url+"?"+v.Encode(), b)
if err != nil {
return err
}
req.Header.Set("Date", time.Now().UTC().Format(http.TimeFormat))
u.s3.Sign(req, u.keys)
resp, err := u.client.Do(req)
if err != nil {
return err
}
if resp.StatusCode != 200 {
return newRespError(resp)
}
resp.Body.Close()
return nil
}
func (u *uploader) abort() {
// TODO(kr): devise a reasonable way to report an error here in addition
// to the error that caused the abort.
v := url.Values{}
v.Set("uploadId", u.UploadId)
s := u.url + "?" + v.Encode()
req, err := http.NewRequest("DELETE", s, nil)
if err != nil {
return
}
req.Header.Set("Date", time.Now().UTC().Format(http.TimeFormat))
u.s3.Sign(req, u.keys)
resp, err := u.client.Do(req)
if err != nil {
return
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return
}
}
func min(a, b int64) int64 {
if a < b {
return a
}
return b
}
// Package s3 signs HTTP requests for Amazon S3 and compatible services.
package s3
// See
// http://docs.amazonwebservices.com/AmazonS3/2006-03-01/dev/RESTAuthentication.html.
import (
"crypto/hmac"
"crypto/sha1"
"encoding/base64"
"io"
"net/http"
"sort"
"strings"
)
var signParams = map[string]bool{
"acl": true,
"delete": true,
"lifecycle": true,
"location": true,
"logging": true,
"notification": true,
"partNumber": true,
"policy": true,
"requestPayment": true,
"response-cache-control": true,
"response-content-disposition": true,
"response-content-encoding": true,
"response-content-language": true,
"response-content-type": true,
"response-expires": true,
"restore": true,
"torrent": true,
"uploadId": true,
"uploads": true,
"versionId": true,
"versioning": true,
"versions": true,
"website": true,
}
// Keys holds a set of Amazon Security Credentials.
type Keys struct {
AccessKey string
SecretKey string
// SecurityToken is used for temporary security credentials.
// If set, it will be added to header field X-Amz-Security-Token
// before signing a request.
SecurityToken string
// See http://docs.aws.amazon.com/AmazonS3/latest/dev/MakingRequests.html#TypesofSecurityCredentials
}
// IdentityBucket returns subdomain.
// It is designed to be used with S3-compatible services that
// treat the entire subdomain as the bucket name, for example
// storage.io.
func IdentityBucket(subdomain string) string {
return subdomain
}
// AmazonBucket returns everything up to the last '.' in subdomain.
// It is designed to be used with the Amazon service.
// "johnsmith.s3" becomes "johnsmith"
// "johnsmith.s3-eu-west-1" becomes "johnsmith"
// "www.example.com.s3" becomes "www.example.com"
func AmazonBucket(subdomain string) string {
if i := strings.LastIndex(subdomain, "."); i != -1 {
return subdomain[:i]
}
return ""
}
// DefaultService is the default Service used by Sign.
var DefaultService = &Service{Domain: "amazonaws.com"}
// Sign signs an HTTP request with the given S3 keys.
//
// This function is a wrapper around DefaultService.Sign.
func Sign(r *http.Request, k Keys) {
DefaultService.Sign(r, k)
}
// Service represents an S3-compatible service.
type Service struct {
// Domain is the service's root domain. It is used to extract
// the subdomain from an http.Request before passing the
// subdomain to Bucket.
Domain string
// Bucket derives the bucket name from a subdomain.
// If nil, AmazonBucket is used.
Bucket func(subdomain string) string
}
// Sign signs an HTTP request with the given S3 keys for use on service s.
func (s *Service) Sign(r *http.Request, k Keys) {
if k.SecurityToken != "" {
r.Header.Set("X-Amz-Security-Token", k.SecurityToken)
}
h := hmac.New(sha1.New, []byte(k.SecretKey))
s.writeSigData(h, r)
sig := make([]byte, base64.StdEncoding.EncodedLen(h.Size()))
base64.StdEncoding.Encode(sig, h.Sum(nil))
r.Header.Set("Authorization", "AWS "+k.AccessKey+":"+string(sig))
}
func (s *Service) writeSigData(w io.Writer, r *http.Request) {
w.Write([]byte(r.Method))
w.Write([]byte{'\n'})
w.Write([]byte(r.Header.Get("content-md5")))
w.Write([]byte{'\n'})
w.Write([]byte(r.Header.Get("content-type")))
w.Write([]byte{'\n'})
if _, ok := r.Header["X-Amz-Date"]; !ok {
w.Write([]byte(r.Header.Get("date")))
}
w.Write([]byte{'\n'})
writeAmzHeaders(w, r)
s.writeResource(w, r)
}
func (s *Service) writeResource(w io.Writer, r *http.Request) {
s.writeVhostBucket(w, strings.ToLower(r.Host))
path := r.URL.RequestURI()
if r.URL.RawQuery != "" {
path = path[:len(path)-len(r.URL.RawQuery)-1]
}
w.Write([]byte(path))
s.writeSubResource(w, r)
}
func (s *Service) writeVhostBucket(w io.Writer, host string) {
if i := strings.Index(host, ":"); i != -1 {
host = host[:i]
}
if host == s.Domain {
// no vhost - do nothing
} else if strings.HasSuffix(host, "."+s.Domain) {
// vhost - bucket may be in prefix
b := s.Bucket
if b == nil {
b = AmazonBucket
}
bucket := b(host[:len(host)-len(s.Domain)-1])
if bucket != "" {
w.Write([]byte{'/'})
w.Write([]byte(bucket))
}
} else {
// cname - bucket is host
w.Write([]byte{'/'})
w.Write([]byte(host))
}
}
func (s *Service) writeSubResource(w io.Writer, r *http.Request) {
var a []string
for k, vs := range r.URL.Query() {
if signParams[k] {
for _, v := range vs {
if v == "" {
a = append(a, k)
} else {
a = append(a, k+"="+v)
}
}
}
}
sort.Strings(a)
var p byte = '?'
for _, s := range a {
w.Write([]byte{p})
w.Write([]byte(s))
p = '&'
}
}
func writeAmzHeaders(w io.Writer, r *http.Request) {
var keys []string
for k, _ := range r.Header {
if strings.HasPrefix(strings.ToLower(k), "x-amz-") {
keys = append(keys, k)
}
}
sort.Strings(keys)
var a []string
for _, k := range keys {
v := r.Header[k]
a = append(a, strings.ToLower(k)+":"+strings.Join(v, ","))
}
for _, h := range a {
w.Write([]byte(h))
w.Write([]byte{'\n'})
}
}
package assertions
import (
"fmt"
"testing"
"time"
)
func TestShouldContain(t *testing.T) {
fail(t, so([]int{}, ShouldContain), "This assertion requires exactly 1 comparison values (you provided 0).")
fail(t, so([]int{}, ShouldContain, 1, 2, 3), "This assertion requires exactly 1 comparison values (you provided 3).")
fail(t, so(Thing1{}, ShouldContain, 1), "You must provide a valid container (was assertions.Thing1)!")
fail(t, so(nil, ShouldContain, 1), "You must provide a valid container (was <nil>)!")
fail(t, so([]int{1}, ShouldContain, 2), "Expected the container ([]int) to contain: '2' (but it didn't)!")
pass(t, so([]int{1}, ShouldContain, 1))
pass(t, so([]int{1, 2, 3}, ShouldContain, 2))
}
func TestShouldNotContain(t *testing.T) {
fail(t, so([]int{}, ShouldNotContain), "This assertion requires exactly 1 comparison values (you provided 0).")
fail(t, so([]int{}, ShouldNotContain, 1, 2, 3), "This assertion requires exactly 1 comparison values (you provided 3).")
fail(t, so(Thing1{}, ShouldNotContain, 1), "You must provide a valid container (was assertions.Thing1)!")
fail(t, so(nil, ShouldNotContain, 1), "You must provide a valid container (was <nil>)!")
fail(t, so([]int{1}, ShouldNotContain, 1), "Expected the container ([]int) NOT to contain: '1' (but it did)!")
fail(t, so([]int{1, 2, 3}, ShouldNotContain, 2), "Expected the container ([]int) NOT to contain: '2' (but it did)!")
pass(t, so([]int{1}, ShouldNotContain, 2))
}
func TestShouldBeIn(t *testing.T) {
fail(t, so(4, ShouldBeIn), shouldHaveProvidedCollectionMembers)
container := []int{1, 2, 3, 4}
pass(t, so(4, ShouldBeIn, container))
pass(t, so(4, ShouldBeIn, 1, 2, 3, 4))
fail(t, so(4, ShouldBeIn, 1, 2, 3), "Expected '4' to be in the container ([]interface {}, but it wasn't)!")
fail(t, so(4, ShouldBeIn, []int{1, 2, 3}), "Expected '4' to be in the container ([]int, but it wasn't)!")
}
func TestShouldNotBeIn(t *testing.T) {
fail(t, so(4, ShouldNotBeIn), shouldHaveProvidedCollectionMembers)
container := []int{1, 2, 3, 4}
pass(t, so(42, ShouldNotBeIn, container))
pass(t, so(42, ShouldNotBeIn, 1, 2, 3, 4))
fail(t, so(2, ShouldNotBeIn, 1, 2, 3), "Expected '2' NOT to be in the container ([]interface {}, but it was)!")
fail(t, so(2, ShouldNotBeIn, []int{1, 2, 3}), "Expected '2' NOT to be in the container ([]int, but it was)!")
}
func TestShouldBeEmpty(t *testing.T) {
fail(t, so(1, ShouldBeEmpty, 2, 3), "This assertion requires exactly 0 comparison values (you provided 2).")
pass(t, so([]int{}, ShouldBeEmpty)) // empty slice
pass(t, so([]interface{}{}, ShouldBeEmpty)) // empty slice
pass(t, so(map[string]int{}, ShouldBeEmpty)) // empty map
pass(t, so("", ShouldBeEmpty)) // empty string
pass(t, so(&[]int{}, ShouldBeEmpty)) // pointer to empty slice
pass(t, so(&[0]int{}, ShouldBeEmpty)) // pointer to empty array
pass(t, so(nil, ShouldBeEmpty)) // nil
pass(t, so(make(chan string), ShouldBeEmpty)) // empty channel
fail(t, so([]int{1}, ShouldBeEmpty), "Expected [1] to be empty (but it wasn't)!") // non-empty slice
fail(t, so([]interface{}{1}, ShouldBeEmpty), "Expected [1] to be empty (but it wasn't)!") // non-empty slice
fail(t, so(map[string]int{"hi": 0}, ShouldBeEmpty), "Expected map[hi:0] to be empty (but it wasn't)!") // non-empty map
fail(t, so("hi", ShouldBeEmpty), "Expected hi to be empty (but it wasn't)!") // non-empty string
fail(t, so(&[]int{1}, ShouldBeEmpty), "Expected &[1] to be empty (but it wasn't)!") // pointer to non-empty slice
fail(t, so(&[1]int{1}, ShouldBeEmpty), "Expected &[1] to be empty (but it wasn't)!") // pointer to non-empty array
c := make(chan int, 1) // non-empty channel
go func() { c <- 1 }()
time.Sleep(time.Millisecond)
fail(t, so(c, ShouldBeEmpty), fmt.Sprintf("Expected %+v to be empty (but it wasn't)!", c))
}
func TestShouldNotBeEmpty(t *testing.T) {
fail(t, so(1, ShouldNotBeEmpty, 2, 3), "This assertion requires exactly 0 comparison values (you provided 2).")
fail(t, so([]int{}, ShouldNotBeEmpty), "Expected [] to NOT be empty (but it was)!") // empty slice
fail(t, so([]interface{}{}, ShouldNotBeEmpty), "Expected [] to NOT be empty (but it was)!") // empty slice
fail(t, so(map[string]int{}, ShouldNotBeEmpty), "Expected map[] to NOT be empty (but it was)!") // empty map
fail(t, so("", ShouldNotBeEmpty), "Expected to NOT be empty (but it was)!") // empty string
fail(t, so(&[]int{}, ShouldNotBeEmpty), "Expected &[] to NOT be empty (but it was)!") // pointer to empty slice
fail(t, so(&[0]int{}, ShouldNotBeEmpty), "Expected &[] to NOT be empty (but it was)!") // pointer to empty array
fail(t, so(nil, ShouldNotBeEmpty), "Expected <nil> to NOT be empty (but it was)!") // nil
c := make(chan int, 0) // non-empty channel
fail(t, so(c, ShouldNotBeEmpty), fmt.Sprintf("Expected %+v to NOT be empty (but it was)!", c)) // empty channel
pass(t, so([]int{1}, ShouldNotBeEmpty)) // non-empty slice
pass(t, so([]interface{}{1}, ShouldNotBeEmpty)) // non-empty slice
pass(t, so(map[string]int{"hi": 0}, ShouldNotBeEmpty)) // non-empty map
pass(t, so("hi", ShouldNotBeEmpty)) // non-empty string
pass(t, so(&[]int{1}, ShouldNotBeEmpty)) // pointer to non-empty slice
pass(t, so(&[1]int{1}, ShouldNotBeEmpty)) // pointer to non-empty array
c = make(chan int, 1)
go func() { c <- 1 }()
time.Sleep(time.Millisecond)
pass(t, so(c, ShouldNotBeEmpty))
}
// Copyright 2011 Aaron Jacobs. All Rights Reserved.
// Author: aaronjjacobs@gmail.com (Aaron Jacobs)
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package oglematchers_test
import (
"errors"
. "github.com/smartystreets/goconvey/convey/assertions/oglematchers"
. "github.com/smartystreets/goconvey/convey/assertions/ogletest"
)
////////////////////////////////////////////////////////////////////////
// Helpers
////////////////////////////////////////////////////////////////////////
type allOfFakeMatcher struct {
desc string
err error
}
func (m *allOfFakeMatcher) Matches(c interface{}) error {
return m.err
}
func (m *allOfFakeMatcher) Description() string {
return m.desc
}
type AllOfTest struct {
}
func init() { RegisterTestSuite(&AllOfTest{}) }
////////////////////////////////////////////////////////////////////////
// Tests
////////////////////////////////////////////////////////////////////////
func (t *AllOfTest) DescriptionWithEmptySet() {
m := AllOf()
ExpectEq("is anything", m.Description())
}
func (t *AllOfTest) DescriptionWithOneMatcher() {
m := AllOf(&allOfFakeMatcher{"taco", errors.New("")})
ExpectEq("taco", m.Description())
}
func (t *AllOfTest) DescriptionWithMultipleMatchers() {
m := AllOf(
&allOfFakeMatcher{"taco", errors.New("")},
&allOfFakeMatcher{"burrito", errors.New("")},
&allOfFakeMatcher{"enchilada", errors.New("")})
ExpectEq("taco, and burrito, and enchilada", m.Description())
}
func (t *AllOfTest) EmptySet() {
m := AllOf()
err := m.Matches(17)
ExpectEq(nil, err)
}
func (t *AllOfTest) OneMatcherReturnsFatalErrorAndSomeOthersFail() {
m := AllOf(
&allOfFakeMatcher{"", errors.New("")},
&allOfFakeMatcher{"", NewFatalError("taco")},
&allOfFakeMatcher{"", errors.New("")},
&allOfFakeMatcher{"", nil})
err := m.Matches(17)
ExpectTrue(isFatal(err))
ExpectThat(err, Error(Equals("taco")))
}
func (t *AllOfTest) OneMatcherReturnsNonFatalAndOthersSayTrue() {
m := AllOf(
&allOfFakeMatcher{"", nil},
&allOfFakeMatcher{"", errors.New("taco")},
&allOfFakeMatcher{"", nil})
err := m.Matches(17)
ExpectFalse(isFatal(err))
ExpectThat(err, Error(Equals("taco")))
}
func (t *AllOfTest) AllMatchersSayTrue() {
m := AllOf(
&allOfFakeMatcher{"", nil},
&allOfFakeMatcher{"", nil},
&allOfFakeMatcher{"", nil})
err := m.Matches(17)
ExpectEq(nil, err)
}
// Copyright 2011 Aaron Jacobs. All Rights Reserved.
// Author: aaronjjacobs@gmail.com (Aaron Jacobs)
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package oglematchers_test
import (
"errors"
. "github.com/smartystreets/goconvey/convey/assertions/oglematchers"
. "github.com/smartystreets/goconvey/convey/assertions/ogletest"
)
////////////////////////////////////////////////////////////////////////
// Helpers
////////////////////////////////////////////////////////////////////////
type fakeAnyOfMatcher struct {
desc string
err error
}
func (m *fakeAnyOfMatcher) Matches(c interface{}) error {
return m.err
}
func (m *fakeAnyOfMatcher) Description() string {
return m.desc
}
type AnyOfTest struct {
}
func init() { RegisterTestSuite(&AnyOfTest{}) }
////////////////////////////////////////////////////////////////////////
// Tests
////////////////////////////////////////////////////////////////////////
func (t *AnyOfTest) EmptySet() {
matcher := AnyOf()
err := matcher.Matches(0)
ExpectThat(err, Error(Equals("")))
}
func (t *AnyOfTest) OneTrue() {
matcher := AnyOf(
&fakeAnyOfMatcher{"", NewFatalError("foo")},
17,
&fakeAnyOfMatcher{"", errors.New("foo")},
&fakeAnyOfMatcher{"", nil},
&fakeAnyOfMatcher{"", errors.New("foo")},
)
err := matcher.Matches(0)
ExpectEq(nil, err)
}
func (t *AnyOfTest) OneEqual() {
matcher := AnyOf(
&fakeAnyOfMatcher{"", NewFatalError("foo")},
&fakeAnyOfMatcher{"", errors.New("foo")},
13,
"taco",
19,
&fakeAnyOfMatcher{"", errors.New("foo")},
)
err := matcher.Matches("taco")
ExpectEq(nil, err)
}
func (t *AnyOfTest) OneFatal() {
matcher := AnyOf(
&fakeAnyOfMatcher{"", errors.New("foo")},
17,
&fakeAnyOfMatcher{"", NewFatalError("taco")},
&fakeAnyOfMatcher{"", errors.New("foo")},
)
err := matcher.Matches(0)
ExpectThat(err, Error(Equals("taco")))
}
func (t *AnyOfTest) AllFalseAndNotEqual() {
matcher := AnyOf(
&fakeAnyOfMatcher{"", errors.New("foo")},
17,
&fakeAnyOfMatcher{"", errors.New("foo")},
19,
)
err := matcher.Matches(0)
ExpectThat(err, Error(Equals("")))
}
func (t *AnyOfTest) DescriptionForEmptySet() {
matcher := AnyOf()
ExpectEq("or()", matcher.Description())
}
func (t *AnyOfTest) DescriptionForNonEmptySet() {
matcher := AnyOf(
&fakeAnyOfMatcher{"taco", nil},
"burrito",
&fakeAnyOfMatcher{"enchilada", nil},
)
ExpectEq("or(taco, burrito, enchilada)", matcher.Description())
}
// Copyright 2011 Aaron Jacobs. All Rights Reserved.
// Author: aaronjjacobs@gmail.com (Aaron Jacobs)
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package oglematchers_test
import (
. "github.com/smartystreets/goconvey/convey/assertions/oglematchers"
. "github.com/smartystreets/goconvey/convey/assertions/ogletest"
)
////////////////////////////////////////////////////////////////////////
// Helpers
////////////////////////////////////////////////////////////////////////
type AnyTest struct {
}
func init() { RegisterTestSuite(&AnyTest{}) }
////////////////////////////////////////////////////////////////////////
// Tests
////////////////////////////////////////////////////////////////////////
func (t *AnyTest) Description() {
m := Any()
ExpectEq("is anything", m.Description())
}
func (t *AnyTest) Matches() {
var err error
m := Any()
err = m.Matches(nil)
ExpectEq(nil, err)
err = m.Matches(17)
ExpectEq(nil, err)
err = m.Matches("taco")
ExpectEq(nil, err)
}
// Copyright 2012 Aaron Jacobs. All Rights Reserved.
// Author: aaronjjacobs@gmail.com (Aaron Jacobs)
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package oglematchers_test
import (
. "github.com/smartystreets/goconvey/convey/assertions/oglematchers"
. "github.com/smartystreets/goconvey/convey/assertions/ogletest"
)
////////////////////////////////////////////////////////////////////////
// Helpers
////////////////////////////////////////////////////////////////////////
type ContainsTest struct{}
func init() { RegisterTestSuite(&ContainsTest{}) }
////////////////////////////////////////////////////////////////////////
// Tests
////////////////////////////////////////////////////////////////////////
func (t *ContainsTest) WrongTypeCandidates() {
m := Contains("")
ExpectEq("contains: ", m.Description())
var err error
// Nil candidate
err = m.Matches(nil)
ExpectTrue(isFatal(err))
ExpectThat(err, Error(HasSubstr("array")))
ExpectThat(err, Error(HasSubstr("slice")))
// String candidate
err = m.Matches("")
ExpectTrue(isFatal(err))
ExpectThat(err, Error(HasSubstr("array")))
ExpectThat(err, Error(HasSubstr("slice")))
// Map candidate
err = m.Matches(make(map[string]string))
ExpectTrue(isFatal(err))
ExpectThat(err, Error(HasSubstr("array")))
ExpectThat(err, Error(HasSubstr("slice")))
}
func (t *ContainsTest) NilArgument() {
m := Contains(nil)
ExpectEq("contains: is nil", m.Description())
var c interface{}
var err error
// Empty array of pointers
c = [...]*int{}
err = m.Matches(c)
ExpectThat(err, Error(Equals("")))
// Empty slice of pointers
c = []*int{}
err = m.Matches(c)
ExpectThat(err, Error(Equals("")))
// Non-empty array of integers
c = [...]int{17, 0, 19}
err = m.Matches(c)
ExpectThat(err, Error(Equals("")))
// Non-empty slice of integers
c = []int{17, 0, 19}
err = m.Matches(c)
ExpectThat(err, Error(Equals("")))
// Non-matching array of pointers
c = [...]*int{new(int), new(int)}
err = m.Matches(c)
ExpectThat(err, Error(Equals("")))
// Non-matching slice of pointers
c = []*int{new(int), new(int)}
err = m.Matches(c)
ExpectThat(err, Error(Equals("")))
// Matching array of pointers
c = [...]*int{new(int), nil, new(int)}
err = m.Matches(c)
ExpectEq(nil, err)
// Matching slice of pointers
c = []*int{new(int), nil, new(int)}
err = m.Matches(c)
ExpectEq(nil, err)
// Non-matching slice of pointers from matching array
someArray := [...]*int{new(int), nil, new(int)}
c = someArray[0:1]
err = m.Matches(c)
ExpectThat(err, Error(Equals("")))
}
func (t *ContainsTest) StringArgument() {
m := Contains("taco")
ExpectEq("contains: taco", m.Description())
var c interface{}
var err error
// Non-matching array of strings
c = [...]string{"burrito", "enchilada"}
err = m.Matches(c)
ExpectThat(err, Error(Equals("")))
// Non-matching slice of strings
c = []string{"burrito", "enchilada"}
err = m.Matches(c)
ExpectThat(err, Error(Equals("")))
// Matching array of strings
c = [...]string{"burrito", "taco", "enchilada"}
err = m.Matches(c)
ExpectEq(nil, err)
// Matching slice of strings
c = []string{"burrito", "taco", "enchilada"}
err = m.Matches(c)
ExpectEq(nil, err)
// Non-matching slice of strings from matching array
someArray := [...]string{"burrito", "taco", "enchilada"}
c = someArray[0:1]
err = m.Matches(c)
ExpectThat(err, Error(Equals("")))
}
func (t *ContainsTest) IntegerArgument() {
m := Contains(int(17))
ExpectEq("contains: 17", m.Description())
var c interface{}
var err error
// Non-matching array of integers
c = [...]int{13, 19}
err = m.Matches(c)
ExpectThat(err, Error(Equals("")))
// Non-matching slice of integers
c = []int{13, 19}
err = m.Matches(c)
ExpectThat(err, Error(Equals("")))
// Matching array of integers
c = [...]int{13, 17, 19}
err = m.Matches(c)
ExpectEq(nil, err)
// Matching slice of integers
c = []int{13, 17, 19}
err = m.Matches(c)
ExpectEq(nil, err)
// Non-matching slice of integers from matching array
someArray := [...]int{13, 17, 19}
c = someArray[0:1]
err = m.Matches(c)
ExpectThat(err, Error(Equals("")))
// Non-matching array of floats
c = [...]float32{13, 17.5, 19}
err = m.Matches(c)
ExpectThat(err, Error(Equals("")))
// Non-matching slice of floats
c = []float32{13, 17.5, 19}
err = m.Matches(c)
ExpectThat(err, Error(Equals("")))
// Matching array of floats
c = [...]float32{13, 17, 19}
err = m.Matches(c)
ExpectEq(nil, err)
// Matching slice of floats
c = []float32{13, 17, 19}
err = m.Matches(c)
ExpectEq(nil, err)
}
func (t *ContainsTest) MatcherArgument() {
m := Contains(HasSubstr("ac"))
ExpectEq("contains: has substring \"ac\"", m.Description())
var c interface{}
var err error
// Non-matching array of strings
c = [...]string{"burrito", "enchilada"}
err = m.Matches(c)
ExpectThat(err, Error(Equals("")))
// Non-matching slice of strings
c = []string{"burrito", "enchilada"}
err = m.Matches(c)
ExpectThat(err, Error(Equals("")))
// Matching array of strings
c = [...]string{"burrito", "taco", "enchilada"}
err = m.Matches(c)
ExpectEq(nil, err)
// Matching slice of strings
c = []string{"burrito", "taco", "enchilada"}
err = m.Matches(c)
ExpectEq(nil, err)
// Non-matching slice of strings from matching array
someArray := [...]string{"burrito", "taco", "enchilada"}
c = someArray[0:1]
err = m.Matches(c)
ExpectThat(err, Error(Equals("")))
}
// Copyright 2012 Aaron Jacobs. All Rights Reserved.
// Author: aaronjjacobs@gmail.com (Aaron Jacobs)
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package oglematchers_test
import (
"bytes"
"testing"
. "github.com/smartystreets/goconvey/convey/assertions/oglematchers"
. "github.com/smartystreets/goconvey/convey/assertions/ogletest"
)
////////////////////////////////////////////////////////////////////////
// Helpers
////////////////////////////////////////////////////////////////////////
type DeepEqualsTest struct{}
func init() { RegisterTestSuite(&DeepEqualsTest{}) }
////////////////////////////////////////////////////////////////////////
// Tests
////////////////////////////////////////////////////////////////////////
func (t *DeepEqualsTest) WrongTypeCandidateWithScalarValue() {
var x int = 17
m := DeepEquals(x)
var err error
// Nil candidate.
err = m.Matches(nil)
AssertNe(nil, err)
ExpectTrue(isFatal(err))
ExpectThat(err, Error(HasSubstr("type")))
ExpectThat(err, Error(HasSubstr("<nil>")))
// Int alias candidate.
type intAlias int
err = m.Matches(intAlias(x))
AssertNe(nil, err)
ExpectTrue(isFatal(err))
ExpectThat(err, Error(HasSubstr("type")))
ExpectThat(err, Error(HasSubstr("intAlias")))
// String candidate.
err = m.Matches("taco")
AssertNe(nil, err)
ExpectTrue(isFatal(err))
ExpectThat(err, Error(HasSubstr("type")))
ExpectThat(err, Error(HasSubstr("string")))
// Byte slice candidate.
err = m.Matches([]byte{})
AssertNe(nil, err)
ExpectTrue(isFatal(err))
ExpectThat(err, Error(HasSubstr("type")))
ExpectThat(err, Error(HasSubstr("[]uint8")))
// Other slice candidate.
err = m.Matches([]uint16{})
AssertNe(nil, err)
ExpectTrue(isFatal(err))
ExpectThat(err, Error(HasSubstr("type")))
ExpectThat(err, Error(HasSubstr("[]uint16")))
// Unsigned int candidate.
err = m.Matches(uint(17))
AssertNe(nil, err)
ExpectTrue(isFatal(err))
ExpectThat(err, Error(HasSubstr("type")))
ExpectThat(err, Error(HasSubstr("uint")))
}
func (t *DeepEqualsTest) WrongTypeCandidateWithByteSliceValue() {
x := []byte{}
m := DeepEquals(x)
var err error
// Nil candidate.
err = m.Matches(nil)
AssertNe(nil, err)
ExpectTrue(isFatal(err))
ExpectThat(err, Error(HasSubstr("type")))
ExpectThat(err, Error(HasSubstr("<nil>")))
// String candidate.
err = m.Matches("taco")
AssertNe(nil, err)
ExpectTrue(isFatal(err))
ExpectThat(err, Error(HasSubstr("type")))
ExpectThat(err, Error(HasSubstr("string")))
// Slice candidate with wrong value type.
err = m.Matches([]uint16{})
AssertNe(nil, err)
ExpectTrue(isFatal(err))
ExpectThat(err, Error(HasSubstr("type")))
ExpectThat(err, Error(HasSubstr("[]uint16")))
}
func (t *DeepEqualsTest) WrongTypeCandidateWithOtherSliceValue() {
x := []uint16{}
m := DeepEquals(x)
var err error
// Nil candidate.
err = m.Matches(nil)
AssertNe(nil, err)
ExpectTrue(isFatal(err))
ExpectThat(err, Error(HasSubstr("type")))
ExpectThat(err, Error(HasSubstr("<nil>")))
// String candidate.
err = m.Matches("taco")
AssertNe(nil, err)
ExpectTrue(isFatal(err))
ExpectThat(err, Error(HasSubstr("type")))
ExpectThat(err, Error(HasSubstr("string")))
// Byte slice candidate with wrong value type.
err = m.Matches([]byte{})
AssertNe(nil, err)
ExpectTrue(isFatal(err))
ExpectThat(err, Error(HasSubstr("type")))
ExpectThat(err, Error(HasSubstr("[]uint8")))
// Other slice candidate with wrong value type.
err = m.Matches([]uint32{})
AssertNe(nil, err)
ExpectTrue(isFatal(err))
ExpectThat(err, Error(HasSubstr("type")))
ExpectThat(err, Error(HasSubstr("[]uint32")))
}
func (t *DeepEqualsTest) WrongTypeCandidateWithNilLiteralValue() {
m := DeepEquals(nil)
var err error
// String candidate.
err = m.Matches("taco")
AssertNe(nil, err)
ExpectTrue(isFatal(err))
ExpectThat(err, Error(HasSubstr("type")))
ExpectThat(err, Error(HasSubstr("string")))
// Nil byte slice candidate.
err = m.Matches([]byte(nil))
AssertNe(nil, err)
ExpectTrue(isFatal(err))
ExpectThat(err, Error(HasSubstr("type")))
ExpectThat(err, Error(HasSubstr("[]uint8")))
// Nil other slice candidate.
err = m.Matches([]uint16(nil))
AssertNe(nil, err)
ExpectTrue(isFatal(err))
ExpectThat(err, Error(HasSubstr("type")))
ExpectThat(err, Error(HasSubstr("[]uint16")))
}
func (t *DeepEqualsTest) NilLiteralValue() {
m := DeepEquals(nil)
ExpectEq("deep equals: <nil>", m.Description())
var c interface{}
var err error
// Nil literal candidate.
c = nil
err = m.Matches(c)
ExpectEq(nil, err)
}
func (t *DeepEqualsTest) IntValue() {
m := DeepEquals(int(17))
ExpectEq("deep equals: 17", m.Description())
var c interface{}
var err error
// Matching int.
c = int(17)
err = m.Matches(c)
ExpectEq(nil, err)
// Non-matching int.
c = int(18)
err = m.Matches(c)
ExpectThat(err, Error(Equals("")))
}
func (t *DeepEqualsTest) ByteSliceValue() {
x := []byte{17, 19}
m := DeepEquals(x)
ExpectEq("deep equals: [17 19]", m.Description())
var c []byte
var err error
// Matching.
c = make([]byte, len(x))
AssertEq(len(x), copy(c, x))
err = m.Matches(c)
ExpectEq(nil, err)
// Nil slice.
c = []byte(nil)
err = m.Matches(c)
ExpectThat(err, Error(Equals("which is nil")))
// Prefix.
AssertGt(len(x), 1)
c = make([]byte, len(x)-1)
AssertEq(len(x)-1, copy(c, x))
err = m.Matches(c)
ExpectThat(err, Error(Equals("")))
// Suffix.
c = make([]byte, len(x)+1)
AssertEq(len(x), copy(c, x))
err = m.Matches(c)
ExpectThat(err, Error(Equals("")))
}
func (t *DeepEqualsTest) OtherSliceValue() {
x := []uint16{17, 19}
m := DeepEquals(x)
ExpectEq("deep equals: [17 19]", m.Description())
var c []uint16
var err error
// Matching.
c = make([]uint16, len(x))
AssertEq(len(x), copy(c, x))
err = m.Matches(c)
ExpectEq(nil, err)
// Nil slice.
c = []uint16(nil)
err = m.Matches(c)
ExpectThat(err, Error(Equals("which is nil")))
// Prefix.
AssertGt(len(x), 1)
c = make([]uint16, len(x)-1)
AssertEq(len(x)-1, copy(c, x))
err = m.Matches(c)
ExpectThat(err, Error(Equals("")))
// Suffix.
c = make([]uint16, len(x)+1)
AssertEq(len(x), copy(c, x))
err = m.Matches(c)
ExpectThat(err, Error(Equals("")))
}
func (t *DeepEqualsTest) NilByteSliceValue() {
x := []byte(nil)
m := DeepEquals(x)
ExpectEq("deep equals: <nil slice>", m.Description())
var c []byte
var err error
// Nil slice.
c = []byte(nil)
err = m.Matches(c)
ExpectEq(nil, err)
// Non-nil slice.
c = []byte{}
err = m.Matches(c)
ExpectThat(err, Error(Equals("")))
}
func (t *DeepEqualsTest) NilOtherSliceValue() {
x := []uint16(nil)
m := DeepEquals(x)
ExpectEq("deep equals: <nil slice>", m.Description())
var c []uint16
var err error
// Nil slice.
c = []uint16(nil)
err = m.Matches(c)
ExpectEq(nil, err)
// Non-nil slice.
c = []uint16{}
err = m.Matches(c)
ExpectThat(err, Error(Equals("")))
}
////////////////////////////////////////////////////////////////////////
// Benchmarks
////////////////////////////////////////////////////////////////////////
func benchmarkWithSize(b *testing.B, size int) {
b.StopTimer()
buf := bytes.Repeat([]byte{0x01}, size)
bufCopy := make([]byte, size)
copy(bufCopy, buf)
matcher := DeepEquals(buf)
b.StartTimer()
for i := 0; i < b.N; i++ {
matcher.Matches(bufCopy)
}
b.SetBytes(int64(size))
}
func BenchmarkShortByteSlice(b *testing.B) {
benchmarkWithSize(b, 256)
}
func BenchmarkLongByteSlice(b *testing.B) {
benchmarkWithSize(b, 1<<24)
}
// Copyright 2012 Aaron Jacobs. All Rights Reserved.
// Author: aaronjjacobs@gmail.com (Aaron Jacobs)
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package oglematchers_test
import (
. "github.com/smartystreets/goconvey/convey/assertions/oglematchers"
. "github.com/smartystreets/goconvey/convey/assertions/ogletest"
)
////////////////////////////////////////////////////////////////////////
// Helpers
////////////////////////////////////////////////////////////////////////
type ElementsAreTest struct {
}
func init() { RegisterTestSuite(&ElementsAreTest{}) }
////////////////////////////////////////////////////////////////////////
// Tests
////////////////////////////////////////////////////////////////////////
func (t *ElementsAreTest) EmptySet() {
m := ElementsAre()
ExpectEq("elements are: []", m.Description())
var c []interface{}
var err error
// No candidates.
c = []interface{}{}
err = m.Matches(c)
ExpectEq(nil, err)
// One candidate.
c = []interface{}{17}
err = m.Matches(c)
ExpectThat(err, Error(HasSubstr("length 1")))
}
func (t *ElementsAreTest) OneMatcher() {
m := ElementsAre(LessThan(17))
ExpectEq("elements are: [less than 17]", m.Description())
var c []interface{}
var err error
// No candidates.
c = []interface{}{}
err = m.Matches(c)
ExpectThat(err, Error(HasSubstr("length 0")))
// Matching candidate.
c = []interface{}{16}
err = m.Matches(c)
ExpectEq(nil, err)
// Non-matching candidate.
c = []interface{}{19}
err = m.Matches(c)
ExpectNe(nil, err)
// Two candidates.
c = []interface{}{17, 19}
err = m.Matches(c)
ExpectThat(err, Error(HasSubstr("length 2")))
}
func (t *ElementsAreTest) OneValue() {
m := ElementsAre(17)
ExpectEq("elements are: [17]", m.Description())
var c []interface{}
var err error
// No candidates.
c = []interface{}{}
err = m.Matches(c)
ExpectThat(err, Error(HasSubstr("length 0")))
// Matching int.
c = []interface{}{int(17)}
err = m.Matches(c)
ExpectEq(nil, err)
// Matching float.
c = []interface{}{float32(17)}
err = m.Matches(c)
ExpectEq(nil, err)
// Non-matching candidate.
c = []interface{}{19}
err = m.Matches(c)
ExpectNe(nil, err)
// Two candidates.
c = []interface{}{17, 19}
err = m.Matches(c)
ExpectThat(err, Error(HasSubstr("length 2")))
}
func (t *ElementsAreTest) MultipleElements() {
m := ElementsAre("taco", LessThan(17))
ExpectEq("elements are: [taco, less than 17]", m.Description())
var c []interface{}
var err error
// One candidate.
c = []interface{}{17}
err = m.Matches(c)
ExpectThat(err, Error(HasSubstr("length 1")))
// Both matching.
c = []interface{}{"taco", 16}
err = m.Matches(c)
ExpectEq(nil, err)
// First non-matching.
c = []interface{}{"burrito", 16}
err = m.Matches(c)
ExpectThat(err, Error(Equals("whose element 0 doesn't match")))
// Second non-matching.
c = []interface{}{"taco", 17}
err = m.Matches(c)
ExpectThat(err, Error(Equals("whose element 1 doesn't match")))
// Three candidates.
c = []interface{}{"taco", 17, 19}
err = m.Matches(c)
ExpectThat(err, Error(HasSubstr("length 3")))
}
func (t *ElementsAreTest) ArrayCandidates() {
m := ElementsAre("taco", LessThan(17))
var err error
// One candidate.
err = m.Matches([1]interface{}{"taco"})
ExpectThat(err, Error(HasSubstr("length 1")))
// Both matching.
err = m.Matches([2]interface{}{"taco", 16})
ExpectEq(nil, err)
// First non-matching.
err = m.Matches([2]interface{}{"burrito", 16})
ExpectThat(err, Error(Equals("whose element 0 doesn't match")))
}
func (t *ElementsAreTest) WrongTypeCandidate() {
m := ElementsAre("taco")
var err error
// String candidate.
err = m.Matches("taco")
ExpectTrue(isFatal(err))
ExpectThat(err, Error(HasSubstr("array")))
ExpectThat(err, Error(HasSubstr("slice")))
// Map candidate.
err = m.Matches(map[string]string{})
ExpectTrue(isFatal(err))
ExpectThat(err, Error(HasSubstr("array")))
ExpectThat(err, Error(HasSubstr("slice")))
// Nil candidate.
err = m.Matches(nil)
ExpectTrue(isFatal(err))
ExpectThat(err, Error(HasSubstr("array")))
ExpectThat(err, Error(HasSubstr("slice")))
}
func (t *ElementsAreTest) PropagatesFatality() {
m := ElementsAre(LessThan(17))
ExpectEq("elements are: [less than 17]", m.Description())
var c []interface{}
var err error
// Non-fatal error.
c = []interface{}{19}
err = m.Matches(c)
AssertNe(nil, err)
ExpectFalse(isFatal(err))
// Fatal error.
c = []interface{}{"taco"}
err = m.Matches(c)
AssertNe(nil, err)
ExpectTrue(isFatal(err))
}
// Copyright 2011 Aaron Jacobs. All Rights Reserved.
// Author: aaronjjacobs@gmail.com (Aaron Jacobs)
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package oglematchers_test
import (
"errors"
. "github.com/smartystreets/goconvey/convey/assertions/oglematchers"
. "github.com/smartystreets/goconvey/convey/assertions/ogletest"
)
////////////////////////////////////////////////////////////////////////
// Helpers
////////////////////////////////////////////////////////////////////////
type ErrorTest struct {
matcherCalled bool
suppliedCandidate interface{}
wrappedError error
matcher Matcher
}
func init() { RegisterTestSuite(&ErrorTest{}) }
func (t *ErrorTest) SetUp(i *TestInfo) {
wrapped := &fakeMatcher{
func(c interface{}) error {
t.matcherCalled = true
t.suppliedCandidate = c
return t.wrappedError
},
"is foo",
}
t.matcher = Error(wrapped)
}
func isFatal(err error) bool {
_, isFatal := err.(*FatalError)
return isFatal
}
////////////////////////////////////////////////////////////////////////
// Tests
////////////////////////////////////////////////////////////////////////
func (t *ErrorTest) Description() {
ExpectThat(t.matcher.Description(), Equals("error is foo"))
}
func (t *ErrorTest) CandidateIsNil() {
err := t.matcher.Matches(nil)
ExpectThat(t.matcherCalled, Equals(false))
ExpectThat(err.Error(), Equals("which is not an error"))
ExpectTrue(isFatal(err))
}
func (t *ErrorTest) CandidateIsString() {
err := t.matcher.Matches("taco")
ExpectThat(t.matcherCalled, Equals(false))
ExpectThat(err.Error(), Equals("which is not an error"))
ExpectTrue(isFatal(err))
}
func (t *ErrorTest) CallsWrappedMatcher() {
candidate := errors.New("taco")
t.matcher.Matches(candidate)
ExpectThat(t.matcherCalled, Equals(true))
ExpectThat(t.suppliedCandidate, Equals("taco"))
}
func (t *ErrorTest) ReturnsWrappedMatcherResult() {
t.wrappedError = errors.New("burrito")
err := t.matcher.Matches(errors.New(""))
ExpectThat(err, Equals(t.wrappedError))
}
// Copyright 2011 Aaron Jacobs. All Rights Reserved.
// Author: aaronjjacobs@gmail.com (Aaron Jacobs)
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package oglematchers_test
import (
. "github.com/smartystreets/goconvey/convey/assertions/oglematchers"
. "github.com/smartystreets/goconvey/convey/assertions/ogletest"
)
////////////////////////////////////////////////////////////////////////
// Helpers
////////////////////////////////////////////////////////////////////////
type HasSubstrTest struct {
}
func init() { RegisterTestSuite(&HasSubstrTest{}) }
////////////////////////////////////////////////////////////////////////
// Tests
////////////////////////////////////////////////////////////////////////
func (t *HasSubstrTest) Description() {
matcher := HasSubstr("taco")
ExpectThat(matcher.Description(), Equals("has substring \"taco\""))
}
func (t *HasSubstrTest) CandidateIsNil() {
matcher := HasSubstr("")
err := matcher.Matches(nil)
ExpectThat(err, Error(Equals("which is not a string")))
ExpectTrue(isFatal(err))
}
func (t *HasSubstrTest) CandidateIsInteger() {
matcher := HasSubstr("")
err := matcher.Matches(17)
ExpectThat(err, Error(Equals("which is not a string")))
ExpectTrue(isFatal(err))
}
func (t *HasSubstrTest) CandidateIsByteSlice() {
matcher := HasSubstr("")
err := matcher.Matches([]byte{17})
ExpectThat(err, Error(Equals("which is not a string")))
ExpectTrue(isFatal(err))
}
func (t *HasSubstrTest) CandidateDoesntHaveSubstring() {
matcher := HasSubstr("taco")
err := matcher.Matches("tac")
ExpectThat(err, Error(Equals("")))
ExpectFalse(isFatal(err))
}
func (t *HasSubstrTest) CandidateEqualsArg() {
matcher := HasSubstr("taco")
err := matcher.Matches("taco")
ExpectThat(err, Equals(nil))
}
func (t *HasSubstrTest) CandidateHasProperSubstring() {
matcher := HasSubstr("taco")
err := matcher.Matches("burritos and tacos")
ExpectThat(err, Equals(nil))
}
func (t *HasSubstrTest) EmptyStringIsAlwaysSubString() {
matcher := HasSubstr("")
err := matcher.Matches("asdf")
ExpectThat(err, Equals(nil))
}
// Copyright 2011 Aaron Jacobs. All Rights Reserved.
// Author: aaronjjacobs@gmail.com (Aaron Jacobs)
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package oglematchers_test
import (
. "github.com/smartystreets/goconvey/convey/assertions/oglematchers"
. "github.com/smartystreets/goconvey/convey/assertions/ogletest"
)
////////////////////////////////////////////////////////////////////////
// Helpers
////////////////////////////////////////////////////////////////////////
type MatchesRegexpTest struct {
}
func init() { RegisterTestSuite(&MatchesRegexpTest{}) }
////////////////////////////////////////////////////////////////////////
// Tests
////////////////////////////////////////////////////////////////////////
func (t *MatchesRegexpTest) Description() {
m := MatchesRegexp("foo.*bar")
ExpectEq("matches regexp \"foo.*bar\"", m.Description())
}
func (t *MatchesRegexpTest) InvalidRegexp() {
ExpectThat(
func() { MatchesRegexp("(foo") },
Panics(HasSubstr("missing closing )")))
}
func (t *MatchesRegexpTest) CandidateIsNil() {
m := MatchesRegexp("")
err := m.Matches(nil)
ExpectThat(err, Error(Equals("which is not a string or []byte")))
ExpectTrue(isFatal(err))
}
func (t *MatchesRegexpTest) CandidateIsInteger() {
m := MatchesRegexp("")
err := m.Matches(17)
ExpectThat(err, Error(Equals("which is not a string or []byte")))
ExpectTrue(isFatal(err))
}
func (t *MatchesRegexpTest) NonMatchingCandidates() {
m := MatchesRegexp("fo[op]\\s+x")
var err error
err = m.Matches("fon x")
ExpectThat(err, Error(Equals("")))
ExpectFalse(isFatal(err))
err = m.Matches("fopx")
ExpectThat(err, Error(Equals("")))
ExpectFalse(isFatal(err))
err = m.Matches("fop ")
ExpectThat(err, Error(Equals("")))
ExpectFalse(isFatal(err))
}
func (t *MatchesRegexpTest) MatchingCandidates() {
m := MatchesRegexp("fo[op]\\s+x")
var err error
err = m.Matches("foo x")
ExpectEq(nil, err)
err = m.Matches("fop x")
ExpectEq(nil, err)
err = m.Matches("blah blah foo x blah blah")
ExpectEq(nil, err)
}
// Copyright 2011 Aaron Jacobs. All Rights Reserved.
// Author: aaronjjacobs@gmail.com (Aaron Jacobs)
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package oglematchers_test
import (
"errors"
"testing"
. "github.com/smartystreets/goconvey/convey/assertions/oglematchers"
. "github.com/smartystreets/goconvey/convey/assertions/ogletest"
)
////////////////////////////////////////////////////////////////////////
// Helpers
////////////////////////////////////////////////////////////////////////
type fakeMatcher struct {
matchFunc func(interface{}) error
description string
}
func (m *fakeMatcher) Matches(c interface{}) error {
return m.matchFunc(c)
}
func (m *fakeMatcher) Description() string {
return m.description
}
type NotTest struct {
}
func init() { RegisterTestSuite(&NotTest{}) }
func TestOgletest(t *testing.T) { RunTests(t) }
////////////////////////////////////////////////////////////////////////
// Tests
////////////////////////////////////////////////////////////////////////
func (t *NotTest) CallsWrapped() {
var suppliedCandidate interface{}
matchFunc := func(c interface{}) error {
suppliedCandidate = c
return nil
}
wrapped := &fakeMatcher{matchFunc, ""}
matcher := Not(wrapped)
matcher.Matches(17)
ExpectThat(suppliedCandidate, Equals(17))
}
func (t *NotTest) WrappedReturnsTrue() {
matchFunc := func(c interface{}) error {
return nil
}
wrapped := &fakeMatcher{matchFunc, ""}
matcher := Not(wrapped)
err := matcher.Matches(0)
ExpectThat(err, Error(Equals("")))
}
func (t *NotTest) WrappedReturnsNonFatalError() {
matchFunc := func(c interface{}) error {
return errors.New("taco")
}
wrapped := &fakeMatcher{matchFunc, ""}
matcher := Not(wrapped)
err := matcher.Matches(0)
ExpectEq(nil, err)
}
func (t *NotTest) WrappedReturnsFatalError() {
matchFunc := func(c interface{}) error {
return NewFatalError("taco")
}
wrapped := &fakeMatcher{matchFunc, ""}
matcher := Not(wrapped)
err := matcher.Matches(0)
ExpectThat(err, Error(Equals("taco")))
}
func (t *NotTest) Description() {
wrapped := &fakeMatcher{nil, "taco"}
matcher := Not(wrapped)
ExpectEq("not(taco)", matcher.Description())
}
// Copyright 2011 Aaron Jacobs. All Rights Reserved.
// Author: aaronjjacobs@gmail.com (Aaron Jacobs)
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package oglematchers_test
import (
"errors"
. "github.com/smartystreets/goconvey/convey/assertions/oglematchers"
. "github.com/smartystreets/goconvey/convey/assertions/ogletest"
)
////////////////////////////////////////////////////////////////////////
// Helpers
////////////////////////////////////////////////////////////////////////
type PanicsTest struct {
matcherCalled bool
suppliedCandidate interface{}
wrappedError error
matcher Matcher
}
func init() { RegisterTestSuite(&PanicsTest{}) }
func (t *PanicsTest) SetUp(i *TestInfo) {
wrapped := &fakeMatcher{
func(c interface{}) error {
t.matcherCalled = true
t.suppliedCandidate = c
return t.wrappedError
},
"foo",
}
t.matcher = Panics(wrapped)
}
////////////////////////////////////////////////////////////////////////
// Tests
////////////////////////////////////////////////////////////////////////
func (t *PanicsTest) Description() {
ExpectThat(t.matcher.Description(), Equals("panics with: foo"))
}
func (t *PanicsTest) CandidateIsNil() {
err := t.matcher.Matches(nil)
ExpectThat(err, Error(Equals("which is not a zero-arg function")))
ExpectTrue(isFatal(err))
}
func (t *PanicsTest) CandidateIsString() {
err := t.matcher.Matches("taco")
ExpectThat(err, Error(Equals("which is not a zero-arg function")))
ExpectTrue(isFatal(err))
}
func (t *PanicsTest) CandidateTakesArgs() {
err := t.matcher.Matches(func(i int) string { return "" })
ExpectThat(err, Error(Equals("which is not a zero-arg function")))
ExpectTrue(isFatal(err))
}
func (t *PanicsTest) CallsFunction() {
callCount := 0
t.matcher.Matches(func() string {
callCount++
return ""
})
ExpectThat(callCount, Equals(1))
}
func (t *PanicsTest) FunctionDoesntPanic() {
err := t.matcher.Matches(func() {})
ExpectThat(err, Error(Equals("which didn't panic")))
ExpectFalse(isFatal(err))
}
func (t *PanicsTest) CallsWrappedMatcher() {
expectedErr := 17
t.wrappedError = errors.New("")
t.matcher.Matches(func() { panic(expectedErr) })
ExpectThat(t.suppliedCandidate, Equals(expectedErr))
}
func (t *PanicsTest) WrappedReturnsTrue() {
err := t.matcher.Matches(func() { panic("") })
ExpectEq(nil, err)
}
func (t *PanicsTest) WrappedReturnsFatalErrorWithoutText() {
t.wrappedError = NewFatalError("")
err := t.matcher.Matches(func() { panic(17) })
ExpectThat(err, Error(Equals("which panicked with: 17")))
ExpectFalse(isFatal(err))
}
func (t *PanicsTest) WrappedReturnsFatalErrorWithText() {
t.wrappedError = NewFatalError("which blah")
err := t.matcher.Matches(func() { panic(17) })
ExpectThat(err, Error(Equals("which panicked with: 17, which blah")))
ExpectFalse(isFatal(err))
}
func (t *PanicsTest) WrappedReturnsNonFatalErrorWithoutText() {
t.wrappedError = errors.New("")
err := t.matcher.Matches(func() { panic(17) })
ExpectThat(err, Error(Equals("which panicked with: 17")))
ExpectFalse(isFatal(err))
}
func (t *PanicsTest) WrappedReturnsNonFatalErrorWithText() {
t.wrappedError = errors.New("which blah")
err := t.matcher.Matches(func() { panic(17) })
ExpectThat(err, Error(Equals("which panicked with: 17, which blah")))
ExpectFalse(isFatal(err))
}
// Copyright 2012 Aaron Jacobs. All Rights Reserved.
// Author: aaronjjacobs@gmail.com (Aaron Jacobs)
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package oglematchers_test
import (
"errors"
"testing"
. "github.com/smartystreets/goconvey/convey/assertions/oglematchers"
. "github.com/smartystreets/goconvey/convey/assertions/ogletest"
)
////////////////////////////////////////////////////////////////////////
// Helpers
////////////////////////////////////////////////////////////////////////
type PointeeTest struct{}
func init() { RegisterTestSuite(&PointeeTest{}) }
func TestPointee(t *testing.T) { RunTests(t) }
////////////////////////////////////////////////////////////////////////
// Tests
////////////////////////////////////////////////////////////////////////
func (t *PointeeTest) Description() {
wrapped := &fakeMatcher{nil, "taco"}
matcher := Pointee(wrapped)
ExpectEq("pointee(taco)", matcher.Description())
}
func (t *PointeeTest) CandidateIsNotAPointer() {
matcher := Pointee(HasSubstr(""))
err := matcher.Matches([]byte{})
ExpectThat(err, Error(Equals("which is not a pointer")))
ExpectTrue(isFatal(err))
}
func (t *PointeeTest) CandidateIsANilLiteral() {
matcher := Pointee(HasSubstr(""))
err := matcher.Matches(nil)
ExpectThat(err, Error(Equals("which is not a pointer")))
ExpectTrue(isFatal(err))
}
func (t *PointeeTest) CandidateIsANilPointer() {
matcher := Pointee(HasSubstr(""))
err := matcher.Matches((*int)(nil))
ExpectThat(err, Error(Equals("")))
ExpectTrue(isFatal(err))
}
func (t *PointeeTest) CallsWrapped() {
var suppliedCandidate interface{}
matchFunc := func(c interface{}) error {
suppliedCandidate = c
return nil
}
wrapped := &fakeMatcher{matchFunc, ""}
matcher := Pointee(wrapped)
someSlice := []byte{}
matcher.Matches(&someSlice)
ExpectThat(suppliedCandidate, IdenticalTo(someSlice))
}
func (t *PointeeTest) WrappedReturnsOkay() {
matchFunc := func(c interface{}) error {
return nil
}
wrapped := &fakeMatcher{matchFunc, ""}
matcher := Pointee(wrapped)
err := matcher.Matches(new(int))
ExpectEq(nil, err)
}
func (t *PointeeTest) WrappedReturnsNonFatalNonEmptyError() {
matchFunc := func(c interface{}) error {
return errors.New("taco")
}
wrapped := &fakeMatcher{matchFunc, ""}
matcher := Pointee(wrapped)
i := 17
err := matcher.Matches(&i)
ExpectFalse(isFatal(err))
ExpectThat(err, Error(Equals("taco")))
}
func (t *PointeeTest) WrappedReturnsNonFatalEmptyError() {
matchFunc := func(c interface{}) error {
return errors.New("")
}
wrapped := &fakeMatcher{matchFunc, ""}
matcher := Pointee(wrapped)
i := 17
err := matcher.Matches(&i)
ExpectFalse(isFatal(err))
ExpectThat(err, Error(HasSubstr("whose pointee")))
ExpectThat(err, Error(HasSubstr("17")))
}
func (t *PointeeTest) WrappedReturnsFatalNonEmptyError() {
matchFunc := func(c interface{}) error {
return NewFatalError("taco")
}
wrapped := &fakeMatcher{matchFunc, ""}
matcher := Pointee(wrapped)
i := 17
err := matcher.Matches(&i)
ExpectTrue(isFatal(err))
ExpectThat(err, Error(Equals("taco")))
}
func (t *PointeeTest) WrappedReturnsFatalEmptyError() {
matchFunc := func(c interface{}) error {
return NewFatalError("")
}
wrapped := &fakeMatcher{matchFunc, ""}
matcher := Pointee(wrapped)
i := 17
err := matcher.Matches(&i)
ExpectTrue(isFatal(err))
ExpectThat(err, Error(HasSubstr("whose pointee")))
ExpectThat(err, Error(HasSubstr("17")))
}
`oglemock` is a mocking framework for the Go programming language with the
following features:
* An extensive and extensible set of matchers for expressing call
expectations (provided by the [oglematchers][] package).
* Clean, readable output that tells you exactly what you need to know.
* Style and semantics similar to [Google Mock][googlemock] and
[Google JS Test][google-js-test].
* Seamless integration with the [ogletest][] unit testing framework.
It can be integrated into any testing framework (including Go's `testing`
package), but out of the box support is built in to [ogletest][] and that is the
easiest place to use it.
Installation
------------
First, make sure you have installed Go 1.0.2 or newer. See
[here][golang-install] for instructions.
Use the following command to install `oglemock` and its dependencies, and to
keep them up to date:
go get -u github.com/smartystreets/goconvey/convey/assertions/oglemock
go get -u github.com/smartystreets/goconvey/convey/assertions/oglemock/createmock
Those commands will install the `oglemock` package itself, along with the
`createmock` tool that is used to auto-generate mock types.
Generating and using mock types
-------------------------------
Automatically generating a mock implementation of an interface is easy. If you
want to mock interfaces `Bar` and `Baz` from package `foo`, simply run the
following:
createmock foo Bar Baz
That will print source code that can be saved to a file and used in your tests.
For example, to create a `mock_io` package containing mock implementations of
`io.Reader` and `io.Writer`:
mkdir mock_io
createmock io Reader Writer > mock_io/mock_io.go
The new package will be named `mock_io`, and contain types called `MockReader`
and `MockWriter`, which implement `io.Reader` and `io.Writer` respectively.
For each generated mock type, there is a corresponding function for creating an
instance of that type given a `Controller` object (see below). For example, to
create a mock reader:
```go
someController := [...] // See next section.
someReader := mock_io.NewMockReader(someController, "Mock file reader")
```
The snippet above creates a mock `io.Reader` that reports failures to
`someController`. The reader can subsequently have expectations set up and be
passed to your code under test that uses an `io.Reader`.
Getting ahold of a controller
-----------------------------
[oglemock.Controller][controller-ref] is used to create mock objects, and to set
up and verify expectations for them. You can create one by calling
`NewController` with an `ErrorReporter`, which is the basic type used to
interface between `oglemock` and the testing framework within which it is being
used.
If you are using [ogletest][] you don't need to worry about any of this, since
the `TestInfo` struct provided to your test's `SetUp` function already contains
a working `Controller` that you can use to create mock object, and you can use
the built-in `ExpectCall` function for setting expectations. (See the
[ogletest documentation][ogletest-docs] for more info.) Otherwise, you will need
to implement the simple [ErrorReporter interface][reporter-ref] for your test
environment.
Documentation
-------------
For thorough documentation, including information on how to set up expectations,
see [here][oglemock-docs].
[controller-ref]: http://gopkgdoc.appspot.com/pkg/github.com/smartystreets/goconvey/convey/assertions/oglemock#Controller
[reporter-ref]: http://gopkgdoc.appspot.com/pkg/github.com/smartystreets/goconvey/convey/assertions/oglemock#ErrorReporter
[golang-install]: http://golang.org/doc/install.html
[google-js-test]: http://code.google.com/p/google-js-test/
[googlemock]: http://code.google.com/p/googlemock/
[oglematchers]: https://github.com/smartystreets/goconvey/convey/assertions/oglematchers
[oglemock-docs]: http://gopkgdoc.appspot.com/pkg/github.com/smartystreets/goconvey/convey/assertions/oglemock
[ogletest]: https://github.com/smartystreets/goconvey/convey/assertions/oglematchers
[ogletest-docs]: http://gopkgdoc.appspot.com/pkg/github.com/smartystreets/goconvey/convey/assertions/ogletest
// Copyright 2011 Aaron Jacobs. All Rights Reserved.
// Author: aaronjjacobs@gmail.com (Aaron Jacobs)
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package oglemock
import (
"reflect"
)
// Action represents an action to be taken in response to a call to a mock
// method.
type Action interface {
// Set the signature of the function with which this action is being used.
// This must be called before Invoke is called.
SetSignature(signature reflect.Type) error
// Invoke runs the specified action, given the arguments to the mock method.
// It returns zero or more values that may be treated as the return values of
// the method. If the action doesn't return any values, it may return the nil
// slice.
//
// You must call SetSignature before calling Invoke.
Invoke(methodArgs []interface{}) []interface{}
}
// Copyright 2012 Aaron Jacobs. All Rights Reserved.
// Author: aaronjjacobs@gmail.com (Aaron Jacobs)
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// createmock is used to generate source code for mock versions of interfaces
// from installed packages.
package main
import (
"errors"
"flag"
"fmt"
"go/build"
"io/ioutil"
"log"
"os"
"os/exec"
"path"
"regexp"
"text/template"
// Ensure that the generate package, which is used by the generated code, is
// installed by goinstall.
_ "github.com/smartystreets/goconvey/convey/assertions/oglemock/generate"
)
// A template for generated code that is used to print the result.
const tmplStr = `
{{$inputPkg := .InputPkg}}
{{$outputPkg := .OutputPkg}}
package main
import (
{{range $identifier, $import := .Imports}}
{{$identifier}} "{{$import}}"
{{end}}
)
func getTypeForPtr(ptr interface{}) reflect.Type {
return reflect.TypeOf(ptr).Elem()
}
func main() {
// Reduce noise in logging output.
log.SetFlags(0)
interfaces := []reflect.Type{
{{range $typeName := .TypeNames}}
getTypeForPtr((*{{base $inputPkg}}.{{$typeName}})(nil)),
{{end}}
}
err := generate.GenerateMockSource(os.Stdout, "{{$outputPkg}}", interfaces)
if err != nil {
log.Fatalf("Error generating mock source: %v", err)
}
}
`
// A map from import identifier to package to use that identifier for,
// containing elements for each import needed by the generated code.
type importMap map[string]string
type tmplArg struct {
InputPkg string
OutputPkg string
// Imports needed by the generated code.
Imports importMap
// Types to be mocked, relative to their package's name.
TypeNames []string
}
var unknownPackageRegexp = regexp.MustCompile(
`tool\.go:\d+:\d+: cannot find package "([^"]+)"`)
var undefinedInterfaceRegexp = regexp.MustCompile(`tool\.go:\d+: undefined: [\pL_0-9]+\.([\pL_0-9]+)`)
// Does the 'go build' output indicate that a package wasn't found? If so,
// return the name of the package.
func findUnknownPackage(output []byte) *string {
if match := unknownPackageRegexp.FindSubmatch(output); match != nil {
res := string(match[1])
return &res
}
return nil
}
// Does the 'go build' output indicate that an interface wasn't found? If so,
// return the name of the interface.
func findUndefinedInterface(output []byte) *string {
if match := undefinedInterfaceRegexp.FindSubmatch(output); match != nil {
res := string(match[1])
return &res
}
return nil
}
// Split out from main so that deferred calls are executed even in the event of
// an error.
func run() error {
// Reduce noise in logging output.
log.SetFlags(0)
// Check the command-line arguments.
flag.Parse()
cmdLineArgs := flag.Args()
if len(cmdLineArgs) < 2 {
return errors.New("Usage: createmock [package] [interface ...]")
}
// Create a temporary directory inside of $GOPATH to hold generated code.
buildPkg, err := build.Import("github.com/smartystreets/goconvey/convey/assertions/oglemock", "", build.FindOnly)
if err != nil {
return errors.New(fmt.Sprintf("Couldn't find oglemock in $GOPATH: %v", err))
}
tmpDir, err := ioutil.TempDir(buildPkg.SrcRoot, "tmp-createmock-")
if err != nil {
return errors.New(fmt.Sprintf("Creating temp dir: %v", err))
}
defer os.RemoveAll(tmpDir)
// Create a file to hold generated code.
codeFile, err := os.Create(path.Join(tmpDir, "tool.go"))
if err != nil {
return errors.New(fmt.Sprintf("Couldn't create a file to hold code: %v", err))
}
// Create an appropriate path for the built binary.
binaryPath := path.Join(tmpDir, "tool")
// Create an appropriate template argument.
var arg tmplArg
arg.InputPkg = cmdLineArgs[0]
arg.OutputPkg = "mock_" + path.Base(arg.InputPkg)
arg.TypeNames = cmdLineArgs[1:]
arg.Imports = make(importMap)
arg.Imports[path.Base(arg.InputPkg)] = arg.InputPkg
arg.Imports["generate"] = "github.com/smartystreets/goconvey/convey/assertions/oglemock/generate"
arg.Imports["log"] = "log"
arg.Imports["os"] = "os"
arg.Imports["reflect"] = "reflect"
// Execute the template to generate code that will itself generate the mock
// code. Write the code to the temp file.
tmpl := template.Must(
template.New("code").Funcs(
template.FuncMap{
"base": path.Base,
}).Parse(tmplStr))
if err := tmpl.Execute(codeFile, arg); err != nil {
return errors.New(fmt.Sprintf("Error executing template: %v", err))
}
codeFile.Close()
// Attempt to build the code.
cmd := exec.Command("go", "build", "-o", binaryPath)
cmd.Dir = tmpDir
buildOutput, err := cmd.CombinedOutput()
if err != nil {
// Did the compilation fail due to the user-specified package not being found?
if pkg := findUnknownPackage(buildOutput); pkg != nil && *pkg == arg.InputPkg {
return errors.New(fmt.Sprintf("Unknown package: %s", *pkg))
}
// Did the compilation fail due to an unknown interface?
if in := findUndefinedInterface(buildOutput); in != nil {
return errors.New(fmt.Sprintf("Unknown interface: %s", *in))
}
// Otherwise return a generic error.
return errors.New(fmt.Sprintf(
"%s\n\nError building generated code:\n\n"+
" %v\n\nPlease report this oglemock bug.",
buildOutput,
err))
}
// Run the binary.
cmd = exec.Command(binaryPath)
binaryOutput, err := cmd.CombinedOutput()
if err != nil {
return errors.New(fmt.Sprintf(
"%s\n\nError running generated code:\n\n"+
" %v\n\n Please report this oglemock bug.",
binaryOutput,
err))
}
// Copy its output.
_, err = os.Stdout.Write(binaryOutput)
if err != nil {
return errors.New(fmt.Sprintf("Error copying binary output: %v", err))
}
return nil
}
func main() {
if err := run(); err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
}
// Copyright 2011 Aaron Jacobs. All Rights Reserved.
// Author: aaronjjacobs@gmail.com (Aaron Jacobs)
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package oglemock
// ErrorReporter is an interface that wraps methods for reporting errors that
// should cause test failures.
type ErrorReporter interface {
// Report that some failure (e.g. an unsatisfied expectation) occurred. If
// known, fileName and lineNumber should contain information about where it
// occurred. The test may continue if the test framework supports it.
ReportError(fileName string, lineNumber int, err error)
// Like ReportError, but the test should be halted immediately. It is assumed
// that this method does not return.
ReportFatalError(fileName string, lineNumber int, err error)
}
// Copyright 2011 Aaron Jacobs. All Rights Reserved.
// Author: aaronjjacobs@gmail.com (Aaron Jacobs)
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package oglemock
// Expectation is an expectation for zero or more calls to a mock method with
// particular arguments or sets of arguments.
type Expectation interface {
// Times expresses that a matching method call should happen exactly N times.
// Times must not be called more than once, and must not be called after
// WillOnce or WillRepeatedly.
//
// The full rules for the cardinality of an expectation are as follows:
//
// 1. If an explicit cardinality is set with Times(N), then anything other
// than exactly N matching calls will cause a test failure.
//
// 2. Otherwise, if there are any one-time actions set up, then it is
// expected there will be at least that many matching calls. If there is
// not also a fallback action, then it is expected that there will be
// exactly that many.
//
// 3. Otherwise, if there is a fallback action configured, any number of
// matching calls (including zero) is allowed.
//
// 4. Otherwise, the implicit cardinality is one.
//
Times(n uint) Expectation
// WillOnce configures a "one-time action". WillOnce can be called zero or
// more times, but must be called after any call to Times and before any call
// to WillRepeatedly.
//
// When matching method calls are made on the mock object, one-time actions
// are invoked one per matching call in the order that they were set up until
// they are exhausted. Afterward the fallback action, if any, will be used.
WillOnce(a Action) Expectation
// WillRepeatedly configures a "fallback action". WillRepeatedly can be
// called zero or one times, and must not be called before Times or WillOnce.
//
// Once all one-time actions are exhausted (see above), the fallback action
// will be invoked for any further method calls. If WillRepeatedly is not
// called, the fallback action is implicitly an action that returns zero
// values for the method's return values.
WillRepeatedly(a Action) Expectation
}
// Copyright 2012 Aaron Jacobs. All Rights Reserved.
// Author: aaronjjacobs@gmail.com (Aaron Jacobs)
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Package generate implements code generation for mock classes. This is an
// implementation detail of the createmock command, which you probably want to
// use directly instead.
package generate
import (
"bytes"
"errors"
"go/ast"
"go/parser"
"go/printer"
"go/token"
"io"
"reflect"
"regexp"
"text/template"
)
const tmplStr = `
// This file was auto-generated using createmock. See the following page for
// more information:
//
// https://github.com/smartystreets/goconvey/convey/assertions/oglemock
//
package {{.Pkg}}
import (
{{range $identifier, $import := .Imports}}{{$identifier}} "{{$import}}"
{{end}}
)
{{range .Interfaces}}
{{$interfaceName := printf "Mock%s" .Name}}
{{$structName := printf "mock%s" .Name}}
type {{$interfaceName}} interface {
{{getTypeString .}}
oglemock.MockObject
}
type {{$structName}} struct {
controller oglemock.Controller
description string
}
func New{{printf "Mock%s" .Name}}(
c oglemock.Controller,
desc string) {{$interfaceName}} {
return &{{$structName}}{
controller: c,
description: desc,
}
}
func (m *{{$structName}}) Oglemock_Id() uintptr {
return uintptr(unsafe.Pointer(m))
}
func (m *{{$structName}}) Oglemock_Description() string {
return m.description
}
{{range getMethods .}}
{{$funcType := .Type}}
{{$inputTypes := getInputs $funcType}}
{{$outputTypes := getOutputs $funcType}}
func (m *{{$structName}}) {{.Name}}({{range $i, $type := $inputTypes}}p{{$i}} {{getInputTypeString $i $funcType}}, {{end}}) ({{range $i, $type := $outputTypes}}o{{$i}} {{getTypeString $type}}, {{end}}) {
// Get a file name and line number for the caller.
_, file, line, _ := runtime.Caller(1)
// Hand the call off to the controller, which does most of the work.
retVals := m.controller.HandleMethodCall(
m,
"{{.Name}}",
file,
line,
[]interface{}{ {{range $i, $type := $inputTypes}}p{{$i}}, {{end}} })
if len(retVals) != {{len $outputTypes}} {
panic(fmt.Sprintf("{{$structName}}.{{.Name}}: invalid return values: %v", retVals))
}
{{range $i, $type := $outputTypes}}
// o{{$i}} {{getTypeString $type}}
if retVals[{{$i}}] != nil {
o{{$i}} = retVals[{{$i}}].({{getTypeString $type}})
}
{{end}}
return
}
{{end}}
{{end}}
`
type tmplArg struct {
// The package of the generated code.
Pkg string
// Imports needed by the interfaces.
Imports importMap
// The set of interfaces to mock.
Interfaces []reflect.Type
}
var tmpl *template.Template
func init() {
extraFuncs := make(template.FuncMap)
extraFuncs["getMethods"] = getMethods
extraFuncs["getInputs"] = getInputs
extraFuncs["getOutputs"] = getOutputs
extraFuncs["getInputTypeString"] = getInputTypeString
extraFuncs["getTypeString"] = getTypeString
tmpl = template.New("code")
tmpl.Funcs(extraFuncs)
tmpl.Parse(tmplStr)
}
func getInputTypeString(i int, ft reflect.Type) string {
numInputs := ft.NumIn()
if i == numInputs-1 && ft.IsVariadic() {
return "..." + getTypeString(ft.In(i).Elem())
}
return getTypeString(ft.In(i))
}
func getTypeString(t reflect.Type) string {
return t.String()
}
func getMethods(it reflect.Type) []reflect.Method {
numMethods := it.NumMethod()
methods := make([]reflect.Method, numMethods)
for i := 0; i < numMethods; i++ {
methods[i] = it.Method(i)
}
return methods
}
func getInputs(ft reflect.Type) []reflect.Type {
numIn := ft.NumIn()
inputs := make([]reflect.Type, numIn)
for i := 0; i < numIn; i++ {
inputs[i] = ft.In(i)
}
return inputs
}
func getOutputs(ft reflect.Type) []reflect.Type {
numOut := ft.NumOut()
outputs := make([]reflect.Type, numOut)
for i := 0; i < numOut; i++ {
outputs[i] = ft.Out(i)
}
return outputs
}
// A map from import identifier to package to use that identifier for,
// containing elements for each import needed by a set of mocked interfaces.
type importMap map[string]string
var typePackageIdentifierRegexp = regexp.MustCompile(`^([\pL_0-9]+)\.[\pL_0-9]+$`)
// Add an import for the supplied type, without recursing.
func addImportForType(imports importMap, t reflect.Type) {
// If there is no package path, this is a built-in type and we don't need an
// import.
pkgPath := t.PkgPath()
if pkgPath == "" {
return
}
// Work around a bug in Go:
//
// http://code.google.com/p/go/issues/detail?id=2660
//
var errorPtr *error
if t == reflect.TypeOf(errorPtr).Elem() {
return
}
// Use the identifier that's part of the type's string representation as the
// import identifier. This means that we'll do the right thing for package
// "foo/bar" with declaration "package baz".
match := typePackageIdentifierRegexp.FindStringSubmatch(t.String())
if match == nil {
return
}
imports[match[1]] = pkgPath
}
// Add all necessary imports for the type, recursing as appropriate.
func addImportsForType(imports importMap, t reflect.Type) {
// Add any import needed for the type itself.
addImportForType(imports, t)
// Handle special cases where recursion is needed.
switch t.Kind() {
case reflect.Array, reflect.Chan, reflect.Ptr, reflect.Slice:
addImportsForType(imports, t.Elem())
case reflect.Func:
// Input parameters.
for i := 0; i < t.NumIn(); i++ {
addImportsForType(imports, t.In(i))
}
// Return values.
for i := 0; i < t.NumOut(); i++ {
addImportsForType(imports, t.Out(i))
}
case reflect.Map:
addImportsForType(imports, t.Key())
addImportsForType(imports, t.Elem())
}
}
// Add imports for each of the methods of the interface, but not the interface
// itself.
func addImportsForInterfaceMethods(imports importMap, it reflect.Type) {
// Handle each method.
for i := 0; i < it.NumMethod(); i++ {
m := it.Method(i)
addImportsForType(imports, m.Type)
}
}
// Given a set of interfaces, return a map from import identifier to package to
// use that identifier for, containing elements for each import needed by the
// mock versions of those interfaces.
func getImports(interfaces []reflect.Type) importMap {
imports := make(importMap)
for _, it := range interfaces {
addImportForType(imports, it)
addImportsForInterfaceMethods(imports, it)
}
// Make sure there are imports for other types used by the generated code
// itself.
imports["fmt"] = "fmt"
imports["oglemock"] = "github.com/smartystreets/goconvey/convey/assertions/oglemock"
imports["runtime"] = "runtime"
imports["unsafe"] = "unsafe"
return imports
}
// Given a set of interfaces to mock, write out source code for a package named
// `pkg` that contains mock implementations of those interfaces.
func GenerateMockSource(w io.Writer, pkg string, interfaces []reflect.Type) error {
// Sanity-check arguments.
if pkg == "" {
return errors.New("Package name must be non-empty.")
}
if len(interfaces) == 0 {
return errors.New("List of interfaces must be non-empty.")
}
// Make sure each type is indeed an interface.
for _, it := range interfaces {
if it.Kind() != reflect.Interface {
return errors.New("Invalid type: " + it.String())
}
}
// Create an appropriate template arg, then execute the template. Write the
// raw output into a buffer.
var arg tmplArg
arg.Pkg = pkg
arg.Imports = getImports(interfaces)
arg.Interfaces = interfaces
buf := new(bytes.Buffer)
if err := tmpl.Execute(buf, arg); err != nil {
return err
}
// Parse the output.
fset := token.NewFileSet()
astFile, err := parser.ParseFile(fset, pkg+".go", buf, parser.ParseComments)
if err != nil {
return errors.New("Error parsing generated code: " + err.Error())
}
// Sort the import lines in the AST in the same way that gofmt does.
ast.SortImports(fset, astFile)
// Pretty-print the AST, using the same options that gofmt does by default.
cfg := &printer.Config{
Mode: printer.UseSpaces | printer.TabIndent,
Tabwidth: 8,
}
if err = cfg.Fprint(w, fset, astFile); err != nil {
return errors.New("Error pretty printing: " + err.Error())
}
return nil
}
// Copyright 2012 Aaron Jacobs. All Rights Reserved.
// Author: aaronjjacobs@gmail.com (Aaron Jacobs)
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Package complicated_pkg contains an interface with lots of interesting
// cases, for use in integration testing.
package complicated_pkg
import (
"image"
"io"
"net"
"github.com/smartystreets/goconvey/convey/assertions/oglemock/generate/test_cases/renamed_pkg"
)
type Byte uint8
type ComplicatedThing interface {
Channels(a chan chan<- <-chan net.Conn) chan int
Pointers(a *int, b *net.Conn, c **io.Reader) (*int, error)
Functions(a func(int, image.Image) int) func(string, int) net.Conn
Maps(a map[string]*int) (map[int]*string, error)
Arrays(a [3]string) ([3]int, error)
Slices(a []string) ([]int, error)
NamedScalarType(a Byte) ([]Byte, error)
EmptyInterface(a interface{}) (interface{}, error)
RenamedPackage(a tony.SomeUint8Alias)
Variadic(a int, b ...net.Conn) int
}
// This file was auto-generated using createmock. See the following page for
// more information:
//
// https://github.com/smartystreets/goconvey/convey/assertions/oglemock
//
package some_pkg
import (
fmt "fmt"
image "image"
io "io"
net "net"
runtime "runtime"
unsafe "unsafe"
oglemock "github.com/smartystreets/goconvey/convey/assertions/oglemock"
complicated_pkg "github.com/smartystreets/goconvey/convey/assertions/oglemock/generate/test_cases/complicated_pkg"
tony "github.com/smartystreets/goconvey/convey/assertions/oglemock/generate/test_cases/renamed_pkg"
)
type MockComplicatedThing interface {
complicated_pkg.ComplicatedThing
oglemock.MockObject
}
type mockComplicatedThing struct {
controller oglemock.Controller
description string
}
func NewMockComplicatedThing(
c oglemock.Controller,
desc string) MockComplicatedThing {
return &mockComplicatedThing{
controller: c,
description: desc,
}
}
func (m *mockComplicatedThing) Oglemock_Id() uintptr {
return uintptr(unsafe.Pointer(m))
}
func (m *mockComplicatedThing) Oglemock_Description() string {
return m.description
}
func (m *mockComplicatedThing) Arrays(p0 [3]string) (o0 [3]int, o1 error) {
// Get a file name and line number for the caller.
_, file, line, _ := runtime.Caller(1)
// Hand the call off to the controller, which does most of the work.
retVals := m.controller.HandleMethodCall(
m,
"Arrays",
file,
line,
[]interface{}{p0})
if len(retVals) != 2 {
panic(fmt.Sprintf("mockComplicatedThing.Arrays: invalid return values: %v", retVals))
}
// o0 [3]int
if retVals[0] != nil {
o0 = retVals[0].([3]int)
}
// o1 error
if retVals[1] != nil {
o1 = retVals[1].(error)
}
return
}
func (m *mockComplicatedThing) Channels(p0 chan chan<- <-chan net.Conn) (o0 chan int) {
// Get a file name and line number for the caller.
_, file, line, _ := runtime.Caller(1)
// Hand the call off to the controller, which does most of the work.
retVals := m.controller.HandleMethodCall(
m,
"Channels",
file,
line,
[]interface{}{p0})
if len(retVals) != 1 {
panic(fmt.Sprintf("mockComplicatedThing.Channels: invalid return values: %v", retVals))
}
// o0 chan int
if retVals[0] != nil {
o0 = retVals[0].(chan int)
}
return
}
func (m *mockComplicatedThing) EmptyInterface(p0 interface{}) (o0 interface{}, o1 error) {
// Get a file name and line number for the caller.
_, file, line, _ := runtime.Caller(1)
// Hand the call off to the controller, which does most of the work.
retVals := m.controller.HandleMethodCall(
m,
"EmptyInterface",
file,
line,
[]interface{}{p0})
if len(retVals) != 2 {
panic(fmt.Sprintf("mockComplicatedThing.EmptyInterface: invalid return values: %v", retVals))
}
// o0 interface {}
if retVals[0] != nil {
o0 = retVals[0].(interface{})
}
// o1 error
if retVals[1] != nil {
o1 = retVals[1].(error)
}
return
}
func (m *mockComplicatedThing) Functions(p0 func(int, image.Image) int) (o0 func(string, int) net.Conn) {
// Get a file name and line number for the caller.
_, file, line, _ := runtime.Caller(1)
// Hand the call off to the controller, which does most of the work.
retVals := m.controller.HandleMethodCall(
m,
"Functions",
file,
line,
[]interface{}{p0})
if len(retVals) != 1 {
panic(fmt.Sprintf("mockComplicatedThing.Functions: invalid return values: %v", retVals))
}
// o0 func(string, int) net.Conn
if retVals[0] != nil {
o0 = retVals[0].(func(string, int) net.Conn)
}
return
}
func (m *mockComplicatedThing) Maps(p0 map[string]*int) (o0 map[int]*string, o1 error) {
// Get a file name and line number for the caller.
_, file, line, _ := runtime.Caller(1)
// Hand the call off to the controller, which does most of the work.
retVals := m.controller.HandleMethodCall(
m,
"Maps",
file,
line,
[]interface{}{p0})
if len(retVals) != 2 {
panic(fmt.Sprintf("mockComplicatedThing.Maps: invalid return values: %v", retVals))
}
// o0 map[int]*string
if retVals[0] != nil {
o0 = retVals[0].(map[int]*string)
}
// o1 error
if retVals[1] != nil {
o1 = retVals[1].(error)
}
return
}
func (m *mockComplicatedThing) NamedScalarType(p0 complicated_pkg.Byte) (o0 []complicated_pkg.Byte, o1 error) {
// Get a file name and line number for the caller.
_, file, line, _ := runtime.Caller(1)
// Hand the call off to the controller, which does most of the work.
retVals := m.controller.HandleMethodCall(
m,
"NamedScalarType",
file,
line,
[]interface{}{p0})
if len(retVals) != 2 {
panic(fmt.Sprintf("mockComplicatedThing.NamedScalarType: invalid return values: %v", retVals))
}
// o0 []complicated_pkg.Byte
if retVals[0] != nil {
o0 = retVals[0].([]complicated_pkg.Byte)
}
// o1 error
if retVals[1] != nil {
o1 = retVals[1].(error)
}
return
}
func (m *mockComplicatedThing) Pointers(p0 *int, p1 *net.Conn, p2 **io.Reader) (o0 *int, o1 error) {
// Get a file name and line number for the caller.
_, file, line, _ := runtime.Caller(1)
// Hand the call off to the controller, which does most of the work.
retVals := m.controller.HandleMethodCall(
m,
"Pointers",
file,
line,
[]interface{}{p0, p1, p2})
if len(retVals) != 2 {
panic(fmt.Sprintf("mockComplicatedThing.Pointers: invalid return values: %v", retVals))
}
// o0 *int
if retVals[0] != nil {
o0 = retVals[0].(*int)
}
// o1 error
if retVals[1] != nil {
o1 = retVals[1].(error)
}
return
}
func (m *mockComplicatedThing) RenamedPackage(p0 tony.SomeUint8Alias) {
// Get a file name and line number for the caller.
_, file, line, _ := runtime.Caller(1)
// Hand the call off to the controller, which does most of the work.
retVals := m.controller.HandleMethodCall(
m,
"RenamedPackage",
file,
line,
[]interface{}{p0})
if len(retVals) != 0 {
panic(fmt.Sprintf("mockComplicatedThing.RenamedPackage: invalid return values: %v", retVals))
}
return
}
func (m *mockComplicatedThing) Slices(p0 []string) (o0 []int, o1 error) {
// Get a file name and line number for the caller.
_, file, line, _ := runtime.Caller(1)
// Hand the call off to the controller, which does most of the work.
retVals := m.controller.HandleMethodCall(
m,
"Slices",
file,
line,
[]interface{}{p0})
if len(retVals) != 2 {
panic(fmt.Sprintf("mockComplicatedThing.Slices: invalid return values: %v", retVals))
}
// o0 []int
if retVals[0] != nil {
o0 = retVals[0].([]int)
}
// o1 error
if retVals[1] != nil {
o1 = retVals[1].(error)
}
return
}
func (m *mockComplicatedThing) Variadic(p0 int, p1 ...net.Conn) (o0 int) {
// Get a file name and line number for the caller.
_, file, line, _ := runtime.Caller(1)
// Hand the call off to the controller, which does most of the work.
retVals := m.controller.HandleMethodCall(
m,
"Variadic",
file,
line,
[]interface{}{p0, p1})
if len(retVals) != 1 {
panic(fmt.Sprintf("mockComplicatedThing.Variadic: invalid return values: %v", retVals))
}
// o0 int
if retVals[0] != nil {
o0 = retVals[0].(int)
}
return
}
// This file was auto-generated using createmock. See the following page for
// more information:
//
// https://github.com/smartystreets/goconvey/convey/assertions/oglemock
//
package some_pkg
import (
fmt "fmt"
image "image"
color "image/color"
runtime "runtime"
unsafe "unsafe"
oglemock "github.com/smartystreets/goconvey/convey/assertions/oglemock"
)
type MockImage interface {
image.Image
oglemock.MockObject
}
type mockImage struct {
controller oglemock.Controller
description string
}
func NewMockImage(
c oglemock.Controller,
desc string) MockImage {
return &mockImage{
controller: c,
description: desc,
}
}
func (m *mockImage) Oglemock_Id() uintptr {
return uintptr(unsafe.Pointer(m))
}
func (m *mockImage) Oglemock_Description() string {
return m.description
}
func (m *mockImage) At(p0 int, p1 int) (o0 color.Color) {
// Get a file name and line number for the caller.
_, file, line, _ := runtime.Caller(1)
// Hand the call off to the controller, which does most of the work.
retVals := m.controller.HandleMethodCall(
m,
"At",
file,
line,
[]interface{}{p0, p1})
if len(retVals) != 1 {
panic(fmt.Sprintf("mockImage.At: invalid return values: %v", retVals))
}
// o0 color.Color
if retVals[0] != nil {
o0 = retVals[0].(color.Color)
}
return
}
func (m *mockImage) Bounds() (o0 image.Rectangle) {
// Get a file name and line number for the caller.
_, file, line, _ := runtime.Caller(1)
// Hand the call off to the controller, which does most of the work.
retVals := m.controller.HandleMethodCall(
m,
"Bounds",
file,
line,
[]interface{}{})
if len(retVals) != 1 {
panic(fmt.Sprintf("mockImage.Bounds: invalid return values: %v", retVals))
}
// o0 image.Rectangle
if retVals[0] != nil {
o0 = retVals[0].(image.Rectangle)
}
return
}
func (m *mockImage) ColorModel() (o0 color.Model) {
// Get a file name and line number for the caller.
_, file, line, _ := runtime.Caller(1)
// Hand the call off to the controller, which does most of the work.
retVals := m.controller.HandleMethodCall(
m,
"ColorModel",
file,
line,
[]interface{}{})
if len(retVals) != 1 {
panic(fmt.Sprintf("mockImage.ColorModel: invalid return values: %v", retVals))
}
// o0 color.Model
if retVals[0] != nil {
o0 = retVals[0].(color.Model)
}
return
}
type MockPalettedImage interface {
image.PalettedImage
oglemock.MockObject
}
type mockPalettedImage struct {
controller oglemock.Controller
description string
}
func NewMockPalettedImage(
c oglemock.Controller,
desc string) MockPalettedImage {
return &mockPalettedImage{
controller: c,
description: desc,
}
}
func (m *mockPalettedImage) Oglemock_Id() uintptr {
return uintptr(unsafe.Pointer(m))
}
func (m *mockPalettedImage) Oglemock_Description() string {
return m.description
}
func (m *mockPalettedImage) At(p0 int, p1 int) (o0 color.Color) {
// Get a file name and line number for the caller.
_, file, line, _ := runtime.Caller(1)
// Hand the call off to the controller, which does most of the work.
retVals := m.controller.HandleMethodCall(
m,
"At",
file,
line,
[]interface{}{p0, p1})
if len(retVals) != 1 {
panic(fmt.Sprintf("mockPalettedImage.At: invalid return values: %v", retVals))
}
// o0 color.Color
if retVals[0] != nil {
o0 = retVals[0].(color.Color)
}
return
}
func (m *mockPalettedImage) Bounds() (o0 image.Rectangle) {
// Get a file name and line number for the caller.
_, file, line, _ := runtime.Caller(1)
// Hand the call off to the controller, which does most of the work.
retVals := m.controller.HandleMethodCall(
m,
"Bounds",
file,
line,
[]interface{}{})
if len(retVals) != 1 {
panic(fmt.Sprintf("mockPalettedImage.Bounds: invalid return values: %v", retVals))
}
// o0 image.Rectangle
if retVals[0] != nil {
o0 = retVals[0].(image.Rectangle)
}
return
}
func (m *mockPalettedImage) ColorIndexAt(p0 int, p1 int) (o0 uint8) {
// Get a file name and line number for the caller.
_, file, line, _ := runtime.Caller(1)
// Hand the call off to the controller, which does most of the work.
retVals := m.controller.HandleMethodCall(
m,
"ColorIndexAt",
file,
line,
[]interface{}{p0, p1})
if len(retVals) != 1 {
panic(fmt.Sprintf("mockPalettedImage.ColorIndexAt: invalid return values: %v", retVals))
}
// o0 uint8
if retVals[0] != nil {
o0 = retVals[0].(uint8)
}
return
}
func (m *mockPalettedImage) ColorModel() (o0 color.Model) {
// Get a file name and line number for the caller.
_, file, line, _ := runtime.Caller(1)
// Hand the call off to the controller, which does most of the work.
retVals := m.controller.HandleMethodCall(
m,
"ColorModel",
file,
line,
[]interface{}{})
if len(retVals) != 1 {
panic(fmt.Sprintf("mockPalettedImage.ColorModel: invalid return values: %v", retVals))
}
// o0 color.Model
if retVals[0] != nil {
o0 = retVals[0].(color.Model)
}
return
}
// This file was auto-generated using createmock. See the following page for
// more information:
//
// https://github.com/smartystreets/goconvey/convey/assertions/oglemock
//
package some_pkg
import (
fmt "fmt"
io "io"
runtime "runtime"
unsafe "unsafe"
oglemock "github.com/smartystreets/goconvey/convey/assertions/oglemock"
)
type MockReader interface {
io.Reader
oglemock.MockObject
}
type mockReader struct {
controller oglemock.Controller
description string
}
func NewMockReader(
c oglemock.Controller,
desc string) MockReader {
return &mockReader{
controller: c,
description: desc,
}
}
func (m *mockReader) Oglemock_Id() uintptr {
return uintptr(unsafe.Pointer(m))
}
func (m *mockReader) Oglemock_Description() string {
return m.description
}
func (m *mockReader) Read(p0 []uint8) (o0 int, o1 error) {
// Get a file name and line number for the caller.
_, file, line, _ := runtime.Caller(1)
// Hand the call off to the controller, which does most of the work.
retVals := m.controller.HandleMethodCall(
m,
"Read",
file,
line,
[]interface{}{p0})
if len(retVals) != 2 {
panic(fmt.Sprintf("mockReader.Read: invalid return values: %v", retVals))
}
// o0 int
if retVals[0] != nil {
o0 = retVals[0].(int)
}
// o1 error
if retVals[1] != nil {
o1 = retVals[1].(error)
}
return
}
type MockWriter interface {
io.Writer
oglemock.MockObject
}
type mockWriter struct {
controller oglemock.Controller
description string
}
func NewMockWriter(
c oglemock.Controller,
desc string) MockWriter {
return &mockWriter{
controller: c,
description: desc,
}
}
func (m *mockWriter) Oglemock_Id() uintptr {
return uintptr(unsafe.Pointer(m))
}
func (m *mockWriter) Oglemock_Description() string {
return m.description
}
func (m *mockWriter) Write(p0 []uint8) (o0 int, o1 error) {
// Get a file name and line number for the caller.
_, file, line, _ := runtime.Caller(1)
// Hand the call off to the controller, which does most of the work.
retVals := m.controller.HandleMethodCall(
m,
"Write",
file,
line,
[]interface{}{p0})
if len(retVals) != 2 {
panic(fmt.Sprintf("mockWriter.Write: invalid return values: %v", retVals))
}
// o0 int
if retVals[0] != nil {
o0 = retVals[0].(int)
}
// o1 error
if retVals[1] != nil {
o1 = retVals[1].(error)
}
return
}
// This file was auto-generated using createmock. See the following page for
// more information:
//
// https://github.com/smartystreets/goconvey/convey/assertions/oglemock
//
package some_pkg
import (
fmt "fmt"
runtime "runtime"
unsafe "unsafe"
oglemock "github.com/smartystreets/goconvey/convey/assertions/oglemock"
tony "github.com/smartystreets/goconvey/convey/assertions/oglemock/generate/test_cases/renamed_pkg"
)
type MockSomeInterface interface {
tony.SomeInterface
oglemock.MockObject
}
type mockSomeInterface struct {
controller oglemock.Controller
description string
}
func NewMockSomeInterface(
c oglemock.Controller,
desc string) MockSomeInterface {
return &mockSomeInterface{
controller: c,
description: desc,
}
}
func (m *mockSomeInterface) Oglemock_Id() uintptr {
return uintptr(unsafe.Pointer(m))
}
func (m *mockSomeInterface) Oglemock_Description() string {
return m.description
}
func (m *mockSomeInterface) DoFoo(p0 int) (o0 int) {
// Get a file name and line number for the caller.
_, file, line, _ := runtime.Caller(1)
// Hand the call off to the controller, which does most of the work.
retVals := m.controller.HandleMethodCall(
m,
"DoFoo",
file,
line,
[]interface{}{p0})
if len(retVals) != 1 {
panic(fmt.Sprintf("mockSomeInterface.DoFoo: invalid return values: %v", retVals))
}
// o0 int
if retVals[0] != nil {
o0 = retVals[0].(int)
}
return
}
// Copyright 2012 Aaron Jacobs. All Rights Reserved.
// Author: aaronjjacobs@gmail.com (Aaron Jacobs)
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// A package that calls itself something different than its package path would
// have you believe.
package tony
type SomeUint8Alias uint8
type SomeInterface interface {
DoFoo(a int) int
}
// Copyright 2011 Aaron Jacobs. All Rights Reserved.
// Author: aaronjjacobs@gmail.com (Aaron Jacobs)
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package oglemock
import (
"errors"
"fmt"
"reflect"
"sync"
"github.com/smartystreets/goconvey/convey/assertions/oglematchers"
)
// InternalExpectation is exported for purposes of testing only. You should not
// touch it.
//
// InternalExpectation represents an expectation for zero or more calls to a
// mock method, and a set of actions to be taken when those calls are received.
type InternalExpectation struct {
// The signature of the method to which this expectation is bound, for
// checking action types.
methodSignature reflect.Type
// An error reporter to use for reporting errors in the way that expectations
// are set.
errorReporter ErrorReporter
// A mutex protecting mutable fields of the struct.
mutex sync.Mutex
// Matchers that the arguments to the mock method must satisfy in order to
// match this expectation.
ArgMatchers []oglematchers.Matcher
// The name of the file in which this expectation was expressed.
FileName string
// The line number at which this expectation was expressed.
LineNumber int
// The number of times this expectation should be matched, as explicitly
// listed by the user. If there was no explicit number expressed, this is -1.
ExpectedNumMatches int
// Actions to be taken for the first N calls, one per call in order, where N
// is the length of this slice.
OneTimeActions []Action
// An action to be taken when the one-time actions have expired, or nil if
// there is no such action.
FallbackAction Action
// The number of times this expectation has been matched so far.
NumMatches uint
}
// InternalNewExpectation is exported for purposes of testing only. You should
// not touch it.
func InternalNewExpectation(
reporter ErrorReporter,
methodSignature reflect.Type,
args []interface{},
fileName string,
lineNumber int) *InternalExpectation {
result := &InternalExpectation{}
// Store fields that can be stored directly.
result.methodSignature = methodSignature
result.errorReporter = reporter
result.FileName = fileName
result.LineNumber = lineNumber
// Set up defaults.
result.ExpectedNumMatches = -1
result.OneTimeActions = make([]Action, 0)
// Set up the ArgMatchers slice, using Equals(x) for each x that is not a
// matcher itself.
result.ArgMatchers = make([]oglematchers.Matcher, len(args))
for i, x := range args {
if matcher, ok := x.(oglematchers.Matcher); ok {
result.ArgMatchers[i] = matcher
} else {
result.ArgMatchers[i] = oglematchers.Equals(x)
}
}
return result
}
func (e *InternalExpectation) Times(n uint) Expectation {
e.mutex.Lock()
defer e.mutex.Unlock()
// It is illegal to call this more than once.
if e.ExpectedNumMatches != -1 {
e.reportFatalError("Times called more than once.")
return nil
}
// It is illegal to call this after any actions are configured.
if len(e.OneTimeActions) != 0 {
e.reportFatalError("Times called after WillOnce.")
return nil
}
if e.FallbackAction != nil {
e.reportFatalError("Times called after WillRepeatedly.")
return nil
}
// Make sure the number is reasonable (and will fit in an int).
if n > 1000 {
e.reportFatalError("Expectation.Times: N must be at most 1000")
return nil
}
e.ExpectedNumMatches = int(n)
return e
}
func (e *InternalExpectation) WillOnce(a Action) Expectation {
e.mutex.Lock()
defer e.mutex.Unlock()
// It is illegal to call this after WillRepeatedly.
if e.FallbackAction != nil {
e.reportFatalError("WillOnce called after WillRepeatedly.")
return nil
}
// Tell the action about the method's signature.
if err := a.SetSignature(e.methodSignature); err != nil {
e.reportFatalError(fmt.Sprintf("WillOnce given invalid action: %v", err))
return nil
}
// Store the action.
e.OneTimeActions = append(e.OneTimeActions, a)
return e
}
func (e *InternalExpectation) WillRepeatedly(a Action) Expectation {
e.mutex.Lock()
defer e.mutex.Unlock()
// It is illegal to call this twice.
if e.FallbackAction != nil {
e.reportFatalError("WillRepeatedly called more than once.")
return nil
}
// Tell the action about the method's signature.
if err := a.SetSignature(e.methodSignature); err != nil {
e.reportFatalError(fmt.Sprintf("WillRepeatedly given invalid action: %v", err))
return nil
}
// Store the action.
e.FallbackAction = a
return e
}
func (e *InternalExpectation) reportFatalError(errorText string) {
e.errorReporter.ReportFatalError(e.FileName, e.LineNumber, errors.New(errorText))
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment