From 9702ad0867e27c79390883e3ee7a67581aaa54d6 Mon Sep 17 00:00:00 2001 From: jas-valgotar Date: Wed, 17 Aug 2022 18:05:26 -0500 Subject: [PATCH] Updated "SupertypeAggregateCore" to attach Display names. --- .../Microsoft.PowerFx.Core/Types/DType.cs | 7 +++- .../DisplayNameTests.cs | 35 +++++++++++++++++++ 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/src/libraries/Microsoft.PowerFx.Core/Types/DType.cs b/src/libraries/Microsoft.PowerFx.Core/Types/DType.cs index e68f08980c..7fee5aa6cb 100644 --- a/src/libraries/Microsoft.PowerFx.Core/Types/DType.cs +++ b/src/libraries/Microsoft.PowerFx.Core/Types/DType.cs @@ -2345,7 +2345,12 @@ private static DType SupertypeAggregateCore(DType type1, DType type2, bool useLe } Contracts.Assert(!fError); - return new DType(type1.Kind, treeRes, UnionDataSourceInfoMetadata(type1, type2)); + var returnType = new DType(type1.Kind, treeRes, UnionDataSourceInfoMetadata(type1, type2), type1.DisplayNameProvider); + + returnType = type2.DisplayNameProvider == null ? + returnType : + AttachOrDisableDisplayNameProvider(returnType, type2.DisplayNameProvider); + return returnType; } // Produces the union of the two given types. diff --git a/src/tests/Microsoft.PowerFx.Core.Tests/DisplayNameTests.cs b/src/tests/Microsoft.PowerFx.Core.Tests/DisplayNameTests.cs index 93981ac0f4..135d5a8df2 100644 --- a/src/tests/Microsoft.PowerFx.Core.Tests/DisplayNameTests.cs +++ b/src/tests/Microsoft.PowerFx.Core.Tests/DisplayNameTests.cs @@ -263,5 +263,40 @@ public void ValidateExpressionConversionCommaSeparatedLocale(string inputExpress Assert.Equal(outputExpression, outInvariantExpression); } } + + [Theory] + [InlineData("r1.Display1", true)] + [InlineData("If(true, r1).Display1", true)] + [InlineData("If(true, r1, r1).Display1", true)] + [InlineData("If(true, Blank(), r1).Display1", true)] + + // If types are different, you have no access to Display name. + [InlineData("If(true, r1, r2).Display1", false)] + [InlineData("If(true, r1, r2).Display0", false)] + [InlineData("If(true, r1, {Display1 : 123}).Display1", false)] + + // If types are different, you have access to logical name, only if the name and type are same! + [InlineData("If(true, r1, r2).F1", true)] + [InlineData("If(false, r1, r2).F1", true)] + [InlineData("If(true, r1, r2).F0", false)] + public void DisplayNameTest(string input, bool succeeds) + { + var r1 = RecordType.Empty() + .Add(new NamedFormulaType("F1", FormulaType.Number, "Display1")) + .Add(new NamedFormulaType("F0", FormulaType.String, "Display0")); // F0 is Not a Common type + + var r2 = RecordType.Empty() + .Add(new NamedFormulaType("F1", FormulaType.Number, "Display1")) + .Add(new NamedFormulaType("F0", FormulaType.Number, "Display0")); + var parameters = RecordType.Empty() + .Add("r1", r1) + .Add("r2", r2); + + var engine = new Engine(new PowerFxConfig()); + + var result = engine.Check(input, parameters); + var actual = result.IsSuccess; + Assert.Equal(succeeds, actual); + } } }