Skip to content

Commit

Permalink
no message
Browse files Browse the repository at this point in the history
  • Loading branch information
ZooY committed Mar 16, 2021
1 parent ff639c4 commit 23ca082
Show file tree
Hide file tree
Showing 7 changed files with 203 additions and 25 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,10 @@ Workflow
<td>Получение произведения двух чисел.</td>
</tr>
<tr>
<td>:new: Round</td>
<td>Округление числа по математическим правилам, а также в большую и меньшу стороны.</td>
</tr>
<tr>
<td>:new: Sum</td>
<td>Получение суммы двух чисел.</td>
</tr>
Expand Down
2 changes: 1 addition & 1 deletion Source/Tools/Math/Math Tests/Math Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@
<Reference Include="System.Xml.Linq" />
</ItemGroup>
<ItemGroup>
<Compile Include="Workflow\DiffTests.cs" />
<Compile Include="Workflow\RoundTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
Expand Down
22 changes: 0 additions & 22 deletions Source/Tools/Math/Math Tests/Workflow/DiffTests.cs

This file was deleted.

134 changes: 134 additions & 0 deletions Source/Tools/Math/Math Tests/Workflow/RoundTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Activities;
using System.Collections.Generic;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Workflow;
using PZone.MathTools.Workflow;
using PZone.Xrm.Testing;
using PZone.Xrm.Testing.Workflow;


namespace PZone.Tests.MathTools.Workflow
{
[TestClass]
public class RoundTests
{
[TestMethod]
public void Success()
{
var action = new Round();
var invoker = new WorkflowInvoker(action);
invoker.Extensions.Add<ITracingService>(() => new FakeTracingService());
invoker.Extensions.Add<IWorkflowContext>(() => new FakeWorkflowContext());
invoker.Extensions.Add<IOrganizationServiceFactory>(() => new FakeOrganizationServiceFactory(new FakeOrganizationService()));

var result = invoker.Invoke(new Dictionary<string, object>
{
{ "Value", 2.22m },
{ "Digits", 1 }
});
Assert.AreEqual(2.2m, result["RoundValue"]);
Assert.AreEqual(2.3m, result["CeilingValue"]);
Assert.AreEqual(2.2m, result["FloorValue"]);

result = invoker.Invoke(new Dictionary<string, object>
{
{ "Value", 2.22m },
{ "Digits", 0 }
});
Assert.AreEqual(2m, result["RoundValue"]);
Assert.AreEqual(3m, result["CeilingValue"]);
Assert.AreEqual(2m, result["FloorValue"]);

result = invoker.Invoke(new Dictionary<string, object>
{
{ "Value", 2.22m },
{ "Digits", 2 }
});
Assert.AreEqual(2.22m, result["RoundValue"]);
Assert.AreEqual(2.22m, result["CeilingValue"]);
Assert.AreEqual(2.22m, result["FloorValue"]);

result = invoker.Invoke(new Dictionary<string, object>
{
{ "Value", 2.22m },
{ "Digits", 3 }
});
Assert.AreEqual(2.22m, result["RoundValue"]);
Assert.AreEqual(2.22m, result["CeilingValue"]);
Assert.AreEqual(2.22m, result["FloorValue"]);

result = invoker.Invoke(new Dictionary<string, object>
{
{ "Value", 2.26m },
{ "Digits", 1 }
});
Assert.AreEqual(2.3m, result["RoundValue"]);
Assert.AreEqual(2.3m, result["CeilingValue"]);
Assert.AreEqual(2.2m, result["FloorValue"]);

result = invoker.Invoke(new Dictionary<string, object>
{
{ "Value", 2.25m },
{ "Digits", 1 }
});
Assert.AreEqual(2.3m, result["RoundValue"]);
Assert.AreEqual(2.3m, result["CeilingValue"]);
Assert.AreEqual(2.2m, result["FloorValue"]);

result = invoker.Invoke(new Dictionary<string, object>
{
{ "Value", 2.2m },
{ "Digits", 0 }
});
Assert.AreEqual(2m, result["RoundValue"]);
Assert.AreEqual(3m, result["CeilingValue"]);
Assert.AreEqual(2m, result["FloorValue"]);

result = invoker.Invoke(new Dictionary<string, object>
{
{ "Value", 2.5m },
{ "Digits", 0 }
});
Assert.AreEqual(3m, result["RoundValue"]);
Assert.AreEqual(3m, result["CeilingValue"]);
Assert.AreEqual(2m, result["FloorValue"]);

result = invoker.Invoke(new Dictionary<string, object>
{
{ "Value", -2.22m },
{ "Digits", 1 }
});
Assert.AreEqual(-2.2m, result["RoundValue"]);
Assert.AreEqual(-2.2m, result["CeilingValue"]);
Assert.AreEqual(-2.3m, result["FloorValue"]);

result = invoker.Invoke(new Dictionary<string, object>
{
{ "Value", -1.22m },
{ "Digits", 1 }
});
Assert.AreEqual(-1.2m, result["RoundValue"]);
Assert.AreEqual(-1.2m, result["CeilingValue"]);
Assert.AreEqual(-1.3m, result["FloorValue"]);

result = invoker.Invoke(new Dictionary<string, object>
{
{ "Value", -1.25m },
{ "Digits", 1 }
});
Assert.AreEqual(-1.3m, result["RoundValue"]);
Assert.AreEqual(-1.2m, result["CeilingValue"]);
Assert.AreEqual(-1.3m, result["FloorValue"]);

result = invoker.Invoke(new Dictionary<string, object>
{
{ "Value", -1.15m },
{ "Digits", 1 }
});
Assert.AreEqual(-1.2m, result["RoundValue"]);
Assert.AreEqual(-1.1m, result["CeilingValue"]);
Assert.AreEqual(-1.2m, result["FloorValue"]);
}
}
}
1 change: 1 addition & 0 deletions Source/Tools/Math/Math/Math.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
<Compile Include="Workflow\Diff.cs" />
<Compile Include="Workflow\Division.cs" />
<Compile Include="Workflow\Multiplication.cs" />
<Compile Include="Workflow\Round.cs" />
<Compile Include="Workflow\Sum.cs" />
</ItemGroup>
<ItemGroup>
Expand Down
4 changes: 2 additions & 2 deletions Source/Tools/Math/Math/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("8.0.3.6")]
[assembly: AssemblyFileVersion("8.0.3.6")]
[assembly: AssemblyVersion("8.0.3.7")]
[assembly: AssemblyFileVersion("8.0.3.7")]
61 changes: 61 additions & 0 deletions Source/Tools/Math/Math/Workflow/Round.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
using System;
using System.Activities;
using Microsoft.Xrm.Sdk.Workflow;
using PZone.Xrm.Workflow;


namespace PZone.MathTools.Workflow
{
/// <summary>
/// Округление числа по математическим правилам, а также в большую и меньшу стороны.
/// </summary>
public class Round : WorkflowBase
{
[Input("Value")]
[RequiredArgument]
public InArgument<decimal> Value { get; set; }


[Input("Digits")]
[RequiredArgument]
public InArgument<int> Digits { get; set; }


[Output("Round")]
public OutArgument<decimal> RoundValue { get; set; }


[Output("Ceiling")]
public OutArgument<decimal> CeilingValue { get; set; }


[Output("Floor")]
public OutArgument<decimal> FloorValue { get; set; }


protected override void Execute(Context context)
{
var value = Value.Get(context);
var digits = Digits.Get(context);

if (digits < 0)
throw new InvalidWorkflowException("Digits value cannot be negative.");

var m = digits == 0 ? 1 : (decimal)Math.Pow(10, digits);
var tmp = value * m;
var multiplier = value > 0 ? 1 : -1;

if (tmp % 1 == 0)
{
RoundValue.Set(context, value);
CeilingValue.Set(context, value);
FloorValue.Set(context, value);
return;
}

RoundValue.Set(context, Math.Abs(Math.Round(value, digits, MidpointRounding.AwayFromZero)) * multiplier);
CeilingValue.Set(context, Math.Ceiling(tmp) / m);
FloorValue.Set(context, Math.Floor(tmp) / m);
}
}
}

0 comments on commit 23ca082

Please sign in to comment.