コード例 #1
0
 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" }
         );
 }
コード例 #2
0
 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 }
         );
 }
コード例 #3
0
        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"
            }
                );
        }
コード例 #4
0
 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")));
 }
コード例 #5
0
        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());
        }
コード例 #6
0
        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?);
        }
コード例 #7
0
        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));
        }
コード例 #8
0
        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);
        }