Ejemplo n.º 1
0
        public void CreateContextFromDictionarySearchTest()
        {
            ContextValueDictionary cvd = new ContextValueDictionary();

            // An example of two different contexts having a PersonNameContext.
            var context1 = new EmployeeName();
            var parser1  = new Parser();

            parser1.Parse(context1);

            // Set the same values in the firstname/lastname fields of the two different contexts.
            var cv0fn = cvd.CreateValue <EmployeeName, PersonNameContext, FirstName>(parser1, "Marc");
            var cv0ln = cvd.CreateValue <EmployeeName, PersonNameContext, LastName>(parser1, "Clifton");

            var search       = new PersonNameContext();
            var searchParser = new Parser();

            searchParser.Parse(search);

            var cvFirstName = searchParser.CreateValue <PersonNameContext, FirstName>("Marc");
            var cvLastName  = searchParser.CreateValue <PersonNameContext, LastName>("Clifton");

            List <ContextNode> matches = cvd.Search(cvFirstName, cvLastName);

            Assert.IsTrue(matches.Count == 1);
            Assert.IsTrue(parser1.FieldContextPaths[0].Path.Count == 3);
            Assert.IsTrue(parser1.FieldContextPaths[1].Path.Count == 3);

            // TODO: We should be asserting the values from the match record!

            /*
             * var (newParser, newContext) = cvd.CreateContext(matches[0]);
             *
             * // Verify that the parser's field context path ID's match the dictionary ID's and
             * // the actual ContextValue matches the
             *
             * //Assert.IsTrue(cv0fn.InstancePath[0] == newParser.FieldContextPaths[0].Path[0].InstanceId);
             * //Assert.IsTrue(cv0fn.InstancePath[1] == newParser.FieldContextPaths[0].Path[1].InstanceId);
             * //Assert.IsTrue(cv0fn.InstancePath[2] == newParser.FieldContextPaths[0].Path[2].InstanceId);
             *
             * //Assert.IsTrue(cv0ln.InstancePath[0] == newParser.FieldContextPaths[1].Path[0].InstanceId);
             * //Assert.IsTrue(cv0ln.InstancePath[1] == newParser.FieldContextPaths[1].Path[1].InstanceId);
             * //Assert.IsTrue(cv0ln.InstancePath[2] == newParser.FieldContextPaths[1].Path[2].InstanceId);
             *
             * Assert.IsTrue(newParser.FieldContextPaths[0].Field.ContextValue.Value == "Marc");
             * Assert.IsTrue(newParser.FieldContextPaths[1].Field.ContextValue.Value == "Clifton");
             */
        }
Ejemplo n.º 2
0
        public void TwoContextsShareValuesTest()
        {
            ContextValueDictionary cvd = new ContextValueDictionary();

            // An example of two different contexts having a PersonNameContext.
            var context1 = new EmployeeName();
            var context2 = new AddressBookName();

            // We add a third context that should not match the search criteria.
            var context3 = new EmployeeName();

            var parser1 = new Parser();
            var parser2 = new Parser();
            var parser3 = new Parser();

            parser1.Parse(context1);
            parser2.Parse(context2);
            parser3.Parse(context3);

            Assert.IsTrue(parser1.Groups.Count == 1);
            Assert.IsTrue(parser2.Groups.Count == 1);
            Assert.IsTrue(parser3.Groups.Count == 1);

            Assert.IsTrue(parser1.Groups[0].Fields.Count == 2);
            Assert.IsTrue(parser2.Groups[0].Fields.Count == 2);
            Assert.IsTrue(parser3.Groups[0].Fields.Count == 2);

            Assert.IsTrue(parser1.Groups[0].Fields[0].ContextPath.Count == 3);
            Assert.IsTrue(parser2.Groups[0].Fields[0].ContextPath.Count == 3);
            Assert.IsTrue(parser3.Groups[0].Fields[0].ContextPath.Count == 3);

            // Set the same values in the firstname/lastname fields of the two different contexts.
            var cv0fn = cvd.CreateValue <EmployeeName, PersonNameContext, FirstName>(parser1, "Marc");
            var cv0ln = cvd.CreateValue <EmployeeName, PersonNameContext, LastName>(parser1, "Clifton");

            var cv1fn = cvd.CreateValue <AddressBookName, PersonNameContext, FirstName>(parser2, "Marc");
            var cv1ln = cvd.CreateValue <AddressBookName, PersonNameContext, LastName>(parser2, "Clifton");

            cvd.CreateValue <EmployeeName, PersonNameContext, FirstName>(parser3, "Ian");
            cvd.CreateValue <EmployeeName, PersonNameContext, LastName>(parser3, "Clifton");

            // TODO: Tie in a context instance with a parser for that instance so we're working with a unified object.

            /*
             * // Alternate way of doing this directly from the parser groups.
             *
             * var c1f1 = parser1.Groups[0].Fields[0];
             * var c1f2 = parser1.Groups[0].Fields[1];
             * var c2f1 = parser2.Groups[0].Fields[0];
             * var c2f2 = parser2.Groups[0].Fields[1];
             * var c3f1 = parser3.Groups[0].Fields[0];
             * var c3f2 = parser3.Groups[0].Fields[1];
             *
             * cvd.Add(c1f1.CreateValue("Marc"));
             * cvd.Add(c1f2.CreateValue("Clifton"));
             *
             * cvd.Add(c2f1.CreateValue("Marc"));
             * cvd.Add(c2f2.CreateValue("Clifton"));
             *
             * // Create a first/last name context that should not match.
             * cvd.Add(c3f1.CreateValue("Ian"));
             * cvd.Add(c3f2.CreateValue("Clifton"));
             */

            // When we ask what contexts these values exist, we should get back two context paths
            // as the search criteria (below) exists in two different context.

            var search       = new PersonNameContext();
            var searchParser = new Parser();

            searchParser.Parse(search);

            //var searchFirstNameField = searchParser.Groups[0].Fields[0];
            //var searchLastNameField = searchParser.Groups[0].Fields[1];

            //var cvFirstName = searchFirstNameField.CreateValue("Marc");
            //var cvLastName = searchLastNameField.CreateValue("Clifton");

            // Use the parser to create the search context -- if we use the context value dictionary, the
            // context values get stored in the dictionary, which we don't want!

            var cvFirstName = searchParser.CreateValue <PersonNameContext, FirstName>("Marc");
            var cvLastName  = searchParser.CreateValue <PersonNameContext, LastName>("Clifton");

            List <ContextNode> matches = cvd.Search(cvFirstName, cvLastName);

            Assert.IsTrue(matches.Count == 2);
            Assert.IsTrue(matches.Any(m => m.Parent.Type == typeof(EmployeeName)));
            Assert.IsTrue(matches.Any(m => m.Parent.Type == typeof(AddressBookName)));
            Assert.IsTrue(matches[0].Children[0].InstanceId == cv0fn.InstanceId);
            Assert.IsTrue(matches[0].Children[1].InstanceId == cv0ln.InstanceId);
            Assert.IsTrue(matches[1].Children[0].InstanceId == cv1fn.InstanceId);
            Assert.IsTrue(matches[1].Children[1].InstanceId == cv1ln.InstanceId);
            Assert.IsTrue(matches[0].Children.Any(c => c.ContextValue.Value == "Marc"));
            Assert.IsTrue(matches[0].Children.Any(c => c.ContextValue.Value == "Clifton"));
            Assert.IsTrue(matches[1].Children.Any(c => c.ContextValue.Value == "Marc"));
            Assert.IsTrue(matches[1].Children.Any(c => c.ContextValue.Value == "Clifton"));
        }