Skip to content

Commit 70d8b95

Browse files
committed
Make all JSON strings valid in tests
So that we can get rid of the `ToValidJson` extension method. Now that raw string literals have been introduced (in serilog#354) having valid _readable_ JSON in strings for tests becomes easy. There is no need to escape quotes anymore with either `\"` or `""`. This will also make it easier for newcomers to understand the tests at first glance. I had my WTF moment when I first saw JSON with single quotes. 🤔
1 parent de58300 commit 70d8b95

File tree

6 files changed

+71
-68
lines changed

6 files changed

+71
-68
lines changed

test/Serilog.Settings.Configuration.Tests/ConfigurationReaderTests.cs

Lines changed: 33 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
using System.Globalization;
21
using System.Reflection;
32
using Microsoft.Extensions.Configuration;
43
using Serilog.Events;
@@ -16,7 +15,7 @@ public class ConfigurationReaderTests
1615
public ConfigurationReaderTests()
1716
{
1817
_configurationReader = new ConfigurationReader(
19-
JsonStringConfigSource.LoadSection("{ 'Serilog': { } }", "Serilog"),
18+
JsonStringConfigSource.LoadSection("{ \"Serilog\": { } }", "Serilog"),
2019
AssemblyFinder.ForSource(ConfigurationAssemblySource.UseLoadedAssemblies),
2120
new ConfigurationReaderOptions());
2221
}
@@ -26,7 +25,7 @@ public void WriteToSupportSimplifiedSyntax()
2625
{
2726
var json = """
2827
{
29-
'WriteTo': [ 'LiterateConsole', 'DiagnosticTrace' ]
28+
"WriteTo": [ "LiterateConsole", "DiagnosticTrace" ]
3029
}
3130
""";
3231

@@ -44,8 +43,8 @@ public void WriteToSupportExpandedSyntaxWithoutArgs()
4443
{
4544
var json = """
4645
{
47-
'WriteTo': [ {
48-
'Name': 'LiterateConsole'
46+
"WriteTo": [ {
47+
"Name": "LiterateConsole"
4948
}]
5049
}
5150
""";
@@ -62,10 +61,10 @@ public void WriteToSupportExpandedSyntaxWithArgs()
6261
{
6362
var json = """
6463
{
65-
'WriteTo': [ {
66-
'Name': 'LiterateConsole',
67-
'Args': {
68-
'outputTemplate': '{Message}'
64+
"WriteTo": [ {
65+
"Name": "LiterateConsole",
66+
"Args": {
67+
"outputTemplate": "{Message}"
6968
},
7069
}]
7170
}
@@ -90,25 +89,25 @@ public void WriteToSupportMultipleSinksOfTheSameKind()
9089
{
9190
var json = """
9291
{
93-
'WriteTo': [
92+
"WriteTo": [
9493
{
95-
'Name': 'LiterateConsole',
96-
'Args': {
97-
'outputTemplate': '{Message}'
94+
"Name": "LiterateConsole",
95+
"Args": {
96+
"outputTemplate": "{Message}"
9897
},
9998
},
100-
'DiagnosticTrace'
99+
"DiagnosticTrace"
101100
],
102-
'WriteTo:File1': {
103-
'Name': 'File',
104-
'Args': {
105-
'outputTemplate': '{Message}'
101+
"WriteTo:File1": {
102+
"Name": "File",
103+
"Args": {
104+
"outputTemplate": "{Message}"
106105
},
107106
},
108-
'WriteTo:File2': {
109-
'Name': 'File',
110-
'Args': {
111-
'outputTemplate': '{Message}'
107+
"WriteTo:File2": {
108+
"Name": "File",
109+
"Args": {
110+
"outputTemplate": "{Message}"
112111
},
113112
}
114113
}
@@ -131,7 +130,7 @@ public void Enrich_SupportSimplifiedSyntax()
131130
{
132131
var json = """
133132
{
134-
'Enrich': [ 'FromLogContext', 'WithMachineName', 'WithThreadId' ]
133+
"Enrich": [ "FromLogContext", "WithMachineName", "WithThreadId" ]
135134
}
136135
""";
137136

@@ -274,7 +273,17 @@ public void MixedMinimumLevelCorrectOneIsEnabledOnLogger(IConfigurationRoot root
274273
[Fact]
275274
public void NoConfigurationRootUsedStillValid()
276275
{
277-
var section = JsonStringConfigSource.LoadSection("{ 'Nest': { 'Serilog': { 'MinimumLevel': 'Error' } } }", "Nest");
276+
var json = """
277+
{
278+
"Nest": {
279+
"Serilog": {
280+
"MinimumLevel": "Error"
281+
}
282+
}
283+
}
284+
""";
285+
286+
var section = JsonStringConfigSource.LoadSection(json, "Nest");
278287
var reader = new ConfigurationReader(section.GetSection("Serilog"), AssemblyFinder.ForSource(ConfigurationAssemblySource.UseLoadedAssemblies), new ConfigurationReaderOptions(), section);
279288
var loggerConfig = new LoggerConfiguration();
280289

test/Serilog.Settings.Configuration.Tests/ConfigurationSettingsTests.cs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -395,12 +395,12 @@ public void LoggingFilterSwitchIsConfigured(string switchName)
395395
{
396396
var json = $$"""
397397
{
398-
'Serilog': {
399-
'FilterSwitches': { '{{switchName}}': 'Prop = 42' },
400-
'Filter:BySwitch': {
401-
'Name': 'ControlledBy',
402-
'Args': {
403-
'switch': '$mySwitch'
398+
"Serilog": {
399+
"FilterSwitches": { "{{switchName}}": "Prop = 42" },
400+
"Filter:BySwitch": {
401+
"Name": "ControlledBy",
402+
"Args": {
403+
"switch": "$mySwitch"
404404
}
405405
}
406406
}
@@ -426,10 +426,10 @@ public void LoggingLevelSwitchIsConfigured(string switchName)
426426
{
427427
var json = $$"""
428428
{
429-
'Serilog': {
430-
'LevelSwitches': { '{{switchName}}' : 'Warning' },
431-
'MinimumLevel' : {
432-
'ControlledBy' : '$switch1'
429+
"Serilog": {
430+
"LevelSwitches": { "{{switchName}}" : "Warning" },
431+
"MinimumLevel" : {
432+
"ControlledBy" : "$switch1"
433433
}
434434
}
435435
}
@@ -1379,12 +1379,12 @@ public void TestLogLevelSwitchesCallback(string switchName)
13791379
{
13801380
var json = $$"""
13811381
{
1382-
'Serilog': {
1383-
'LevelSwitches': { '{{switchName}}': 'Information' },
1384-
'MinimumLevel': {
1385-
'Override': {
1386-
'System': 'Warning',
1387-
'System.Threading': 'Debug'
1382+
"Serilog": {
1383+
"LevelSwitches": { "{{switchName}}": "Information" },
1384+
"MinimumLevel": {
1385+
"Override": {
1386+
"System": "Warning",
1387+
"System.Threading": "Debug"
13881388
}
13891389
}
13901390
}

test/Serilog.Settings.Configuration.Tests/DynamicLevelChangeTests.cs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,26 +11,26 @@ public class DynamicLevelChangeTests
1111
{
1212
const string DefaultConfig = """
1313
{
14-
'Serilog': {
15-
'Using': [ 'TestDummies' ],
16-
'MinimumLevel': {
17-
'Default': 'Information',
18-
'Override': {
19-
'Root.Test': 'Information'
14+
"Serilog": {
15+
"Using": [ "TestDummies" ],
16+
"MinimumLevel": {
17+
"Default": "Information",
18+
"Override": {
19+
"Root.Test": "Information"
2020
}
2121
},
22-
'LevelSwitches': { '$mySwitch': 'Information' },
23-
'FilterSwitches': { '$myFilter': null },
24-
'Filter:Dummy': {
25-
'Name': 'ControlledBy',
26-
'Args': {
27-
'switch': '$myFilter'
22+
"LevelSwitches": { "$mySwitch": "Information" },
23+
"FilterSwitches": { "$myFilter": null },
24+
"Filter:Dummy": {
25+
"Name": "ControlledBy",
26+
"Args": {
27+
"switch": "$myFilter"
2828
}
2929
},
30-
'WriteTo:Dummy': {
31-
'Name': 'DummyConsole',
32-
'Args': {
33-
'levelSwitch': '$mySwitch'
30+
"WriteTo:Dummy": {
31+
"Name": "DummyConsole",
32+
"Args": {
33+
"levelSwitch": "$mySwitch"
3434
}
3535
}
3636
}

test/Serilog.Settings.Configuration.Tests/Support/ConfigurationReaderTestHelpers.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,16 @@ static class ConfigurationReaderTestHelpers
77
{
88
public const string minimumLevelFlatTemplate = """
99
{{
10-
'Serilog': {{
11-
'MinimumLevel': '{0}'
10+
"Serilog": {{
11+
"MinimumLevel": "{0}"
1212
}}
1313
}}
1414
""";
1515
public const string minimumLevelObjectTemplate = """
1616
{{
17-
'Serilog': {{
18-
'MinimumLevel': {{
19-
'Default': '{0}'
17+
"Serilog": {{
18+
"MinimumLevel": {{
19+
"Default": "{0}"
2020
}}
2121
}}
2222
}}

test/Serilog.Settings.Configuration.Tests/Support/Extensions.cs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,5 @@ public static object LiteralValue(this LogEventPropertyValue @this)
99
return ((ScalarValue)@this).Value;
1010
}
1111

12-
public static string ToValidJson(this string str)
13-
{
14-
str = str.Replace('\'', '"');
15-
return str;
16-
}
17-
1812
public static string Format(this string template, params object[] paramObjects) => string.Format(template, paramObjects);
1913
}

test/Serilog.Settings.Configuration.Tests/Support/JsonStringConfigSource.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public JsonStringConfigProvider(string json) : base(new JsonConfigurationSource
4242

4343
public override void Load()
4444
{
45-
Load(StringToStream(_json.ToValidJson()));
45+
Load(StringToStream(_json));
4646
}
4747

4848
static Stream StringToStream(string str)

0 commit comments

Comments
 (0)