@@ -20,33 +20,33 @@ public class HidConnectionTests
20
20
{
21
21
private static IHidDDevice GetMockedDevice ( )
22
22
{
23
- var mock = new Mock < IHidDDevice > ( ) ;
24
- return mock . Object ;
23
+ var mock = Substitute . For < IHidDDevice > ( ) ;
24
+ return mock ;
25
25
}
26
26
27
27
private static byte [ ] GetFeatureReport ( ) => Hex . HexToBytes ( "000102030405060708090A0B0C0D0E0F" ) ;
28
28
private static byte [ ] GetInputReport ( ) => Hex . HexToBytes ( "0001020304050607" ) ;
29
29
private static byte [ ] GetOutputReport ( ) => Hex . HexToBytes ( "00010203" ) ;
30
30
31
- private static Mock < IHidDDevice > GetMockedFeatureDevice ( )
31
+ private static IHidDDevice GetMockedFeatureDevice ( )
32
32
{
33
- var mock = new Mock < IHidDDevice > ( ) ;
34
- _ = mock . Setup ( hdd => hdd . FeatureReportByteLength ) . Returns ( 16 ) ;
35
- _ = mock . Setup ( hdd => hdd . InputReportByteLength ) . Throws ( new Exception ( ) ) ;
33
+ var mock = Substitute . For < IHidDDevice > ( ) ;
34
+ _ = mock . When ( hdd => hdd . FeatureReportByteLength ) . Returns ( 16 ) ;
35
+ _ = mock . InputReportByteLength ) . Throws ( new Exception ( ) ) ;
36
36
_ = mock . Setup ( hdd => hdd . OutputReportByteLength ) . Throws ( new Exception ( ) ) ;
37
- _ = mock . Setup ( hdd => hdd . GetInputReport ( ) ) . Throws ( new Exception ( ) ) ;
38
- _ = mock . Setup ( hdd => hdd . GetFeatureReport ( ) ) . Returns ( GetFeatureReport ( ) ) ;
37
+ _ = mock . Setup ( hdd => hdd . GetInputReport ( ) ) . Throw ( new Exception ( ) ) ;
38
+ _ = mock . When ( hdd => hdd . GetFeatureReport ( ) . Returns ( GetFeatureReport ( ) ) ;
39
39
return mock ;
40
40
}
41
41
42
- private static Mock < IHidDDevice > GetMockedIODevice ( )
42
+ private static IHidDDevice GetMockedIODevice ( )
43
43
{
44
- var mock = new Mock < IHidDDevice > ( ) ;
45
- _ = mock . Setup ( hdd => hdd . InputReportByteLength ) . Returns ( 8 ) ;
46
- _ = mock . Setup ( hdd => hdd . OutputReportByteLength ) . Returns ( 4 ) ;
47
- _ = mock . Setup ( hdd => hdd . FeatureReportByteLength ) . Throws ( new Exception ( ) ) ;
48
- _ = mock . Setup ( hdd => hdd . GetFeatureReport ( ) ) . Throws ( new Exception ( ) ) ;
49
- _ = mock . Setup ( hdd => hdd . GetInputReport ( ) ) . Returns ( GetInputReport ( ) ) ;
44
+ var mock = Substitute . For < IHidDDevice > ( ) ;
45
+ _ = mock . InputReportByteLength . Returns ( 8 ) ;
46
+ _ = mock . OutputReportByteLength . Returns ( 4 ) ;
47
+ _ = mock . FeatureReportByteLength ) . Throws ( new Exception ( ) ) ;
48
+ _ = mock . Setup ( hdd => hdd . GetFeatureReport ( ) ) . Throw ( new Exception ( ) ) ;
49
+ _ = mock . Setup ( hdd => hdd . GetInputReport ( ) . Returns ( GetInputReport ( ) ) ;
50
50
return mock ;
51
51
}
52
52
@@ -59,34 +59,34 @@ public void Constructor_GivenDevice_Succeeds()
59
59
[ Fact ]
60
60
public void Constructor_GivenFeatureDevice_CallsOpenFeatureConnection ( )
61
61
{
62
- Mock < IHidDDevice > mock = GetMockedFeatureDevice ( ) ;
63
- using var hc = new HidFeatureReportConnection ( mock . Object ) ;
64
- mock . Verify ( hdd => hdd . OpenFeatureConnection ( ) , Times . Once ( ) ) ;
62
+ IHidDDevice mock = GetMockedFeatureDevice ( ) ;
63
+ using var hc = new HidFeatureReportConnection ( mock ) ;
64
+ mock . Received ( ) . OpenFeatureConnection ( ) ;
65
65
}
66
66
67
67
[ Fact ]
68
68
public void Constructor_GivenFeatureDevice_SetsInputReportSize ( )
69
69
{
70
- Mock < IHidDDevice > mock = GetMockedFeatureDevice ( ) ;
71
- using var hc = new HidFeatureReportConnection ( mock . Object ) ;
70
+ IHidDDevice mock = GetMockedFeatureDevice ( ) ;
71
+ using var hc = new HidFeatureReportConnection ( mock ) ;
72
72
73
- Assert . Equal ( hc . InputReportSize , mock . Object . FeatureReportByteLength ) ;
73
+ Assert . Equal ( hc . InputReportSize , mock . FeatureReportByteLength ) ;
74
74
}
75
75
76
76
[ Fact ]
77
77
public void Constructor_GivenFeatureDevice_SetsOutputReportSize ( )
78
78
{
79
- Mock < IHidDDevice > mock = GetMockedFeatureDevice ( ) ;
80
- using var hc = new HidFeatureReportConnection ( mock . Object ) ;
79
+ IHidDDevice mock = GetMockedFeatureDevice ( ) ;
80
+ using var hc = new HidFeatureReportConnection ( mock ) ;
81
81
82
- Assert . Equal ( hc . OutputReportSize , mock . Object . FeatureReportByteLength ) ;
82
+ Assert . Equal ( hc . OutputReportSize , mock . FeatureReportByteLength ) ;
83
83
}
84
84
85
85
[ Fact ]
86
86
public void SetReport_GivenNullReport_ThrowsArgumentNullException ( )
87
87
{
88
- Mock < IHidDDevice > mock = GetMockedFeatureDevice ( ) ;
89
- using var hc = new HidFeatureReportConnection ( mock . Object ) ;
88
+ IHidDDevice mock = GetMockedFeatureDevice ( ) ;
89
+ using var hc = new HidFeatureReportConnection ( mock ) ;
90
90
91
91
#pragma warning disable CS8625 // Cannot convert null literal to non-nullable reference type.
92
92
_ = Assert . Throws < ArgumentNullException > ( ( ) => hc . SetReport ( null ) ) ;
@@ -96,78 +96,78 @@ public void SetReport_GivenNullReport_ThrowsArgumentNullException()
96
96
[ Fact ]
97
97
public void SetReport_GivenFeatureReports_CallsSetFeatureReport ( )
98
98
{
99
- Mock < IHidDDevice > mock = GetMockedFeatureDevice ( ) ;
100
- using var hc = new HidFeatureReportConnection ( mock . Object ) ;
99
+ IHidDDevice mock = GetMockedFeatureDevice ( ) ;
100
+ using var hc = new HidFeatureReportConnection ( mock ) ;
101
101
102
102
hc . SetReport ( GetFeatureReport ( ) ) ;
103
103
104
- mock . Verify ( hdd => hdd . SetFeatureReport ( IsSeqEqual ( GetFeatureReport ( ) ) ) , Times . Once ( ) ) ;
104
+ mock . Received ( ) . SetFeatureReport ( IsSeqEqual ( GetFeatureReport ( ) ) ) ;
105
105
}
106
106
107
107
[ Fact ]
108
108
public void GetReport_GivenFeatureReports_CallsGetFeatureReport ( )
109
109
{
110
- Mock < IHidDDevice > mock = GetMockedFeatureDevice ( ) ;
111
- using var hc = new HidFeatureReportConnection ( mock . Object ) ;
110
+ IHidDDevice mock = GetMockedFeatureDevice ( ) ;
111
+ using var hc = new HidFeatureReportConnection ( mock ) ;
112
112
113
113
byte [ ] report = hc . GetReport ( ) ;
114
114
115
- mock . Verify ( hdd => hdd . GetFeatureReport ( ) , Times . Once ( ) ) ;
115
+ mock . Received ( ) . GetFeatureReport ( ) ;
116
116
Assert . Equal ( GetFeatureReport ( ) , report ) ;
117
117
}
118
118
119
119
[ Fact ]
120
120
public void Constructor_GivenIODevice_CallsOpenIOConnection ( )
121
121
{
122
- Mock < IHidDDevice > mock = GetMockedIODevice ( ) ;
123
- using var hc = new HidIOReportConnection ( mock . Object ) ;
124
- mock . Verify ( hdd => hdd . OpenIOConnection ( ) , Times . Once ( ) ) ;
122
+ IHidDDevice mock = GetMockedIODevice ( ) ;
123
+ using var hc = new HidIOReportConnection ( mock ) ;
124
+ mock . Received ( ) . OpenIOConnection ( ) ;
125
125
}
126
126
127
127
[ Fact ]
128
128
public void Constructor_GivenIODevice_SetsInputReportSize ( )
129
129
{
130
- Mock < IHidDDevice > mock = GetMockedIODevice ( ) ;
131
- using var hc = new HidIOReportConnection ( mock . Object ) ;
130
+ IHidDDevice mock = GetMockedIODevice ( ) ;
131
+ using var hc = new HidIOReportConnection ( mock ) ;
132
132
133
- Assert . Equal ( hc . InputReportSize , mock . Object . InputReportByteLength ) ;
133
+ Assert . Equal ( hc . InputReportSize , mock . InputReportByteLength ) ;
134
134
}
135
135
136
136
[ Fact ]
137
137
public void Constructor_GivenIODevice_SetsOutputReportSize ( )
138
138
{
139
- Mock < IHidDDevice > mock = GetMockedIODevice ( ) ;
140
- using var hc = new HidIOReportConnection ( mock . Object ) ;
139
+ IHidDDevice mock = GetMockedIODevice ( ) ;
140
+ using var hc = new HidIOReportConnection ( mock ) ;
141
141
142
- Assert . Equal ( hc . OutputReportSize , mock . Object . OutputReportByteLength ) ;
142
+ Assert . Equal ( hc . OutputReportSize , mock . OutputReportByteLength ) ;
143
143
}
144
144
145
145
[ Fact ]
146
146
public void SetReport_GivenIOReports_CallsSetOutputReport ( )
147
147
{
148
- Mock < IHidDDevice > mock = GetMockedIODevice ( ) ;
149
- using var hc = new HidIOReportConnection ( mock . Object ) ;
148
+ IHidDDevice mock = GetMockedIODevice ( ) ;
149
+ using var hc = new HidIOReportConnection ( mock ) ;
150
150
151
151
hc . SetReport ( GetOutputReport ( ) ) ;
152
152
153
- mock . Verify ( hdd => hdd . SetOutputReport ( IsSeqEqual ( GetOutputReport ( ) ) ) , Times . Once ( ) ) ;
153
+ mock . Received ( ) . SetOutputReport ( IsSeqEqual ( GetOutputReport ( ) ) ) ;
154
154
}
155
155
156
156
[ Fact ]
157
157
public void GetReport_GivenIOReports_CallsGetInputReport ( )
158
158
{
159
- Mock < IHidDDevice > mock = GetMockedIODevice ( ) ;
160
- using var hc = new HidIOReportConnection ( mock . Object ) ;
159
+ IHidDDevice mock = GetMockedIODevice ( ) ;
160
+ using var hc = new HidIOReportConnection ( mock ) ;
161
161
162
162
byte [ ] report = hc . GetReport ( ) ;
163
163
164
- mock . Verify ( hdd => hdd . GetInputReport ( ) , Times . Once ( ) ) ;
164
+ mock . Received ( ) . GetInputReport ( ) ;
165
165
Assert . Equal ( GetInputReport ( ) , report ) ;
166
166
}
167
167
168
168
private static byte [ ] IsSeqEqual ( byte [ ] val )
169
169
{
170
- return It . Is < byte [ ] > ( b => b . SequenceEqual ( val ) ) ;
170
+ return Arg . Is < byte [ ] > ( b => b . SequenceEqual ( val ) ) ;
171
171
}
172
172
}
173
173
#endif
0 commit comments