internal virtual FlexiCodeBlock CreateCore(ProxyLeafBlock proxyLeafBlock, BlockProcessor blockProcessor)
        {
            if (proxyLeafBlock == null)
            {
                throw new ArgumentNullException(nameof(proxyLeafBlock));
            }

            (IFlexiCodeBlockOptions flexiCodeBlockOptions, IFlexiCodeBlocksExtensionOptions _) = _optionsService.CreateOptions(blockProcessor);

            // Code
            StringLineGroup lines        = proxyLeafBlock.Lines;
            string          code         = proxyLeafBlock.Lines.ToString();
            int             codeNumLines = lines.Count;

            // Block name
            string blockName = ResolveBlockName(flexiCodeBlockOptions.BlockName);

            // Syntax highlighter
            SyntaxHighlighter syntaxHighlighter = flexiCodeBlockOptions.SyntaxHighlighter;

            ValidateSyntaxHighlighter(syntaxHighlighter);

            // Line numbers
            ReadOnlyCollection <NumberedLineRange> lineNumbers = TryCreateSortedLineRanges(flexiCodeBlockOptions.LineNumbers, codeNumLines);

            ValidateSortedLineNumbers(lineNumbers, codeNumLines);

            // Highlighted lines
            ReadOnlyCollection <LineRange> highlightedLines = TryCreateSortedLineRanges(flexiCodeBlockOptions.HighlightedLines, codeNumLines);

            // Highlighted phrases
            ReadOnlyCollection <Phrase> highlightedPhrases = ResolveHighlightedPhrases(code, flexiCodeBlockOptions.HighlightedPhrases);

            // Rendering mode
            FlexiCodeBlockRenderingMode renderingMode = flexiCodeBlockOptions.RenderingMode;

            ValidateRenderingMode(renderingMode);

            return(new FlexiCodeBlock(
                       blockName,
                       flexiCodeBlockOptions.Title,
                       flexiCodeBlockOptions.CopyIcon,
                       flexiCodeBlockOptions.RenderHeader,
                       flexiCodeBlockOptions.Language,
                       code,
                       codeNumLines,
                       syntaxHighlighter,
                       lineNumbers,
                       flexiCodeBlockOptions.OmittedLinesIcon,
                       highlightedLines,
                       highlightedPhrases,
                       renderingMode,
                       flexiCodeBlockOptions.Attributes,
                       proxyLeafBlock.Parser)
            {
                Line = proxyLeafBlock.Line,
                Column = proxyLeafBlock.Column,
                Span = proxyLeafBlock.Span
            });
        }
        public void CloseProxy_RemovesTrailingBlankLinesAndReturnsNewFlexiCodeBlock()
        {
            // Arrange
            var dummyLines = new StringLineGroup(3);

            dummyLines.Add(new StringSlice(string.Empty)); // Should not get removed
            dummyLines.Add(new StringSlice("dummy line"));
            dummyLines.Add(new StringSlice(string.Empty)); // Should get removed
            dummyLines.Add(new StringSlice(string.Empty)); // Should get removed
            var dummyProxyLeafBlock = new ProxyLeafBlock(null, null);

            dummyProxyLeafBlock.Lines = dummyLines;
            BlockProcessor dummyBlockProcessor = MarkdigTypesFactory.CreateBlockProcessor();
            var            dummyFlexiCodeBlock = new FlexiCodeBlock(default, default, default, default, default, default, default, default, default, default, default, default, default, default, default);
Exemplo n.º 3
0
        public void Create_FromProxyLeafBlock_CreatesFlexiCodeBlock()
        {
            // Arrange
            var            dummyProxyLeafBlock           = new ProxyLeafBlock(null, null);
            BlockProcessor dummyBlockProcessor           = MarkdigTypesFactory.CreateBlockProcessor();
            FlexiCodeBlock dummyFlexiCodeBlock           = CreateFlexiCodeBlock();
            Mock <FlexiCodeBlockFactory> mockTestSubject = CreateMockFlexiCodeBlockFactory();

            mockTestSubject.CallBase = true;
            mockTestSubject.Setup(t => t.CreateCore(dummyProxyLeafBlock, dummyBlockProcessor)).Returns(dummyFlexiCodeBlock);

            // Act
            FlexiCodeBlock result = mockTestSubject.Object.Create(dummyProxyLeafBlock, dummyBlockProcessor);

            // Assert
            _mockRepository.VerifyAll();
            Assert.Same(dummyFlexiCodeBlock, result);
        }
        public void TryContinueBlock_ParsesLine()
        {
            // Arrange
            const BlockState dummyBlockState   = BlockState.Continue;
            var            dummyProxyLeafBlock = new ProxyLeafBlock(null, null);
            BlockProcessor dummyBlockProcessor = MarkdigTypesFactory.CreateBlockProcessor();
            Mock <ExposedIndentedFlexiCodeBlockParser> mockTestSubject = CreateMockExposedIndentedFlexiCodeBlockParser();

            mockTestSubject.CallBase = true;
            mockTestSubject.Setup(t => t.ParseLine(dummyBlockProcessor, dummyProxyLeafBlock)).Returns(dummyBlockState);

            // Act
            BlockState result = mockTestSubject.Object.ExposedTryContinueBlock(dummyBlockProcessor, dummyProxyLeafBlock);

            // Assert
            _mockRepository.VerifyAll();
            Assert.Equal(dummyBlockState, result);
        }
Exemplo n.º 5
0
        public void CreateProxyLeafBlock_CreatesProxyLeafBlock()
        {
            // Arrange
            const int      dummyColumn         = 2;
            const int      dummyLineStart      = 7;
            const int      dummyLineEnd        = 99;
            BlockProcessor dummyBlockProcessor = MarkdigTypesFactory.CreateBlockProcessor();

            dummyBlockProcessor.Column = dummyColumn;
            dummyBlockProcessor.Line   = new StringSlice("", dummyLineStart, dummyLineEnd);
            Mock <BlockParser>    dummyBlockParser = _mockRepository.Create <BlockParser>();
            FlexiCodeBlockFactory testSubject      = CreateFlexiCodeBlockFactory();

            // Act
            ProxyLeafBlock result = testSubject.CreateProxyLeafBlock(dummyBlockProcessor, dummyBlockParser.Object);

            // Assert
            Assert.Equal(dummyColumn, result.Column);
            Assert.Equal(dummyLineStart, result.Span.Start);
            Assert.Equal(dummyLineEnd, result.Span.End);
            Assert.Same(dummyBlockParser.Object, result.Parser);
            Assert.Equal(nameof(FlexiCodeBlock), result.MainTypeName);
        }
 /// <inheritdoc />
 public FlexiCodeBlock Create(ProxyLeafBlock proxyLeafBlock, BlockProcessor blockProcessor)
 {
     return(CreateCore(proxyLeafBlock, blockProcessor));
 }
Exemplo n.º 7
0
        public void CreateCore_CreatesFlexiCodeBlock()
        {
            // Arrange
            const int    dummyLine         = 6;
            const int    dummyColumn       = 2;
            var          dummySpan         = new SourceSpan(5, 10);
            const string dummyBlockName    = "dummyBlockName";
            const string dummyTitle        = "dummyTitle";
            const string dummyCopyIcon     = "dummyCopyIcon";
            const bool   dummyRenderHeader = false;
            const string dummyLanguage     = "dummyLanguage";
            var          dummyLines        = new StringLineGroup(2);

            dummyLines.Add(new StringSlice("dummy"));
            dummyLines.Add(new StringSlice("line"));
            const SyntaxHighlighter dummySyntaxHighlighter = SyntaxHighlighter.HighlightJS;
            var          dummyLineNumbers                        = new ReadOnlyCollection <NumberedLineRange>(new List <NumberedLineRange>());
            var          dummyResolvedLineNumbers                = new ReadOnlyCollection <NumberedLineRange>(new List <NumberedLineRange>());
            const string dummyOmittedLinesIcon                   = "dummyOmittedLinesIcon";
            var          dummyHighlightedLines                   = new ReadOnlyCollection <LineRange>(new List <LineRange>());
            var          dummyResolvedHighlightedLines           = new ReadOnlyCollection <LineRange>(new List <LineRange>());
            var          dummyHighlightedPhrases                 = new ReadOnlyCollection <PhraseGroup>(new List <PhraseGroup>());
            var          dummyResolvedHighlightedPhrases         = new ReadOnlyCollection <Phrase>(new List <Phrase>());
            const FlexiCodeBlockRenderingMode dummyRenderingMode = FlexiCodeBlockRenderingMode.Classic;
            var dummyAttributes = new ReadOnlyDictionary <string, string>(new Dictionary <string, string>());
            Mock <BlockParser> dummyBlockParser = _mockRepository.Create <BlockParser>();
            var dummyProxyLeafBlock             = new ProxyLeafBlock(null, dummyBlockParser.Object);

            dummyProxyLeafBlock.Line   = dummyLine;
            dummyProxyLeafBlock.Column = dummyColumn;
            dummyProxyLeafBlock.Span   = dummySpan;
            dummyProxyLeafBlock.Lines  = dummyLines;
            BlockProcessor dummyBlockProcessor = MarkdigTypesFactory.CreateBlockProcessor();
            Mock <IFlexiCodeBlockOptions> mockFlexiCodeBlockOptions = _mockRepository.Create <IFlexiCodeBlockOptions>();

            mockFlexiCodeBlockOptions.Setup(f => f.BlockName).Returns(dummyBlockName);
            mockFlexiCodeBlockOptions.Setup(f => f.Title).Returns(dummyTitle);
            mockFlexiCodeBlockOptions.Setup(f => f.CopyIcon).Returns(dummyCopyIcon);
            mockFlexiCodeBlockOptions.Setup(f => f.RenderHeader).Returns(dummyRenderHeader);
            mockFlexiCodeBlockOptions.Setup(f => f.Language).Returns(dummyLanguage);
            mockFlexiCodeBlockOptions.Setup(f => f.SyntaxHighlighter).Returns(dummySyntaxHighlighter);
            mockFlexiCodeBlockOptions.Setup(f => f.LineNumbers).Returns(dummyLineNumbers);
            mockFlexiCodeBlockOptions.Setup(f => f.OmittedLinesIcon).Returns(dummyOmittedLinesIcon);
            mockFlexiCodeBlockOptions.Setup(f => f.HighlightedLines).Returns(dummyHighlightedLines);
            mockFlexiCodeBlockOptions.Setup(f => f.HighlightedPhrases).Returns(dummyHighlightedPhrases);
            mockFlexiCodeBlockOptions.Setup(f => f.RenderingMode).Returns(dummyRenderingMode);
            mockFlexiCodeBlockOptions.Setup(f => f.Attributes).Returns(dummyAttributes);
            Mock <IOptionsService <IFlexiCodeBlockOptions, IFlexiCodeBlocksExtensionOptions> > mockOptionsService = _mockRepository.
                                                                                                                    Create <IOptionsService <IFlexiCodeBlockOptions, IFlexiCodeBlocksExtensionOptions> >();

            mockOptionsService.Setup(f => f.CreateOptions(dummyBlockProcessor)).
            Returns((mockFlexiCodeBlockOptions.Object, null));
            Mock <FlexiCodeBlockFactory> mockTestSubject = CreateMockFlexiCodeBlockFactory(mockOptionsService.Object);

            mockTestSubject.CallBase = true;
            mockTestSubject.Setup(t => t.ResolveBlockName(dummyBlockName)).Returns(dummyBlockName);
            mockTestSubject.Setup(t => t.ValidateSyntaxHighlighter(dummySyntaxHighlighter));
            mockTestSubject.Setup(t => t.TryCreateSortedLineRanges(dummyLineNumbers, dummyLines.Count)).Returns(dummyResolvedLineNumbers);
            mockTestSubject.Setup(t => t.ValidateSortedLineNumbers(It.Is <ReadOnlyCollection <NumberedLineRange> >(lineNumbers => lineNumbers == dummyResolvedLineNumbers), dummyLines.Count));
            mockTestSubject.Setup(t => t.TryCreateSortedLineRanges(dummyHighlightedLines, dummyLines.Count)).Returns(dummyResolvedHighlightedLines);
            mockTestSubject.
            Setup(t => t.ResolveHighlightedPhrases(dummyLines.ToString(), It.Is <ReadOnlyCollection <PhraseGroup> >(highlightedPhrases => highlightedPhrases == dummyHighlightedPhrases)))
            .Returns(dummyResolvedHighlightedPhrases);
            mockTestSubject.Setup(t => t.ValidateRenderingMode(dummyRenderingMode));

            // Act
            FlexiCodeBlock result = mockTestSubject.Object.CreateCore(dummyProxyLeafBlock, dummyBlockProcessor);

            // Assert
            _mockRepository.VerifyAll();
            Assert.Equal(dummyBlockName, result.BlockName);
            Assert.Equal(dummyTitle, result.Title);
            Assert.Equal(dummyCopyIcon, result.CopyIcon);
            Assert.Equal(dummyRenderHeader, result.RenderHeader);
            Assert.Equal(dummyLanguage, result.Language);
            Assert.Equal(dummyLines.ToString(), result.Code);
            Assert.Equal(dummyLines.Count, result.CodeNumLines);
            Assert.Equal(dummySyntaxHighlighter, result.SyntaxHighlighter);
            Assert.Same(dummyResolvedLineNumbers, result.LineNumbers);
            Assert.Same(dummyOmittedLinesIcon, result.OmittedLinesIcon);
            Assert.Same(dummyResolvedHighlightedLines, result.HighlightedLines);
            Assert.Same(dummyResolvedHighlightedPhrases, result.HighlightedPhrases);
            Assert.Equal(dummyRenderingMode, result.RenderingMode);
            Assert.Same(dummyAttributes, result.Attributes);
            Assert.Same(dummyBlockParser.Object, result.Parser);
            Assert.Equal(dummyLine, result.Line);
            Assert.Equal(dummyColumn, result.Column);
            Assert.Equal(dummySpan, result.Span);
        }