示例#1
0
        public void TestValidatorWithContext()
        {
            const string content         = @"# Title-1
# Title-2";
            const string expected        = @"<h1>Title-1</h1>
<h1>Title-2</h1>
";
            const string expectedMessage = "expected one title in one document.";
            string       message         = null;

            var context   = new Dictionary <string, object>();
            var validator = MarkdownObjectValidatorFactory.FromLambda <HeadingBlock>(
                block =>
            {
                if (block.Level == 1)
                {
                    if (context.TryGetValue("count", out object countObj) && countObj is int count)
                    {
                        context["count"] = count + 1;
                    }
                }
            },
                tree =>
            {
                context.Add("count", 0);
            },
                tree =>
            {
                if (context.TryGetValue("count", out object countObj) && countObj is int count)
                {
                    if (count != 1)
                    {
                        message = expectedMessage;
                    }
                }
            }
                );

            var rewriter = MarkdownObjectRewriterFactory.FromValidator(validator);
            var html     = Markup(content, rewriter, null);

            Assert.Equal(expected.Replace("\r\n", "\n"), html);
            Assert.Equal(expectedMessage, message);
        }
示例#2
0
        public void TestTokenValidator()
        {
            const string content         = "# Hello World";
            const string expected        = "<h1>Hello World</h1>\n";
            const string expectedMessage = "a space is expected after '#'";
            string       message         = null;

            var rewriter = MarkdownObjectRewriterFactory.FromValidator(
                MarkdownObjectValidatorFactory.FromLambda <HeadingBlock>(
                    block =>
            {
                if (!block.Lines.ToString().StartsWith("# "))
                {
                    message = expectedMessage;
                }
            })
                );

            var html = Markup(content, rewriter, null);

            Assert.Equal(expected.Replace("\r\n", "\n"), html);
            Assert.Equal(expectedMessage, message);
        }
示例#3
0
        public void TestGetSchemaName()
        {
            const string expectedSchemaName = "YamlMime:ModuleUnit";
            const string yamlFilename       = "moduleunit.yml";
            const string yamlContent        = @"### YamlMime:ModuleUnit
uid: learn.azure.introduction";

            File.WriteAllText(yamlFilename, yamlContent);
            InclusionContext.PushFile(yamlFilename);
            InclusionContext.PushInclusion("introduction-included.md");

            string schemaName = string.Empty;

            var rewriter = MarkdownObjectRewriterFactory.FromValidator(
                MarkdownObjectValidatorFactory.FromLambda <MarkdownDocument>(
                    root =>
            {
                schemaName = root.GetData("SchemaName")?.ToString();
            })
                );
            var html = Markup("# Hello World", rewriter, null);

            Assert.Equal(expectedSchemaName, schemaName);
        }