Ejemplo n.º 1
0
    void UseSnippetExtractor()
    {
    // get files containing snippets
    var filesToParse = Directory.EnumerateFiles(@"C:\path", "*.*", SearchOption.AllDirectories)
        .Where(s => s.EndsWith(".vm") || s.EndsWith(".cs"));

    // setup version convention and extract snippets from files
    var snippetExtractor = new SnippetExtractor(InferVersion);
    var readSnippets = snippetExtractor.FromFiles(filesToParse);

    // Grouping
    var snippetGroups = SnippetGrouper.Group(readSnippets)
        .ToList();

    // Merge with some markdown text
    var markdownProcessor = new MarkdownProcessor();

    //In this case the text will be extracted from a file path
    ProcessResult result;
    using (var reader = File.OpenText(@"C:\path\myInputMarkdownFile.md"))
    using (var writer = File.CreateText(@"C:\path\myOutputMarkdownFile.md"))
    {
        result = markdownProcessor.Apply(snippetGroups, reader, writer);
    }

    // List of all snippets that the markdown file expected but did not exist in the input snippets 
    var missingSnippets = result.MissingSnippets;

    // List of all snippets that the markdown file used
    var usedSnippets = result.UsedSnippets;

    }
    void Run(string folder, string input, string expectedOutput)
    {
        var extractor = new SnippetExtractor(s => VersionRange.All);
        var snippets = extractor.FromFiles(Directory.EnumerateFiles(folder, "code.cs"));

        var snippetGroups = SnippetGrouper.Group(snippets)
            .ToList();

        using (var reader = File.OpenText(input))
        {
            var markdownProcessor = new MarkdownProcessor(alsoParseImportSyntax:true);
            var stringBuilder = new StringBuilder();
            using (var writer = new StringWriter(stringBuilder))
            {
                markdownProcessor.Apply(snippetGroups, reader, writer);
            }

            var expected = File.ReadAllText(expectedOutput).FixNewLines();
            var fixNewLines = stringBuilder
                .ToString()
                .FixNewLines()
                .TrimTrailingNewLine();
            Assert.AreEqual(expected, fixNewLines, folder);
        }
    }
 static void Verify(string markdownContent, List<SnippetGroup> availableSnippets)
 {
     var processor = new MarkdownProcessor();
     var stringBuilder = new StringBuilder();
     using (var reader = new StringReader(markdownContent))
     using (var writer = new StringWriter(stringBuilder))
     {
         var processResult = processor.Apply(availableSnippets, reader, writer);
         var output = new object[]
         {
             processResult.MissingSnippets, processResult.UsedSnippets, stringBuilder.ToString()
         };
         ObjectApprover.VerifyWithJson(output, s => s.Replace("\\r\\n", "\r\n"));
     }
 }