<commit_before>// Package mock provides a mocking framework for Go.
//
// https://github.com/maraino/go-mock
package mock

import (
	"fmt"
	"reflect"
	"runtime"
	"strings"
	"time"

	"github.com/kr/pretty"
)

// Mock should be embedded in the struct that we want to act as a Mock.
//
// Example:
// 		type MyClient struct {
//			mock.Mock
// 		}
type Mock struct {
	Functions []*MockFunction
	inorder   bool
	order     uint
}

type MockCountCheckType int

const (
	NONE MockCountCheckType = iota
	TIMES
	AT_LEAST
	AT_MOST
	BETWEEN
)

// MockFunction is the struct used to store the properties of a method stub.
type MockFunction struct {
	Name              string
	Arguments         []interface{}
	ReturnValues      []interface{}
	ReturnToArguments []MockReturnToArgument
	PanicValue        interface{}
	count             int
	countCheck        MockCountCheckType
	times             [2]int
	order             uint
	timeout           time.Duration
}

// MockReturnToArgument defines the function arguments used as return parameters.
type MockReturnToArgument struct {
	Argument int
	Value    interface{}
}

// MockResult struct is used to store the return arguments of a method stub.
type MockResult struct {
	Result []interface{}
}

// AnyType defines the type used as a replacement for any kind of argument in the stub configuration.
type AnyType string

// Any is the constant that should be used to represent a AnyType.
//
// Example:
//		mock.When("MyMethod", mock.Any, mock.Any).Return(0)
const (
	Any AnyType = "mock.any"
)

// AnythingOfType defines the type used as a replacement for an argument of a
// specific type in the stub configuration.
type AnythingOfType string

// AnyOfType is a helper to define AnythingOfType arguments.
//
// Example:
// 		mock.When("MyMethod", mock.AnyOfType("int"), mock.AnyOfType("string")).Return(0)
func AnyOfType(t string) AnythingOfType {
	return AnythingOfType(t)
}

// AnyIfType defines the type used as an argument that satisfies a condition.
type AnyIfType func(interface{}) bool

// AnyIf is a helper to define AnyIfType arguments.
//
// Example:
// 		f := func(i interface{}) bool {
// 			ii, ok := i.(MyType)
// 			return ok && ii.ID = "the-id"
// 		}
// 		mock.When("MyMethod", mock.AnyIf(f)).Return(0)
func AnyIf(f func(interface{}) bool) AnyIfType {
	return AnyIfType(f)
}

// Verify verifies the restrictions set in the stubbing.
func (m *Mock) Verify() (bool, error) {
	for i, f := range m.Functions {
		switch f.countCheck {
		case TIMES:
			if f.count != f.times[1] {
				return false, fmt.Errorf("Function #%d %s executed %d times, expected: %d", i+1, f.Name, f.count, f.times[1])
			}
		case AT_LEAST:
			if f.count < f.times[1] {
				return false, fmt.Errorf("Function #%d %s executed %d times, expected at least: %d", i+1, f.Name, f.count, f.times[1])
			}
		case AT_MOST:
			if f.count > f.times[1] {
				return false, fmt.Errorf("Function #%d %s executed %d times, expected at most: %d", i+1, f.Name, f.count, f.times[1])
			}
		case BETWEEN:
			if f.count < f.times[0] || f.count > f.times[1] {
				return false, fmt.Errorf("Function #%d %s executed %d times, expected between: [%d, %d]", i+1, f.Name, f.count, f.times[0], f.times[1])
			}
		}
	}
	return true, nil
}

// HasVerify is used as the input of VerifyMocks (Mock satisfies it, obviously)
type HasVerify interface {
	Verify() (bool, error)
}

// VerifyMocks verifies a list of mocks, and returns the first error, if any.
func VerifyMocks(mocks ...HasVerify) (bool, error) {
	for _, m := range mocks {
		if ok, err := m.Verify(); !ok {
			return ok, err
		}
	}
	return true, nil
}

// Reset removes all stubs defined.
func (m *Mock) Reset() *Mock {
	m.Functions = nil
	m.order = 0
	return m
}<commit_msg>Convenience function: fail a test if one in a list of mocks fails verification<commit_after>// Package mock provides a mocking framework for Go.
//
// https://github.com/maraino/go-mock
package mock

import (
	"fmt"
	"reflect"
	"runtime"
	"strings"
	"time"
	"testing"

	"github.com/kr/pretty"
)

// Mock should be embedded in the struct that we want to act as a Mock.
//
// Example:
// 		type MyClient struct {
//			mock.Mock
// 		}
type Mock struct {
	Functions []*MockFunction
	inorder   bool
	order     uint
}

type MockCountCheckType int

const (
	NONE MockCountCheckType = iota
	TIMES
	AT_LEAST
	AT_MOST
	BETWEEN
)

// MockFunction is the struct used to store the properties of a method stub.
type MockFunction struct {
	Name              string
	Arguments         []interface{}
	ReturnValues      []interface{}
	ReturnToArguments []MockReturnToArgument
	PanicValue        interface{}
	count             int
	countCheck        MockCountCheckType
	times             [2]int
	order             uint
	timeout           time.Duration
}

// MockReturnToArgument defines the function arguments used as return parameters.
type MockReturnToArgument struct {
	Argument int
	Value    interface{}
}

// MockResult struct is used to store the return arguments of a method stub.
type MockResult struct {
	Result []interface{}
}

// AnyType defines the type used as a replacement for any kind of argument in the stub configuration.
type AnyType string

// Any is the constant that should be used to represent a AnyType.
//
// Example:
//		mock.When("MyMethod", mock.Any, mock.Any).Return(0)
const (
	Any AnyType = "mock.any"
)

// AnythingOfType defines the type used as a replacement for an argument of a
// specific type in the stub configuration.
type AnythingOfType string

// AnyOfType is a helper to define AnythingOfType arguments.
//
// Example:
// 		mock.When("MyMethod", mock.AnyOfType("int"), mock.AnyOfType("string")).Return(0)
func AnyOfType(t string) AnythingOfType {
	return AnythingOfType(t)
}

// AnyIfType defines the type used as an argument that satisfies a condition.
type AnyIfType func(interface{}) bool

// AnyIf is a helper to define AnyIfType arguments.
//
// Example:
// 		f := func(i interface{}) bool {
// 			ii, ok := i.(MyType)
// 			return ok && ii.ID = "the-id"
// 		}
// 		mock.When("MyMethod", mock.AnyIf(f)).Return(0)
func AnyIf(f func(interface{}) bool) AnyIfType {
	return AnyIfType(f)
}

// Verify verifies the restrictions set in the stubbing.
func (m *Mock) Verify() (bool, error) {
	for i, f := range m.Functions {
		switch f.countCheck {
		case TIMES:
			if f.count != f.times[1] {
				return false, fmt.Errorf("Function #%d %s executed %d times, expected: %d", i+1, f.Name, f.count, f.times[1])
			}
		case AT_LEAST:
			if f.count < f.times[1] {
				return false, fmt.Errorf("Function #%d %s executed %d times, expected at least: %d", i+1, f.Name, f.count, f.times[1])
			}
		case AT_MOST:
			if f.count > f.times[1] {
				return false, fmt.Errorf("Function #%d %s executed %d times, expected at most: %d", i+1, f.Name, f.count, f.times[1])
			}
		case BETWEEN:
			if f.count < f.times[0] || f.count > f.times[1] {
				return false, fmt.Errorf("Function #%d %s executed %d times, expected between: [%d, %d]", i+1, f.Name, f.count, f.times[0], f.times[1])
			}
		}
	}
	return true, nil
}

// HasVerify is used as the input of VerifyMocks (Mock satisfies it, obviously)
type HasVerify interface {
	Verify() (bool, error)
}

// VerifyMocks verifies a list of mocks, and returns the first error, if any.
func VerifyMocks(mocks ...HasVerify) (bool, error) {
	for _, m := range mocks {
		if ok, err := m.Verify(); !ok {
			return ok, err
		}
	}
	return true, nil
}

// Fail the test if any of the mocks fail verification
func AssertVerifyMocks(t *testing.T, mocks ...HasVerify) {
	if ok, err := VerifyMocks(mocks...); !ok {
		t.Error(err)
	}
}

// Reset removes all stubs defined.
func (m *Mock) Reset() *Mock {
	m.Functions = nil
	m.order = 0
	return m
}
{'fraction_non_alphanumeric': 0.12242251329269874, 'fraction_numerical': 0.004928024899494229, 'mean_word_length': 3.1152614727854857, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7398619', 'n_tokens_mistral': 2661, 'n_tokens_neox': 2576, 'n_words': 895}