Exemple #1
0
        public async Task ParseParameters_EscapedInclude()
        {
            var mock = new Mock <IDynamicContentTransformer>();

            mock.Setup(m => m.Transform(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <ContextModel>()))
            .Returns(new Func <string, string, IContextModel, Task <string> >(
                         (c, k, m) => Task.FromResult("---")));

            var handler = new IncludesHandler(mock.Object, new Mock <IIncludesHandlerExtension>().Object);

            var s = new Site("", "", new PageMock("", ""), null);

            s.Includes.Add(new TemplateMock("include", ""));

            var res1 = await handler.ResolveAll("\\{% include %}", s, s.MainPage, "");

            var res2 = await handler.ResolveAll("\\\\{% include %}", s, s.MainPage, "");

            var res3 = await handler.ResolveAll("\\\\\\{% include %}", s, s.MainPage, "");

            var res4 = await handler.ResolveAll("abc \\{% include %}xyz", s, s.MainPage, "");

            Assert.AreEqual("{% include %}", res1);
            Assert.AreEqual("\\\\---", res2);
            Assert.AreEqual("\\\\\\---", res3);
            Assert.AreEqual("abc {% include %}xyz", res4);
        }
Exemple #2
0
        public void Setup()
        {
            string GetPageName(IContextPage page)
            {
                var name = page.Url.Split("/", StringSplitOptions.RemoveEmptyEntries).LastOrDefault();

                if (string.IsNullOrEmpty(name))
                {
                    name = "index";
                }
                name += ".html";

                return(name);
            }

            var mock = new Mock <IDynamicContentTransformer>();

            mock.Setup(m => m.Transform(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <ContextModel>()))
            .Returns(new Func <string, string, IContextModel, Task <string> >(
                         (c, k, m) => Task.FromResult(
                             $"{c}_{GetPageName((m as ContextModel).Page)}_{string.Join(",", m.Data.OrderBy(p => p.Key).Select(p => $"{p.Key}={p.Value}").ToArray())}")));

            var incExt = new Mock <IIncludesHandlerExtension>();

            incExt.Setup(m => m.ResolveInclude(It.IsAny <string>(), It.IsAny <IMetadata>(), It.IsAny <IPage>()))
            .Returns((string c, IMetadata m, IPage p) => throw new MissingIncludeException(c));

            m_Handler = new IncludesHandler(mock.Object, incExt.Object);
        }
Exemple #3
0
        public async Task ParseParameters_MultipleLine()
        {
            IContextMetadata data = null;

            var mock = new Mock <IDynamicContentTransformer>();

            mock.Setup(m => m.Transform(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <ContextModel>()))
            .Returns(new Func <string, string, IContextModel, Task <string> >(
                         (c, k, m) =>
            {
                data = m.Data;
                return(Task.FromResult(""));
            }));

            var handler = new IncludesHandler(mock.Object, new Mock <IIncludesHandlerExtension>().Object);

            var s = new Site("", "", new PageMock("", ""), null);

            s.Includes.Add(new TemplateMock("include", ""));

            await handler.ResolveAll("{% include a1: A\r\na2: B\r\na3:\r\n    - X\r\n    - Y %}", s, s.MainPage, "");

            Assert.AreEqual(3, data.Count);
            Assert.AreEqual("A", data["a1"]);
            Assert.AreEqual("B", data["a2"]);
            Assert.AreEqual(2, (data["a3"] as List <object>).Count);
            Assert.AreEqual("X", (data["a3"] as List <object>)[0]);
            Assert.AreEqual("Y", (data["a3"] as List <object>)[1]);
        }
Exemple #4
0
        public async Task Render_PluginIncludes()
        {
            var extMock = new Mock <IIncludesHandlerExtension>();

            extMock.Setup(m => m.ResolveInclude(It.IsAny <string>(), It.IsAny <IMetadata>(), It.IsAny <IPage>()))
            .Returns((string i, IMetadata m, IPage p) => Task.FromResult($"_{i}_render-result"));

            var includesHandler = new IncludesHandler(new Mock <IDynamicContentTransformer>().Object,
                                                      extMock.Object);

            var p1 = new PageMock("", "{% plugin-include { param1: x, param2: b} %}");
            var s  = new Site("", "", p1, null);

            var res = await includesHandler.ResolveAll("{% plugin-include %}", s, p1, "/page1/");

            Assert.AreEqual("_plugin-include_render-result", res);
        }
Exemple #5
0
        public async Task ParseParameters_SingleLine()
        {
            IContextMetadata data = null;

            var mock = new Mock <IDynamicContentTransformer>();

            mock.Setup(m => m.Transform(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <ContextModel>()))
            .Returns(new Func <string, string, IContextModel, Task <string> >(
                         (c, k, m) =>
            {
                data = m.Data;
                return(Task.FromResult(""));
            }));

            var handler = new IncludesHandler(mock.Object, new Mock <IIncludesHandlerExtension>().Object);

            var s = new Site("", "", new PageMock("", ""), null);

            s.Includes.Add(new TemplateMock("include", ""));

            IContextMetadata p1, p2, p3;

            await handler.ResolveAll("{% include a1: A %}", s, s.MainPage, "");

            p1 = data;

            await handler.ResolveAll("{%  include  a1: A %}", s, s.MainPage, "");

            p2 = data;

            await handler.ResolveAll("{% include { a1: A, a2: 0.2 } %}", s, s.MainPage, "");

            p3 = data;

            Assert.AreEqual(1, p1.Count);
            Assert.AreEqual("A", p1["a1"]);

            Assert.AreEqual(1, p2.Count);
            Assert.AreEqual("A", p2["a1"]);

            Assert.AreEqual(2, p3.Count);
            Assert.AreEqual("A", p3["a1"]);
            Assert.AreEqual("0.2", p3["a2"]);
        }
Exemple #6
0
        public async Task ParseParameters_NoParameters()
        {
            IContextMetadata data = null;

            var mock = new Mock <IDynamicContentTransformer>();

            mock.Setup(m => m.Transform(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <ContextModel>()))
            .Returns(new Func <string, string, IContextModel, Task <string> >(
                         (c, k, m) =>
            {
                data = m.Data;
                return(Task.FromResult(""));
            }));

            var handler = new IncludesHandler(mock.Object, new Mock <IIncludesHandlerExtension>().Object);

            var s = new Site("", "", new PageMock("", ""), null);

            s.Includes.Add(new TemplateMock("include", ""));

            await handler.ResolveAll("{% include %}", s, s.MainPage, "");

            Assert.AreEqual(0, data.Count);
        }