From ccd7c7063bef6c486951479591895bbf34f22ce9 Mon Sep 17 00:00:00 2001 From: Kasdejong Date: Wed, 28 Aug 2024 11:50:04 +0200 Subject: [PATCH] It really was this easy? --- .../FhirPath/Expressions/SymbolTableInit.cs | 14 ++++++-------- .../PocoTests/FhirPathEvaluatorTest.cs | 17 ++++++----------- 2 files changed, 12 insertions(+), 19 deletions(-) diff --git a/src/Hl7.Fhir.Base/FhirPath/Expressions/SymbolTableInit.cs b/src/Hl7.Fhir.Base/FhirPath/Expressions/SymbolTableInit.cs index 48df4ef9d..e7544ce47 100644 --- a/src/Hl7.Fhir.Base/FhirPath/Expressions/SymbolTableInit.cs +++ b/src/Hl7.Fhir.Base/FhirPath/Expressions/SymbolTableInit.cs @@ -328,14 +328,12 @@ private static IEnumerable runIif(Closure ctx, IEnumerable 1) - { - return runSelect(ctx, [arguments.First(), (closure, _) => runIif(closure, new List{(_, _) => closure.GetThis(), arguments.Skip(1).First(), arguments.Skip(2).First(), arguments.Skip(3).FirstOrDefault()})]); - } - var expression = arguments.Skip(1).First()(ctx, InvokeeFactory.EmptyArgs); + var newContext = ctx.Nest(focus); + newContext.SetThis(focus); + + var expression = arguments.Skip(1).First()(newContext, InvokeeFactory.EmptyArgs); var trueResult = arguments.Skip(2).First(); var otherResult = arguments.Skip(3).FirstOrDefault(); @@ -343,8 +341,8 @@ private static IEnumerable runIif(Closure ctx, IEnumerable runWhere(Closure ctx, IEnumerable arguments) diff --git a/src/Hl7.FhirPath.R4.Tests/PocoTests/FhirPathEvaluatorTest.cs b/src/Hl7.FhirPath.R4.Tests/PocoTests/FhirPathEvaluatorTest.cs index a3363485c..b2b1eff45 100644 --- a/src/Hl7.FhirPath.R4.Tests/PocoTests/FhirPathEvaluatorTest.cs +++ b/src/Hl7.FhirPath.R4.Tests/PocoTests/FhirPathEvaluatorTest.cs @@ -22,6 +22,7 @@ using System.Diagnostics; using System.IO; using System.Linq; +using System.Reflection.Metadata.Ecma335; using System.Xml.Linq; namespace Hl7.FhirPath.R4.Tests @@ -164,18 +165,12 @@ public void TestSubsetting() [TestMethod] - public void TestIIf() + public void TestIIfContext() { - fixture.IsTrue(@"Patient.contained[0].name.iif(exists(), 'named', 'unnamed').join(', ') = 'named, named'"); - fixture.IsTrue(@"Patient.contained[0].name.iif(empty(), 'unnamed', 'named').join(', ') = 'named, named'"); - fixture.IsTrue(@"Patient.name.iif({}, 'named', 'unnamed').join(', ') = 'unnamed'"); - - var resultWithSelect = fixture.PatientExample.Select("Patient.name.select(iif(use = 'official', given, 'not official')).join(', ')"); - var resultWithoutSelect = fixture.PatientExample.Select("Patient.name.iif(use = 'official', given, 'not official').join(', ')"); - resultWithSelect.Should().BeEquivalentTo(resultWithoutSelect); - resultWithSelect.Should().ContainSingle().Subject.Should().BeEquivalentTo(new FhirString("Peter, James, not official")); - - // fixture.IsTrue(@"Patient.name[0].family.iif(length()-8 != 0, 5/(length()-8), 'no result') = 'no result'"); + var res = fixture.TestInput.Scalar("Patient.contained[0].name[0].given[0].iif(true, single())"); + res.Should().BeOfType().Subject.Should().Be("Eve"); + res = fixture.TestInput.Scalar("Patient.contained[0].name.iif(use = 'official', 'given', given.join(''))"); // comparison between collection and string + res.Should().BeOfType().Subject.Should().Be("EveEveline"); } [TestMethod]