Skip to content

Commit

Permalink
Add PrefixWriter allocation tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ltrzesniewski committed Jul 23, 2024
1 parent 6634cec commit c2e62e5
Showing 1 changed file with 40 additions and 3 deletions.
43 changes: 40 additions & 3 deletions src/ZeroLog.Tests/Formatting/PrefixWriterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,9 @@ public class PrefixWriterTests
[TestCase("abc%{column:10}def%{column:15}ghi", "abc def ghi")]
public void should_write_prefix(string pattern, string expectedResult)
{
var localTimeZone = TimeZoneInfo.FindSystemTimeZoneById("Etc/GMT+10");

var prefixWriter = new PrefixWriter(pattern)
{
LocalTimeZone = localTimeZone
LocalTimeZone = TimeZoneInfo.FindSystemTimeZoneById("Etc/GMT+10")
};

var logMessage = new LogMessage("Foo");
Expand All @@ -64,7 +62,9 @@ public void should_write_prefix(string pattern, string expectedResult)

[Test]
[TestCase("%{date:\\}")]
[TestCase("%{localDate:\\}")]
[TestCase("%{time:\\}")]
[TestCase("%{localTime:\\}")]
[TestCase("%{level:-3}")]
[TestCase("%{level:lol}")]
[TestCase("%{logger:-3}")]
Expand All @@ -83,6 +83,43 @@ public void should_throw_on_invalid_format(string pattern)
PrefixWriter.IsValidPattern(pattern).ShouldBeFalse();
}

[Test]
[TestCase("")]
[TestCase("foo")]
[TestCase("%date")]
[TestCase("%localDate")]
[TestCase("%time")]
[TestCase("%localTime")]
[TestCase("%thread")]
[TestCase("%level")]
[TestCase("%logger")]
[TestCase("%loggerCompact")]
[TestCase("%newline")]
[TestCase("abc%{column:10}def")]
[TestCase("foo %level bar %logger baz")]
[TestCase("%{date:dd MM yyyy HH mm ss}")]
[TestCase("%{localDate:dd MM yyyy HH mm ss}")]
[TestCase("%{level:pad}")]
public void should_not_allocate(string pattern)
{
var prefixWriter = new PrefixWriter(pattern);

var logMessage = new LogMessage("Foo");
logMessage.Initialize(new Log("Foo.Bar.TestLog"), LogLevel.Info);
logMessage.Timestamp = new DateTime(2020, 01, 02, 03, 04, 05, 06);

var buffer = new char[256];
var formattedLogMessage = new LoggedMessage(256, ZeroLogConfiguration.Default);
formattedLogMessage.SetMessage(logMessage);

GcTester.ShouldNotAllocate(() =>
{
prefixWriter.WritePrefix(formattedLogMessage, buffer, out _);
});

PrefixWriter.IsValidPattern(pattern).ShouldBeTrue();
}

[Test, RequiresThread]
public void should_write_thread_name()
{
Expand Down

0 comments on commit c2e62e5

Please sign in to comment.