public void TraversePairwise(TraverseFunc func) { var funcContinue = true; var dirs = new Stack <ParentNode>(10); dirs.Push(this); while (funcContinue && dirs.Count > 0) { var pn = dirs.Pop(); if (pn.Children != null) { foreach (var n in pn.Children) { if (func != null) { funcContinue = func(pn, n); if (!funcContinue) { break; } } if (n.Children.Count > 0) { dirs.Push(n); } } } } }
public static void TraverseTreePair(IEnumerable <CommonEntry> rootEntries, TraverseFunc func) { if (func == null) { return; } // nothing to do. var funcContinue = true; var dirs = new Stack <CommonEntry>(rootEntries.Reverse()); // Reverse to keep same traversal order as prior code. while (funcContinue && dirs.Count > 0) { var commonEntry = dirs.Pop(); if (commonEntry.Children == null) { continue; } // empty directories may not have Children initialized. foreach (var dirEntry in commonEntry.Children) { funcContinue = func(commonEntry, dirEntry); if (!funcContinue) { break; } if (dirEntry.IsDirectory) { dirs.Push(dirEntry); } } } }
public TraverseFunc GetFindFunc(int[] progressCount, int[] limitCount) { var findPredicate = GetFindPredicate(); TraverseFunc findFunc = (p, d) => { ++progressCount[0]; if (progressCount[0] <= SkipCount) { // skip enforced return(true); } if (progressCount[0] % ProgressModifier == 0) { ProgressFunc(progressCount[0], ProgressEnd); // only check for cancel on progress modifier. if (Worker != null && Worker.CancellationPending) { return(false); // end the find. } } if (findPredicate(p, d)) { if (!VisitorFunc(p, d) || --limitCount[0] <= 0) { return(false); // end the find. } } return(true); }; return(findFunc); }
public void TraversePairwise(TraverseFunc func) { var funcContinue = true; var dirs = new Stack<ParentNode>(10); dirs.Push(this); while (funcContinue && dirs.Count > 0) { var pn = dirs.Pop(); if (pn.Children != null) { foreach (var n in pn.Children) { if (func != null) { funcContinue = func(pn, n); if (!funcContinue) { break; } } if (n.Children.Count > 0) { dirs.Push(n); } } } } }
String OutputGraph(TraverseFunc traverseFunc, SymbolContext <SymbolT, CtxKey> ctx, int depth, int maxh, bool symbs) { string text = (symbs ? ctx.ListContext() : ctx.ToString()); text = String.Concat(Enumerable.Repeat(" ", depth)) + text + Environment.NewLine; if (maxh > 0) { foreach (var p in traverseFunc(ctx)) { text += OutputGraph(traverseFunc, p, depth + 1, maxh - 1, symbs); } } return(text); }
public void Given_FindOptions_config_match_all() { _findOptions = new FindOptions(); _visitorFunc = Substitute.For <TraverseFunc>(); var matcherAll = Substitute.For <Func <CommonEntry, DirEntry, bool> >(); matcherAll(null, null).ReturnsForAnyArgs(true); _findOptions.VisitorFunc = _visitorFunc; _findOptions.PatternMatcher = matcherAll; _rootEntry = new RootEntry { FullPath = @"C:\" }; _testFile = new DirEntry(false) { Path = @"TestFile1" }; _testDir = new DirEntry(true) { Path = @"TestDir" }; }
public void given_FindOptions_with_matcherAll() { FindOptions findOptions = null; TraverseFunc visitorFunc = null; TraverseFunc findFunc = null; RootEntry rootEntry = null; DirEntry testFile = null; DirEntry testDir = null; before = () => { findOptions = new FindOptions(); visitorFunc = Substitute.For <TraverseFunc>(); var matcherAll = Substitute.For <Func <CommonEntry, DirEntry, bool> >(); matcherAll(null, null).ReturnsForAnyArgs(true); findOptions.VisitorFunc = visitorFunc; findOptions.PatternMatcher = matcherAll; rootEntry = new RootEntry { FullPath = @"C:\" }; testFile = new DirEntry(false) { Path = @"TestFile1" }; testDir = new DirEntry(true) { Path = @"TestDir" }; }; describe["with test entry \"TestFile1\""] = () => { describe["when find items before limit"] = () => { before = () => { findFunc = findOptions.GetFindFunc(new[] { 0 }, new[] { int.MaxValue }); }; it["find calls found"] = () => { findFunc(rootEntry, testFile); //findOptions.VisitorFunc.Received().Invoke(rootEntry, testFile); findOptions.VisitorFunc.Received()(rootEntry, testFile); }; it["find returns true if found returns true"] = () => { visitorFunc(null, null).ReturnsForAnyArgs(true); findFunc(rootEntry, testFile).should_be_true(); }; it["find returns false if found returns false"] = () => { // findprecicate is seperate from foundVisitor :(........ // this is borked. visitorFunc(null, null).ReturnsForAnyArgs(false); findFunc(rootEntry, testFile).should_be_false(); }; }; describe["when find items at limit"] = () => { before = () => { findFunc = findOptions.GetFindFunc(new[] { 0 }, new[] { 1 }); }; it["find calls found for first matched entry"] = () => { findFunc(rootEntry, testFile); findOptions.VisitorFunc.Received().Invoke(rootEntry, testFile); }; it["find returns false if found returns true"] = () => { visitorFunc(null, null).ReturnsForAnyArgs(true); findFunc(rootEntry, testFile).should_be_false(); }; }; describe["given find excludes Files"] = () => { before = () => { findOptions.IncludeFiles = false; findFunc = findOptions.GetFindFunc(new[] { 0 }, new[] { int.MaxValue }); }; it["find doesn't call found"] = () => { findFunc(rootEntry, testFile); findOptions.VisitorFunc.DidNotReceiveWithAnyArgs().Invoke(null, null); }; }; describe["given find excludes Folders"] = () => { before = () => { findOptions.IncludeFolders = false; findFunc = findOptions.GetFindFunc(new[] { 0 }, new[] { int.MaxValue }); }; it["find calls found"] = () => { findFunc(rootEntry, testFile); findOptions.VisitorFunc.ReceivedWithAnyArgs().Invoke(null, null); }; }; }; describe["with test entry \"TestDir1\""] = () => { describe["given find excludes Files"] = () => { before = () => { findOptions.IncludeFiles = false; findFunc = findOptions.GetFindFunc(new[] { 0 }, new[] { int.MaxValue }); }; it["find calls found"] = () => { findFunc(rootEntry, testDir); findOptions.VisitorFunc.ReceivedWithAnyArgs().Invoke(null, null); }; }; describe["given find excludes Folders"] = () => { before = () => { findOptions.IncludeFolders = false; findFunc = findOptions.GetFindFunc(new[] { 0 }, new[] { int.MaxValue }); }; it["find doesn't call found"] = () => { findFunc(rootEntry, testDir); findOptions.VisitorFunc.DidNotReceiveWithAnyArgs().Invoke(null, null); }; }; }; }
public void FindFunc_Spec_for_limit() { describe["given test entry tree"] = () => { // ReSharper disable TooWideLocalVariableScope DirEntry de2a; DirEntry de2b; DirEntry de2c; DirEntry de3a = null; DirEntry de4a = null; // ReSharper restore TooWideLocalVariableScope RootEntry re = null; FindOptions findOptions = null; Func <CommonEntry, DirEntry, bool> matcherAll = null; before = () => { // NOTE: test tree entry entry is de2c,de2a,de2b,de3a,de4a re = RootEntryTestBase.NewTestRootEntry(out de2a, out de2b, out de2c, out de3a, out de4a); re.SetInMemoryFields(); matcherAll = Substitute.For <Func <CommonEntry, DirEntry, bool> >(); matcherAll(null, null).ReturnsForAnyArgs(true); findOptions = new FindOptions(); }; describe["with FindOptions matching all entries"] = () => { TraverseFunc visitorFunc = null; before = () => { visitorFunc = Substitute.For <TraverseFunc>(); visitorFunc(null, null).ReturnsForAnyArgs(x => true); findOptions.Pattern = string.Empty; findOptions.PatternMatcher = matcherAll; findOptions.VisitorFunc = visitorFunc; }; describe["given find limit 2"] = () => { before = () => { findOptions.LimitResultCount = 2; findOptions.Find(new[] { re }); }; specify = () => visitorFunc.ReceivedWithAnyArgs(2).Invoke(null, null); specify = () => findOptions.ProgressCount.should_be(2); }; describe["given find limit 4"] = () => { before = () => { findOptions.LimitResultCount = 4; findOptions.Find(new[] { re }); }; specify = () => visitorFunc.ReceivedWithAnyArgs(4).Invoke(null, null); specify = () => findOptions.ProgressCount.should_be(4); }; describe["given large limit"] = () => { before = () => { findOptions.LimitResultCount = int.MaxValue; }; describe["will find all"] = () => { before = () => findOptions.Find(new[] { re }); specify = () => visitorFunc.ReceivedWithAnyArgs(5).Invoke(null, null); specify = () => findOptions.ProgressCount.should_be(5); }; describe["and given SkipCount 5"] = () => { before = () => { findOptions.SkipCount = 5; findOptions.Find(new[] { re }); }; specify = () => visitorFunc.ReceivedWithAnyArgs(0).Invoke(null, null); specify = () => findOptions.ProgressCount.should_be(5); }; describe["and given SkipCount 6"] = () => { before = () => { findOptions.SkipCount = 6; findOptions.Find(new[] { re }); }; specify = () => visitorFunc.ReceivedWithAnyArgs(0).Invoke(null, null); specify = () => findOptions.ProgressCount.should_be(5); }; }; describe["When find limit 2, pattern 'a'"] = () => { before = () => { findOptions.Pattern = "a"; findOptions.LimitResultCount = 2; findOptions.Find(new[] { re }); }; specify = () => visitorFunc.ReceivedWithAnyArgs(2).Invoke(null, null); specify = () => findOptions.ProgressCount.should_be(4); }; describe["When find limit 2, pattern 'a' with Skip count 2"] = () => { before = () => { findOptions.LimitResultCount = 2; findOptions.SkipCount = 2; findOptions.Pattern = "a"; findOptions.Find(new[] { re }); }; specify = () => visitorFunc.ReceivedWithAnyArgs(2).Invoke(null, null); specify = () => findOptions.ProgressCount.Is(5); it["found received in expected order"] = () => Received.InOrder(() => { visitorFunc.Received().Invoke(Arg.Any <CommonEntry>(), de3a); visitorFunc.Received().Invoke(Arg.Any <CommonEntry>(), de4a); }); }; }; }; // TODO - capture SkipCount and interface and pass it back later ? [ProgressCount from findOptions] // maybe this involves holding onto FindOptions on serverside in Web ? // TODO - if SkipCount >= Limit then dont do any work return empty. }
//public CommonEntry FindClosestParentDir(string relativePath) //{ // if (string.IsNullOrWhiteSpace(relativePath)) // { // throw new ArgumentException("Argument relativePath must be non empty."); // } // var indexOfDirectorySeperator = relativePath.IndexOf(Filesystem.Path.DirectorySeparatorChar); // var firstPathElement = relativePath; // var remainderPath = string.Empty; // if (indexOfDirectorySeperator > 0) // { // firstPathElement = relativePath.Remove(indexOfDirectorySeperator); // remainderPath = relativePath.Substring(indexOfDirectorySeperator + Filesystem.Path.DirectorySeparatorChar.Length); // } // var de = Children.FirstOrDefault(x => x.Path == firstPathElement); // if (de != null) // { // if (remainderPath == string.Empty) // { // return de; // } // var foundDe = de.FindClosestParentDir(remainderPath); // if (foundDe == null) // { // return de; // } // return foundDe; // } // return this; //} public void TraverseTreePair(TraverseFunc func) { TraverseTreePair(new List <CommonEntry> { this }, func); }
void And_given_Find_excludes_folders() { _findOptions.IncludeFolders = false; _findFunc = _findOptions.GetFindFunc(new[] { 0 }, new[] { int.MaxValue }); }