public void TestPocoPath()
        {
            Patient p = new Patient();

            p.Active = true;
            p.ActiveElement.ElementId = "314";
            p.ActiveElement.AddExtension("http://something.org", new FhirBoolean(false));
            p.ActiveElement.AddExtension("http://something.org", new Integer(314));
            p.Telecom = new List <ContactPoint>();
            p.Telecom.Add(new ContactPoint(ContactPoint.ContactPointSystem.Phone, null, "555-phone"));
            p.Telecom[0].Rank = 1;

            var patient = new PocoNavigator(p);

            Assert.AreEqual("Patient", patient.Location);

            patient.MoveToFirstChild();
            Assert.AreEqual("Patient.active[0]", patient.Location);
            Assert.AreEqual("Patient.active", patient.ShortPath);

            patient.MoveToFirstChild();
            Assert.AreEqual("Patient.active[0].id[0]", patient.Location);

            Assert.IsTrue(patient.MoveToNext());
            Assert.AreEqual("Patient.active[0].extension[0]", patient.Location);

            PocoNavigator v1 = patient.Clone() as PocoNavigator;

            v1.MoveToFirstChild();
            v1.MoveToNext();
            Assert.AreEqual("Patient.active[0].extension[0].value[0]", v1.Location);
            Assert.AreEqual("Patient.active.extension[0].value", v1.ShortPath);

            PocoNavigator v2 = patient.Clone() as PocoNavigator; v2.MoveToNext(); v2.MoveToFirstChild(); v2.MoveToNext();

            Assert.AreEqual("Patient.active[0].extension[1].value[0]", v2.Location);
            Assert.AreEqual("Patient.active.extension[1].value", v2.ShortPath);
            Assert.AreEqual("Patient.active.extension('http://something.org').value", v1.CommonPath);

            PocoNavigator v3 = new PocoNavigator(p);

            v3.MoveToFirstChild();
            v3.MoveToNext();
            v3.MoveToNext();
            v3.MoveToNext();
            v3.MoveToFirstChild();
            Assert.AreEqual("Patient.telecom[0].system[0]", v3.Location);
            Assert.AreEqual("Patient.telecom[0].system", v3.ShortPath);
            Assert.AreEqual("Patient.telecom.where(system='phone').system", v3.CommonPath);
        }
Пример #2
0
        public void TestPocoPath()
        {
            // Ensure the FHIR extensions are registered
            ElementNavFhirExtensions.PrepareFhirSymbolTableFunctions();
            FhirPathCompiler.DefaultSymbolTable.Add("shortpathname",
                                                    (object f) =>
            {
                if (f is IEnumerable <IElementNavigator> )
                {
                    object[] bits = (f as IEnumerable <IElementNavigator>).Select(i =>
                    {
                        if (i is PocoNavigator)
                        {
                            return((i as PocoNavigator).ShortPath);
                        }
                        return("?");
                    }).ToArray();
                    return(FhirValueList.Create(bits));
                }
                return(FhirValueList.Create(new object[] { "?" }));
            });

            Patient p = new Patient();

            p.Active = true;
            p.ActiveElement.ElementId = "314";
            p.ActiveElement.AddExtension("http://something.org", new FhirBoolean(false));
            p.ActiveElement.AddExtension("http://something.org", new Integer(314));
            p.Telecom = new List <ContactPoint>();
            p.Telecom.Add(new ContactPoint(ContactPoint.ContactPointSystem.Phone, null, "555-phone"));
            p.Telecom[0].Rank = 1;

            foreach (var item in p.Select("descendants().shortpathname()"))
            {
                System.Diagnostics.Trace.WriteLine(item.ToString());
            }
            var patient = new PocoNavigator(p);

            Assert.AreEqual("Patient", patient.Location);

            patient.MoveToFirstChild();
            Assert.AreEqual("Patient.active[0]", patient.Location);
            Assert.AreEqual("Patient.active", patient.ShortPath);

            patient.MoveToFirstChild();
            Assert.AreEqual("Patient.active[0].id[0]", patient.Location);
            Assert.AreEqual("Patient.active.id", patient.ShortPath);

            Assert.IsTrue(patient.MoveToNext());
            Assert.AreEqual("Patient.active[0].extension[0]", patient.Location);
            Assert.AreEqual("Patient.active.extension[0]", patient.ShortPath);

            PocoNavigator v1 = patient.Clone() as PocoNavigator;

            v1.MoveToFirstChild();
            v1.MoveToNext();
            Assert.AreEqual("Patient.active[0].extension[0].value[0]", v1.Location);
            Assert.AreEqual("Patient.active.extension[0].value", v1.ShortPath);
            Assert.IsFalse(v1.MoveToNext());

            // Ensure that the original navigator hasn't changed
            Assert.AreEqual("Patient.active[0].extension[0]", patient.Location);
            Assert.AreEqual("Patient.active.extension[0]", patient.ShortPath);

            PocoNavigator v2 = patient.Clone() as PocoNavigator;

            v2.MoveToNext();
            v2.MoveToFirstChild();
            v2.MoveToNext();
            Assert.AreEqual("Patient.active[0].extension[1].value[0]", v2.Location);
            Assert.AreEqual("Patient.active.extension[1].value", v2.ShortPath);
            Assert.AreEqual("Patient.active.extension('http://something.org').value", v2.CommonPath);

            PocoNavigator v3 = new PocoNavigator(p);

            v3.MoveToFirstChild(); System.Diagnostics.Trace.WriteLine($"{v3.ShortPath} = {v3.FhirValue.ToString()}");
            v3.MoveToNext(); System.Diagnostics.Trace.WriteLine($"{v3.ShortPath} = {v3.FhirValue.ToString()}");
            // v3.MoveToNext(); System.Diagnostics.Trace.WriteLine($"{v3.ShortPath} = {v3.FhirValue.ToString()}");
            // v3.MoveToNext(); System.Diagnostics.Trace.WriteLine($"{v3.ShortPath} = {v3.FhirValue.ToString()}");
            v3.MoveToFirstChild("system"); System.Diagnostics.Trace.WriteLine($"{v3.ShortPath} = {v3.FhirValue.ToString()}");
            Assert.AreEqual("Patient.telecom[0].system[0]", v3.Location);
            Assert.AreEqual("Patient.telecom[0].system", v3.ShortPath);
            Assert.AreEqual("Patient.telecom.where(system='phone').system", v3.CommonPath);

            // Now check navigation bits
            var v4 = new PocoNavigator(p);

            Assert.AreEqual("Patient.telecom.where(system='phone').system",
                            (v4.Select("Patient.telecom.where(system='phone').system").First() as PocoNavigator).CommonPath);
            v4 = new PocoNavigator(p);
            Assert.AreEqual("Patient.telecom[0].system",
                            (v4.Select("Patient.telecom.where(system='phone').system").First() as PocoNavigator).ShortPath);
            v4 = new PocoNavigator(p);
            Assert.AreEqual("Patient.telecom[0].system[0]",
                            (v4.Select("Patient.telecom.where(system='phone').system").First() as PocoNavigator).Location);
            v4 = new PocoNavigator(p);
            Assert.AreEqual("Patient.telecom.where(system='phone').system",
                            (v4.Select("Patient.telecom[0].system").First() as PocoNavigator).CommonPath);
            v4 = new PocoNavigator(p);
            Assert.AreEqual("Patient.telecom[0].system",
                            (v4.Select("Patient.telecom[0].system").First() as PocoNavigator).ShortPath);
        }