codeeffects

Element <method> has the incorrect method name from GetRuleXml() after instantiating rule from XML


I manipulate the rule XML dynamically. Unfortunately, after I create the rule from the XML (RuleModel.Create) the resulting XML (RuleModel.GetRuleXml()) loses the action method name. I am able to workaround the issue by rebinding the rule to the type, but that seems redundant since the type is already a parameter to the Create method.

Here are a couple XUnit tests that illustrate the issue: (CodeEffects Version 5.0.10.2)

using System;
using CodeEffects.Rule.Attributes;
using CodeEffects.Rule.Models;
using Xunit;

namespace ReproSteps
{
    public class ReproTests
    {
        [Fact]
        public void Should_succeed_but_fails()
        {
            // Arrange
            var rule = RuleModel.Create(ExecutionRuleXml, typeof(ReproType));

            rule.IsValid();

            // Act
            var ruleXml = rule.GetRuleXml();

            // Assert
            // Expected XML should contain: <method name="MyAction">
            // Actual XML contains: <method name="326589FDEC894FF5693EC37EB78FC387">
            Assert.Contains("MyAction", ruleXml); 
        }

        [Fact]
        public void Should_succeed_with_rebind()
        {
            // Arrange
            var rule = RuleModel.Create(ExecutionRuleXml, typeof(ReproType));

            rule.BindSource(typeof(ReproType)); // this forces the correct method name in the XML

            rule.IsValid();

            // Act
            var ruleXml = rule.GetRuleXml();

            // Assert
            Assert.Contains("MyAction", ruleXml);
        }

        private const string ExecutionRuleXml = @"<?xml version=""1.0"" encoding=""utf-8""?><codeeffects xmlns=""http://codeeffects.com/schemas/rule/41"" xmlns:ui=""http://codeeffects.com/schemas/ui/4""><rule id=""__ruleid__"" webrule=""5.0.10.2"" utc=""2020-04-24T20:09:50.5568"" type=""ReproSteps.ReproType, ReproSteps.Test, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"" eval=""false""><name>__rule_name__</name><definition><if><clause><condition type=""equal""><property name=""MyProperty"" /><value type=""string"">A</value></condition></clause><then><method name=""MyAction""><value type=""System.Int32"">1</value></method></then></if></definition><format><lines/></format></rule></codeeffects>";
    }

    public class ReproType
    {
        public string MyProperty { get; set; }

        [Action]
        public void MyAction(int p)
        {
            Console.WriteLine(p);
        }
    }
}


Solution

  • This issue has been fixed in the latest minor version. Update your NuGet references.