-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18 from sandermvanvliet/feature/assertion-improve…
…ments Assertion improvements
- Loading branch information
Showing
10 changed files
with
427 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 43 additions & 3 deletions
46
src/Serilog.Sinks.InMemory.Assertions/InMemorySinkAssertions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
src/Serilog.Sinks.InMemory.Assertions/LogEventsPropertyAssertion.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using FluentAssertions; | ||
using FluentAssertions.Execution; | ||
using FluentAssertions.Primitives; | ||
using Serilog.Events; | ||
|
||
namespace Serilog.Sinks.InMemory.Assertions | ||
{ | ||
public class LogEventsPropertyAssertion : ReferenceTypeAssertions<IEnumerable<LogEvent>, LogEventsPropertyAssertion> | ||
{ | ||
private readonly LogEventsAssertions _logEventsAssertions; | ||
private readonly IEnumerable<LogEvent> _logEvents; | ||
|
||
public LogEventsPropertyAssertion(LogEventsAssertions logEventsAssertions, string propertyName) | ||
{ | ||
_logEventsAssertions = logEventsAssertions; | ||
_logEvents = logEventsAssertions.Subject; | ||
Identifier = propertyName; | ||
} | ||
|
||
protected override string Identifier { get; } | ||
|
||
public AndConstraint<LogEventsAssertions> WithValues(params object[] values) | ||
{ | ||
Execute.Assertion | ||
.ForCondition(_logEvents.Count() == values.Length) | ||
.FailWith( | ||
$"Can't assert property values because {values.Length} values were provided while only {_logEvents.Count()} messages were expected"); | ||
|
||
var propertyValues = _logEvents | ||
.Select(logEvent => GetValueFromProperty(logEvent.Properties[Identifier])) | ||
.ToArray(); | ||
|
||
var notFound = values | ||
.Where(v => !propertyValues.Contains(v)) | ||
.ToArray(); | ||
|
||
Execute.Assertion | ||
.ForCondition(!notFound.Any()) | ||
.FailWith("Expected property values {0} to contain {1} but did not find {2}", | ||
propertyValues, | ||
values, | ||
notFound); | ||
|
||
return new AndConstraint<LogEventsAssertions>(_logEventsAssertions); | ||
} | ||
|
||
private object GetValueFromProperty(LogEventPropertyValue instance) | ||
{ | ||
switch(instance) | ||
{ | ||
case ScalarValue scalarValue: | ||
return scalarValue.Value; | ||
default: | ||
return Subject.ToString(); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
test/Serilog.Sinks.InMemory.Assertions.Tests.Unit/WhenAssertingAndSInkIsWrittenTo.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
using Xunit; | ||
|
||
namespace Serilog.Sinks.InMemory.Assertions.Tests.Unit | ||
{ | ||
public class WhenAssertingAndSInkIsWrittenTo | ||
{ | ||
[Fact] | ||
public void AssertionShouldNotFailWhenInstanceIsLoggedToAfterInvokingTheAssertion() | ||
{ | ||
/* | ||
* This test is meant to verify the behaviour of the assertions | ||
* when a message is logged to the instance we're asserting on | ||
* after we invoked the assertion. | ||
* | ||
* The behaviour is that when calling Should(), the assertion | ||
* should (pun intended) capture a snapshot of the log events | ||
* at the point in time it was invoked. | ||
* | ||
* This prevents InvalidOperationExceptions because the internal | ||
* collection is modified. | ||
* | ||
* See https://github.com/sandermvanvliet/SerilogSinksInMemory/issues/16 | ||
*/ | ||
var sink = new InMemorySink(); | ||
|
||
var logger = new LoggerConfiguration() | ||
.WriteTo.Sink(sink) | ||
.CreateLogger(); | ||
|
||
// Log 2 messages | ||
logger.Information("Message"); | ||
logger.Information("Message"); | ||
|
||
// Start assertion | ||
var assertion = sink.Should(); | ||
|
||
// Pretend another thread/task/fiber logs another message | ||
logger.Information("Message"); | ||
|
||
// Continue with the assertion | ||
assertion | ||
.HaveMessage("Message") | ||
.Appearing().Times(2); | ||
} | ||
} | ||
} |
Oops, something went wrong.