Esempio n. 1
0
            public void FindsDocumentInChildOfParentContext()
            {
                // Given
                TestDocument a  = new TestDocument();
                TestDocument b  = new TestDocument();
                TestDocument c  = new TestDocument();
                TestDocument d  = new TestDocument();
                TestDocument e  = new TestDocument();
                TestDocument e1 = new TestDocument();
                TestDocument e2 = new TestDocument();
                TestDocument e3 = new TestDocument();

                e.Add(Keys.Children, new IDocument[] { e1, e2, e3 });
                TestDocument         f       = new TestDocument();
                TestExecutionContext parent  = new TestExecutionContext(d, e, f);
                TestExecutionContext context = new TestExecutionContext(a, b, c);

                context.Parent = parent;
                LazyDocumentMetadataValue value = new LazyDocumentMetadataValue(e2);

                // When
                object result = value.Get(null);

                // Then
                result.ShouldBe(e2);
                IExecutionContext.Current.ShouldBe(context); // Sanity check
            }
        /// <inheritdoc />
        protected override IEnumerable <IDocument> ExecuteContext(IExecutionContext context)
        {
            // Partition the pages and get a total before skip/take
            IDocument[][] pages =
                Partition(context.Inputs, _pageSize)
                .ToArray();

            // Skip/take the pages
            pages = pages
                    .Skip(_skipPages)
                    .Take(_takePages)
                    .ToArray();

            // Special case for no pages, create an empty one
            if (pages.Length == 0)
            {
                pages = new[] { Array.Empty <IDocument>() };
            }

            // Create the documents per page, setting previous and next values as we go
            Stack <(IDocument, LazyDocumentMetadataValue)> results = new Stack <(IDocument, LazyDocumentMetadataValue)>();

            for (int c = 0; c < pages.Length; c++)
            {
                MetadataItems items = new MetadataItems
                {
                    { Keys.Children, pages[c] },
                    { Keys.Index, c + 1 },
                    { Keys.TotalPages, pages.Length },
                    { Keys.TotalItems, context.Inputs.Length }
                };
                if (results.Count > 0)
                {
                    items.Add(Keys.Previous, new LazyDocumentMetadataValue(results.Peek().Item1));
                }
                LazyDocumentMetadataValue next = null;
                if (c < pages.Length - 1)
                {
                    next = new LazyDocumentMetadataValue();
                    items.Add(Keys.Next, next);
                }
                IDocument document = context.CreateDocument(
                    _source,
                    _source.IsNull ? _source : _source.GetRelativeInputPath(),
                    items);
                if (results.Count > 0)
                {
                    results.Peek().Item2.OriginalDocument = document;
                }
                results.Push((document, next));
            }

            return(results.Select(x => x.Item1).Reverse());
        }
Esempio n. 3
0
            public void DoesNotThrowForNullOriginalDocument()
            {
                // Given
                TestExecutionContext      context = new TestExecutionContext();
                LazyDocumentMetadataValue value   = new LazyDocumentMetadataValue();

                // When
                object result = value.Get(null);

                // Then
                result.ShouldBeNull();
            }
Esempio n. 4
0
            public void FindsDocumentInInputs()
            {
                // Given
                TestDocument              a       = new TestDocument();
                TestDocument              b       = new TestDocument();
                TestDocument              c       = new TestDocument();
                TestExecutionContext      context = new TestExecutionContext(a, b, c);
                LazyDocumentMetadataValue value   = new LazyDocumentMetadataValue(b);

                // When
                object result = value.Get(null);

                // Then
                result.ShouldBe(b);
            }
Esempio n. 5
0
            public void ReturnsOriginalDocumentIfNotFound()
            {
                // Given
                TestDocument              a       = new TestDocument();
                TestDocument              b       = new TestDocument();
                TestDocument              c       = new TestDocument();
                TestDocument              d       = new TestDocument();
                TestExecutionContext      context = new TestExecutionContext(a, b, c);
                LazyDocumentMetadataValue value   = new LazyDocumentMetadataValue(d);

                // When
                object result = value.Get(null);

                // Then
                result.ShouldBe(d);
            }
Esempio n. 6
0
            public void FindsDocumentInChildren()
            {
                // Given
                TestDocument a  = new TestDocument();
                TestDocument b  = new TestDocument();
                TestDocument b1 = new TestDocument();
                TestDocument b2 = new TestDocument();
                TestDocument b3 = new TestDocument();

                b.Add(Keys.Children, new IDocument[] { b1, b2, b3 });
                TestDocument              c       = new TestDocument();
                TestExecutionContext      context = new TestExecutionContext(a, b, c);
                LazyDocumentMetadataValue value   = new LazyDocumentMetadataValue(b2);

                // When
                object result = value.Get(null);

                // Then
                result.ShouldBe(b2);
            }
Esempio n. 7
0
            public void FindsDocumentInParentContext()
            {
                // Given
                TestDocument         a       = new TestDocument();
                TestDocument         b       = new TestDocument();
                TestDocument         c       = new TestDocument();
                TestDocument         d       = new TestDocument();
                TestDocument         e       = new TestDocument();
                TestDocument         f       = new TestDocument();
                TestExecutionContext parent  = new TestExecutionContext(d, e, f);
                TestExecutionContext context = new TestExecutionContext(a, b, c);

                context.Parent = parent;
                LazyDocumentMetadataValue value = new LazyDocumentMetadataValue(e);

                // When
                object result = value.Get(null);

                // Then
                result.ShouldBe(e);
                IExecutionContext.Current.ShouldBe(context); // Sanity check
            }