summaryrefslogtreecommitdiff
path: root/src/mongo/gotools/vendor/src/github.com/smartystreets/assertions/internal/oglemock/save_arg_test.go
blob: 4051907e0dd3efffbdc58f494cfed11a6d7cae59 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
// Copyright 2015 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_test

import (
	"io"
	"os"
	"reflect"
	"testing"

	. "github.com/smartystreets/assertions/internal/oglematchers"
	"github.com/smartystreets/assertions/internal/oglemock"
	. "github.com/smartystreets/assertions/internal/ogletest"
)

func TestSaveArg(t *testing.T) { RunTests(t) }

////////////////////////////////////////////////////////////
// Boilerplate
////////////////////////////////////////////////////////////

type SaveArgTest struct {
}

func init() { RegisterTestSuite(&SaveArgTest{}) }

////////////////////////////////////////////////////////////
// Test functions
////////////////////////////////////////////////////////////

func (t *SaveArgTest) FunctionHasNoArguments() {
	const index = 0
	var dst int
	f := func() (int, string) { return 0, "" }

	err := oglemock.SaveArg(index, &dst).SetSignature(reflect.TypeOf(f))
	ExpectThat(err, Error(HasSubstr("index 0")))
	ExpectThat(err, Error(HasSubstr("Out of range")))
	ExpectThat(err, Error(HasSubstr("func() (int, string)")))
}

func (t *SaveArgTest) ArgumentIndexOutOfRange() {
	const index = 2
	var dst int
	f := func(a int, b int) {}

	err := oglemock.SaveArg(index, &dst).SetSignature(reflect.TypeOf(f))
	ExpectThat(err, Error(HasSubstr("index 2")))
	ExpectThat(err, Error(HasSubstr("Out of range")))
	ExpectThat(err, Error(HasSubstr("func(int, int)")))
}

func (t *SaveArgTest) DestinationIsLiteralNil() {
	const index = 0
	f := func(a int, b int) {}

	err := oglemock.SaveArg(index, nil).SetSignature(reflect.TypeOf(f))
	ExpectThat(err, Error(HasSubstr("not a pointer")))
}

func (t *SaveArgTest) DestinationIsNotAPointer() {
	const index = 0
	f := func(a int, b int) {}

	err := oglemock.SaveArg(index, uint(17)).SetSignature(reflect.TypeOf(f))
	ExpectThat(err, Error(HasSubstr("pointer")))
	ExpectThat(err, Error(HasSubstr("uint")))
}

func (t *SaveArgTest) DestinationIsNilPointer() {
	const index = 1
	var dst *int
	f := func(a int, b int) {}

	err := oglemock.SaveArg(index, dst).SetSignature(reflect.TypeOf(f))
	ExpectThat(err, Error(HasSubstr("pointer")))
	ExpectThat(err, Error(HasSubstr("non-nil")))
}

func (t *SaveArgTest) DestinationNotAssignableFromSource() {
	const index = 1
	var dst int
	f := func(a int, b string) {}

	err := oglemock.SaveArg(index, &dst).SetSignature(reflect.TypeOf(f))
	ExpectThat(err, Error(HasSubstr("int")))
	ExpectThat(err, Error(HasSubstr("assignable")))
	ExpectThat(err, Error(HasSubstr("string")))
}

func (t *SaveArgTest) ExactTypeMatch() {
	const index = 1
	var dst int
	f := func(a int, b int) {}

	action := oglemock.SaveArg(index, &dst)
	AssertEq(nil, action.SetSignature(reflect.TypeOf(f)))

	var a int = 17
	var b int = 19
	_ = action.Invoke([]interface{}{a, b})

	ExpectEq(19, dst)
}

func (t *SaveArgTest) AssignableTypeMatch() {
	const index = 1
	var dst io.Reader
	f := func(a int, b *os.File) {}

	action := oglemock.SaveArg(index, &dst)
	AssertEq(nil, action.SetSignature(reflect.TypeOf(f)))

	var a int = 17
	var b *os.File = os.Stdout
	_ = action.Invoke([]interface{}{a, b})

	ExpectEq(os.Stdout, dst)
}