public void DoesNotRenderLinkIfNoImage()
            {
                // Given
                TestExecutionContext context  = new TestExecutionContext();
                TestDocument         document = new TestDocument();

                KeyValuePair <string, string>[] args = new KeyValuePair <string, string>[]
                {
                    new KeyValuePair <string, string>("Link", "/c/d"),
                    new KeyValuePair <string, string>("Target", "abc"),
                    new KeyValuePair <string, string>("Rel", "def"),
                    new KeyValuePair <string, string>("Alt", "ghi"),
                    new KeyValuePair <string, string>("Class", "jkl"),
                    new KeyValuePair <string, string>("Height", "100px"),
                    new KeyValuePair <string, string>("Width", "200px")
                };
                FigureShortcode shortcode = new FigureShortcode();

                // When
                ShortcodeResult result = shortcode.Execute(args, "foo bar", document, context);

                // Then
                result.ContentProvider.GetStream().ReadToEnd().ShouldBe(
                    @"<figure class=""jkl"">
  <figcaption>foo bar</figcaption>
</figure>",
                    StringCompareShould.IgnoreLineEndings);
            }
            public async Task IncludesFile()
            {
                // Given
                TestFileProvider fileProvider = new TestFileProvider();

                fileProvider.AddDirectory("/");
                fileProvider.AddDirectory("/A");
                fileProvider.AddDirectory("/A/B");
                fileProvider.AddFile("/A/B/c.txt", "foo");
                TestFileSystem fileSystem = new TestFileSystem
                {
                    FileProvider = fileProvider
                };

                fileSystem.InputPaths.Clear();
                fileSystem.InputPaths.Add("/A");

                TestExecutionContext context = new TestExecutionContext
                {
                    FileSystem = fileSystem
                };
                TestDocument document = new TestDocument();

                KeyValuePair <string, string>[] args = new KeyValuePair <string, string>[]
                {
                    new KeyValuePair <string, string>(null, "B/c.txt")
                };
                IncludeShortcode shortcode = new IncludeShortcode();

                // When
                ShortcodeResult result = await shortcode.ExecuteAsync(args, string.Empty, document, context);

                // Then
                result.ContentProvider.GetStream().ReadToEnd().ShouldBe("foo");
            }
            public void RendersFigureWithoutLink()
            {
                // Given
                TestExecutionContext context  = new TestExecutionContext();
                TestDocument         document = new TestDocument();

                KeyValuePair <string, string>[] args = new KeyValuePair <string, string>[]
                {
                    new KeyValuePair <string, string>("Src", "/a/b"),
                    new KeyValuePair <string, string>("Alt", "ghi"),
                    new KeyValuePair <string, string>("Class", "jkl"),
                    new KeyValuePair <string, string>("Height", "100px"),
                    new KeyValuePair <string, string>("Width", "200px")
                };
                FigureShortcode shortcode = new FigureShortcode();

                // When
                ShortcodeResult result = shortcode.Execute(args, "foo bar", document, context);

                // Then
                result.ContentProvider.GetStream().ReadToEnd().ShouldBe(
                    @"<figure class=""jkl"">
  <img src=""/a/b"" alt=""ghi"" height=""100px"" width=""200px"" />
  <figcaption>foo bar</figcaption>
</figure>",
                    StringCompareShould.IgnoreLineEndings);
            }
            public async Task IncludesWebResource()
            {
                // Given
                TestExecutionContext context = new TestExecutionContext
                {
                    Engine = new TestEngine
                    {
                        HttpResponseFunc = (_, __) => new HttpResponseMessage
                        {
                            StatusCode = HttpStatusCode.OK,
                            Content    = new StringContent("Hello from the other side.")
                        }
                    }
                };
                TestDocument document = new TestDocument();

                KeyValuePair <string, string>[] args = new KeyValuePair <string, string>[]
                {
                    new KeyValuePair <string, string>(null, "http://foo.com/bar")
                };
                IncludeShortcode shortcode = new IncludeShortcode();

                // When
                ShortcodeResult result = await shortcode.ExecuteAsync(args, string.Empty, document, context);

                // Then
                result.ContentProvider.GetStream().ReadToEnd().ShouldBe("Hello from the other side.");
            }
Пример #5
0
    public override async Task <ShortcodeResult> ExecuteAsync(KeyValuePair <string, string>[] args, string content,
                                                              IDocument document, IExecutionContext context)
    {
        var props      = args.ToDictionary(Solution, Project, Symbol, BodyOnly);
        var symbolName = props.GetString(Symbol);
        var bodyOnly   = props.Get <bool?>(BodyOnly) ?? symbolName.StartsWith("m:", StringComparison.InvariantCultureIgnoreCase);

        if (!context.TryGetCommentIdDocument(symbolName, out var apiDocument, out _))
        {
            return(string.Empty);
        }

        var options = HighlightService.HighlightOption.All;

        if (bodyOnly)
        {
            options = HighlightService.HighlightOption.Body;
        }

        var comp             = apiDocument.Get <Compilation>(CodeAnalysisKeys.Compilation);
        var symbol           = apiDocument.Get <ISymbol>(CodeAnalysisKeys.Symbol);
        var highlightElement = await HighlightService.Highlight(comp, symbol, options);

        ShortcodeResult shortcodeResult = $"<pre><code>{highlightElement}</code></pre>";

        return(shortcodeResult);
    }
Пример #6
0
            public void RendersTableWithoutSettings()
            {
                // Given
                TestExecutionContext context  = new TestExecutionContext();
                TestDocument         document = new TestDocument();
                string content = @"
1 2 ""3 4""
a ""b c"" d
e f g
5 678
""h i""  j ""k""
l=m nop
";

                KeyValuePair <string, string>[] args = new KeyValuePair <string, string>[] { };
                TableShortcode shortcode             = new TableShortcode();

                // When
                ShortcodeResult result = shortcode.Execute(args, content, document, context);

                // Then
                result.ContentProvider.GetStream().ReadToEnd().ShouldBe(
                    @"<table>
  <tbody>
    <tr>
      <td>1</td>
      <td>2</td>
      <td>3 4</td>
    </tr>
    <tr>
      <td>a</td>
      <td>b c</td>
      <td>d</td>
    </tr>
    <tr>
      <td>e</td>
      <td>f</td>
      <td>g</td>
    </tr>
    <tr>
      <td>5</td>
      <td>678</td>
    </tr>
    <tr>
      <td>h i</td>
      <td>j</td>
      <td>k</td>
    </tr>
    <tr>
      <td>l=m</td>
      <td>nop</td>
    </tr>
  </tbody>
</table>",
                    StringCompareShould.IgnoreLineEndings);
            }
            public async Task EvaluatesExpression()
            {
                TestExecutionContext context = new TestExecutionContext();

                context.ScriptHelper = new ScriptHelper(context);
                TestDocument  document         = new TestDocument();
                EvalShortcode shortcode        = new EvalShortcode();
                string        shortcodeContent = "1 + 2";

                // When
                ShortcodeResult result = await shortcode.ExecuteAsync(null, shortcodeContent, document, context);

                // Then
                result.ContentProvider.GetStream().ReadToEnd().ShouldBe("3");
            }
            public void ReturnsNullIfMissingKey()
            {
                // Given
                TestExecutionContext context  = new TestExecutionContext();
                TestDocument         document = new TestDocument();

                KeyValuePair <string, string>[] args = new KeyValuePair <string, string>[]
                {
                    new KeyValuePair <string, string>("Key", "Foo")
                };
                IfShortcode shortcode = new IfShortcode();

                // When
                ShortcodeResult result = shortcode.Execute(args, "Fizzbuzz", document, context);

                // Then
                result.ShouldBeNull();
            }
Пример #9
0
            public void RendersLink(string path, string expected)
            {
                // Given
                TestExecutionContext context  = new TestExecutionContext();
                TestDocument         document = new TestDocument();

                KeyValuePair <string, string>[] args = new KeyValuePair <string, string>[]
                {
                    new KeyValuePair <string, string>("Path", path)
                };
                LinkShortcode shortcode = new LinkShortcode();

                // When
                ShortcodeResult result = shortcode.Execute(args, string.Empty, document, context);

                // Then
                result.ContentProvider.GetStream().ReadToEnd().ShouldBe(expected);
            }
            public async Task CanAccessMetadataAsProperties()
            {
                TestExecutionContext context = new TestExecutionContext();

                context.ScriptHelper = new ScriptHelper(context);
                TestDocument document = new TestDocument
                {
                    { "Foo", 4 }
                };
                EvalShortcode shortcode        = new EvalShortcode();
                string        shortcodeContent = "return 1 + (int)Foo;";

                // When
                ShortcodeResult result = await shortcode.ExecuteAsync(null, shortcodeContent, document, context);

                // Then
                result.ContentProvider.GetStream().ReadToEnd().ShouldBe("5");
            }
Пример #11
0
            public void RendersGistWithoutFile()
            {
                TestExecutionContext context  = new TestExecutionContext();
                TestDocument         document = new TestDocument();

                KeyValuePair <string, string>[] args = new KeyValuePair <string, string>[]
                {
                    new KeyValuePair <string, string>(null, "abc"),
                    new KeyValuePair <string, string>(null, "def")
                };
                GistShortcode shortcode = new GistShortcode();

                // When
                ShortcodeResult result = shortcode.Execute(args, null, document, context);

                // Then
                result.ContentProvider.GetStream().ReadToEnd().ShouldBe("<script src=\"//gist.github.com/def/abc.js\" type=\"text/javascript\"></script>");
            }
            public void ReturnsNullIfCanNotConvertToBool()
            {
                // Given
                TestExecutionContext context  = new TestExecutionContext();
                TestDocument         document = new TestDocument(new MetadataItems
                {
                    { "Foo", "abc" }
                });

                KeyValuePair <string, string>[] args = new KeyValuePair <string, string>[]
                {
                    new KeyValuePair <string, string>("Key", "Foo")
                };
                IfShortcode shortcode = new IfShortcode();

                // When
                ShortcodeResult result = shortcode.Execute(args, "Fizzbuzz", document, context);

                // Then
                result.ShouldBeNull();
            }
            public void ReturnsDocumentIfConvertedTrue()
            {
                // Given
                TestExecutionContext context  = new TestExecutionContext();
                TestDocument         document = new TestDocument(new MetadataItems
                {
                    { "Foo", "true" }
                });

                KeyValuePair <string, string>[] args = new KeyValuePair <string, string>[]
                {
                    new KeyValuePair <string, string>("Key", "Foo")
                };
                IfShortcode shortcode = new IfShortcode();

                // When
                ShortcodeResult result = shortcode.Execute(args, "Fizzbuzz", document, context);

                // Then
                result.ContentProvider.GetStream().ReadToEnd().ShouldBe("Fizzbuzz");
            }
Пример #14
0
            public void EmptyForMissingMetadata()
            {
                // Given
                TestExecutionContext context  = new TestExecutionContext();
                TestDocument         document = new TestDocument(new MetadataItems
                {
                    { "Foo", "Bar" }
                });

                KeyValuePair <string, string>[] args = new KeyValuePair <string, string>[]
                {
                    new KeyValuePair <string, string>(null, "Fizz")
                };
                MetaShortcode shortcode = new MetaShortcode();

                // When
                ShortcodeResult result = shortcode.Execute(args, string.Empty, document, context);

                // Then
                result.ShouldBeNull();
            }
Пример #15
0
            public void RendersMetadata()
            {
                // Given
                TestExecutionContext context  = new TestExecutionContext();
                TestDocument         document = new TestDocument(new MetadataItems
                {
                    { "Foo", "Bar" }
                });

                KeyValuePair <string, string>[] args = new KeyValuePair <string, string>[]
                {
                    new KeyValuePair <string, string>(null, "Foo")
                };
                MetaShortcode shortcode = new MetaShortcode();

                // When
                ShortcodeResult result = shortcode.Execute(args, string.Empty, document, context);

                // Then
                result.ContentProvider.GetStream().ReadToEnd().ShouldBe("Bar");
            }
Пример #16
0
            public void RendersLinkWithAlternateHost(string path, string expected)
            {
                // Given
                TestExecutionContext context = new TestExecutionContext();

                context.Settings[Keys.Host] = "domain.com";
                TestDocument document = new TestDocument();

                KeyValuePair <string, string>[] args = new KeyValuePair <string, string>[]
                {
                    new KeyValuePair <string, string>("Path", path),
                    new KeyValuePair <string, string>("Host", "google.com")
                };
                LinkShortcode shortcode = new LinkShortcode();

                // When
                ShortcodeResult result = shortcode.Execute(args, string.Empty, document, context);

                // Then
                result.ContentProvider.GetStream().ReadToEnd().ShouldBe(expected);
            }
Пример #17
0
            public void RendersMarkdown()
            {
                // Given
                const string         content  = @"Line 1
*Line 2*
# Line 3";
                TestExecutionContext context  = new TestExecutionContext();
                TestDocument         document = new TestDocument();

                KeyValuePair <string, string>[] args = new KeyValuePair <string, string>[] { };
                MarkdownShortcode shortcode          = new MarkdownShortcode();

                // When
                ShortcodeResult result = shortcode.Execute(args, content, document, context);

                // Then
                result.ContentProvider.GetStream().ReadToEnd().ShouldBe(
                    @"<p>Line 1
<em>Line 2</em></p>
<h1>Line 3</h1>
",
                    StringCompareShould.IgnoreLineEndings);
            }
            public async Task NullResultIfFileDoesNotExist()
            {
                // Given
                TestFileProvider fileProvider = new TestFileProvider();

                fileProvider.AddDirectory("/");
                fileProvider.AddDirectory("/A");
                fileProvider.AddDirectory("/A/B");
                fileProvider.AddFile("/A/B/c.txt", "foo");
                TestFileSystem fileSystem = new TestFileSystem
                {
                    FileProvider = fileProvider
                };

                fileSystem.InputPaths.Clear();
                fileSystem.InputPaths.Add("/A");

                TestExecutionContext context = new TestExecutionContext
                {
                    FileSystem = fileSystem
                };

                context.TestLoggerProvider.ThrowLogLevel = LogLevel.Error;
                TestDocument document = new TestDocument();

                KeyValuePair <string, string>[] args = new KeyValuePair <string, string>[]
                {
                    new KeyValuePair <string, string>(null, "B/d.txt")
                };
                IncludeShortcode shortcode = new IncludeShortcode();

                // When
                ShortcodeResult result = await shortcode.ExecuteAsync(args, string.Empty, document, context);

                // Then
                result.ShouldBeNull();
            }
            public void ReturnsDocumentIfScriptEqual()
            {
                // Given
                TestExecutionContext context = new TestExecutionContext();

                context.ScriptHelper = new ScriptHelper(context);
                TestDocument document = new TestDocument()
                {
                    { "Foo", 122 }
                };

                KeyValuePair <string, string>[] args = new KeyValuePair <string, string>[]
                {
                    new KeyValuePair <string, string>("Key", "=> (int)Foo + 1"),
                    new KeyValuePair <string, string>("Value", "123")
                };
                IfShortcode shortcode = new IfShortcode();

                // When
                ShortcodeResult result = shortcode.Execute(args, "Fizzbuzz", document, context);

                // Then
                result.ContentProvider.GetStream().ReadToEnd().ShouldBe("Fizzbuzz");
            }
Пример #20
0
            public void RendersTableWithSettings()
            {
                // Given
                TestExecutionContext context  = new TestExecutionContext();
                TestDocument         document = new TestDocument();
                string content = @"
1 2 ""3 4""
a ""b c"" d
e f g
5 678
""h i""  j ""k""
l=m nop
";

                KeyValuePair <string, string>[] args = new KeyValuePair <string, string>[]
                {
                    new KeyValuePair <string, string>("Class", "tclass"),
                    new KeyValuePair <string, string>("HeaderRows", "1"),
                    new KeyValuePair <string, string>("FooterRows", "2"),
                    new KeyValuePair <string, string>("HeaderCols", "1"),
                    new KeyValuePair <string, string>("HeaderClass", "hclass"),
                    new KeyValuePair <string, string>("BodyClass", "bclass"),
                    new KeyValuePair <string, string>("FooterClass", "fclass")
                };
                TableShortcode shortcode = new TableShortcode();

                // When
                ShortcodeResult result = shortcode.Execute(args, content, document, context);

                // Then
                result.ContentProvider.GetStream().ReadToEnd().ShouldBe(
                    @"<table class=""tclass"">
  <thead class=""hclass"">
    <tr>
      <th>1</th>
      <th>2</th>
      <th>3 4</th>
    </tr>
  </thead>
  <tbody class=""bclass"">
    <tr>
      <th>a</th>
      <td>b c</td>
      <td>d</td>
    </tr>
    <tr>
      <th>e</th>
      <td>f</td>
      <td>g</td>
    </tr>
    <tr>
      <th>5</th>
      <td>678</td>
    </tr>
  </tbody>
  <tfoot class=""fclass"">
    <tr>
      <th>h i</th>
      <td>j</td>
      <td>k</td>
    </tr>
    <tr>
      <th>l=m</th>
      <td>nop</td>
    </tr>
  </tfoot>
</table>",
                    StringCompareShould.IgnoreLineEndings);
            }