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 TestBreadthFirst() { Assert.Throws <ArgumentNullException>(() => Traverse.BreadthFirst("a", null !)); CollectionAssert.AreEqual( actual: Traverse.BreadthFirst("abcd", s => s.Length < 2 ? Enumerable.Empty <string>() : new[] { s.Substring(0, s.Length - 1), s.Substring(1) }), expected: new[] { "abcd", "abc", "bcd", "ab", "bc", "bc", "cd", "a", "b", "b", "c", "b", "c", "c", "d", } ); }
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 TestBreadthFirstNullChildren() { // throwing NRE is consistent with SelectMany() var enumerable1 = Traverse.BreadthFirst(new[] { 1, 2, 3 }, _ => default(IEnumerable <int[]>) !); Assert.Throws <NullReferenceException>(() => enumerable1.ToList()); var enumerable2 = Traverse.BreadthFirst(roots: new[] { 1, 2, 3 }, children: _ => default(IEnumerable <int>) !); Assert.Throws <NullReferenceException>(() => enumerable2.ToList()); }
public void TestBreadthFirstMultipleRoots() { Assert.Throws <ArgumentNullException>(() => Traverse.BreadthFirst(default(IEnumerable <string>) !, _ => Enumerable.Empty <string>())); Assert.Throws <ArgumentNullException>(() => Traverse.BreadthFirst(Enumerable.Empty <string>(), default(Func <string, IEnumerable <string> >) !)); CollectionAssert.AreEqual( actual: Traverse.BreadthFirst(new[] { 3, 5, 4 }, i => i <= 1 ? Enumerable.Empty <int>() : new[] { (i / 2) + (i % 2), i / 2 }), expected: new[] { 3, 5, 4, 2, 1, 3, 2, 2, 2, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1 } ); }
public void BreadthFirstEnumeratorsAreLazyAndDisposeProperly() { var helper = new EnumeratorHelper(); var sequence = Traverse.BreadthFirst(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); }