Пример #1
0
        public void Create_CreatesFlexiSectionBlockAndUpdatesOpenFlexiSectionBlocks()
        {
            // Arrange
            var          dummyLine                                  = new StringSlice("dummyLine", 4, 8);
            const int    dummyColumn                                = 4;
            const string dummyBlockName                             = "dummyBlockName";
            const string dummyResolvedBlockName                     = "dummyResolvedBlockName";
            const SectioningContentElement dummyElement             = SectioningContentElement.Aside;
            const string dummyLinkIcon                              = "dummyLinkIcon";
            const FlexiSectionBlockRenderingMode dummyRenderingMode = FlexiSectionBlockRenderingMode.Classic;
            const int dummyLevel      = 5;
            var       dummyAttributes = new ReadOnlyDictionary <string, string>(new Dictionary <string, string>());
            Mock <IFlexiSectionBlockOptions> dummyFlexiSectionBlockOptions = _mockRepository.Create <IFlexiSectionBlockOptions>();

            dummyFlexiSectionBlockOptions.Setup(f => f.BlockName).Returns(dummyBlockName);
            dummyFlexiSectionBlockOptions.Setup(f => f.Element).Returns(dummyElement);
            dummyFlexiSectionBlockOptions.Setup(f => f.RenderingMode).Returns(dummyRenderingMode);
            dummyFlexiSectionBlockOptions.Setup(f => f.LinkIcon).Returns(dummyLinkIcon);
            dummyFlexiSectionBlockOptions.Setup(f => f.Attributes).Returns(dummyAttributes);
            Mock <BlockParser> dummyBlockParser    = _mockRepository.Create <BlockParser>();
            BlockProcessor     dummyBlockProcessor = MarkdigTypesFactory.CreateBlockProcessor();

            dummyBlockProcessor.Column = dummyColumn;
            dummyBlockProcessor.Line   = dummyLine;
            var dummyFlexiSectionHeadingBlock = new FlexiSectionHeadingBlock(null);
            Mock <IFlexiSectionHeadingBlockFactory> mockFlexiSectionHeadingBlockFactory = _mockRepository.Create <IFlexiSectionHeadingBlockFactory>();

            mockFlexiSectionHeadingBlockFactory.
            Setup(f => f.Create(dummyBlockProcessor, dummyFlexiSectionBlockOptions.Object, dummyBlockParser.Object)).
            Returns(dummyFlexiSectionHeadingBlock);
            Mock <IOptionsService <IFlexiSectionBlockOptions, IFlexiSectionBlocksExtensionOptions> > mockOptionsService = _mockRepository.Create <IOptionsService <IFlexiSectionBlockOptions, IFlexiSectionBlocksExtensionOptions> >();

            mockOptionsService.Setup(o => o.CreateOptions(dummyBlockProcessor)).Returns((dummyFlexiSectionBlockOptions.Object, null));
            Mock <FlexiSectionBlockFactory> mockTestSubject = CreateMockFlexiSectionBlockFactory(mockOptionsService.Object, mockFlexiSectionHeadingBlockFactory.Object);

            mockTestSubject.CallBase = true;
            mockTestSubject.Setup(t => t.ValidateLevel(dummyLevel));
            mockTestSubject.Setup(t => t.ResolveBlockName(dummyBlockName)).Returns(dummyResolvedBlockName);
            mockTestSubject.Setup(t => t.ValidateElement(dummyElement));
            mockTestSubject.Setup(t => t.ValidateRenderingMode(dummyRenderingMode));
            mockTestSubject.Setup(t => t.UpdateOpenFlexiSectionBlocks(dummyBlockProcessor, It.IsAny <FlexiSectionBlock>()));

            // Act
            FlexiSectionBlock result = mockTestSubject.Object.Create(dummyLevel, dummyBlockProcessor, dummyBlockParser.Object);

            // Assert
            _mockRepository.VerifyAll();
            Assert.Equal(dummyResolvedBlockName, result.BlockName);
            Assert.Equal(dummyElement, result.Element);
            Assert.Equal(dummyLinkIcon, result.LinkIcon);
            Assert.Equal(dummyRenderingMode, result.RenderingMode);
            Assert.Equal(dummyLevel, result.Level);
            Assert.Same(dummyAttributes, result.Attributes);
            Assert.Same(dummyBlockParser.Object, result.Parser);
            Assert.Equal(dummyColumn, result.Column);
            Assert.Equal(dummyLine.Start, result.Span.Start);
            Assert.Equal(dummyLine.End, result.Span.End);
            Assert.Single(result);
            Assert.Same(dummyFlexiSectionHeadingBlock, result[0]);
        }
        public void WriteBlock_OnlyWritesChildrenIfEnableHtmlForBlockIsFalse()
        {
            // Arrange
            const string dummyChildText       = "dummyChildText";
            var          dummyContainerInline = new ContainerInline();

            dummyContainerInline.AppendChild(new LiteralInline(dummyChildText));
            var dummyParagraphBlock = new ParagraphBlock()
            {
                Inline = dummyContainerInline
            };
            FlexiSectionBlock dummyFlexiSectionBlock = CreateFlexiSectionBlock();

            dummyFlexiSectionBlock.Add(dummyParagraphBlock);
            var dummyStringWriter = new StringWriter();
            var dummyHtmlRenderer = new HtmlRenderer(dummyStringWriter)
            {
                EnableHtmlForBlock = false
            };
            ExposedFlexiSectionBlockRenderer testSubject = CreateExposedFlexiSectionBlockRenderer();

            // Act
            testSubject.ExposedWriteBlock(dummyHtmlRenderer, dummyFlexiSectionBlock);
            string result = dummyStringWriter.ToString();

            // Assert
            Assert.Equal(dummyChildText + "\n", result, ignoreLineEndingDifferences: true);
        }
Пример #3
0
        public void UpdateOpenFlexiSectionBlocks_DiscardsStacksForClosedTreesAndCreatesANewStackForANewTree()
        {
            // Arrange
            var dummyCurrentBranch = new Stack <FlexiSectionBlock>();

            dummyCurrentBranch.Push(CreateFlexiSectionBlock());
            FlexiSectionBlock dummyClosedFlexiSectionBlock = CreateFlexiSectionBlock();

            dummyClosedFlexiSectionBlock.IsOpen = false;
            var dummyClosedBranchStack = new Stack <FlexiSectionBlock>();

            dummyClosedBranchStack.Push(dummyClosedFlexiSectionBlock);
            var dummyOpenSectionBlocks = new Stack <Stack <FlexiSectionBlock> >();

            dummyOpenSectionBlocks.Push(dummyCurrentBranch);
            dummyOpenSectionBlocks.Push(dummyClosedBranchStack);
            BlockProcessor dummyBlockProcessor = MarkdigTypesFactory.CreateBlockProcessor();

            dummyBlockProcessor.ProcessLine(new StringSlice("")); // This line sets BlockProcessor.CurrentContainer to a MarkdownDocument
            FlexiSectionBlock dummyNewFlexiSectionBlock     = CreateFlexiSectionBlock();
            Mock <FlexiSectionBlockFactory> mockTestSubject = CreateMockFlexiSectionBlockFactory();

            mockTestSubject.CallBase = true;
            mockTestSubject.Setup(t => t.GetOrCreateOpenFlexiSectionBlocks(dummyBlockProcessor.Document)).Returns(dummyOpenSectionBlocks);

            // Act
            mockTestSubject.Object.UpdateOpenFlexiSectionBlocks(dummyBlockProcessor, dummyNewFlexiSectionBlock);

            // Assert
            _mockRepository.VerifyAll();
            Assert.Equal(2, dummyOpenSectionBlocks.Count); // Closed tree removed, open tree remains, new tree added
            Assert.Same(dummyNewFlexiSectionBlock, dummyOpenSectionBlocks.Pop().Peek());
            Assert.Same(dummyCurrentBranch, dummyOpenSectionBlocks.Peek());
        }
        public void TryContinueBlock_ContinuesBlock()
        {
            // Arrange
            FlexiSectionBlock dummyFlexiSectionBlock = CreateFlexiSectionBlock();

            dummyFlexiSectionBlock.IsOpen = false; // Set to false so we can verify that it gets set to true
            ExposedFlexiSectionBlockParser testSubject = CreateExposedFlexiSectionBlockParser();

            // Act
            BlockState result = testSubject.ExposedTryContinueBlock(null, dummyFlexiSectionBlock);

            // Assert
            Assert.Equal(BlockState.Skip, result);
            Assert.True(dummyFlexiSectionBlock.IsOpen);
        }
        public void WriteBlock_RendersStandardFlexiSectionBlockIfRenderingModeIsStandard()
        {
            // Arrange
            var dummyStringWriter = new StringWriter();
            var dummyHtmlRenderer = new HtmlRenderer(dummyStringWriter);
            FlexiSectionBlock dummyFlexiSectionBlock = CreateFlexiSectionBlock(renderingMode: FlexiSectionBlockRenderingMode.Standard);
            Mock <ExposedFlexiSectionBlockRenderer> mockTestSubject = CreateMockExposedFlexiSectionBlockRenderer();

            mockTestSubject.CallBase = true;
            mockTestSubject.Setup(t => t.WriteStandard(dummyHtmlRenderer, dummyFlexiSectionBlock));

            // Act
            mockTestSubject.Object.ExposedWriteBlock(dummyHtmlRenderer, dummyFlexiSectionBlock);

            // Assert
            _mockRepository.VerifyAll();
        }
        public void WriteStandard_RendersStandardFlexiSectionBlock(FlexiSectionBlock dummyFlexiSectionBlock,
                                                                   FlexiSectionHeadingBlock dummyFlexiSectionHeadingBlock,
                                                                   string expectedResult)
        {
            // Arrange
            dummyFlexiSectionBlock.Add(dummyFlexiSectionHeadingBlock);
            var dummyStringWriter = new StringWriter();
            var dummyHtmlRenderer = new HtmlRenderer(dummyStringWriter); // Note that markdig changes dummyStringWriter.NewLine to '\n'
            FlexiSectionBlockRenderer testSubject = CreateFlexiSectionBlockRenderer();

            // Act
            testSubject.Write(dummyHtmlRenderer, dummyFlexiSectionBlock);
            string result = dummyStringWriter.ToString();

            // Assert
            Assert.Equal(expectedResult, result, ignoreLineEndingDifferences: true);
        }
Пример #7
0
        public void UpdateOpenFlexiSectionBlocks_IgnoresAncestorContainerBlocksThatAreClosed()
        {
            // Arrange
            BlockProcessor dummyBlockProcessor = MarkdigTypesFactory.CreateBlockProcessor();
            // Create abitrary closed blocks (ListItemBlock and ListBlock) and set BlockProcessor.CurrentContainer to dummyListItemBlock.
            // Closed blocks should be ignored, so UpdateOpenFlexiSectionBlocks should not create a new stack.
            Mock <BlockParser> mockBlockParser = _mockRepository.Create <BlockParser>();

            mockBlockParser.Setup(b => b.TryContinue(dummyBlockProcessor, It.IsAny <Block>())).Returns(BlockState.Continue);
            var dummyListItemBlock = new ListItemBlock(mockBlockParser.Object);
            var dummyListBlock     = new ListBlock(null)
            {
                dummyListItemBlock
            };
            FlexiSectionBlock dummyFlexiSectionBlock = CreateFlexiSectionBlock();

            dummyFlexiSectionBlock.Add(dummyListBlock);
            dummyBlockProcessor.Open(dummyListItemBlock);
            dummyBlockProcessor.ProcessLine(new StringSlice("")); // This line sets BlockProcessor.CurrentContainer to dummyListItemBlock
            dummyListItemBlock.IsOpen = false;
            dummyListBlock.IsOpen     = false;
            FlexiSectionBlock dummyNewFlexiSectionBlock = CreateFlexiSectionBlock();
            var dummyCurrentBranch = new Stack <FlexiSectionBlock>();

            dummyCurrentBranch.Push(dummyFlexiSectionBlock);
            var dummyOpenSectionBlocks = new Stack <Stack <FlexiSectionBlock> >();

            dummyOpenSectionBlocks.Push(dummyCurrentBranch);
            Mock <FlexiSectionBlockFactory> mockTestSubject = CreateMockFlexiSectionBlockFactory();

            mockTestSubject.CallBase = true;
            mockTestSubject.Setup(t => t.GetOrCreateOpenFlexiSectionBlocks(dummyBlockProcessor.Document)).Returns(dummyOpenSectionBlocks);

            // Act
            mockTestSubject.Object.UpdateOpenFlexiSectionBlocks(dummyBlockProcessor, dummyNewFlexiSectionBlock);

            // Assert
            _mockRepository.VerifyAll();
            Assert.Single(dummyCurrentBranch);
            Assert.Equal(dummyNewFlexiSectionBlock, dummyCurrentBranch.Peek());
        }
Пример #8
0
        public void UpdateOpenFlexiSectionBlocks_ClosesFlexiSectionBlocksInCurrentBranchWithTheSameOrHigherLevels()
        {
            // Arrange
            BlockProcessor     dummyBlockProcessor = MarkdigTypesFactory.CreateBlockProcessor();
            Mock <BlockParser> mockBlockParser     = _mockRepository.Create <BlockParser>();

            mockBlockParser.Setup(b => b.TryContinue(dummyBlockProcessor, It.IsAny <Block>())).Returns(BlockState.Continue);
            FlexiSectionBlock dummyLevel1FlexiSectionBlock = CreateFlexiSectionBlock(level: 1, blockParser: mockBlockParser.Object);
            FlexiSectionBlock dummyLevel2FlexiSectionBlock = CreateFlexiSectionBlock(level: 2, blockParser: mockBlockParser.Object);
            FlexiSectionBlock dummyLevel3FlexiSectionBlock = CreateFlexiSectionBlock(level: 3, blockParser: mockBlockParser.Object);

            dummyBlockProcessor.Open(dummyLevel1FlexiSectionBlock);
            dummyBlockProcessor.Open(dummyLevel2FlexiSectionBlock);
            dummyBlockProcessor.Open(dummyLevel3FlexiSectionBlock);
            dummyBlockProcessor.ProcessLine(new StringSlice("")); // Sets BlockProcessor.CurrentContainer to a FlexiSectionBlock
            var dummyCurrentBranch = new Stack <FlexiSectionBlock>();

            dummyCurrentBranch.Push(dummyLevel1FlexiSectionBlock);
            dummyCurrentBranch.Push(dummyLevel2FlexiSectionBlock);
            dummyCurrentBranch.Push(dummyLevel3FlexiSectionBlock);
            var dummyOpenSectionBlocks = new Stack <Stack <FlexiSectionBlock> >();

            dummyOpenSectionBlocks.Push(dummyCurrentBranch);
            FlexiSectionBlock dummyNewFlexiSectionBlock     = CreateFlexiSectionBlock(level: 2);
            Mock <FlexiSectionBlockFactory> mockTestSubject = CreateMockFlexiSectionBlockFactory();

            mockTestSubject.CallBase = true;
            mockTestSubject.Setup(t => t.GetOrCreateOpenFlexiSectionBlocks(dummyBlockProcessor.Document)).Returns(dummyOpenSectionBlocks);

            // Act
            mockTestSubject.Object.UpdateOpenFlexiSectionBlocks(dummyBlockProcessor, dummyNewFlexiSectionBlock);

            // Assert
            _mockRepository.VerifyAll();
            Assert.Equal(2, dummyCurrentBranch.Count); // Level 2 and level 3 blocks get removed
            Assert.Same(dummyNewFlexiSectionBlock, dummyCurrentBranch.Pop());
            Assert.Same(dummyLevel1FlexiSectionBlock, dummyCurrentBranch.Peek());
            mockBlockParser.Verify(b => b.Close(dummyBlockProcessor, dummyLevel2FlexiSectionBlock)); // Gets closed
            mockBlockParser.Verify(b => b.Close(dummyBlockProcessor, dummyLevel3FlexiSectionBlock)); // Gets closed
        }
        public void WriteClassic_RendersClassicFlexiSectionBlock()
        {
            // Arrange
            const int    dummyLevel                  = 6;
            const string dummyHeadingText            = "dummyHeadingText";
            var          dummyHeadingContainerInline = new ContainerInline();

            dummyHeadingContainerInline.AppendChild(new LiteralInline(dummyHeadingText));
            FlexiSectionHeadingBlock dummyFlexiSectionHeadingBlock = CreateFlexiSectionHeadingBlock();

            dummyFlexiSectionHeadingBlock.Inline = dummyHeadingContainerInline;
            const string dummyChildText            = "dummyChildText";
            var          dummyChildContainerInline = new ContainerInline();

            dummyChildContainerInline.AppendChild(new LiteralInline(dummyChildText));
            var dummyChildBlock = new ParagraphBlock()
            {
                Inline = dummyChildContainerInline
            };
            var dummyStringWriter = new StringWriter();
            var dummyHtmlRenderer = new HtmlRenderer(dummyStringWriter);
            FlexiSectionBlockRenderer testSubject       = CreateFlexiSectionBlockRenderer();
            FlexiSectionBlock         flexiSectionBlock = CreateFlexiSectionBlock(level: dummyLevel);

            flexiSectionBlock.Add(dummyFlexiSectionHeadingBlock);
            flexiSectionBlock.Add(dummyChildBlock);

            // Act
            testSubject.WriteClassic(dummyHtmlRenderer, flexiSectionBlock);

            // Assert
            string result = dummyStringWriter.ToString();

            Assert.Equal($@"<h{dummyLevel}>{dummyHeadingText}</h{dummyLevel}>
<p>{dummyChildText}</p>
",
                         result,
                         ignoreLineEndingDifferences: true);
        }
        public void TryOpenBlock_CreatesFlexiSectionBlockAndReturnsBlockStateContinueDiscardIfSuccessful(string dummyLineText, int expectedLevel)
        {
            // Arrange
            BlockProcessor dummyBlockProcessor = MarkdigTypesFactory.CreateBlockProcessor();

            dummyBlockProcessor.Line = new StringSlice(dummyLineText);
            FlexiSectionBlock dummyFlexiSectionBlock = CreateFlexiSectionBlock();
            Mock <IFlexiSectionBlockFactory>      mockFlexiSectionBlockFactory = _mockRepository.Create <IFlexiSectionBlockFactory>();
            Mock <ExposedFlexiSectionBlockParser> mockTestSubject = CreateMockExposedFlexiSectionBlockParser(mockFlexiSectionBlockFactory.Object);

            mockFlexiSectionBlockFactory.Setup(f => f.Create(expectedLevel, dummyBlockProcessor, mockTestSubject.Object)).Returns(dummyFlexiSectionBlock);
            mockTestSubject.CallBase = true;
            mockTestSubject.Setup(t => t.DiscardRedundantCharacters(expectedLevel, dummyBlockProcessor));

            // Act
            BlockState result = mockTestSubject.Object.ExposedTryOpenBlock(dummyBlockProcessor);

            // Assert
            _mockRepository.VerifyAll();
            Assert.Equal(BlockState.ContinueDiscard, result);
            Assert.Single(dummyBlockProcessor.NewBlocks);
            Assert.Same(dummyFlexiSectionBlock, dummyBlockProcessor.NewBlocks.Peek());
        }
        public void WriteStandard_RendersChildren()
        {
            // Arrange
            const string dummyChildContent         = "dummyChildText";
            var          dummyChildContainerInline = new ContainerInline();

            dummyChildContainerInline.AppendChild(new LiteralInline(dummyChildContent));
            var dummyChildBlock = new ParagraphBlock()
            {
                Inline = dummyChildContainerInline
            };
            FlexiSectionBlock dummyFlexiSectionBlock = CreateFlexiSectionBlock(level: 2); // Level must be specified or we'll get an out of range exception

            dummyFlexiSectionBlock.Add(CreateFlexiSectionHeadingBlock());
            dummyFlexiSectionBlock.Add(dummyChildBlock);
            var dummyStringWriter = new StringWriter();
            var dummyHtmlRenderer = new HtmlRenderer(dummyStringWriter); // Note that markdig changes dummyStringWriter.NewLine to '\n'
            FlexiSectionBlockRenderer testSubject = CreateFlexiSectionBlockRenderer();

            // Act
            testSubject.Write(dummyHtmlRenderer, dummyFlexiSectionBlock);
            string result = dummyStringWriter.ToString();

            // Assert
            Assert.Equal($@"<section class="" _level_2 _no-link-icon"">
<header class=""__header"">
<h2 class=""__heading""></h2>
<button class=""__link-button"" aria-label=""Copy link"">
</button>
</header>
<p>{dummyChildContent}</p>
</section>
",
                         result,
                         ignoreLineEndingDifferences: true);
        }
 public void ExposedWriteBlock(HtmlRenderer htmlRenderer, FlexiSectionBlock block)
 {
     WriteBlock(htmlRenderer, block);
 }
 public BlockState ExposedTryContinueBlock(BlockProcessor processor, FlexiSectionBlock flexiSectionBlock)
 {
     return(TryContinueBlock(processor, flexiSectionBlock));
 }