Beispiel #1
0
        public void TemplateDoesNotRunThroughCacheHit()
        {
            _viewFolder.Add("home\\index.spark", @"
<viewdata model=""System.Func<string>""/>
<div>
<cache key='string.Empty'>
<p>${ViewData.Model()}</p>
</cache>
</div>");
            int calls = 0;
            var data  = new StubViewData <Func <string> >
            {
                Model = () => (++calls).ToString()
            };

            var contents = Render("index", data);

            Assert.That(contents, Is.EqualTo(@"
<div>
<p>1</p>
</div>"));
            Assert.That(calls, Is.EqualTo(1));

            contents = Render("index", data);
            Assert.That(contents, Is.EqualTo(@"
<div>
<p>1</p>
</div>"));
            Assert.That(calls, Is.EqualTo(1));
        }
Beispiel #2
0
        public void CacheInMacroShouldActAsSameSite()
        {
            _viewFolder.Add("home\\index.spark", @"
<viewdata model=""System.Func<string>""/>
<macro name=""foo"">
<cache><p>${ViewData.Model()}</p></cache>
</macro>
<div>
${foo()}
${foo()}
</div>");

            int calls = 0;
            var data  = new StubViewData <Func <string> >
            {
                Model = () => (++calls).ToString()
            };

            var contents = Render("index", data);

            Assert.That(contents, Contains.InOrder(
                            "<p>1</p>",
                            "<p>1</p>"));

            Assert.That(calls, Is.EqualTo(1));

            contents = Render("index", data);
            Assert.That(contents, Contains.InOrder(
                            "<p>1</p>",
                            "<p>1</p>"));

            Assert.That(calls, Is.EqualTo(1));
        }
Beispiel #3
0
        public void CommaCreatesMultiPartKey()
        {
            _viewFolder.Add("home\\index.spark",
                            @"
<viewdata model=""System.Func<string>""/>
<for each='var x in new[]{1,2,3,1,2,3}'>
<p cache='x,xIndex'>${x}:${ViewData.Model()}</p>
</for>");

            var calls = 0;
            var data  = new StubViewData <Func <string> >
            {
                Model = () => (++calls).ToString()
            };

            var contents = Render("index", data);

            Assert.That(contents, Contains.InOrder(
                            "<p>1:1</p>",
                            "<p>2:2</p>",
                            "<p>3:3</p>",
                            "<p>1:4</p>",
                            "<p>2:5</p>",
                            "<p>3:6</p>"));

            Assert.That(_cacheService.AllKeys.Count(x => x.Substring(32) == "1\u001f0"), Is.EqualTo(1));
            Assert.That(_cacheService.AllKeys.Count(x => x.Substring(32) == "2\u001f1"), Is.EqualTo(1));
            Assert.That(_cacheService.AllKeys.Count(x => x.Substring(32) == "3\u001f2"), Is.EqualTo(1));
            Assert.That(_cacheService.AllKeys.Count(x => x.Substring(32) == "1\u001f3"), Is.EqualTo(1));
            Assert.That(_cacheService.AllKeys.Count(x => x.Substring(32) == "2\u001f4"), Is.EqualTo(1));
            Assert.That(_cacheService.AllKeys.Count(x => x.Substring(32) == "3\u001f5"), Is.EqualTo(1));
        }
Beispiel #4
0
        public void MarkupBasedMacros()
        {
            var data = new StubViewData
            {
                { "username", "Bob" },
                { "comments", new[] {
                      new Comment {
                          Text = "Alpha"
                      },
                      new Comment {
                          Text = "Beta"
                      },
                      new Comment {
                          Text = "Gamma"
                      }
                  } }
            };

            mocks.ReplayAll();
            var viewContext = MakeViewContext("macros", null, data);

            factory.RenderView(viewContext);
            mocks.VerifyAll();
            string content = sb.ToString();

            Assert.That(content, Contains.InOrder(
                            "<p>Bob</p>", "<p>Alpha</p>",
                            "<p>Bob</p>", "<p>Beta</p>",
                            "<p>Bob</p>", "<p>Gamma</p>",
                            "<span class=\"yadda\">Rating: 5</span>"));
        }
Beispiel #5
0
        public void OutputWhileNamedContentActiveShouldAppearOnceAtCorrectTarget()
        {
            _viewFolder.Add("home\\index.spark", @"
<viewdata model=""System.Func<string>""/>
<ul>
<content name='foo'>
<li>${ViewData.Model()}[1]</li>
</content>
<li>${ViewData.Model()}[2]</li>
<content name='foo'>
<cache>
<li>${ViewData.Model()}[3]c</li>
<content name='foo'>
hana
</content>
<li>${ViewData.Model()}[4]c</li>
</cache>
</content>
<li>${ViewData.Model()}[5]</li>
<content name='foo'>
<li>${ViewData.Model()}[6]</li>
</content>
<use content='foo'/>
</ul>");

            var calls = 0;
            var data  = new StubViewData <Func <string> >
            {
                Model = () => (++calls).ToString()
            };

            var contents = Render("index", data);

            Assert.That(calls, Is.EqualTo(6));
            Assert.That(contents, Is.EqualTo(@"
<ul>
<li>2[2]</li>
<li>5[5]</li>
<li>1[1]</li>
<li>3[3]c</li>
hana
<li>4[4]c</li>
<li>6[6]</li>
</ul>"));


            contents = Render("index", data);
            Assert.That(calls, Is.EqualTo(10));
            Assert.That(contents, Is.EqualTo(@"
<ul>
<li>8[2]</li>
<li>9[5]</li>
<li>7[1]</li>
<li>3[3]c</li>
hana
<li>4[4]c</li>
<li>10[6]</li>
</ul>"));
        }
Beispiel #6
0
 public void AddViewDataDifferentTypes()
 {
     mocks.ReplayAll();
     var viewData = new StubViewData { { "comment", new Comment { Text = "Hello world" } } };
     var viewContext = MakeViewContext("addviewdatadifferenttypes", null, viewData);
     Assert.That(() => factory.RenderView(viewContext), Throws.TypeOf<CompilerException>());
     mocks.VerifyAll();
 }
 public void AddViewDataDifferentTypes()
 {
     mocks.ReplayAll();
     var viewData = new StubViewData { { "comment", new Comment { Text = "Hello world" } } };
     var viewContext = MakeViewContext("addviewdatadifferenttypes", null, viewData);
     factory.RenderView(viewContext);
     mocks.VerifyAll();
 }
 private string ExecuteView(StubViewData viewData)
 {
     var view = (StubSparkView)Activator.CreateInstance(_compiler.CompiledType);
     _languageFactory.InstanceCreated(_compiler, view);
     view.ViewData = viewData;
     var contents = new StringWriter();
     view.RenderView(contents);
     _languageFactory.InstanceReleased(_compiler, view);
     return contents.ToString();
 }
Beispiel #9
0
        private string Render(string viewName, StubViewData viewData)
        {
            var context = new StubViewContext()
            {
                ControllerName = "vbhome", ViewName = viewName, Output = new StringBuilder(), Data = viewData
            };

            _factory.RenderView(context);
            return(context.Output.ToString());
        }
Beispiel #10
0
        private string ExecuteView(StubViewData viewData)
        {
            var view = (StubSparkView)Activator.CreateInstance(_compiler.CompiledType);

            _languageFactory.InstanceCreated(_compiler, view);
            view.ViewData = viewData;
            var contents = new StringWriter();

            view.RenderView(contents);
            _languageFactory.InstanceReleased(_compiler, view);
            return(contents.ToString());
        }
Beispiel #11
0
        public void AddViewDataDifferentTypes()
        {
            mocks.ReplayAll();
            var viewData = new StubViewData {
                { "comment", new Comment {
                      Text = "Hello world"
                  } }
            };
            var viewContext = MakeViewContext("addviewdatadifferenttypes", null, viewData);

            Assert.That(() => factory.RenderView(viewContext), Throws.TypeOf <CompilerException>());
            mocks.VerifyAll();
        }
Beispiel #12
0
        public void AddViewDataDifferentTypes()
        {
            mocks.ReplayAll();
            var viewData = new StubViewData {
                { "comment", new Comment {
                      Text = "Hello world"
                  } }
            };
            var viewContext = MakeViewContext("addviewdatadifferenttypes", null, viewData);

            factory.RenderView(viewContext);
            mocks.VerifyAll();
        }
        public void AddViewDataMoreThanOnce()
        {
            mocks.ReplayAll();
            var viewData = new StubViewData { { "comment", new Comment { Text = "Hello world" } } };
            var viewContext = MakeViewContext("addviewdatamorethanonce", null, viewData);
            factory.RenderView(viewContext);
            mocks.VerifyAll();

            string content = sb.ToString();

            Assert.That(content, Contains.InOrder(
                "<div>Hello world</div>",
                "<div>\r\n  Again: Hello world\r\n</div>"));
        }
Beispiel #14
0
        public void SignalWillExpireOutputCachingEntry()
        {
            _viewFolder.Add("home\\index.spark", @"
<viewdata model=""System.Func<string>"" datasignal='Spark.ICacheSignal'/>
<div>
<cache key='string.Empty' signal='datasignal'>
<p>${ViewData.Model()}</p>
</cache>
</div>");
            var signal = new CacheSignal();
            var calls  = 0;
            var data   = new StubViewData <Func <string> >
            {
                Model = () => (++calls).ToString()
            };

            data["datasignal"] = signal;

            var contents = Render("index", data);

            Assert.That(contents, Is.EqualTo(@"
<div>
<p>1</p>
</div>"));
            Assert.That(calls, Is.EqualTo(1));

            contents = Render("index", data);
            Assert.That(contents, Is.EqualTo(@"
<div>
<p>1</p>
</div>"));
            Assert.That(calls, Is.EqualTo(1));

            signal.FireChanged();

            contents = Render("index", data);
            Assert.That(contents, Is.EqualTo(@"
<div>
<p>2</p>
</div>"));
            Assert.That(calls, Is.EqualTo(2));

            contents = Render("index", data);
            Assert.That(contents, Is.EqualTo(@"
<div>
<p>2</p>
</div>"));
            Assert.That(calls, Is.EqualTo(2));
        }
Beispiel #15
0
        public void DefaultValuesStandInForNullViewData()
        {
            mocks.ReplayAll();
            var viewData    = new StubViewData();
            var viewContext = MakeViewContext("DefaultValuesStandInForNullViewData", null, viewData);

            factory.RenderView(viewContext);
            mocks.VerifyAll();
            string content = sb.ToString();

            Assert.IsFalse(content.Contains("default"));

            Assert.That(content, Contains.InOrder("ok1", "ok2"));
            Assert.IsFalse(content.Contains("fail"));
        }
Beispiel #16
0
        public void NullExceptionHandledAutomatically()
        {
            mocks.ReplayAll();
            var viewData    = new StubViewData();
            var viewContext = MakeViewContext("NullExceptionHandledAutomatically", null, viewData);

            factory.RenderView(viewContext);
            mocks.VerifyAll();
            string content = sb.ToString();

            Assert.IsFalse(content.Contains("default"));

            Assert.That(content, Contains.InOrder(
                            "<p>name kaboom *${user.Name}*</p>",
                            "<p>name silently **</p>",
                            "<p>name fixed *fred*</p>"));
        }
        public void AlternateViewdataSyntax()
        {
            mocks.ReplayAll();
            var viewData = new StubViewData<IList<string>> { { "my-data", "alpha" } };
            viewData.Model = new[] { "beta", "gamma", "delta" };

            var viewContext = MakeViewContext("alternate-viewdata-syntax", null, viewData);
            factory.RenderView(viewContext);
            mocks.VerifyAll();
            string content = sb.ToString();

            Assert.That(content, Contains.InOrder(
                            "<p>", "alpha", "</p>",
                            "<p>", "beta", "</p>",
                            "<p>", "gamma", "</p>",
                            "<p>", "delta", "</p>"
                            ));
        }
Beispiel #18
0
        public void AddViewDataMoreThanOnce()
        {
            mocks.ReplayAll();
            var viewData = new StubViewData {
                { "comment", new Comment {
                      Text = "Hello world"
                  } }
            };
            var viewContext = MakeViewContext("addviewdatamorethanonce", null, viewData);

            factory.RenderView(viewContext);
            mocks.VerifyAll();

            string content = sb.ToString();

            Assert.That(content, Contains.InOrder(
                            "<div>Hello world</div>",
                            "<div>\r\n  Again: Hello world\r\n</div>"));
        }
Beispiel #19
0
        public void LateBoundEvalResolvesViewData()
        {
            mocks.ReplayAll();
            var viewData = new StubViewData()
            {
                { "alpha", "<strong>hi</strong>" },
                { "beta", "yadda" }
            };
            var viewContext = MakeViewContext("LateBoundEvalResolvesViewData", null, viewData);

            factory.RenderView(viewContext);
            mocks.VerifyAll();
            string content = sb.ToString();

            Assert.That(content, Contains.InOrder(
                            "<p><strong>hi</strong></p>",
                            "<p>&lt;strong&gt;hi&lt;/strong&gt;</p>",
                            "yadda",
                            "<p>42</p>"));
        }
Beispiel #20
0
        public void OnceFlagsSetWhenCacheRecordedShouldBeSetWhenCacheReplayed()
        {
            _viewFolder.Add("home\\index.spark", @"
<viewdata model=""System.Func<string>""/>
<ul>
<li once='foo'>${ViewData.Model()}[1]</li>
<cache>
<li once='bar'>${ViewData.Model()}[2]</li>
<li once='foo'>${ViewData.Model()}[3]</li>
</cache>
<li once='quux'>${ViewData.Model()}[4]</li>
<li once='bar'>${ViewData.Model()}[5]</li>
<li once='foo'>${ViewData.Model()}[6]</li>
<li once='quux'>${ViewData.Model()}[7]</li>
</ul>");

            var calls = 0;
            var data  = new StubViewData <Func <string> >
            {
                Model = () => (++calls).ToString()
            };

            var contents = Render("index", data);

            Assert.That(calls, Is.EqualTo(3));
            Assert.That(contents, Is.EqualTo(@"
<ul>
<li>1[1]</li>
<li>2[2]</li>
<li>3[4]</li>
</ul>"));

            contents = Render("index", data);
            Assert.That(calls, Is.EqualTo(5));
            Assert.That(contents, Is.EqualTo(@"
<ul>
<li>4[1]</li>
<li>2[2]</li>
<li>5[4]</li>
</ul>"));
        }
Beispiel #21
0
        public void AlternateViewdataSyntax()
        {
            mocks.ReplayAll();
            var viewData = new StubViewData <IList <string> > {
                { "my-data", "alpha" }
            };

            viewData.Model = new[] { "beta", "gamma", "delta" };

            var viewContext = MakeViewContext("alternate-viewdata-syntax", null, viewData);

            factory.RenderView(viewContext);
            mocks.VerifyAll();
            string content = sb.ToString();

            Assert.That(content, Contains.InOrder(
                            "<p>", "alpha", "</p>",
                            "<p>", "beta", "</p>",
                            "<p>", "gamma", "</p>",
                            "<p>", "delta", "</p>"
                            ));
        }
Beispiel #22
0
        public void MultipleCachesShouldActAsDifferentSite()
        {
            _viewFolder.Add("home\\index.spark", @"
<viewdata model=""System.Func<string>""/>
<div>
<cache>
<p>${ViewData.Model()}</p>
</cache>
<cache>
<p>${ViewData.Model()}</p>
</cache>
</div>");

            var calls = 0;
            var data  = new StubViewData <Func <string> >
            {
                Model = () => (++calls).ToString()
            };

            var contents = Render("index", data);

            Assert.That(contents, Is.EqualTo(@"
<div>
<p>1</p>
<p>2</p>
</div>"));
            Assert.That(calls, Is.EqualTo(2));


            contents = Render("index", data);
            Assert.That(contents, Is.EqualTo(@"
<div>
<p>1</p>
<p>2</p>
</div>"));
            Assert.That(calls, Is.EqualTo(2));
        }
 StubViewContext MakeViewContext(string viewName, string masterName, StubViewData data)
 {
     return new StubViewContext { ControllerName = "Home", ViewName = viewName, MasterName = masterName, Output = sb, Data = data };
 }
Beispiel #24
0
        public void CacheExpiresTakesOutContentAfterTime()
        {
            _viewFolder.Add("home\\index.spark", @"
<viewdata model=""System.Func<string>""/>
<for each='var x in new[]{1,2,3,1,2,3}'>
<cache key='x' expires='System.TimeSpan.FromSeconds(30)'>
<p>${x}:${ViewData.Model()}</p>
</cache>
</for>
<p cache.expires='40'>last:${ViewData.Model()}</p>
");

            var calls = 0;
            var data  = new StubViewData <Func <string> >
            {
                Model = () => (++calls).ToString()
            };

            var contents = Render("index", data);

            Assert.That(contents, Contains.InOrder(
                            "<p>1:1</p>",
                            "<p>2:2</p>",
                            "<p>3:3</p>",
                            "<p>1:1</p>",
                            "<p>2:2</p>",
                            "<p>3:3</p>",
                            "<p>last:4</p>"));

            _cacheService.UtcNow = _cacheService.UtcNow.AddSeconds(25);
            contents             = Render("index", data);
            Assert.That(contents, Contains.InOrder(
                            "<p>1:1</p>",
                            "<p>2:2</p>",
                            "<p>3:3</p>",
                            "<p>1:1</p>",
                            "<p>2:2</p>",
                            "<p>3:3</p>",
                            "<p>last:4</p>"));

            _cacheService.UtcNow = _cacheService.UtcNow.AddSeconds(10);
            contents             = Render("index", data);
            Assert.That(contents, Contains.InOrder(
                            "<p>1:5</p>",
                            "<p>2:6</p>",
                            "<p>3:7</p>",
                            "<p>1:5</p>",
                            "<p>2:6</p>",
                            "<p>3:7</p>",
                            "<p>last:4</p>"));

            _cacheService.UtcNow = _cacheService.UtcNow.AddSeconds(10);
            contents             = Render("index", data);
            Assert.That(contents, Contains.InOrder(
                            "<p>1:5</p>",
                            "<p>2:6</p>",
                            "<p>3:7</p>",
                            "<p>1:5</p>",
                            "<p>2:6</p>",
                            "<p>3:7</p>",
                            "<p>last:8</p>"));
        }
Beispiel #25
0
        public void CacheInMacroShouldActAsSameSite()
        {
            _viewFolder.Add("home\\index.spark", @"
            <viewdata model=""System.Func<string>""/>
            <macro name=""foo"">
            <cache><p>${ViewData.Model()}</p></cache>
            </macro>
            <div>
            ${foo()}
            ${foo()}
            </div>");

            int calls = 0;
            var data = new StubViewData<Func<string>>
            {
                Model = () => (++calls).ToString()
            };

            var contents = Render("index", data);
            Assert.That(contents, Contains.InOrder(
                "<p>1</p>",
                "<p>1</p>"));

            Assert.That(calls, Is.EqualTo(1));

            contents = Render("index", data);
            Assert.That(contents, Contains.InOrder(
                "<p>1</p>",
                "<p>1</p>"));

            Assert.That(calls, Is.EqualTo(1));
        }
Beispiel #26
0
        public void TemplateDoesNotRunThroughCacheHit()
        {
            _viewFolder.Add("home\\index.spark", @"
            <viewdata model=""System.Func<string>""/>
            <div>
            <cache key='string.Empty'>
            <p>${ViewData.Model()}</p>
            </cache>
            </div>");
            int calls = 0;
            var data = new StubViewData<Func<string>>
                       {
                           Model = () => (++calls).ToString()
                       };

            var contents = Render("index", data);
            Assert.That(contents, Is.EqualTo(@"
            <div>
            <p>1</p>
            </div>"));
            Assert.That(calls, Is.EqualTo(1));

            contents = Render("index", data);
            Assert.That(contents, Is.EqualTo(@"
            <div>
            <p>1</p>
            </div>"));
            Assert.That(calls, Is.EqualTo(1));
        }
        public void MarkupBasedMacros()
        {
            var data = new StubViewData
                           {
                               {"username", "Bob"},
                               {"comments", new[] {
                                   new Comment {Text = "Alpha"},
                                   new Comment {Text = "Beta"},
                                   new Comment {Text = "Gamma"}
                               }}
                           };

            mocks.ReplayAll();
            var viewContext = MakeViewContext("macros", null, data);

            factory.RenderView(viewContext);
            mocks.VerifyAll();
            string content = sb.ToString();
            Assert.That(content, Contains.InOrder(
                "<p>Bob</p>", "<p>Alpha</p>",
                "<p>Bob</p>", "<p>Beta</p>",
                "<p>Bob</p>", "<p>Gamma</p>",
                "<span class=\"yadda\">Rating: 5</span>"));
        }
        public void LateBoundEvalResolvesViewData()
        {
            mocks.ReplayAll();
            var viewData = new StubViewData()
                               {
                                   {"alpha", "<strong>hi</strong>"},
                                   {"beta", "yadda"}
                               };
            var viewContext = MakeViewContext("LateBoundEvalResolvesViewData", null, viewData);
            factory.RenderView(viewContext);
            mocks.VerifyAll();
            string content = sb.ToString();

            Assert.That(content, Contains.InOrder(
                            "<p><strong>hi</strong></p>",
                            "<p>&lt;strong&gt;hi&lt;/strong&gt;</p>",
                            "yadda",
                            "<p>42</p>"));
        }
Beispiel #29
0
 StubViewContext MakeViewContext(string viewName, string masterName, StubViewData data)
 {
     return(new StubViewContext {
         ControllerName = "Home", ViewName = viewName, MasterName = masterName, Output = sb, Data = data
     });
 }
Beispiel #30
0
        public void NamedContentShouldIndividuallySpoolAndCache()
        {
            _viewFolder.Add("home\\index.spark", @"
            <viewdata model=""System.Func<string>""/>
            <div>
            <content name='foo'>
            <p>f${ViewData.Model()}[1]</p>
            </content>
            cache
            <cache>
            <p>${ViewData.Model()}[2]c</p>
            <content name='foo'>
            <p>f${ViewData.Model()}[3]c</p>
            </content>
            <content name='bar'>
            <p>b${ViewData.Model()}[4]c</p>
            </content>
            <p>${ViewData.Model()}[5]c</p>
            </cache>
            <content name='bar'>
            <p>b${ViewData.Model()}[6]</p>
            </content>
            placed
            <p>${ViewData.Model()}[7]</p>
            <use content='foo'/>
            <use content='bar'/>
            <p>${ViewData.Model()}[8]</p>
            </div>");

            var calls = 0;
            var data = new StubViewData<Func<string>>
            {
                Model = () => (++calls).ToString()
            };

            var contents = Render("index", data);
            Assert.That(calls, Is.EqualTo(8));
            Assert.That(contents, Is.EqualTo(@"
            <div>
            cache
            <p>2[2]c</p>
            <p>5[5]c</p>
            placed
            <p>7[7]</p>
            <p>f1[1]</p>
            <p>f3[3]c</p>
            <p>b4[4]c</p>
            <p>b6[6]</p>
            <p>8[8]</p>
            </div>"));

            contents = Render("index", data);
            Assert.That(calls, Is.EqualTo(12));
            Assert.That(contents, Is.EqualTo(@"
            <div>
            cache
            <p>2[2]c</p>
            <p>5[5]c</p>
            placed
            <p>11[7]</p>
            <p>f9[1]</p>
            <p>f3[3]c</p>
            <p>b4[4]c</p>
            <p>b10[6]</p>
            <p>12[8]</p>
            </div>"));
        }
Beispiel #31
0
        public void OnceFlagsSetWhenCacheRecordedShouldBeSetWhenCacheReplayed()
        {
            _viewFolder.Add("home\\index.spark", @"
            <viewdata model=""System.Func<string>""/>
            <ul>
            <li once='foo'>${ViewData.Model()}[1]</li>
            <cache>
            <li once='bar'>${ViewData.Model()}[2]</li>
            <li once='foo'>${ViewData.Model()}[3]</li>
            </cache>
            <li once='quux'>${ViewData.Model()}[4]</li>
            <li once='bar'>${ViewData.Model()}[5]</li>
            <li once='foo'>${ViewData.Model()}[6]</li>
            <li once='quux'>${ViewData.Model()}[7]</li>
            </ul>");

            var calls = 0;
            var data = new StubViewData<Func<string>>
            {
                Model = () => (++calls).ToString()
            };

            var contents = Render("index", data);
            Assert.That(calls, Is.EqualTo(3));
            Assert.That(contents, Is.EqualTo(@"
            <ul>
            <li>1[1]</li>
            <li>2[2]</li>
            <li>3[4]</li>
            </ul>"));

            contents = Render("index", data);
            Assert.That(calls, Is.EqualTo(5));
            Assert.That(contents, Is.EqualTo(@"
            <ul>
            <li>4[1]</li>
            <li>2[2]</li>
            <li>5[4]</li>
            </ul>"));
        }
Beispiel #32
0
        public void OutputWhileNamedContentActiveShouldAppearOnceAtCorrectTarget()
        {
            _viewFolder.Add("home\\index.spark", @"
            <viewdata model=""System.Func<string>""/>
            <ul>
            <content name='foo'>
            <li>${ViewData.Model()}[1]</li>
            </content>
            <li>${ViewData.Model()}[2]</li>
            <content name='foo'>
            <cache>
            <li>${ViewData.Model()}[3]c</li>
            <content name='foo'>
            hana
            </content>
            <li>${ViewData.Model()}[4]c</li>
            </cache>
            </content>
            <li>${ViewData.Model()}[5]</li>
            <content name='foo'>
            <li>${ViewData.Model()}[6]</li>
            </content>
            <use content='foo'/>
            </ul>");

            var calls = 0;
            var data = new StubViewData<Func<string>>
            {
                Model = () => (++calls).ToString()
            };

            var contents = Render("index", data);
            Assert.That(calls, Is.EqualTo(6));
            Assert.That(contents, Is.EqualTo(@"
            <ul>
            <li>2[2]</li>
            <li>5[5]</li>
            <li>1[1]</li>
            <li>3[3]c</li>
            hana
            <li>4[4]c</li>
            <li>6[6]</li>
            </ul>"));

            contents = Render("index", data);
            Assert.That(calls, Is.EqualTo(10));
            Assert.That(contents, Is.EqualTo(@"
            <ul>
            <li>8[2]</li>
            <li>9[5]</li>
            <li>7[1]</li>
            <li>3[3]c</li>
            hana
            <li>4[4]c</li>
            <li>10[6]</li>
            </ul>"));
        }
Beispiel #33
0
        public void SignalWillExpireOutputCachingEntry()
        {
            _viewFolder.Add("home\\index.spark", @"
            <viewdata model=""System.Func<string>"" datasignal='Spark.ICacheSignal'/>
            <div>
            <cache key='string.Empty' signal='datasignal'>
            <p>${ViewData.Model()}</p>
            </cache>
            </div>");
            var signal = new CacheSignal();
            var calls = 0;
            var data = new StubViewData<Func<string>>
            {
                Model = () => (++calls).ToString()
            };
            data["datasignal"] = signal;

            var contents = Render("index", data);
            Assert.That(contents, Is.EqualTo(@"
            <div>
            <p>1</p>
            </div>"));
            Assert.That(calls, Is.EqualTo(1));

            contents = Render("index", data);
            Assert.That(contents, Is.EqualTo(@"
            <div>
            <p>1</p>
            </div>"));
            Assert.That(calls, Is.EqualTo(1));

            signal.FireChanged();

            contents = Render("index", data);
            Assert.That(contents, Is.EqualTo(@"
            <div>
            <p>2</p>
            </div>"));
            Assert.That(calls, Is.EqualTo(2));

            contents = Render("index", data);
            Assert.That(contents, Is.EqualTo(@"
            <div>
            <p>2</p>
            </div>"));
            Assert.That(calls, Is.EqualTo(2));
        }
        public void DefaultValuesStandInForNullViewData()
        {
            mocks.ReplayAll();
            var viewData = new StubViewData();
            var viewContext = MakeViewContext("DefaultValuesStandInForNullViewData", null, viewData);
            factory.RenderView(viewContext);
            mocks.VerifyAll();
            string content = sb.ToString();

            Assert.IsFalse(content.Contains("default"));

            Assert.That(content, Contains.InOrder("ok1", "ok2"));
            Assert.IsFalse(content.Contains("fail"));
        }
Beispiel #35
0
        public void NamedContentShouldIndividuallySpoolAndCache()
        {
            _viewFolder.Add("home\\index.spark", @"
<viewdata model=""System.Func<string>""/>
<div>
<content name='foo'>
<p>f${ViewData.Model()}[1]</p>
</content>
cache
<cache>
<p>${ViewData.Model()}[2]c</p>
<content name='foo'>
<p>f${ViewData.Model()}[3]c</p>
</content>
<content name='bar'>
<p>b${ViewData.Model()}[4]c</p>
</content>
<p>${ViewData.Model()}[5]c</p>
</cache>
<content name='bar'>
<p>b${ViewData.Model()}[6]</p>
</content>
placed
<p>${ViewData.Model()}[7]</p>
<use content='foo'/>
<use content='bar'/>
<p>${ViewData.Model()}[8]</p>
</div>");

            var calls = 0;
            var data  = new StubViewData <Func <string> >
            {
                Model = () => (++calls).ToString()
            };

            var contents = Render("index", data);

            Assert.That(calls, Is.EqualTo(8));
            Assert.That(contents, Is.EqualTo(@"
<div>
cache
<p>2[2]c</p>
<p>5[5]c</p>
placed
<p>7[7]</p>
<p>f1[1]</p>
<p>f3[3]c</p>
<p>b4[4]c</p>
<p>b6[6]</p>
<p>8[8]</p>
</div>"));


            contents = Render("index", data);
            Assert.That(calls, Is.EqualTo(12));
            Assert.That(contents, Is.EqualTo(@"
<div>
cache
<p>2[2]c</p>
<p>5[5]c</p>
placed
<p>11[7]</p>
<p>f9[1]</p>
<p>f3[3]c</p>
<p>b4[4]c</p>
<p>b10[6]</p>
<p>12[8]</p>
</div>"));
        }
Beispiel #36
0
        public void MultipleCachesShouldActAsDifferentSite()
        {
            _viewFolder.Add("home\\index.spark", @"
            <viewdata model=""System.Func<string>""/>
            <div>
            <cache>
            <p>${ViewData.Model()}</p>
            </cache>
            <cache>
            <p>${ViewData.Model()}</p>
            </cache>
            </div>");

            var calls = 0;
            var data = new StubViewData<Func<string>>
            {
                Model = () => (++calls).ToString()
            };

            var contents = Render("index", data);
            Assert.That(contents, Is.EqualTo(@"
            <div>
            <p>1</p>
            <p>2</p>
            </div>"));
            Assert.That(calls, Is.EqualTo(2));

            contents = Render("index", data);
            Assert.That(contents, Is.EqualTo(@"
            <div>
            <p>1</p>
            <p>2</p>
            </div>"));
            Assert.That(calls, Is.EqualTo(2));
        }
Beispiel #37
0
        public void CommaCreatesMultiPartKey()
        {
            _viewFolder.Add("home\\index.spark",
                            @"
            <viewdata model=""System.Func<string>""/>
            <for each='var x in new[]{1,2,3,1,2,3}'>
            <p cache='x,xIndex'>${x}:${ViewData.Model()}</p>
            </for>");

            var calls = 0;
            var data = new StubViewData<Func<string>>
                       {
                           Model = () => (++calls).ToString()
                       };

            var contents = Render("index", data);
            Assert.That(contents, Contains.InOrder(
                "<p>1:1</p>",
                "<p>2:2</p>",
                "<p>3:3</p>",
                "<p>1:4</p>",
                "<p>2:5</p>",
                "<p>3:6</p>"));

            Assert.That(_cacheService.AllKeys.Count(x => x.Substring(32) == "1\u001f0"), Is.EqualTo(1));
            Assert.That(_cacheService.AllKeys.Count(x => x.Substring(32) == "2\u001f1"), Is.EqualTo(1));
            Assert.That(_cacheService.AllKeys.Count(x => x.Substring(32) == "3\u001f2"), Is.EqualTo(1));
            Assert.That(_cacheService.AllKeys.Count(x => x.Substring(32) == "1\u001f3"), Is.EqualTo(1));
            Assert.That(_cacheService.AllKeys.Count(x => x.Substring(32) == "2\u001f4"), Is.EqualTo(1));
            Assert.That(_cacheService.AllKeys.Count(x => x.Substring(32) == "3\u001f5"), Is.EqualTo(1));
        }
        public void DefaultValuesDontReplaceViewData()
        {
            mocks.ReplayAll();
            var viewData = new StubViewData { { "x1", 5 }, { "x2", 5 } };
            var viewContext = MakeViewContext("DefaultValuesDontReplaceViewData", null, viewData);
            factory.RenderView(viewContext);
            mocks.VerifyAll();
            string content = sb.ToString();

            Assert.IsFalse(content.Contains("default"));

            ContainsInOrder(content, "ok1", "ok2");
            Assert.IsFalse(content.Contains("fail"));
        }
        public void NullExceptionHandledAutomatically()
        {
            mocks.ReplayAll();
            var viewData = new StubViewData();
            var viewContext = MakeViewContext("NullExceptionHandledAutomatically", null, viewData);
            factory.RenderView(viewContext);
            mocks.VerifyAll();
            string content = sb.ToString();

            Assert.IsFalse(content.Contains("default"));

            Assert.That(content, Contains.InOrder(
                "<p>name kaboom *${user.Name}*</p>",
                "<p>name silently **</p>",
                "<p>name fixed *fred*</p>"));
        }
Beispiel #40
0
        public void CacheExpiresTakesOutContentAfterTime()
        {
            _viewFolder.Add("home\\index.spark", @"
            <viewdata model=""System.Func<string>""/>
            <for each='var x in new[]{1,2,3,1,2,3}'>
            <cache key='x' expires='System.TimeSpan.FromSeconds(30)'>
            <p>${x}:${ViewData.Model()}</p>
            </cache>
            </for>
            <p cache.expires='40'>last:${ViewData.Model()}</p>
            ");

            var calls = 0;
            var data = new StubViewData<Func<string>>
            {
                Model = () => (++calls).ToString()
            };

            var contents = Render("index", data);
            Assert.That(contents, Contains.InOrder(
                "<p>1:1</p>",
                "<p>2:2</p>",
                "<p>3:3</p>",
                "<p>1:1</p>",
                "<p>2:2</p>",
                "<p>3:3</p>",
                "<p>last:4</p>"));

            _cacheService.UtcNow = _cacheService.UtcNow.AddSeconds(25);
            contents = Render("index", data);
            Assert.That(contents, Contains.InOrder(
                "<p>1:1</p>",
                "<p>2:2</p>",
                "<p>3:3</p>",
                "<p>1:1</p>",
                "<p>2:2</p>",
                "<p>3:3</p>",
                "<p>last:4</p>"));

            _cacheService.UtcNow = _cacheService.UtcNow.AddSeconds(10);
            contents = Render("index", data);
            Assert.That(contents, Contains.InOrder(
                "<p>1:5</p>",
                "<p>2:6</p>",
                "<p>3:7</p>",
                "<p>1:5</p>",
                "<p>2:6</p>",
                "<p>3:7</p>",
                "<p>last:4</p>"));

            _cacheService.UtcNow = _cacheService.UtcNow.AddSeconds(10);
            contents = Render("index", data);
            Assert.That(contents, Contains.InOrder(
                "<p>1:5</p>",
                "<p>2:6</p>",
                "<p>3:7</p>",
                "<p>1:5</p>",
                "<p>2:6</p>",
                "<p>3:7</p>",
                "<p>last:8</p>"));
        }
 private string Render(string viewName, StubViewData viewData)
 {
     var context = new StubViewContext { ControllerName = "home", ViewName = viewName, Output = new StringBuilder(), Data = viewData };
     _factory.RenderView(context);
     return context.Output.ToString()
         .Replace("\r\n\r\n", "\r\n")
         .Replace("\r\n\r\n", "\r\n");
 }
Beispiel #42
0
 protected StubSparkView()
 {
     ViewData = new StubViewData();
 }