public void TestDepthFirstPostorder() { CollectionAssert.AreEqual( actual: Traverse.DepthFirst("abcd", s => s.Length < 2 ? Enumerable.Empty <string>() : new[] { s.Substring(0, s.Length - 1), s.Substring(1) }, postorder: true), expected: new[] { "a", "b", "ab", "b", "c", "bc", "abc", "b", "c", "bc", "c", "d", "cd", "bcd", "abcd" } ); }
public void TestDepthFirstMultipleRootsPostorder() { CollectionAssert.AreEqual( actual: new[] { 3, 5, 4 }.SelectMany(x => Traverse.DepthFirst(x, i => i <= 1 ? Enumerable.Empty <int>() : new[] { (i / 2) + (i % 2), i / 2 }, postorder: true)), expected: new[] { 1, 1, 2, 1, 3, 1, 1, 2, 1, 3, 1, 1, 2, 5, 1, 1, 2, 1, 1, 2, 4 } ); }
public void TestDepthFirst() { Assert.Throws <ArgumentNullException>(() => Traverse.DepthFirst("a", null !)); CollectionAssert.AreEqual( actual: Traverse.DepthFirst("abcd", s => s.Length < 2 ? Enumerable.Empty <string>() : new[] { s.Substring(0, s.Length - 1), s.Substring(1) }), expected: new[] { "abcd", "abc", "ab", "a", "b", "bc", "b", "c", "bcd", "bc", "b", "c", "cd", "c", "d" } ); }
public void TestEmpty() { Assert.IsEmpty(Traverse.Along(default(object), _ => throw new InvalidOperationException("should never get here"))); Assert.IsEmpty(Traverse.DepthFirst(1, _ => Enumerable.Empty <int>()).Skip(1)); Assert.IsEmpty(Traverse.BreadthFirst(1, _ => Enumerable.Empty <int>()).Skip(1)); Assert.IsEmpty(Traverse.BreadthFirst <char>(Enumerable.Empty <char>(), _ => throw new InvalidOperationException("should never get here"))); }
public void TestDepthFirstNullChildren() { // throwing NRE is consistent with SelectMany() var enumerable = Traverse.DepthFirst(new[] { 1, 2, 3 }, _ => default(IEnumerable <int[]>) !); Assert.Throws <NullReferenceException>(() => enumerable.ToList()); }
public void given_command_tree_sets_all_default() { var config = Config.Build(Path.GetTempFileName()) .SetString("foo", "name", "foo") .SetString("foo", "bar", "name", "bar") .SetBoolean("foo", "baz", "force", true) .SetString("foo", "baz.update", "name", "baz"); var command = new Command("foo") { new Command("bar") { new Argument <string?>("name"), }, new Command("baz") { new Command("update") { new Argument <string?>("name"), }, new Command("install") { new Argument <string?>("name"), }, new Command("uninstall") { new Argument <string?>("name"), new Option <bool?>("force"), }, }, }.WithConfigurableDefaults(configuration: config); Assert.True(Traverse .BreadthFirst(command, c => c.Children.OfType <Command>()) .SelectMany(x => x.Arguments).All(x => x.HasDefaultValue)); // Gets from the command-specific section Assert.Equal("bar", command.Children.OfType <Command>().First().Arguments.First().GetDefaultValue()); Assert.Equal("baz", Traverse .DepthFirst(command, c => c.Children.OfType <Command>()) .First(c => c.Name == "update") .Arguments.First().GetDefaultValue()); // Uses default from lifted top-level section for shared "name" argument Assert.Equal("foo", Traverse .DepthFirst(command, c => c.Children.OfType <Command>()) .First(c => c.Name == "install") .Arguments.First().GetDefaultValue()); // Non-shared but still lifted since no conflicts Assert.True(Traverse .DepthFirst(command, c => c.Children.OfType <Command>()) .First(c => c.Name == "uninstall") .Options.OfType <IOption>().First().Argument.GetDefaultValue() as bool?); }
public void TestHandlingOfEnumeratorDisposalErrors() { var enumerators = new List <ThrowingEnumeratorEnumerable>(); var disposalException = Assert.Throws <InvalidOperationException>( () => Traverse.DepthFirst( 5, i => { var enumerator = i > 0 ? new ThrowingEnumeratorEnumerable(i - 1) : new ThrowingEnumeratorEnumerable(); enumerators.Add(enumerator); return(enumerator); } ) .ToArray() ); Assert.AreEqual("Throw from 4 dispose", disposalException.Message); Assert.AreEqual(6, enumerators.Count); Assert.IsTrue(enumerators.All(e => e.Disposed)); }
public void DepthFirstEnumeratorsAreLazyAndDisposeProperly() { var helper = new EnumeratorHelper(); var sequence = Traverse.DepthFirst(10, i => helper.MakeEnumerator(i - 1)); Assert.AreEqual(0, helper.IterateCount); Assert.AreEqual(0, helper.StartCount); Assert.AreEqual(0, helper.EndCount); using (var enumerator = sequence.GetEnumerator()) { for (var i = 0; i < 10; ++i) { Assert.IsTrue(enumerator.MoveNext()); } Assert.AreEqual(9, helper.IterateCount); // -1 for root } Assert.AreEqual(helper.EndCount, helper.StartCount); }