Skip to content

Commit 5aa8d8b

Browse files
committed
StyleCop enabled for PartialResponse
1 parent f711def commit 5aa8d8b

4 files changed

Lines changed: 61 additions & 31 deletions

File tree

test/PartialResponse.AspNetCore.Mvc.Formatters.Json.Test/JsonSerializerExtensionsTests.cs

Lines changed: 34 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// Copyright (c) Arjen Post. See License.txt and Notice.txt in the project root for license information.
2+
13
using System.Collections.Generic;
24
using System.IO;
35
using System.Text;
@@ -25,10 +27,10 @@ public void TheSerializeMethodShouldFilterArrayElements()
2527
var value = new List<dynamic> { new { foo = "bar", baz = "qux" } };
2628

2729
// Act
28-
jsonSerializer.Serialize(this.jsonWriter, value, _ => _ == "foo");
30+
this.jsonSerializer.Serialize(this.jsonWriter, value, _ => _ == "foo");
2931

3032
// Assert
31-
Assert.Equal("[{\"foo\":\"bar\"}]", result.ToString());
33+
Assert.Equal("[{\"foo\":\"bar\"}]", this.result.ToString());
3234
}
3335

3436
[Fact]
@@ -38,10 +40,10 @@ public void TheSerializeMethodShouldRemoveEmptyArray()
3840
var value = new { foo = new List<dynamic> { new { bar = "baz" } }, qux = "quux" };
3941

4042
// Act
41-
jsonSerializer.Serialize(this.jsonWriter, value, _ => _ == "qux");
43+
this.jsonSerializer.Serialize(this.jsonWriter, value, _ => _ == "qux");
4244

4345
// Assert
44-
Assert.Equal("{\"qux\":\"quux\"}", result.ToString());
46+
Assert.Equal("{\"qux\":\"quux\"}", this.result.ToString());
4547
}
4648

4749
[Fact]
@@ -51,10 +53,10 @@ public void TheSerializeMethodShouldNotRemoveEmptyArrayIfRoot()
5153
var value = new List<dynamic> { new { foo = "bar" } };
5254

5355
// Act
54-
jsonSerializer.Serialize(this.jsonWriter, value, _ => false);
56+
this.jsonSerializer.Serialize(this.jsonWriter, value, _ => false);
5557

5658
// Assert
57-
Assert.Equal("[]", result.ToString());
59+
Assert.Equal("[]", this.result.ToString());
5860
}
5961

6062
[Fact]
@@ -64,10 +66,10 @@ public void TheSerializeMethodShouldFilterObjectPropertiesInsideArrayElement()
6466
var value = new List<dynamic> { new { foo = new { bar = "baz", qux = "quux" } } };
6567

6668
// Act
67-
jsonSerializer.Serialize(this.jsonWriter, value, _ => _ == "foo" || _ == "foo/bar");
69+
this.jsonSerializer.Serialize(this.jsonWriter, value, _ => _ == "foo" || _ == "foo/bar");
6870

6971
// Assert
70-
Assert.Equal("[{\"foo\":{\"bar\":\"baz\"}}]", result.ToString());
72+
Assert.Equal("[{\"foo\":{\"bar\":\"baz\"}}]", this.result.ToString());
7173
}
7274

7375
[Fact]
@@ -77,10 +79,10 @@ public void TheSerializeMethodShouldFilterObjectProperties()
7779
var value = new { foo = "bar", baz = "qux" };
7880

7981
// Act
80-
jsonSerializer.Serialize(this.jsonWriter, value, _ => _ == "foo");
82+
this.jsonSerializer.Serialize(this.jsonWriter, value, _ => _ == "foo");
8183

8284
// Assert
83-
Assert.Equal("{\"foo\":\"bar\"}", result.ToString());
85+
Assert.Equal("{\"foo\":\"bar\"}", this.result.ToString());
8486
}
8587

8688
[Fact]
@@ -90,10 +92,10 @@ public void TheSerializeMethodShouldRemoveEmptyObject()
9092
var value = new { foo = new { bar = "baz" }, qux = "quux" };
9193

9294
// Act
93-
jsonSerializer.Serialize(this.jsonWriter, value, _ => _ == "qux");
95+
this.jsonSerializer.Serialize(this.jsonWriter, value, _ => _ == "qux");
9496

9597
// Assert
96-
Assert.Equal("{\"qux\":\"quux\"}", result.ToString());
98+
Assert.Equal("{\"qux\":\"quux\"}", this.result.ToString());
9799
}
98100

99101
[Fact]
@@ -103,10 +105,10 @@ public void TheSerializeMethodShouldNotRemoveEmptyObjectIfRoot()
103105
var value = new { foo = "bar" };
104106

105107
// Act
106-
jsonSerializer.Serialize(this.jsonWriter, value, _ => false);
108+
this.jsonSerializer.Serialize(this.jsonWriter, value, _ => false);
107109

108110
// Assert
109-
Assert.Equal("{}", result.ToString());
111+
Assert.Equal("{}", this.result.ToString());
110112
}
111113

112114
[Fact]
@@ -116,10 +118,10 @@ public void TheSerializeMethodShouldFilterObjectPropertiesInsideObjectProperties
116118
var value = new { foo = new { bar = "baz", qux = "quux" } };
117119

118120
// Act
119-
jsonSerializer.Serialize(this.jsonWriter, value, _ => _ == "foo" || _ == "foo/bar");
121+
this.jsonSerializer.Serialize(this.jsonWriter, value, _ => _ == "foo" || _ == "foo/bar");
120122

121123
// Assert
122-
Assert.Equal("{\"foo\":{\"bar\":\"baz\"}}", result.ToString());
124+
Assert.Equal("{\"foo\":{\"bar\":\"baz\"}}", this.result.ToString());
123125
}
124126

125127
[Fact]
@@ -129,10 +131,10 @@ public void TheSerializeMethodShouldFilterArrayElementsInsideObjectProperties()
129131
var value = new { foo = new List<dynamic> { new { bar = "baz", qux = "quux" } } };
130132

131133
// Act
132-
jsonSerializer.Serialize(this.jsonWriter, value, _ => _ == "foo" || _ == "foo/bar");
134+
this.jsonSerializer.Serialize(this.jsonWriter, value, _ => _ == "foo" || _ == "foo/bar");
133135

134136
// Assert
135-
Assert.Equal("{\"foo\":[{\"bar\":\"baz\"}]}", result.ToString());
137+
Assert.Equal("{\"foo\":[{\"bar\":\"baz\"}]}", this.result.ToString());
136138
}
137139

138140
[Fact]
@@ -142,10 +144,10 @@ public void TheSerializeMethodShouldFilterArrayElementInsideArrayElement()
142144
var value = new List<dynamic> { new { foo = new List<dynamic> { new { bar = "baz", qux = "quux" } } } };
143145

144146
// Act
145-
jsonSerializer.Serialize(this.jsonWriter, value, _ => _ == "foo" || _ == "foo/bar");
147+
this.jsonSerializer.Serialize(this.jsonWriter, value, _ => _ == "foo" || _ == "foo/bar");
146148

147149
// Assert
148-
Assert.Equal("[{\"foo\":[{\"bar\":\"baz\"}]}]", result.ToString());
150+
Assert.Equal("[{\"foo\":[{\"bar\":\"baz\"}]}]", this.result.ToString());
149151
}
150152

151153
[Fact]
@@ -155,8 +157,15 @@ public void TheSerializeMethodShouldApplyCaching()
155157
var value = new List<dynamic> { new { foo = "bar" }, new { foo = "bar" } };
156158
var count = 0;
157159

160+
bool shouldSerialize(string path)
161+
{
162+
count++;
163+
164+
return path == "foo";
165+
}
166+
158167
// Act
159-
jsonSerializer.Serialize(this.jsonWriter, value, _ => { count++; return _ == "foo"; });
168+
this.jsonSerializer.Serialize(this.jsonWriter, value, shouldSerialize);
160169

161170
// Assert
162171
Assert.Equal(1, count);
@@ -169,10 +178,10 @@ public void TheSerializeMethodFilterCorrectlyUsingCache()
169178
var value = new List<dynamic> { new { foo = "bar", baz = "qux" }, new { foo = "bar", baz = "qux" } };
170179

171180
// Act
172-
jsonSerializer.Serialize(this.jsonWriter, value, _ => _ == "foo");
181+
this.jsonSerializer.Serialize(this.jsonWriter, value, _ => _ == "foo");
173182

174183
// Assert
175-
Assert.Equal("[{\"foo\":\"bar\"},{\"foo\":\"bar\"}]", result.ToString());
184+
Assert.Equal("[{\"foo\":\"bar\"},{\"foo\":\"bar\"}]", this.result.ToString());
176185
}
177186

178187
[Fact]
@@ -182,10 +191,10 @@ public void TheSerializeMethodShouldFilterArrayElementInsideArrayElement2()
182191
var value = new { totalCount = 0, data = new object[0] };
183192

184193
// Act
185-
jsonSerializer.Serialize(this.jsonWriter, value, _ => true);
194+
this.jsonSerializer.Serialize(this.jsonWriter, value, _ => true);
186195

187196
// Assert
188-
Assert.Equal("{\"totalCount\":0,\"data\":[]}", result.ToString());
197+
Assert.Equal("{\"totalCount\":0,\"data\":[]}", this.result.ToString());
189198
}
190199
}
191200
}

test/PartialResponse.AspNetCore.Mvc.Formatters.Json.Test/PartialJsonOutputFormatterTests.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// Copyright (c) Arjen Post. See License.txt and Notice.txt in the project root for license information.
2+
13
using System.Buffers;
24
using System.Collections.Generic;
35
using System.IO;
@@ -59,7 +61,7 @@ public async Task TheWriteResponseBodyAsyncMethodShouldReturnStatusCode400IfFiel
5961
.SetupGet(queryCollection => queryCollection["fields"])
6062
.Returns("foo/");
6163

62-
var writeContext = new OutputFormatterWriteContext(this.httpContext, (stream, encoding) => new StringWriter(this.body), typeof(object), new {});
64+
var writeContext = new OutputFormatterWriteContext(this.httpContext, (stream, encoding) => new StringWriter(this.body), typeof(object), new { });
6365
var formatter = new PartialJsonOutputFormatter(new JsonSerializerSettings(), Mock.Of<ArrayPool<char>>(), false);
6466

6567
// Act
@@ -86,7 +88,7 @@ public async Task TheWriteResponseBodyAsyncMethodShouldNotWriteBodyIfFieldsMalfo
8688
.SetupGet(queryCollection => queryCollection["fields"])
8789
.Returns("foo/");
8890

89-
var writeContext = new OutputFormatterWriteContext(this.httpContext, (stream, encoding) => new StringWriter(this.body), typeof(object), new {});
91+
var writeContext = new OutputFormatterWriteContext(this.httpContext, (stream, encoding) => new StringWriter(this.body), typeof(object), new { });
9092
var formatter = new PartialJsonOutputFormatter(new JsonSerializerSettings(), Mock.Of<ArrayPool<char>>(), false);
9193

9294
// Act
@@ -218,7 +220,7 @@ public async Task TheWriteResponseBodyAsyncMethodShouldBypassPartialResponseIfCo
218220

219221
Mock.Get(this.queryCollection)
220222
.SetupGet(queryCollection => queryCollection["fields"])
221-
.Returns("");
223+
.Returns(string.Empty);
222224

223225
this.httpRequest.BypassPartialResponse();
224226

@@ -248,7 +250,7 @@ public async Task TheWriteResponseBodyAsyncMethodShouldBypassPartialResponseIfSt
248250

249251
Mock.Get(this.queryCollection)
250252
.SetupGet(queryCollection => queryCollection["fields"])
251-
.Returns("");
253+
.Returns(string.Empty);
252254

253255
var value = new { foo = "bar" };
254256

test/PartialResponse.AspNetCore.Mvc.Formatters.Json.Test/PartialJsonResultExecutorTests.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// Copyright (c) Arjen Post. See License.txt and Notice.txt in the project root for license information.
2+
13
using System.Buffers;
24
using System.IO;
35
using System.Text;
@@ -66,7 +68,7 @@ public async Task TheExecuteAsyncMethodShouldReturnStatusCode400IfFieldsMalforme
6668
.SetupGet(queryCollection => queryCollection["fields"])
6769
.Returns("foo/");
6870

69-
var partialJsonResult = new PartialJsonResult(new {});
71+
var partialJsonResult = new PartialJsonResult(new { });
7072

7173
// Act
7274
await this.executor.ExecuteAsync(this.actionContext, partialJsonResult);
@@ -88,7 +90,7 @@ public async Task TheExecuteAsyncMethodShouldNotWriteBodyIfFieldsMalformed()
8890
.SetupGet(queryCollection => queryCollection["fields"])
8991
.Returns("foo/");
9092

91-
var partialJsonResult = new PartialJsonResult(new {});
93+
var partialJsonResult = new PartialJsonResult(new { });
9294

9395
// Act
9496
await this.executor.ExecuteAsync(this.actionContext, partialJsonResult);

test/PartialResponse.AspNetCore.Mvc.Formatters.Json.Test/PartialResponse.AspNetCore.Mvc.Formatters.Json.Test.csproj

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,21 @@
2525
<PackageReference Include="xunit.runner.visualstudio" Version="2.3.0" />
2626
</ItemGroup>
2727

28+
<ItemGroup>
29+
<PackageReference Include="StyleCop.Analyzers" Version="1.0.2" PrivateAssets="All" />
30+
</ItemGroup>
31+
32+
<ItemGroup>
33+
<AdditionalFiles Include="..\stylecop.json">
34+
<Link>stylecop.json</Link>
35+
</AdditionalFiles>
36+
<AdditionalFiles Include="..\stylecop.ruleset">
37+
<Link>stylecop.ruleset</Link>
38+
</AdditionalFiles>
39+
</ItemGroup>
40+
41+
<PropertyGroup>
42+
<CodeAnalysisRuleSet>..\stylecop.ruleset</CodeAnalysisRuleSet>
43+
</PropertyGroup>
44+
2845
</Project>

0 commit comments

Comments
 (0)