From e1acbb39c0c3186f26b3fa8de610c92d33e6237d Mon Sep 17 00:00:00 2001 From: Andrei-Liviu Ungureanu Date: Wed, 24 Apr 2024 11:28:24 +0300 Subject: [PATCH] added unit tests --- .../TestCases.Workflows/ExpressionTests.cs | 61 +++++++++++++++++++ .../WF4Samples/Expressions.cs | 19 +++++- 2 files changed, 79 insertions(+), 1 deletion(-) diff --git a/src/Test/TestCases.Workflows/ExpressionTests.cs b/src/Test/TestCases.Workflows/ExpressionTests.cs index 21dc74f5..968613c8 100644 --- a/src/Test/TestCases.Workflows/ExpressionTests.cs +++ b/src/Test/TestCases.Workflows/ExpressionTests.cs @@ -158,6 +158,25 @@ public void Vb_CompareLambdas() validationResults.Errors[0].Message.ShouldContain("A null propagating operator cannot be converted into an expression tree."); } + [Fact] + public void Cs_CompareLambdas() + { + CSharpValue csv = new(@$"string.Concat(""alpha "", b?.Substring(0, 10), ""beta "", 1)"); + WriteLine writeLine = new(); + writeLine.Text = new InArgument(csv); + Sequence workflow = new(); + workflow.Activities.Add(writeLine); + workflow.Variables.Add(new Variable("b", "I'm a variable")); + + ValidationResults validationResults = ActivityValidationServices.Validate(workflow, _forceCache); + validationResults.Errors.Count.ShouldBe(1, string.Join("\n", validationResults.Errors.Select(e => e.Message))); + validationResults.Errors[0].Message.ShouldContain("An expression tree lambda may not contain a null propagating operator."); + + validationResults = ActivityValidationServices.Validate(workflow, _useValidator); + validationResults.Errors.Count.ShouldBe(1, string.Join("\n", validationResults.Errors.Select(e => e.Message))); + validationResults.Errors[0].Message.ShouldContain("An expression tree lambda may not contain a null propagating operator."); + } + [Fact] public void Vb_LambdaExtension() { @@ -172,6 +191,20 @@ public void Vb_LambdaExtension() validationResults.Errors.Count.ShouldBe(0, string.Join("\n", validationResults.Errors.Select(e => e.Message))); } + [Fact] + public void Cs_LambdaExtension() + { + CSharpValue csv = new("list.First()"); + WriteLine writeLine = new(); + writeLine.Text = new InArgument(csv); + Sequence workflow = new(); + workflow.Activities.Add(writeLine); + workflow.Variables.Add(new Variable>("list")); + + ValidationResults validationResults = ActivityValidationServices.Validate(workflow, _useValidator); + validationResults.Errors.Count.ShouldBe(0, string.Join("\n", validationResults.Errors.Select(e => e.Message))); + } + [Fact] public void Vb_Dictionary() { @@ -185,6 +218,20 @@ public void Vb_Dictionary() ValidationResults validationResults = ActivityValidationServices.Validate(workflow, _useValidator); validationResults.Errors.Count.ShouldBe(0, string.Join("\n", validationResults.Errors.Select(e => e.Message))); } + + [Fact] + public void Cs_Dictionary() + { + CSharpValue csv = new("something.FooDictionary[\"key\"].ToString()"); + WriteLine writeLine = new(); + writeLine.Text = new InArgument(csv); + Sequence workflow = new(); + workflow.Activities.Add(writeLine); + workflow.Variables.Add(new Variable("something")); + + ValidationResults validationResults = ActivityValidationServices.Validate(workflow, _useValidator); + validationResults.Errors.Count.ShouldBe(0, string.Join("\n", validationResults.Errors.Select(e => e.Message))); + } #region Check locations are not readonly [Fact] public void VB_Readonly_ThrowsError() @@ -291,6 +338,20 @@ public void Vb_IntOverflow() validationResults.Errors[0].Message.ShouldContain("Constant expression not representable in type 'Integer'"); } + [Fact] + public void Cs_IntOverflow() + { + VisualBasicValue csv = new("2147483648"); + Sequence workflow = new(); + workflow.Variables.Add(new Variable("someint")); + Assign assign = new() { To = new OutArgument(workflow.Variables[0]), Value = new InArgument(csv) }; + workflow.Activities.Add(assign); + + ValidationResults validationResults = ActivityValidationServices.Validate(workflow, _useValidator); + validationResults.Errors.Count.ShouldBe(1, string.Join("\n", validationResults.Errors.Select(e => e.Message))); + validationResults.Errors[0].Message.ShouldContain("Constant expression not representable in type 'Integer'"); + } + [Fact] public void VBValidator_StrictOn() { diff --git a/src/Test/TestCases.Workflows/WF4Samples/Expressions.cs b/src/Test/TestCases.Workflows/WF4Samples/Expressions.cs index 214a1091..573278a6 100644 --- a/src/Test/TestCases.Workflows/WF4Samples/Expressions.cs +++ b/src/Test/TestCases.Workflows/WF4Samples/Expressions.cs @@ -87,7 +87,7 @@ public class AheadOfTimeExpressions : ExpressionsBase static readonly string CSharpCalculationResult = "Result == XX^2" + Environment.NewLine; static readonly StringDictionary CSharpCalculationInputs = new() { ["XX"] = 16, ["YY"] = 16 }; [Fact] - public void SameTextDifferentTypes() + public void VBSameTextDifferentTypes() { var text = new VisualBasicValue("var"); var values = new VisualBasicValue>("var"); @@ -101,6 +101,23 @@ public void SameTextDifferentTypes() ((LambdaExpression)values.GetExpressionTree()).ReturnType.ShouldBe(typeof(IEnumerable)); } [Fact] + public void CSSameTextDifferentTypes() + { + var text = new CSharpValue("var"); + var values = new CSharpValue>("var"); + var root = new DynamicActivity + { + Implementation = () => new Sequence + { + Variables = { new Variable("var") }, + Activities = { new ForEach { Values = new InArgument>(values) }, new WriteLine { Text = new InArgument(text) } } + } + }; + ActivityXamlServices.Compile(root, new()); + ((LambdaExpression)text.GetExpressionTree()).ReturnType.ShouldBe(typeof(string)); + ((LambdaExpression)values.GetExpressionTree()).ReturnType.ShouldBe(typeof(IEnumerable)); + } + [Fact] public void CompileCSharpCalculation() { var activity = Compile(TestXamls.CSharpCalculation);