Skip to content

Commit aca1ad7

Browse files
committed
Add new tests
Updates the package version to 3.2.0. Adds integration tests for empty collections, dictionaries with null and non-string keys, and scalar/non-formattable objects.
1 parent db442d2 commit aca1ad7

File tree

5 files changed

+74
-10
lines changed

5 files changed

+74
-10
lines changed

Serilog.Sinks.RichTextBox.WinForms.Colored.Test/Integration/JsonFormattingTests.cs

Lines changed: 71 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -190,8 +190,9 @@ public void PrettyPrintJson_FormatsNestedStructures()
190190
public void PrettyPrintJson_EmptyCollectionsFormatCorrectly()
191191
{
192192
var emptyArray = new LogEventProperty("EmptyArray", new SequenceValue(Array.Empty<LogEventPropertyValue>()));
193+
var emptyDict = new LogEventProperty("EmptyDict", new DictionaryValue(new Dictionary<ScalarValue, LogEventPropertyValue>()));
193194
var emptyObject = new LogEventProperty("EmptyObject", new StructureValue(Array.Empty<LogEventProperty>(), null));
194-
var logEvent = new LogEvent(DateTimeOffset.Now, LogEventLevel.Information, null, _parser.Parse("Array: {EmptyArray:j}, Object: {EmptyObject:j}"), new[] { emptyArray, emptyObject });
195+
var logEvent = new LogEvent(DateTimeOffset.Now, LogEventLevel.Information, null, _parser.Parse("Array: {EmptyArray:j}, Dict: {EmptyDict:j}, Object: {EmptyObject:j}"), new[] { emptyArray, emptyDict, emptyObject });
195196

196197
var options = new RichTextBoxSinkOptions(
197198
theme: _defaultTheme,
@@ -201,6 +202,7 @@ public void PrettyPrintJson_EmptyCollectionsFormatCorrectly()
201202

202203
var result = RenderAndGetText(logEvent, "{Message:l}", options);
203204
Assert.Contains("Array: []", result);
205+
Assert.Contains("Dict: {}", result);
204206
Assert.Contains("Object: {}", result);
205207
}
206208

@@ -249,9 +251,75 @@ public void CompactJson_StillWorksByDefault()
249251
{
250252
var complexProp = new LogEventProperty("ComplexProp", new StructureValue(new[] { new LogEventProperty("Id", new ScalarValue(123)) }, "MyObj"));
251253
var logEvent = new LogEvent(DateTimeOffset.Now, LogEventLevel.Information, null, _parser.Parse("Value: {ComplexProp}"), new[] { complexProp });
252-
253-
// Default behavior (compact)
254254
Assert.Equal("Value: {\"Id\": 123, \"$type\": \"MyObj\"}", RenderAndGetText(logEvent, "{Message:j}"));
255255
}
256+
257+
[Fact]
258+
public void ScalarValue_IFormattableButNotNumericValueType()
259+
{
260+
var enumValue = System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance;
261+
var prop = new LogEventProperty("EnumProp", new ScalarValue(enumValue));
262+
var logEvent = new LogEvent(DateTimeOffset.Now, LogEventLevel.Information, null, _parser.Parse("{EnumProp:j}"), new[] { prop });
263+
264+
var result = RenderAndGetText(logEvent, "{Message:j}");
265+
Assert.NotNull(result);
266+
Assert.NotEmpty(result);
267+
}
268+
269+
[Fact]
270+
public void ScalarValue_NonFormattableObject()
271+
{
272+
var plainObject = new object();
273+
var prop = new LogEventProperty("ObjectProp", new ScalarValue(plainObject));
274+
var logEvent = new LogEvent(DateTimeOffset.Now, LogEventLevel.Information, null, _parser.Parse("{ObjectProp:j}"), new[] { prop });
275+
276+
var result = RenderAndGetText(logEvent, "{Message:j}");
277+
Assert.NotNull(result);
278+
Assert.NotEmpty(result);
279+
Assert.StartsWith("\"", result);
280+
Assert.EndsWith("\"", result);
281+
}
282+
283+
[Fact]
284+
public void PrettyPrintJson_DictionaryWithNullKey()
285+
{
286+
var dict = new Dictionary<ScalarValue, LogEventPropertyValue>
287+
{
288+
{ new ScalarValue(null), new ScalarValue("value") }
289+
};
290+
var dictProp = new LogEventProperty("DictProp", new DictionaryValue(dict));
291+
var logEvent = new LogEvent(DateTimeOffset.Now, LogEventLevel.Information, null, _parser.Parse("{DictProp:j}"), new[] { dictProp });
292+
293+
var options = new RichTextBoxSinkOptions(
294+
theme: _defaultTheme,
295+
prettyPrintJson: true,
296+
indentSize: 4,
297+
useSpacesForIndent: true);
298+
299+
var result = RenderAndGetText(logEvent, "{Message:l}", options);
300+
Assert.Contains("\"null\"", result);
301+
Assert.Contains("\"value\"", result);
302+
}
303+
304+
[Fact]
305+
public void PrettyPrintJson_DictionaryWithNonStringKey()
306+
{
307+
var dict = new Dictionary<ScalarValue, LogEventPropertyValue>
308+
{
309+
{ new ScalarValue(123), new ScalarValue("value") }
310+
};
311+
var dictProp = new LogEventProperty("DictProp", new DictionaryValue(dict));
312+
var logEvent = new LogEvent(DateTimeOffset.Now, LogEventLevel.Information, null, _parser.Parse("{DictProp:j}"), new[] { dictProp });
313+
314+
var options = new RichTextBoxSinkOptions(
315+
theme: _defaultTheme,
316+
prettyPrintJson: true,
317+
indentSize: 4,
318+
useSpacesForIndent: true);
319+
320+
var result = RenderAndGetText(logEvent, "{Message:l}", options);
321+
Assert.Contains("\"123\"", result);
322+
Assert.Contains("\"value\"", result);
323+
}
256324
}
257325
}

Serilog.Sinks.RichTextBox.WinForms.Colored/Serilog.Sinks.RichTextBox.WinForms.Colored.csproj

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,13 @@
2222
<TargetFrameworks>net462;net471;net6.0-windows;net8.0-windows;net9.0-windows;netcoreapp3.0-windows;netcoreapp3.1-windows</TargetFrameworks>
2323
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
2424
<UseWindowsForms>true</UseWindowsForms>
25-
<Version>3.1.3</Version>
25+
<Version>3.2.0</Version>
2626
<PackageReleaseNotes>
27-
- Fixed performance issues when logging large numbers of complex messages that could cause application freezing.
28-
- Fixed `ArgumentOutOfRangeException` thrown from `RtfBuilder.Clear()`
27+
- Added support for JSON pretty-printing in log messages.
2928

3029
See repository for more information:
3130
https://github.com/vonhoff/Serilog.Sinks.RichTextBox.WinForms.Colored
32-
</PackageReleaseNotes>
31+
</PackageReleaseNotes>
3332
<SupportedOSPlatformVersion>7.0</SupportedOSPlatformVersion>
3433
<SupportedOSPlatform>windows</SupportedOSPlatform>
3534
<NeutralLanguage>en-US</NeutralLanguage>

Serilog.Sinks.RichTextBox.WinForms.Colored/Sinks/RichTextBoxForms/Rendering/MessageTemplateTokenRenderer.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
using Serilog.Events;
2020
using Serilog.Parsing;
21-
using Serilog.Sinks.RichTextBoxForms;
2221
using Serilog.Sinks.RichTextBoxForms.Formatting;
2322
using Serilog.Sinks.RichTextBoxForms.Rtf;
2423
using Serilog.Sinks.RichTextBoxForms.Themes;

Serilog.Sinks.RichTextBox.WinForms.Colored/Sinks/RichTextBoxForms/Rendering/PropertiesTokenRenderer.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
using Serilog.Events;
2020
using Serilog.Parsing;
21-
using Serilog.Sinks.RichTextBoxForms;
2221
using Serilog.Sinks.RichTextBoxForms.Formatting;
2322
using Serilog.Sinks.RichTextBoxForms.Rtf;
2423
using Serilog.Sinks.RichTextBoxForms.Themes;

Serilog.Sinks.RichTextBox.WinForms.Colored/Sinks/RichTextBoxForms/Rendering/TemplateRenderer.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
using Serilog.Events;
2020
using Serilog.Formatting.Display;
2121
using Serilog.Parsing;
22-
using Serilog.Sinks.RichTextBoxForms;
2322
using Serilog.Sinks.RichTextBoxForms.Rtf;
2423
using Serilog.Sinks.RichTextBoxForms.Themes;
2524
using System;

0 commit comments

Comments
 (0)