Skip to content

Commit 8c21027

Browse files
committed
WIP Fix test warnings
1 parent f0e5cd8 commit 8c21027

27 files changed

Lines changed: 432 additions & 361 deletions

test/Tesla.NET.Tests/DebuggerDisplayTests.cs

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@ namespace Tesla.NET
1212

1313
public abstract class DebuggerDisplayTestsBase : FixtureContext
1414
{
15-
private Type _sutType;
16-
private DebuggerDisplayAttribute _debuggerDisplay;
17-
private PropertyInfo _debuggerDisplayPropertyInfo;
18-
private MethodInfo _debuggerDisplayGetMethod;
19-
private object _debuggerDisplayValue;
20-
protected string DebuggerDisplayText;
15+
private Type? _sutType;
16+
private DebuggerDisplayAttribute? _debuggerDisplay;
17+
private PropertyInfo? _debuggerDisplayPropertyInfo;
18+
private MethodInfo? _debuggerDisplayGetMethod;
19+
private object? _debuggerDisplayValue;
20+
protected string? DebuggerDisplayText;
2121

2222
protected DebuggerDisplayTestsBase(ITestOutputHelper output)
2323
: base(output)
@@ -26,18 +26,18 @@ protected DebuggerDisplayTestsBase(ITestOutputHelper output)
2626

2727
protected void GetDebuggerDisplay<TSut>(TSut sut)
2828
{
29-
_sutType = sut.GetType();
29+
_sutType = sut?.GetType();
3030

31-
_debuggerDisplay = _sutType.GetTypeInfo().GetCustomAttribute<DebuggerDisplayAttribute>(inherit: false);
31+
_debuggerDisplay = _sutType?.GetCustomAttribute<DebuggerDisplayAttribute>(inherit: false);
3232

3333
_debuggerDisplayPropertyInfo =
34-
_sutType.GetProperty("DebuggerDisplay", BindingFlags.NonPublic | BindingFlags.Instance);
34+
_sutType?.GetProperty("DebuggerDisplay", BindingFlags.NonPublic | BindingFlags.Instance);
3535

36-
_debuggerDisplayGetMethod = _debuggerDisplayPropertyInfo.GetGetMethod(true);
36+
_debuggerDisplayGetMethod = _debuggerDisplayPropertyInfo?.GetGetMethod(true);
3737

38-
_debuggerDisplayValue = _debuggerDisplayGetMethod.Invoke(sut, new object[] { });
38+
_debuggerDisplayValue = _debuggerDisplayGetMethod?.Invoke(sut, new object[] { });
3939

40-
DebuggerDisplayText = _debuggerDisplayValue.ToString();
40+
DebuggerDisplayText = _debuggerDisplayValue?.ToString();
4141
}
4242

4343
[Fact]
@@ -49,6 +49,8 @@ public void have_the_debugger_display_attribute()
4949
[Fact]
5050
public void specify_the_debugger_display_property()
5151
{
52+
if (_debuggerDisplay is null) throw new InvalidOperationException(nameof(_debuggerDisplay) + " is null");
53+
5254
_debuggerDisplay.Value.Should().BeEquivalentTo("{DebuggerDisplay,nq}");
5355
}
5456

@@ -73,6 +75,8 @@ public void provide_a_string_debugger_display_property()
7375
[Fact]
7476
public void include_the_type_in_the_debugger_display()
7577
{
78+
if (_sutType is null) throw new InvalidOperationException(nameof(_sutType) + " is null");
79+
7680
DebuggerDisplayText.Should().StartWith($"{_sutType.Name}:");
7781
}
7882
}

test/Tesla.NET.Tests/FixedContructorArgument.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public FixedContructorArgument(string parameterName, Func<TParameterType> valueF
4141
_valueFunc = valueFunc ?? throw new ArgumentNullException(nameof(valueFunc));
4242
}
4343

44-
public object Create(object request, ISpecimenContext context)
44+
public object? Create(object request, ISpecimenContext context)
4545
{
4646
if (!(request is ParameterInfo pi))
4747
return new NoSpecimen();

test/Tesla.NET.Tests/GetChargeStateTests.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,8 @@ public When_getting_the_charge_state_for_a_vehicle_the_raw_JSON(ITestOutputHelpe
160160
// Add random values to test whether it is correctly passed through.
161161
_expected["randomValue1"] = Fixture.Create("randomValue1");
162162
_expected["randomValue2"] = JObject.FromObject(new { fakeId = Guid.NewGuid() });
163-
_expected["response"]["randomValue3"] = Fixture.Create("randomValue3");
163+
JToken response = _expected["response"] ?? throw new InvalidOperationException("response is null.");
164+
response["randomValue3"] = Fixture.Create("randomValue3");
164165

165166
Handler.SetResponseContent(_expected);
166167
}

test/Tesla.NET.Tests/GetDriveStateTests.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,8 @@ public When_getting_the_drive_state_for_a_vehicle_the_raw_JSON(ITestOutputHelper
160160
// Add random values to test whether it is correctly passed through.
161161
_expected["randomValue1"] = Fixture.Create("randomValue1");
162162
_expected["randomValue2"] = JObject.FromObject(new { fakeId = Guid.NewGuid() });
163-
_expected["response"]["randomValue3"] = Fixture.Create("randomValue3");
163+
JToken response = _expected["response"] ?? throw new InvalidOperationException("response is null.");
164+
response["randomValue3"] = Fixture.Create("randomValue3");
164165

165166
Handler.SetResponseContent(_expected);
166167
}

test/Tesla.NET.Tests/GetVehicleStateTests.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,8 @@ public When_getting_the_vehicle_state_for_a_vehicle_the_raw_JSON(ITestOutputHelp
160160
// Add random values to test whether it is correctly passed through.
161161
_expected["randomValue1"] = Fixture.Create("randomValue1");
162162
_expected["randomValue2"] = JObject.FromObject(new { fakeId = Guid.NewGuid() });
163-
_expected["response"]["randomValue3"] = Fixture.Create("randomValue3");
163+
JToken response = _expected["response"] ?? throw new InvalidOperationException("response is null.");
164+
response["randomValue3"] = Fixture.Create("randomValue3");
164165

165166
Handler.SetResponseContent(_expected);
166167
}
@@ -191,7 +192,8 @@ public When_getting_the_vehicle_state_for_a_vehicle_the_raw_JSON_with_null_value
191192
// Add random values to test whether it is correctly passed through.
192193
_expected["randomValue1"] = Fixture.Create("randomValue1");
193194
_expected["randomValue2"] = JObject.FromObject(new { fakeId = Guid.NewGuid() });
194-
_expected["response"]["randomValue3"] = Fixture.Create("randomValue3");
195+
JToken response = _expected["response"] ?? throw new InvalidOperationException("response is null.");
196+
response["randomValue3"] = Fixture.Create("randomValue3");
195197

196198
Handler.SetResponseContent(_expected);
197199
}
@@ -202,6 +204,8 @@ public async Task Should_be_passed_through_in_the_response()
202204
// Act
203205
IMessageResponse<IResponseDataWrapper<IVehicleState>> response = await Sut.GetVehicleStateAsync(_vehicleId).ConfigureAwait(false);
204206

207+
if (response.Data is null) throw new InvalidOperationException("response data is null.");
208+
205209
response.Data.Response.SunRoofPercentOpen.Should().Be(null);
206210

207211
// Assert

test/Tesla.NET.Tests/GetVehiclesTests.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,8 @@ public When_getting_the_vehicles_for_an_account_the_raw_JSON(ITestOutputHelper o
153153
// Add random values to test whether it is correctly passed through.
154154
_expected["randomValue1"] = Fixture.Create("randomValue1");
155155
_expected["randomValue2"] = JObject.FromObject(new { fakeId = Guid.NewGuid() });
156-
_expected["response"][0]["randomValue3"] = Fixture.Create("randomValue3");
156+
JToken response = _expected["response"]?[0] ?? throw new InvalidOperationException("response is null.");
157+
response["randomValue3"] = Fixture.Create("randomValue3");
157158

158159
Handler.SetResponseContent(_expected);
159160
}

test/Tesla.NET.Tests/HttpHandlers/ForcedAsyncStream.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,6 @@ protected override void Dispose(bool disposing)
7979
_inner?.Dispose();
8080
}
8181

82-
_inner = null;
8382
base.Dispose(disposing);
8483
}
8584
}

test/Tesla.NET.Tests/HttpHandlers/TestHttpHandler.cs

Lines changed: 79 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,63 +17,126 @@ namespace Tesla.NET.HttpHandlers
1717
using Newtonsoft.Json.Linq;
1818
using Xunit.Abstractions;
1919

20+
/// <summary>
21+
/// A test <see cref="HttpMessageHandler"/> that records requests and returned fixed responses.
22+
/// </summary>
2023
public class TestHttpHandler : HttpMessageHandler
2124
{
2225
private readonly ITestOutputHelper _output;
2326

2427
private int _responseIndex;
2528

29+
/// <summary>
30+
/// Initializes a new instance of the <see cref="TestHttpHandler"/> class.
31+
/// </summary>
32+
/// <param name="output">The <see cref="ITestOutputHelper"/>.</param>
2633
public TestHttpHandler(ITestOutputHelper output)
2734
{
2835
_output = output ?? throw new ArgumentNullException(nameof(output));
2936
}
3037

38+
/// <summary>
39+
/// Gets or sets the last received request.
40+
/// </summary>
3141
public HttpRequestMessage Request
3242
{
33-
get => Requests.Count == 0 ? null : Requests[Requests.Count - 1];
43+
get
44+
{
45+
if (Requests.Count == 0)
46+
throw new InvalidOperationException("There are no requests to return.");
47+
48+
return Requests[Requests.Count - 1];
49+
}
50+
3451
set
3552
{
53+
if (value is null)
54+
throw new ArgumentNullException(nameof(value));
55+
3656
Requests.Clear();
3757
Requests.Add(value);
3858
}
3959
}
4060

61+
/// <summary>
62+
/// Gets the list of received requests.
63+
/// </summary>
4164
public List<HttpRequestMessage> Requests { get; } = new List<HttpRequestMessage>(1);
4265

66+
/// <summary>
67+
/// Gets the list of fixed responses.
68+
/// </summary>
4369
public List<HttpResponseMessage> Responses { get; } = new List<HttpResponseMessage>(1);
4470

45-
public List<string> RequestContents { get; } = new List<string>(1);
71+
/// <summary>
72+
/// Gets the list of request contents.
73+
/// </summary>
74+
public List<string?> RequestContents { get; } = new List<string?>(1);
4675

76+
/// <summary>
77+
/// Gets or sets the last fixed response.
78+
/// </summary>
4779
public HttpResponseMessage Response
4880
{
49-
get => Responses.Count == 0 ? null : Responses[Responses.Count - 1];
81+
get
82+
{
83+
if (Responses.Count == 0)
84+
throw new InvalidOperationException("There are no responses to return.");
85+
86+
return Responses[Responses.Count - 1];
87+
}
88+
5089
set
5190
{
91+
if (value is null)
92+
throw new ArgumentNullException(nameof(value));
93+
5294
_responseIndex = 0;
5395
Responses.Clear();
5496
Responses.Add(value);
5597
}
5698
}
5799

58-
public Func<HttpRequestMessage, Task<HttpRequestMessage>> OnSendingRequest { get; set; }
100+
/// <summary>
101+
/// Get or sets a function to intercept request sending.
102+
/// </summary>
103+
public Func<HttpRequestMessage, Task<HttpRequestMessage>>? OnSendingRequest { get; set; }
59104

105+
/// <summary>
106+
/// Sets the fixed response with the specified <paramref name="code"/>.
107+
/// </summary>
108+
/// <param name="code">The response <see cref="HttpStatusCode"/> to use.</param>
60109
public void SetResponse(HttpStatusCode code) => SetResponseContent(null, code);
61110

62-
public void SetResponseContent(object content, HttpStatusCode code = HttpStatusCode.OK)
111+
/// <summary>
112+
/// Sets the fixed response with the specified <paramref name="content"/> and <paramref name="code"/>.
113+
/// </summary>
114+
/// <param name="content">The response content to use.</param>
115+
/// <param name="code">The response <see cref="HttpStatusCode"/> to use.</param>
116+
public void SetResponseContent(object? content, HttpStatusCode code = HttpStatusCode.OK)
63117
{
64118
Response = CreateResponse(content, code);
65119
}
66120

121+
/// <summary>
122+
/// Adds the fixed response with the specified <paramref name="content"/> and <paramref name="code"/>.
123+
/// </summary>
124+
/// <param name="content">The response content to use.</param>
125+
/// <param name="code">The response <see cref="HttpStatusCode"/> to use.</param>
67126
public void AddResponseContent(object content, HttpStatusCode code = HttpStatusCode.OK)
68127
{
69128
Responses.Add(CreateResponse(content, code));
70129
}
71130

131+
/// <inheritdoc />
72132
protected override async Task<HttpResponseMessage> SendAsync(
73133
HttpRequestMessage request,
74134
CancellationToken cancellationToken)
75135
{
76-
string requestContent = await GetStringContent(request.Content).ConfigureAwait(false);
136+
if (request is null)
137+
throw new ArgumentNullException(nameof(request));
138+
139+
string? requestContent = await GetStringContent(request.Content).ConfigureAwait(false);
77140
RequestContents.Add(requestContent);
78141

79142
_output.WriteLine($"Started {request.Method} '{request.RequestUri}'");
@@ -97,15 +160,15 @@ protected override async Task<HttpResponseMessage> SendAsync(
97160
HttpResponseMessage responseMessage =
98161
_responseIndex < Responses.Count
99162
? Responses[_responseIndex++]
100-
: CreateResponse(string.Empty);
163+
: CreateResponse("{}");
101164

102165
responseMessage.RequestMessage = request;
103166

104167
_output.WriteLine(
105168
$"Completed {request.Method} '{request.RequestUri}' " +
106169
$"with {responseMessage.StatusCode:G} ({responseMessage.StatusCode:D})");
107170

108-
string responseContent = await GetStringContent(responseMessage.Content).ConfigureAwait(false);
171+
string? responseContent = await GetStringContent(responseMessage.Content).ConfigureAwait(false);
109172
if (responseContent != null)
110173
{
111174
_output.WriteLine(responseContent);
@@ -114,15 +177,15 @@ protected override async Task<HttpResponseMessage> SendAsync(
114177
return responseMessage;
115178
}
116179

117-
private static async Task<string> GetStringContent(HttpContent content)
180+
private static async Task<string?> GetStringContent(HttpContent content)
118181
{
119-
if (content == null)
182+
if (content is null)
120183
return null;
121184

122185
string contentData;
123186
if (content is ForcedAsyncStreamContent delayedContent)
124187
{
125-
using (var s = new StreamReader(delayedContent.Stream, Encoding.UTF8, true, 10240, leaveOpen:true))
188+
using (var s = new StreamReader(delayedContent.Stream, Encoding.UTF8, true, 10240, leaveOpen: true))
126189
{
127190
contentData = s.ReadToEnd();
128191
}
@@ -134,7 +197,8 @@ private static async Task<string> GetStringContent(HttpContent content)
134197
contentData = await content.ReadAsStringAsync().ConfigureAwait(false);
135198
}
136199

137-
bool? isJson = content.Headers?.ContentType?.ToString().Equals("application/json");
200+
bool? isJson = content.Headers?.ContentType?.ToString()
201+
.Equals("application/json", StringComparison.OrdinalIgnoreCase);
138202
if (isJson.GetValueOrDefault())
139203
{
140204
contentData = JToken.Parse(contentData).ToString(Formatting.Indented);
@@ -143,9 +207,9 @@ private static async Task<string> GetStringContent(HttpContent content)
143207
return contentData;
144208
}
145209

146-
private static HttpResponseMessage CreateResponse(object content, HttpStatusCode code = HttpStatusCode.OK)
210+
private static HttpResponseMessage CreateResponse(object? content, HttpStatusCode code = HttpStatusCode.OK)
147211
{
148-
if (content == null)
212+
if (content is null)
149213
{
150214
return new HttpResponseMessage(code);
151215
}
@@ -166,7 +230,7 @@ private static HttpResponseMessage CreateResponse(object content, HttpStatusCode
166230
Headers =
167231
{
168232
ContentType = new MediaTypeHeaderValue("application/json"),
169-
}
233+
},
170234
};
171235

172236
return new HttpResponseMessage(code)

0 commit comments

Comments
 (0)