public void Should_return_a_valid_model_in_json_format()
 {
     using (var stream = new MemoryStream())
     {
         response.Contents(stream);
         stream.ShouldEqual("{\"FirstName\":\"Andy\",\"LastName\":\"Pike\"}");
     }
 }
 public void Should_return_a_valid_model_in_csv_format()
 {
     using (var stream = new MemoryStream())
     {
         response.Contents(stream);
         stream.ShouldEqual("FirstName,LastName\r\nEmmanuel,Morales");
     }
 }
 public void Should_return_null_in_json_format()
 {
     var nullResponse = formatter.AsJson<Person>(null);
     using (var stream = new MemoryStream())
     {
         nullResponse.Contents(stream);
         stream.ShouldEqual("null");
     }
 }
        public void GetCompiledView_should_render_to_stream()
        {
            // Given
            var location = new ViewLocationResult(
                string.Empty,
                "django",
                new StringReader(@"{% ifequal a a %}<h1>Hello Mr. test</h1>{% endifequal %}")
            );

            var stream = new MemoryStream();

            // When
            var action = engine.RenderView(location, null);
            action.Invoke(stream);

            // Then
            stream.ShouldEqual("<h1>Hello Mr. test</h1>");
        }
        public void GetCompiledView_should_render_to_stream()
        {
            // Given
            var location = new ViewLocationResult(
                string.Empty,
                "cshtml",
                new StringReader(@"@{var x = ""test"";}<h1>Hello Mr. @x</h1>")
            );

            var stream = new MemoryStream();

            // When
            var action = this.engine.RenderView(location, null);
            action.Invoke(stream);

            // Then
            stream.ShouldEqual("<h1>Hello Mr. test</h1>");
        }
        public void Include_should_look_for_a_partial()
        {
            // Given
            var location = new ViewLocationResult(
                string.Empty,
                string.Empty,
                "liquid",
                () => new StringReader(@"<h1>Including a partial</h1>{% include 'partial' %}")
            );

            var stream = new MemoryStream();

            // When
            var response = this.engine.RenderView(location, null, this.renderContext);
            response.Contents.Invoke(stream);

            // Then
            stream.ShouldEqual("<h1>Including a partial</h1>Some template.");
        }
        public void GetCompiledView_should_render_to_stream()
        {
            // Given
            var location = new ViewLocationResult(
                string.Empty,
                string.Empty,
                "django",
                () => new StringReader(@"{% ifequal a a %}<h1>Hello Mr. test</h1>{% endifequal %}")
            );
            A.CallTo(() => this.renderContext.LocateView(".django", null)).Returns(location);

            var stream = new MemoryStream();

            // When
            var action = engine.RenderView(location, null, this.renderContext);
            action.Invoke(stream);

            // Then
            stream.ShouldEqual("<h1>Hello Mr. test</h1>");
        }
        public void RenderView_should_accept_a_model_and_read_from_it_into_the_stream()
        {
            // Given
            var location = new ViewLocationResult(
                string.Empty,
                string.Empty,
                "liquid",
                () => new StringReader(@"<h1>Hello Mr. {{ Model.name }}</h1>")
            );

            var currentStartupContext =
                CreateContext(new[] { location });

            this.engine.Initialize(currentStartupContext);
            var stream = new MemoryStream();

            // When
            var response = this.engine.RenderView(location, new { name = "test" }, this.renderContext);
            response.Contents.Invoke(stream);

            // Then
            stream.ShouldEqual("<h1>Hello Mr. test</h1>");
        }
Exemple #9
0
        public void RenderView_csharp_should_be_able_to_use_a_using_statement()
        {
            // Given
            var view = new StringBuilder()
                .AppendLine("@model Nancy.ViewEngines.Razor.Tests.Models.Person")
                .AppendLine("@using Nancy.ViewEngines.Razor.Tests.Models")
                .AppendLine(@"@{ var hobby = new Hobby { Name = ""Music"" }; }")
                .Append("<h1>Mr. @Model.Name likes @hobby.Name!</h1>");

            var location = new ViewLocationResult(
                string.Empty,
                string.Empty,
                "cshtml",
                () => new StringReader(view.ToString())
            );

            var stream = new MemoryStream();

            var model = new Person { Name = "Jeff" };

            // When
            var response = this.engine.RenderView(location, model, this.renderContext);
            response.Contents.Invoke(stream);

            // Then
            stream.ShouldEqual("<h1>Mr. Jeff likes Music!</h1>", true);
        }
Exemple #10
0
        public void RenderView_csharp_should_be_able_to_use_a_model_from_another_assembly()
        {
            // Given
            var view = new StringBuilder()
                .AppendLine("@model Nancy.ViewEngines.Razor.Tests.Models.Person")
                .Append("<h1>Hello Mr. @Model.Name</h1>");

            var location = new ViewLocationResult(
                string.Empty,
                string.Empty,
                "cshtml",
                () => new StringReader(view.ToString())
            );

            var stream = new MemoryStream();

            var model = new Person { Name = "Jeff" };

            // When
            var response = this.engine.RenderView(location, model, this.renderContext);
            response.Contents.Invoke(stream);

            // Then
            stream.ShouldEqual("<h1>Hello Mr. Jeff</h1>");
        }
        public void Include_should_work_with_double_quotes()
        {
            // Set up the view startup context
            string partialPath = Path.Combine(Environment.CurrentDirectory, @"TestViews\_partial.liquid");

            // Set up a ViewLocationResult that the test can use
            var testLocation = new ViewLocationResult(
                Environment.CurrentDirectory,
                "test",
                "liquid",
                () => new StringReader(@"<h1>Including a partial</h1>{% include ""partial"" %}")
            );

            var partialLocation = new ViewLocationResult(
                partialPath,
                "partial",
                "liquid",
                () => new StringReader(File.ReadAllText(partialPath))
            );

            var currentStartupContext = CreateContext(new [] {testLocation, partialLocation});

            this.engine = new DotLiquidViewEngine(new LiquidNancyFileSystem(currentStartupContext));

            // Given
            var stream = new MemoryStream();

            // When
            var response = this.engine.RenderView(testLocation, null, this.renderContext);
            response.Contents.Invoke(stream);

            // Then
            stream.ShouldEqual("<h1>Including a partial</h1>Some template.");
        }
        public void RenderView_should_accept_a_model_with_a_list_and_iterate_over_it()
        {
            // TODO - Fixup on Mono
            // Given
            var location = new ViewLocationResult(
                string.Empty,
                string.Empty,
                "liquid",
                () => new StringReader(@"<ul>{% for item in model.Widgets %}<li>{{ item.name }}</li>{% endfor %}</ul>")
            );

            var currentStartupContext = CreateContext(new [] {location});
            this.engine = new DotLiquidViewEngine(new LiquidNancyFileSystem(currentStartupContext));
            var stream = new MemoryStream();

            // When
            var widgets = new List<object> { new { name = "Widget 1" }, new { name = "Widget 2" }, new { name = "Widget 3" }, new { name = "Widget 4" } };
            var response = this.engine.RenderView(location, new { Widgets = widgets }, this.renderContext);
            response.Contents.Invoke(stream);

            // Then
            stream.ShouldEqual("<ul><li>Widget 1</li><li>Widget 2</li><li>Widget 3</li><li>Widget 4</li></ul>");
        }
        public void RenderView_should_accept_a_model_and_read_from_it_into_the_stream()
        {
            // Given
            var location = new ViewLocationResult(
                string.Empty,
                string.Empty,
                "liquid",
                () => new StringReader(@"<h1>Hello Mr. {{ model.name }}</h1>")
            );

            var stream = new MemoryStream();

            // When
            var action = this.engine.RenderView(location, new { name = "test" }, this.renderContext);
            action.Invoke(stream);

            // Then
            stream.ShouldEqual("<h1>Hello Mr. test</h1>");
        }
Exemple #14
0
 public void should_serialize_to_stream(Action<Stream> serialize, Stream output)
 {
     var stream = new MemoryStream();
     serialize(stream);
     stream.Seek(0, SeekOrigin.Begin);
     stream.ShouldEqual(output);
 }
Exemple #15
0
        public void should_work_on_multiple_threads()
        {
            // Given
            var location = new ViewLocationResult(
                string.Empty,
                string.Empty,
                "cshtml",
                () =>
                {
                    Thread.Sleep(500);
                    return new StringReader(@"@{var x = ""test"";}<h1>Hello Mr. @x</h1>");
                });

            var wait = new ManualResetEvent(false);

            var stream = new MemoryStream();

            // When
            ThreadPool.QueueUserWorkItem(_ =>
                {
                    var response2 = this.engine.RenderView(location, null, this.renderContext);
                    response2.Contents.Invoke(new MemoryStream());
                    wait.Set();
                });
            var response = this.engine.RenderView(location, null, this.renderContext);
            response.Contents.Invoke(stream);

            wait.WaitOne(1000).ShouldBeTrue();

            // Then
            stream.ShouldEqual("<h1>Hello Mr. test</h1>");
        }
        public void RenderView_should_render_to_stream()
        {
            // Given
            var location = new ViewLocationResult(
                string.Empty,
                string.Empty,
                "liquid",
                () => new StringReader(@"{% assign name = 'test' %}<h1>Hello Mr. {{ name }}</h1>")
            );

            var stream = new MemoryStream();

            // When
            var response = this.engine.RenderView(location, null, this.renderContext);
            response.Contents.Invoke(stream);

            // Then
            stream.ShouldEqual("<h1>Hello Mr. test</h1>");
        }
        public void When_passing_a_null_model_should_return_an_empty_string()
        {
            // Given
            var location = new ViewLocationResult(
                string.Empty,
                string.Empty,
                "liquid",
                () => new StringReader(@"<h1>Hello Mr. {{ model.name }}</h1>")
            );

            var currentStartupContext =
                CreateContext(new [] {location});

            this.engine.Initialize(currentStartupContext);

            var stream = new MemoryStream();

            // When
            var response = engine.RenderView(location, null, this.renderContext);
            response.Contents.Invoke(stream);

            // Then
            stream.ShouldEqual("<h1>Hello Mr. </h1>");
        }
        public void RenderView_should_expose_ViewBag_to_the_template()
        {
            // Given
            var location = new ViewLocationResult(
                string.Empty,
                string.Empty,
                "liquid",
                () => new StringReader(@"<h1>Hello Mr. {{ viewbag.name }}</h1>")
            );

            var currentStartupContext =
                CreateContext(new[] { location });

            this.engine.Initialize(currentStartupContext);
            var stream = new MemoryStream();
            this.renderContext.Context.ViewBag.Name = "test";

            // When
            var response = this.engine.RenderView(location, new { name = "incorrect" }, this.renderContext);
            response.Contents.Invoke(stream);

            // Then
            stream.ShouldEqual("<h1>Hello Mr. test</h1>");
        }
        public void Should_use_custom_view_base_with_vb_views()
        {
            // Given
            var view = new StringBuilder()
                .AppendLine("@inherits Nancy.ViewEngines.Razor.Tests.GreetingViewBase")
                .Append("<h1>@Greet()</h1>");

            var location = new ViewLocationResult(
                string.Empty,
                string.Empty,
                "vbhtml",
                () => new StringReader(view.ToString())
            );

            var stream = new MemoryStream();

            A.CallTo(() => this.configuration.GetAssemblyNames()).Returns(new[] { "Nancy.ViewEngines.Razor.Tests" });

            // When
            var response = this.engine.RenderView(location, null, this.renderContext);
            response.Contents.Invoke(stream);

            // Then
            stream.ShouldEqual("<h1>Hi, Nancy!</h1>");
        }
Exemple #20
0
        public void RenderView_csharp_should_be_able_to_find_the_model_when_a_null_model_is_passed()
        {
            // Given
            AppDomainAssemblyTypeScanner.AssembliesToScan =
                AppDomainAssemblyTypeScanner.DefaultAssembliesToScan.Union(new Func<Assembly, bool>[]
                                                                               {
                                                                                   x =>
                                                                                   x.GetName().Name.StartsWith("Nancy")
                                                                               });
            var view = new StringBuilder()
                .AppendLine("@model Nancy.ViewEngines.Razor.Tests.Models.Person")
                .AppendLine(@"@{ var hobby = new Hobby { Name = ""Music"" }; }")
                .Append("<h1>Mr. Somebody likes @hobby.Name!</h1>");

            var location = new ViewLocationResult(
                string.Empty,
                string.Empty,
                "cshtml",
                () => new StringReader(view.ToString())
            );

            var stream = new MemoryStream();

            A.CallTo(() => this.configuration.AutoIncludeModelNamespace).Returns(true);

            // When
            var response = this.engine.RenderView(location, null, this.renderContext);
            response.Contents.Invoke(stream);

            // Then
            stream.ShouldEqual("<h1>Mr. Somebody likes Music!</h1>", true);
        }
Exemple #21
0
        public void RenderView_csharp_should_include_namespace_of_model_if_specified_in_the_configuration()
        {
            // Given
            var view = new StringBuilder()
                .AppendLine("@model Nancy.ViewEngines.Razor.Tests.Models.Person")
                .AppendLine(@"@{ var hobby = new Hobby { Name = ""Music"" }; }")
                .Append("<h1>Mr. @Model.Name likes @hobby.Name!</h1>");

            var location = new ViewLocationResult(
                string.Empty,
                string.Empty,
                "cshtml",
                () => new StringReader(view.ToString())
            );

            var stream = new MemoryStream();

            var model = new Person { Name = "Jeff" };

            A.CallTo(() => this.configuration.AutoIncludeModelNamespace).Returns(true);

            // When
            var response = this.engine.RenderView(location, model, this.renderContext);
            response.Contents.Invoke(stream);

            // Then
            stream.ShouldEqual("<h1>Mr. Jeff likes Music!</h1>", true);
        }
        public void RenderView_csharp_should_use_model_directive_for_strongly_typed_view()
        {
            // Given
            var location = FindView("ViewThatUsesModelCSharp");

            var stream = new MemoryStream();

            var model = new DateTime(2000, 1, 1);

            // When
            var response = this.engine.RenderView(location, model, this.renderContext);
            response.Contents.Invoke(stream);

            // Then
            stream.ShouldEqual("\r\n<h1>Hello at " + model.ToString("MM/dd/yyyy") + "</h1>");
        }
Exemple #23
0
        public void Should_be_able_to_render_vb_view_with_generic_model(Type expectedType)
        {
            // Given
            var location = this.CreateViewLocationWithModel(expectedType, new VBCodeProvider(), "ModelType");

            // When
            var stream = new MemoryStream();
            var response = this.engine.RenderView(location, null, this.renderContext);
            response.Contents.Invoke(stream);

            // Then
            stream.ShouldEqual(expectedType.FullName, true);
        }
        public void Should_be_able_to_render_view_with_partial_to_stream()
        {
            // Given
            var location = new ViewLocationResult(
                string.Empty,
                string.Empty,
                "cshtml",
                () => new StringReader(@"@{var x = ""test"";}<h1>Hello Mr. @x</h1> @Html.Partial(""partial.cshtml"")")
            );

            var partialLocation = new ViewLocationResult(
                string.Empty,
                "partial.cshtml",
                "cshtml",
                () => new StringReader(@"this is partial")
            );

            A.CallTo(() => this.renderContext.LocateView("partial.cshtml",null)).Returns(partialLocation);

            var stream = new MemoryStream();

            // When
            var response = this.engine.RenderView(location, null,this.renderContext);
            response.Contents.Invoke(stream);

            // Then
            stream.ShouldEqual("<h1>Hello Mr. test</h1> this is partial");
        }
        public void RenderView_should_render_to_stream()
        {
            // Given
            var location = new ViewLocationResult(
                string.Empty,
                string.Empty,
                "liquid",
                () => new StringReader(@"{% assign name = 'test' %}<h1>Hello Mr. {{ name }}</h1>")
            );

            var currentStartupContext = CreateContext(new [] {location});
            this.engine = new DotLiquidViewEngine(new LiquidNancyFileSystem(currentStartupContext));

            var stream = new MemoryStream();

            // When
            var response = this.engine.RenderView(location, null, this.renderContext);
            response.Contents.Invoke(stream);

            // Then
            stream.ShouldEqual("<h1>Hello Mr. test</h1>");
        }
        public void When_passing_a_null_model_should_return_a_null_model_message_if_called()
        {
            // Given
            var location = new ViewLocationResult(
                string.Empty,
                string.Empty,
                "liquid",
                () => new StringReader(@"<h1>Hello Mr. {{ model.name }}</h1>")
            );

            var stream = new MemoryStream();

            // When
            var action = this.engine.RenderView(location, null, this.renderContext);
            action.Invoke(stream);

            // Then
            stream.ShouldEqual("<h1>Hello Mr. [Model is null]</h1>");
        }
        public void RenderView_should_accept_a_model_and_read_from_it_into_the_stream()
        {
            // Given
            var location = new ViewLocationResult(
                string.Empty,
                string.Empty,
                "cshtml",
                () => new StringReader(@"<h1>Hello Mr. @Model.Name</h1>")
            );

            var stream = new MemoryStream();

            dynamic model = new ExpandoObject();
            model.Name = "test";

            // When
            var response = this.engine.RenderView(location, model, this.renderContext);
            response.Contents.Invoke(stream);

            // Then
            stream.ShouldEqual("<h1>Hello Mr. test</h1>");
        }
        public void when_calling_a_missing_member_should_return_a_missing_member_message()
        {
            // Given
            var location = new ViewLocationResult(
                string.Empty,
                string.Empty,
                "liquid",
                () => new StringReader(@"<h1>Hello Mr. {{ model.name }}</h1>")
            );

            var currentStartupContext = CreateContext(new [] {location});
            this.engine = new DotLiquidViewEngine(new LiquidNancyFileSystem(currentStartupContext));
            var stream = new MemoryStream();

            // When
            var response = this.engine.RenderView(location, new { lastname = "test" }, this.renderContext);
            response.Contents.Invoke(stream);

            // Then
            stream.ShouldEqual("<h1>Hello Mr. [Can't find :name in the model]</h1>");
        }
        public void Render_should_return_nhaml_results()
        {
            // Given, When
            var result = engine.RenderView(GetDummyViewLocationResult(), null, this.renderContext);

            //Then
            var actualResult = new MemoryStream();
            const string expectedResult = "Hello World";
            result.Contents.Invoke(actualResult);
            actualResult.ShouldEqual(expectedResult);
        }
        public void When_passing_a_null_model_should_return_a_null_model_message_if_called()
        {
            // Given
            var location = new ViewLocationResult(
                string.Empty,
                string.Empty,
                "liquid",
                () => new StringReader(@"<h1>Hello Mr. {{ model.name }}</h1>")
            );

            var currentStartupContext = CreateContext(new [] {location});
            this.engine = new DotLiquidViewEngine(new LiquidNancyFileSystem(currentStartupContext));

            var stream = new MemoryStream();

            // When
            var response = this.engine.RenderView(location, null, this.renderContext);
            response.Contents.Invoke(stream);

            // Then
            stream.ShouldEqual("<h1>Hello Mr. [Model is null]</h1>");
        }