Value capture iterator
This allows you to capture random values and reuse them, until you wish to reevaluate the sources.
Наследование: ISource, IDebugInfoProvider, IIterable
Пример #1
0
        public void ThenNextMustUpdateDependentIterators()
        {
            var sources = new List<TestSource>
            {
                new TestSource{ Count = 0 },
                new TestSource{ Count = 100 }
            };

            using (var first = new Iterator(sources.Cast<object>().ToList(), null))
            using (var second = new Iterator(new List<object>{first}, null))
            {
                Assert.AreEqual(0, ((IList<object>)first.GetItem())[0]);
                Assert.AreEqual(100, ((IList<object>)first.GetItem())[1]);

                Assert.AreEqual(0, ((IList<object>)second.GetItem())[0]);
                Assert.AreEqual(100, ((IList<object>)second.GetItem())[1]);

                Iterator.NextAll(Iterator.DefaultScope);

                Assert.AreEqual(1, ((IList<object>)first.GetItem())[0]);
                Assert.AreEqual(101, ((IList<object>)first.GetItem())[1]);

                Assert.AreEqual(1, ((IList<object>)second.GetItem())[0]);
                Assert.AreEqual(101, ((IList<object>)second.GetItem())[1]);
            }
        }
Пример #2
0
        public void ThenNewInstancesMustRegister()
        {
            Assert.AreEqual(0, Iterator.Instances.Count, "The iterator context is not clean");

            using (var iterator = new Iterator(new List<object> {new TestSource()}, null))
            {
                Assert.AreEqual(1, Iterator.Instances.Count, "The iterator did not register");
            }
        }
Пример #3
0
        public void ThenGetItemMustNotNext()
        {
            var sources = new List<TestSource>
            {
                new TestSource{ Count = 0 },
                new TestSource{ Count = 100 }
            };

            using (var iterator = new Iterator(sources.Cast<object>().ToList(), null))
            {
                //Incremented on next due to constructor
                Assert.AreEqual(1, sources[0].Count);
                Assert.AreEqual(101, sources[1].Count);

                Assert.AreEqual(0, ((IList<object>)iterator.GetItem())[0]);
                Assert.AreEqual(100, ((IList<object>)iterator.GetItem())[1]);

                //Test again to make sure it doesn't pull
                Assert.AreEqual(0, ((IList<object>)iterator.GetItem())[0]);
                Assert.AreEqual(100, ((IList<object>)iterator.GetItem())[1]);
            }
        }
Пример #4
0
        private Iterator CreateIterator(JToken item)
        {
            IEnumerable<object> sources = item["sources"].Select(Create);
            string scope = (string)item["scope"];

            Iterator result = new Iterator(sources.ToList(), scope);

            result.DebugInfo = GetDebugInfo(item);

            return result;
        }
Пример #5
0
        private FormatString CreateFormatString(JToken item)
        {
            JToken sourceItem = item["source"];
            JToken formatItem = item["format"];
            bool? suppressEndLine = (bool?)item["suppressEndLine"];
            string scope = (string)item["scope"];

            object source;
            string formatString;
            if (sourceItem == null)
            {
                List<object> sources = new List<object>();
                List<string> formatStringComponents = new List<string>();
                foreach (JToken subItem in formatItem)
                {
                    if (subItem.Type == JTokenType.String)
                    {
                        formatStringComponents.Add((string)subItem);
                    }
                    else
                    {
                        string componentFormat = (string) subItem["format"] ?? string.Empty;
                        formatStringComponents.Add(string.Concat("{", sources.Count, componentFormat, "}"));
                        sources.Add(Create(subItem["value"]));
                    }
                }
                source = new Iterator(sources, scope);
                formatString = formatStringComponents.Aggregate(string.Concat);
            }
            else
            {
                source = CreateDataReference(sourceItem);
                formatString = formatItem.Type == JTokenType.Array
                    ? formatItem.Select(p => (string)p).Aggregate(string.Concat)
                    : (string)formatItem;
            }

            var result = source is DataReference
                ? new FormatString((DataReference) source, formatString, suppressEndLine ?? false)
                : new FormatString((ISource) source, formatString, suppressEndLine ?? false);

            result.DebugInfo = GetDebugInfo(item);

            return result;
        }
Пример #6
0
        public void ThenNextMustUpdateFromSources()
        {
            var sources = new List<TestSource>
            {
                new TestSource{ Count = 0 },
                new TestSource{ Count = 100 }
            };

            using (var iterator = new Iterator(sources.Cast<object>().ToList(),null))
            {
                //Incremented on next due to constructor
                Assert.AreEqual(1, sources[0].Count);
                Assert.AreEqual(101, sources[1].Count);

                Assert.AreEqual(0, ((IList<object>)iterator.GetItem())[0] );
                Assert.AreEqual(100, ((IList<object>)iterator.GetItem())[1]);

                Iterator.NextAll(Iterator.DefaultScope);

                Assert.AreEqual(2, sources[0].Count);
                Assert.AreEqual(102, sources[1].Count);

                Assert.AreEqual(1, ((IList<object>)iterator.GetItem())[0]);
                Assert.AreEqual(101, ((IList<object>)iterator.GetItem())[1]);
            }
        }
Пример #7
0
        public void ThenSourcesMustBeFlattened()
        {
            var sources = new List<object>
            {
                new TestSource{ Count = 0 },
                new TestSource{ Count = 100 },
                new StringList(new List<object>{"a", "b"})
            };

            using (var iterator = new Iterator(sources, null))
            {
                var result = ((IList<object>) iterator.GetItem());
                Assert.AreEqual(0, result[0]);
                Assert.AreEqual(100, result[1]);
                Assert.AreEqual("a", result[2]);
                Assert.AreEqual("b", result[3]);
            }
        }
Пример #8
0
        public void ThenSequentialIteratorSourcesMustBeFlattened()
        {
            var sources = new List<object>
            {
                new TestSource{ Count = 0 },
                new TestSource{ Count = 100 },
                new StringList(new List<object>{"a", "b"})
            };

            using (var first = new Iterator(sources, null))
            using (var second = new Iterator(new List<object> { first, sources[2] }, null))
            {
                var result = ((IList<object>)second.GetItem());

                Assert.AreEqual(0, result[0]);
                Assert.AreEqual(100, result[1]);
                Assert.AreEqual("a", result[2]);
                Assert.AreEqual("b", result[3]);

                Assert.AreEqual("a", result[4]);
                Assert.AreEqual("b", result[5]);
            }
        }