public void testExploration() { NodeExpander nodeExpander = new PropertyReflectionNodeExpander(); TestData testData = new TestData(); ObjectExplorerImpl explorer = new ObjectExplorerImpl(); explorer.NodeExpander = nodeExpander; Object valueFound = null; int depth = 0; int? depthFound=null; MoveBack up = delegate(Object from, String propertyName, Object to, bool isIndexed) { depth--; }; MoveAway down = delegate(Object from, String propertyName, Object to, bool isIndexed, int ?index) { depth++; return true; }; OnLeaf leaf = delegate(Object from, String propertyName, Object to, int? index) { if (propertyName!=null && propertyName.Equals("Greeting") ) { valueFound = to; depthFound = depth + 1; } }; explorer.explore(testData, down, up, leaf); Assert.AreEqual(testData.TheSub.TheSubSub.Greeting, valueFound, "expected greeting "); Assert.AreEqual(4, depthFound, "expected depth"); }
public List <InstancePathElement> findSingleObjectPath(object root, object object2Find) { bool found = false; ObjectExplorerImpl explorer = new ObjectExplorerImpl(); Stack <InstancePathElement> currentPath = new Stack <InstancePathElement>(); MoveAway down = delegate(Object from, string propertyName, Object to, bool isIndexed, int?index) { if (found) { return(false); } InstancePathElement element; InstancePathElement thepeek = currentPath.Count > 0 ? currentPath.Peek() : null; bool parentIsIndexed = currentPath.Count > 0 && currentPath.Peek().IsIndexed; if (parentIsIndexed) { IndexPathElement ielement = new IndexPathElement(); ielement.Index = (int)index; element = ielement; } else { NamePathElement nElement = new NamePathElement(); nElement.Name = propertyName; element = nElement; } element.IsIndexed = isIndexed; currentPath.Push(element); found = object2Find == to; return(!found); }; MoveBack up = (from, propertyName, to, isIndexed) => { if (!found) { currentPath.Pop(); } }; OnLeaf onLeaf = (from, propertyName, to, index) => { }; PropertyReflectionNodeExpander expander = new PropertyReflectionNodeExpander(); expander.ExcludeReadOnlyProperties = true; explorer.NodeExpander = expander; explorer.explore(root, down, up, onLeaf); List <InstancePathElement> result = null; if (found) { result = new List <InstancePathElement>(currentPath.ToArray().Reverse()); // remove the first element because it is to the root result.RemoveAt(0); } return(result); }
public void testExploration() { NodeExpander nodeExpander = new PropertyReflectionNodeExpander(); TestData testData = new TestData(); ObjectExplorerImpl explorer = new ObjectExplorerImpl(); explorer.NodeExpander = nodeExpander; Object valueFound = null; int depth = 0; int? depthFound = null; MoveBack up = delegate(Object from, String propertyName, Object to, bool isIndexed) { depth--; }; MoveAway down = delegate(Object from, String propertyName, Object to, bool isIndexed, int?index) { depth++; return(true); }; OnLeaf leaf = delegate(Object from, String propertyName, Object to, int?index) { if (propertyName != null && propertyName.Equals("Greeting")) { valueFound = to; depthFound = depth + 1; } }; explorer.explore(testData, down, up, leaf); Assert.AreEqual(testData.TheSub.TheSubSub.Greeting, valueFound, "expected greeting "); Assert.AreEqual(4, depthFound, "expected depth"); }