Skip to content

Commit

Permalink
Cleanup tests/events. Closes #161
Browse files Browse the repository at this point in the history
  • Loading branch information
snikolayev committed Jun 4, 2018
1 parent 08d77d5 commit 0d9d3ee
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 22 deletions.
14 changes: 3 additions & 11 deletions src/NRules/NRules/Diagnostics/RhsExpressionEventArgs.cs
@@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using NRules.RuleModel;

Expand All @@ -10,8 +9,6 @@ namespace NRules.Diagnostics
/// </summary>
public class RhsExpressionEventArgs : ExpressionEventArgs
{
private readonly IMatch _match;

/// <summary>
/// Initializes a new instance of the <c>RhsExpressionEventArgs</c> class.
/// </summary>
Expand All @@ -22,17 +19,12 @@ public class RhsExpressionEventArgs : ExpressionEventArgs
public RhsExpressionEventArgs(Expression expression, Exception exception, object[] arguments, IMatch match)
: base(expression, exception, arguments, null)
{
_match = match;
Match = match;
}

/// <summary>
/// Rule related to the event.
/// </summary>
public IRuleDefinition Rule => _match.Rule;

/// <summary>
/// Facts related to the event.
/// Rule match related to the event.
/// </summary>
public IEnumerable<IFactMatch> Facts => _match.Facts;
public IMatch Match { get; }
}
}
Expand Up @@ -6,9 +6,9 @@

namespace NRules.IntegrationTests
{
public class ActionTriggerTest : BaseRuleTestFixture
public class ActionTriggerRepeatableTest : BaseRuleTestFixture
{
public ActionTriggerTest()
public ActionTriggerRepeatableTest()
{
_matchActionCount = 0;
_rematchActionCount = 0;
Expand Down
Expand Up @@ -3,6 +3,7 @@
using NRules.Diagnostics;
using NRules.Fluent;
using NRules.Fluent.Dsl;
using NRules.RuleModel;
using Xunit;

namespace NRules.IntegrationTests
Expand Down Expand Up @@ -180,7 +181,7 @@ public void Insert_Fact_RaisesFilterExpressionEvalEvent()
}

[Fact]
public void Insert_Fact_RaisesActionExpressionEvalEvent()
public void Fire_Rule_RaisesActionExpressionEvalEvent()
{
//Arrange
var factory = CreateTarget();
Expand All @@ -204,10 +205,11 @@ public void Insert_Fact_RaisesActionExpressionEvalEvent()
Assert.Equal(1, handledEvents.Count);
var eventArgs = handledEvents[0];
Assert.Collection(eventArgs.Arguments, x => Assert.Equal(fact1, x), x => Assert.Equal("1234567890A", x));
Assert.Collection(eventArgs.Facts.Select(x => x.Value), x => Assert.Equal(fact1, x), x => Assert.Equal("1234567890A", x), x => Assert.Equal(11, x));
Assert.Collection(eventArgs.Match.Facts.Select(x => x.Value), x => Assert.Equal(fact1, x), x => Assert.Equal("1234567890A", x), x => Assert.Equal(11, x));
Assert.Null(eventArgs.Result);
Assert.Null(eventArgs.Exception);
Assert.Equal("Test Rule", eventArgs.Rule.Name);
Assert.Equal("Test Rule", eventArgs.Match.Rule.Name);
Assert.Equal(MatchTrigger.Created, eventArgs.Match.Trigger);
}

private ISessionFactory CreateTarget()
Expand Down
Expand Up @@ -184,7 +184,7 @@ public void Fire_ActionErrorNoErrorHandler_Throws()
Expression expression = null;
IList<IFactMatch> facts = null;
Session.Events.RhsExpressionFailedEvent += (sender, args) => expression = args.Expression;
Session.Events.RhsExpressionFailedEvent += (sender, args) => facts = args.Facts.ToList();
Session.Events.RhsExpressionFailedEvent += (sender, args) => facts = args.Match.Facts.ToList();

var fact = new FactType { TestProperty = "Valid Value" };
Session.Insert(fact);
Expand Down
Expand Up @@ -50,9 +50,10 @@ public void Fire_OneMatchingFactInsertedThenUpdated_FiresFirstRuleAndChainsSecon
//Assert - I
AssertFiredOnce<ForwardChainingFirstRule>();
AssertFiredOnce<ForwardChainingSecondRule>();
Assert.Equal(1, matchedFact2.Count);
Assert.Equal(1, matchedFact2.UpdateCount);
Assert.Equal("Valid Value 1", matchedFact2.TestProperty);
Assert.Equal("Valid Value 1", matchedFact3.TestProperty);
Assert.Equal(1, fact1.ReferenceCount);

//Act - II
fact1.ChainProperty = "Valid Value 2";
Expand All @@ -62,9 +63,10 @@ public void Fire_OneMatchingFactInsertedThenUpdated_FiresFirstRuleAndChainsSecon
//Assert - II
AssertFiredTwice<ForwardChainingFirstRule>();
AssertFiredTwice<ForwardChainingSecondRule>();
Assert.Equal(2, matchedFact2.Count);
Assert.Equal(2, matchedFact2.UpdateCount);
Assert.Equal("Valid Value 2", matchedFact2.TestProperty);
Assert.Equal("Valid Value 2", matchedFact3.TestProperty);
Assert.Equal(1, fact1.ReferenceCount);
}

[Fact]
Expand All @@ -82,6 +84,7 @@ public void Fire_OneMatchingFactInsertedThenRetracted_FiresFirstRuleAndChainsSec
AssertFiredOnce<ForwardChainingSecondRule>();
Assert.Equal(1, Session.Query<FactType2>().Count());
Assert.Equal(1, Session.Query<FactType3>().Count());
Assert.Equal(1, fact1.ReferenceCount);

//Act - II
Session.Retract(fact1);
Expand All @@ -90,6 +93,7 @@ public void Fire_OneMatchingFactInsertedThenRetracted_FiresFirstRuleAndChainsSec
//Assert - II
Assert.Equal(0, Session.Query<FactType2>().Count());
Assert.Equal(0, Session.Query<FactType3>().Count());
Assert.Equal(0, fact1.ReferenceCount);
}

[Fact]
Expand Down Expand Up @@ -160,11 +164,12 @@ public class FactType1
{
public string TestProperty { get; set; }
public string ChainProperty { get; set; }
public int ReferenceCount { get; set; } = 0;
}

public class FactType2
{
public int Count { get; set; } = 1;
public int UpdateCount { get; set; } = 1;
public string TestProperty { get; set; }
}

Expand All @@ -182,16 +187,28 @@ public override void Define()
When()
.Match<FactType1>(() => fact1, f => f.TestProperty.StartsWith("Valid"));
Then()
.Yield(ctx => new FactType2 {TestProperty = fact1.ChainProperty}, (ctx, fact2) => Update(fact1, fact2))
.Yield(ctx => Create(fact1), (ctx, fact2) => Update(fact1, fact2), (ctx, fact2) => Remove(fact1, fact2))
.Yield(ctx => new FactType3 {TestProperty = fact1.ChainProperty});
}

private static FactType2 Create(FactType1 fact1)
{
var fact2 = new FactType2 {TestProperty = fact1.ChainProperty};
fact1.ReferenceCount++;
return fact2;
}

private static FactType2 Update(FactType1 fact1, FactType2 fact2)
{
fact2.TestProperty = fact1.ChainProperty;
fact2.Count++;
fact2.UpdateCount++;
return fact2;
}

private static void Remove(FactType1 fact1, FactType2 fact2)
{
fact1.ReferenceCount--;
}
}

public class ForwardChainingSecondRule : Rule
Expand Down

0 comments on commit 0d9d3ee

Please sign in to comment.