Exemplo n.º 1
0
        public async Task Ensure_PrerenderCallbacks_Are_Invoked()
        {
            //Assign
            var page = TemplatePageTest.CreatePage(t => t.Write("test"));

            bool triggered1 = false, triggered2 = false;
            var  callbacks = new List <Action <ITemplatePage> >
            {
                t => triggered1 = true,
                t => triggered2 = true
            };

            var options = new RazorLightOptions {
                PreRenderCallbacks = callbacks
            };
            var engineMock = new Mock <IEngineHandler>();

            engineMock.SetupGet(e => e.Options).Returns(options);

            //Act
            var templateRenderer = new TemplateRenderer(engineMock.Object, HtmlEncoder.Default, new MemoryPoolViewBufferScope());
            await templateRenderer.RenderAsync(page);

            //Assert
            Assert.True(triggered1);
            Assert.True(triggered2);
        }
Exemplo n.º 2
0
        public void Ensure_Callbacks_Exeuted_In_Correct_Order()
        {
            var page            = TemplatePageTest.CreatePage(v => { });
            var callbackRecords = new List <string>();

            var context = new PageContext();

            context.PrerenderCallbacks.Add((t) =>
            {
                callbackRecords.Add("PAGE_SPECIFIC_CALLBACK");
            });

            page.PageContext = context;

            var renderer = new PageRenderer(page, CreateLookupForPageInstance(page));

            renderer.PreRenderCallbacks.Add(new Action <TemplatePage>((t) =>
            {
                callbackRecords.Add("GLOBAL_CALLBACK");
            }));

            using (var writer = new StringWriter())
            {
                page.PageContext.Writer = writer;
                renderer.RenderAsync(context).Wait();
            }

            Assert.True(callbackRecords.Count == 2);
            Assert.Equal(callbackRecords[0], "PAGE_SPECIFIC_CALLBACK");
            Assert.Equal(callbackRecords[1], "GLOBAL_CALLBACK");
        }
Exemplo n.º 3
0
        public async Task Template_Shares_Context_With_Layout()
        {
            var encoder = new TemplatePageTest.HtmlTestEncoder();

            string expected = encoder.Encode("Begin Layout") +
                              encoder.Encode("Hello") +
                              encoder.Encode("Begin") +
                              encoder.Encode("Hello") +
                              encoder.Encode("End") +
                              encoder.Encode("End Layout");

            var layout = TemplatePageTest.CreatePage(v =>
            {
                v.Write("Begin Layout");
                v.Write(v.ViewBag.Title);
                v.Write(v.RenderBodyPublic());
                v.Write("End Layout");
            });

            var page = TemplatePageTest.CreatePage(v =>
            {
                v.Write("Begin");
                v.Write(v.ViewBag.Title);
                v.Write("End");
            });

            var engineMock = new Mock <IEngineHandler>();

            engineMock.Setup(t => t.CompileTemplateAsync(It.IsAny <string>()))
            .Returns(new Func <Task <ITemplatePage> >(() => { return(Task.FromResult((ITemplatePage)layout)); }));

            engineMock.SetupGet(t => t.Options).Returns(new RazorLightOptions());

            dynamic viewbag = new ExpandoObject();

            viewbag.Title = "Hello";
            var context = new PageContext(viewbag);

            page.Layout      = "_";
            page.PageContext = context;

            string output;

            using (var writer = new StringWriter())
            {
                page.PageContext.Writer = writer;
                var renderer = new TemplateRenderer(engineMock.Object, encoder, new MemoryPoolViewBufferScope());
                await renderer.RenderAsync(page);

                output = writer.ToString();
            }

            Assert.Equal(expected, output, StringComparer.Ordinal);
        }
Exemplo n.º 4
0
        public void Template_Shares_Context_With_Layout()
        {
            string expected = "Begin Layout" +
                              "Hello" +
                              "Begin" +
                              "Hello" +
                              "End" +
                              "End Layout";

            var layout = TemplatePageTest.CreatePage(v =>
            {
                v.Write("Begin Layout");
                v.Write(v.ViewBag.Title);
                v.Write(v.RenderBodyPublic());
                v.Write("End Layout");
            });

            var page = TemplatePageTest.CreatePage(v =>
            {
                v.Write("Begin");
                v.Write(v.ViewBag.Title);
                v.Write("End");
            });

            var lookup = new Mock <IPageLookup>();

            lookup.Setup(p => p.GetPage(It.IsAny <string>()))
            .Returns(new PageLookupResult(
                         new PageLookupItem(It.IsAny <string>(), () => layout), new List <PageLookupItem>()));

            dynamic viewbag = new ExpandoObject();

            viewbag.Title = "Hello";
            var context = new PageContext(viewbag);

            page.Layout      = "_";
            page.PageContext = context;

            string output;

            using (var writer = new StringWriter())
            {
                page.PageContext.Writer = writer;
                var renderer = new PageRenderer(page, lookup.Object);
                renderer.RenderAsync(context).Wait();

                output = writer.ToString();
            }

            Assert.Equal(expected, output, StringComparer.Ordinal);
        }