Skip to content

Commit

Permalink
Fixed issue #23
Browse files Browse the repository at this point in the history
  • Loading branch information
rstaib authored Mar 13, 2018
1 parent c35a40c commit b726808
Show file tree
Hide file tree
Showing 14 changed files with 331 additions and 162 deletions.
2 changes: 1 addition & 1 deletion src/Analyzer.Legacy/Analyzer.Legacy.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
<Compile Include="..\Analyzer\Rules\IRuleSet.cs" Link="Rules\IRuleSet.cs" />
<Compile Include="..\Analyzer\Rules\MustBeSealed.cs" Link="Rules\MustBeSealed.cs" />
<Compile Include="..\Analyzer\Rules\MustHaveSinglePrivateConstructor.cs" Link="Rules\MustHaveSinglePrivateConstructor.cs" />
<Compile Include="..\Analyzer\Rules\MustHaveStaticLogProperty.cs" Link="Rules\MustHaveStaticLogProperty.cs" />
<Compile Include="..\Analyzer\Rules\MustHaveStaticLogFieldOrProperty.cs" Link="Rules\MustHaveStaticLogFieldOrProperty.cs" />
<Compile Include="..\Analyzer\Rules\MustHaveValidName.cs" Link="Rules\MustHaveValidName.cs" />
<Compile Include="..\Analyzer\Rules\RequiredRuleSet.cs" Link="Rules\RequiredRuleSet.cs" />
<Compile Include="..\Analyzer\Rules\Success.cs" Link="Rules\Success.cs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System.Diagnostics.Tracing;

namespace Thor.Analyzer.Tests.EventSources
{
[EventSource(Name = "LogFieldAndPropertyDoesNotExist")]
public sealed class LogFieldAndPropertyDoNotExistEventSource
: EventSource
{
}
}
10 changes: 0 additions & 10 deletions src/Analyzer.Tests/EventSources/LogFieldDoesNotExistEventSource.cs

This file was deleted.

This file was deleted.

11 changes: 11 additions & 0 deletions src/Analyzer.Tests/EventSources/LogFieldNullEventSource.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System.Diagnostics.Tracing;

namespace Thor.Analyzer.Tests.EventSources
{
[EventSource(Name = "LogFieldNull")]
public sealed class LogFieldNullEventSource
: EventSource
{
public static readonly LogFieldNullEventSource Log;
}
}
11 changes: 11 additions & 0 deletions src/Analyzer.Tests/EventSources/LogPropertyEventSource.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System.Diagnostics.Tracing;

namespace Thor.Analyzer.Tests.EventSources
{
[EventSource(Name = "LogProperty")]
public sealed class LogPropertyEventSource
: EventSource
{
public static LogPropertyEventSource Log { get; } = new LogPropertyEventSource();
}
}
12 changes: 12 additions & 0 deletions src/Analyzer.Tests/EventSources/LogPropertyNotPublicEventSource.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System.Diagnostics.Tracing;

namespace Thor.Analyzer.Tests.EventSources
{
[EventSource(Name = "LogPropertyNotPublic")]
public sealed class LogPropertyNotPublicEventSource
: EventSource
{
internal static LogPropertyNotPublicEventSource Log { get; } =
new LogPropertyNotPublicEventSource();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System.Diagnostics.Tracing;

namespace Thor.Analyzer.Tests.EventSources
{
[EventSource(Name = "LogPropertyNotReadOnly")]
public sealed class LogPropertyNotReadOnlyEventSource
: EventSource
{
public static LogPropertyNotReadOnlyEventSource Log { get; set; } =
new LogPropertyNotReadOnlyEventSource();
}
}
12 changes: 12 additions & 0 deletions src/Analyzer.Tests/EventSources/LogPropertyNotStaticEventSource.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System.Diagnostics.Tracing;

namespace Thor.Analyzer.Tests.EventSources
{
[EventSource(Name = "LogPropertyNotStatic")]
public sealed class LogPropertyNotStaticEventSource
: EventSource
{
public LogPropertyNotStaticEventSource Log { get; } =
new LogPropertyNotStaticEventSource();
}
}
11 changes: 11 additions & 0 deletions src/Analyzer.Tests/EventSources/LogPropertyNullEventSource.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System.Diagnostics.Tracing;

namespace Thor.Analyzer.Tests.EventSources
{
[EventSource(Name = "LogPropertyNull")]
public sealed class LogPropertyNullEventSource
: EventSource
{
public static LogPropertyNullEventSource Log { get; }
}
}
224 changes: 224 additions & 0 deletions src/Analyzer.Tests/Rules/MustHaveStaticLogFieldOrPropertyTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,224 @@
using Thor.Analyzer.Rules;
using Thor.Analyzer.Tests.EventSources;
using FluentAssertions;
using Moq;
using Xunit;

namespace Thor.Analyzer.Tests.Rules
{
public class MustHaveStaticLogFieldOrPropertyTests
: EventSourceRuleTestBase<MustHaveStaticLogFieldOrProperty>
{
protected override MustHaveStaticLogFieldOrProperty CreateRule(IRuleSet ruleSet)
{
return new MustHaveStaticLogFieldOrProperty(ruleSet);
}

[Fact(DisplayName = "Apply: Should return an error if log field and property does not exist")]
public void Apply_NoLogFieldAndProperty()
{
// arrange
LogFieldAndPropertyDoNotExistEventSource eventSource = new LogFieldAndPropertyDoNotExistEventSource();
SchemaReader reader = new SchemaReader(eventSource);
EventSourceSchema schema = reader.Read();
IRuleSet ruleSet = new Mock<IRuleSet>().Object;
IEventSourceRule rule = CreateRule(ruleSet);

// act
IResult result = rule.Apply(schema, eventSource);

// assert
result.Should().NotBeNull();
result.Should().BeOfType<Error>();
}

[Fact(DisplayName = "Apply: Should return an error if log field is not public")]
public void Apply_LogFieldNotPublic()
{
// arrange
LogFieldNotPublicEventSource eventSource = new LogFieldNotPublicEventSource();
SchemaReader reader = new SchemaReader(eventSource);
EventSourceSchema schema = reader.Read();
IRuleSet ruleSet = new Mock<IRuleSet>().Object;
IEventSourceRule rule = CreateRule(ruleSet);

// act
IResult result = rule.Apply(schema, eventSource);

// assert
result.Should().NotBeNull();
result.Should().BeOfType<Error>();
}

[Fact(DisplayName = "Apply: Should return an error if log field is not readonly")]
public void Apply_LogFieldNotReadOnly()
{
// arrange
LogFieldNotReadOnlyEventSource eventSource = new LogFieldNotReadOnlyEventSource();
SchemaReader reader = new SchemaReader(eventSource);
EventSourceSchema schema = reader.Read();
IRuleSet ruleSet = new Mock<IRuleSet>().Object;
IEventSourceRule rule = CreateRule(ruleSet);

// act
IResult result = rule.Apply(schema, eventSource);

// assert
result.Should().NotBeNull();
result.Should().BeOfType<Error>();
}

/*
todo: figure out why this test leads to StackOverflowException
[Fact(DisplayName = "Apply: Should return an error if log field is not static")]
public void Apply_LogFieldNotStatic()
{
// arrange
LogFieldNotStaticEventSource eventSource = new LogFieldNotStaticEventSource();
SchemaReader reader = new SchemaReader(eventSource);
EventSourceSchema schema = reader.Read();
IRuleSet ruleSet = new Mock<IRuleSet>().Object;
IEventSourceRule rule = CreateRule(ruleSet);
// act
IResult result = rule.Apply(schema, eventSource);
// assert
result.Should().NotBeNull();
result.Should().BeOfType<Error>();
}
*/

[Fact(DisplayName = "Apply: Should return an error if log field returns no value")]
public void Apply_LogFieldNull()
{
// arrange
LogFieldNullEventSource eventSource = new LogFieldNullEventSource();
SchemaReader reader = new SchemaReader(eventSource);
EventSourceSchema schema = reader.Read();
IRuleSet ruleSet = new Mock<IRuleSet>().Object;
IEventSourceRule rule = CreateRule(ruleSet);

// act
IResult result = rule.Apply(schema, eventSource);

// assert
result.Should().NotBeNull();
result.Should().BeOfType<Error>();
}

[Fact(DisplayName = "Apply: Should return a success if the log field was found as expected")]
public void Apply_LogFieldAsExpected()
{
// arrange
LogFieldEventSource eventSource = new LogFieldEventSource();
SchemaReader reader = new SchemaReader(eventSource);
EventSourceSchema schema = reader.Read();
IRuleSet ruleSet = new Mock<IRuleSet>().Object;
IEventSourceRule rule = CreateRule(ruleSet);

// act
IResult result = rule.Apply(schema, eventSource);

// assert
result.Should().NotBeNull();
result.Should().BeOfType<Success>();
}

[Fact(DisplayName = "Apply: Should return an error if log property is not public")]
public void Apply_LogPropertyNotPublic()
{
// arrange
LogPropertyNotPublicEventSource eventSource = new LogPropertyNotPublicEventSource();
SchemaReader reader = new SchemaReader(eventSource);
EventSourceSchema schema = reader.Read();
IRuleSet ruleSet = new Mock<IRuleSet>().Object;
IEventSourceRule rule = CreateRule(ruleSet);

// act
IResult result = rule.Apply(schema, eventSource);

// assert
result.Should().NotBeNull();
result.Should().BeOfType<Error>();
}

[Fact(DisplayName = "Apply: Should return an error if log property is not readonly")]
public void Apply_LogPropertyNotReadOnly()
{
// arrange
LogPropertyNotReadOnlyEventSource eventSource = new LogPropertyNotReadOnlyEventSource();
SchemaReader reader = new SchemaReader(eventSource);
EventSourceSchema schema = reader.Read();
IRuleSet ruleSet = new Mock<IRuleSet>().Object;
IEventSourceRule rule = CreateRule(ruleSet);

// act
IResult result = rule.Apply(schema, eventSource);

// assert
result.Should().NotBeNull();
result.Should().BeOfType<Error>();
}

/*
todo: figure out why this test leads to StackOverflowException
[Fact(DisplayName = "Apply: Should return an error if log property is not static")]
public void Apply_LogPropertyNotStatic()
{
// arrange
LogPropertyNotStaticEventSource eventSource = new LogPropertyNotStaticEventSource();
SchemaReader reader = new SchemaReader(eventSource);
EventSourceSchema schema = reader.Read();
IRuleSet ruleSet = new Mock<IRuleSet>().Object;
IEventSourceRule rule = CreateRule(ruleSet);
// act
IResult result = rule.Apply(schema, eventSource);
// assert
result.Should().NotBeNull();
result.Should().BeOfType<Error>();
}
*/

[Fact(DisplayName = "Apply: Should return an error if log property returns no value")]
public void Apply_LogPropertyNull()
{
// arrange
LogPropertyNullEventSource eventSource = new LogPropertyNullEventSource();
SchemaReader reader = new SchemaReader(eventSource);
EventSourceSchema schema = reader.Read();
IRuleSet ruleSet = new Mock<IRuleSet>().Object;
IEventSourceRule rule = CreateRule(ruleSet);

// act
IResult result = rule.Apply(schema, eventSource);

// assert
result.Should().NotBeNull();
result.Should().BeOfType<Error>();
}

[Fact(DisplayName = "Apply: Should return a success if the log property was found as expected")]
public void Apply_LogPropertyAsExpected()
{
// arrange
LogPropertyEventSource eventSource = new LogPropertyEventSource();
SchemaReader reader = new SchemaReader(eventSource);
EventSourceSchema schema = reader.Read();
IRuleSet ruleSet = new Mock<IRuleSet>().Object;
IEventSourceRule rule = CreateRule(ruleSet);

// act
IResult result = rule.Apply(schema, eventSource);

// assert
result.Should().NotBeNull();
result.Should().BeOfType<Success>();
}
}
}

Loading

0 comments on commit b726808

Please sign in to comment.