-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
203 additions
and
25 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
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 was deleted.
Oops, something went wrong.
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,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"]); | ||
} | ||
} | ||
} |
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
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); | ||
} | ||
} | ||
} |