forked from devlooped/moq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAnyValueAttributeTests3.cs
205 lines (168 loc) · 4.22 KB
/
AnyValueAttributeTests3.cs
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
using Xunit;
using System;
/// <summary>
/// Tests for https://github.com/moq/moq4/issues/1199
/// </summary>
namespace Moq.Tests.Matchers.Wildcard
{
using static AutoIsAny; // note using static to simplify syntax
/// <summary>
/// Helper class provided by user
/// </summary>
public abstract class AutoIsAny
{
public static AnyValue _
{
get
{
It.IsAny<object>(); // This allows it to work for interface types. It doesn't hit the implicit conversion guard, because no implicit conversion
// is required for interfaces. Implicit interface conversions also aren't allowed in C#.
// For non-interface types, the AnyValue implicit conversion below will overwrite `It.IsAny<object>()`
// with the appropriate `It.IsAny<T>()`.
return new AnyValue();
}
}
}
/// <summary>
/// Helper class provided by user. Interfaces implemented via IDE auto explicit interface implementation
/// or Roslyn analyzer/code fix.
/// </summary>
public class AnyValue : ISomeService
{
int ISomeService.Calc(int a, int b, int c, int d)
{
throw new NotImplementedException();
}
int ISomeService.DoSomething(ISomeService a, GearId b, int c)
{
throw new NotImplementedException();
}
int ISomeService.Echo(int a)
{
throw new NotImplementedException();
}
int ISomeService.UseAnimal(Animal a)
{
throw new NotImplementedException();
}
int ISomeService.UseDolphin(Dolphin a)
{
throw new NotImplementedException();
}
int ISomeService.UseInterface(ISomeService a)
{
throw new NotImplementedException();
}
public static implicit operator int(AnyValue _) => It.IsAny<int>();
public static implicit operator byte(AnyValue _) => It.IsAny<byte>();
public static implicit operator GearId(AnyValue _) => It.IsAny<GearId>();
public static implicit operator Animal(AnyValue _) => It.IsAny<Animal>();
public static implicit operator Dolphin(AnyValue _) => It.IsAny<Dolphin>();
}
public class Tests
{
Mock<ISomeService> mock;
ISomeService obj;
public Tests()
{
mock = new Mock<ISomeService>();
obj = mock.Object;
}
[Fact]
public void Echo_1Primitive()
{
mock.Setup(obj => obj.Echo(_)).Returns(777);
Assert.Equal(777, obj.Echo(1));
}
[Fact]
public void Calc_4Primitives()
{
mock.Setup(obj => obj.Calc(_, _, _, _)).Returns(999);
Assert.Equal(999, obj.Calc(1, 2, 3, 4));
}
[Fact]
public void UseInterface()
{
mock.Setup(obj => obj.UseInterface(_)).Returns(555);
Assert.Equal(555, obj.UseInterface(null));
var realService = new SomeService();
Assert.Equal(555, obj.UseInterface(realService));
}
[Fact]
public void DoSomething_MixedTypes()
{
mock.Setup(obj => obj.DoSomething(_, _, _)).Returns(444);
Assert.Equal(444, obj.DoSomething(null, GearId.Neutral, 1));
}
[Fact]
public void UseAnimal()
{
mock.Setup(obj => obj.UseAnimal(_)).Returns(777);
Assert.Equal(777, obj.UseAnimal(new Animal()));
Assert.Equal(777, obj.UseAnimal(new Dolphin()));
}
[Fact]
public void UseDolphin()
{
mock.Setup(obj => obj.UseDolphin(_)).Returns(888);
Assert.Equal(888, obj.UseDolphin(new Dolphin()));
}
}
/// <summary>
/// Example enum
/// </summary>
public enum GearId
{
Reverse,
Neutral,
Gear1,
}
/// <summary>
/// Example interface
/// </summary>
public interface ISomeService
{
int Echo(int a);
int Calc(int a, int b, int c, int d);
int UseInterface(ISomeService a);
int DoSomething(ISomeService a, GearId b, int c);
int UseAnimal(Animal a);
int UseDolphin(Dolphin a);
}
/// <summary>
/// just a class that implements interface
/// </summary>
public class SomeService : ISomeService
{
public int Calc(int a, int b, int c, int d)
{
throw new NotImplementedException();
}
public int DoSomething(ISomeService a, GearId b, int c)
{
throw new NotImplementedException();
}
public int Echo(int a)
{
throw new NotImplementedException();
}
public int UseAnimal(Animal a)
{
throw new NotImplementedException();
}
public int UseDolphin(Dolphin a)
{
throw new NotImplementedException();
}
public int UseInterface(ISomeService a)
{
throw new NotImplementedException();
}
}
public class Animal
{
}
public class Dolphin : Animal
{
}
}