Add tests for HtmlContentBuilderExtensions

This commit is contained in:
Ryan Nowak 2015-09-15 23:45:25 -07:00
parent 7ef2242805
commit e56c3806b6
4 changed files with 267 additions and 0 deletions

View File

@ -51,6 +51,8 @@ Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "Microsoft.Framework.Primiti
EndProject
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "Microsoft.Framework.Primitives.Tests", "test\Microsoft.Framework.Primitives.Tests\Microsoft.Framework.Primitives.Tests.xproj", "{61F72E92-B3AE-4A10-B838-44F80AED40AE}"
EndProject
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "Microsoft.AspNet.Html.Abstractions.Test", "test\Microsoft.AspNet.Html.Abstractions.Test\Microsoft.AspNet.Html.Abstractions.Test.xproj", "{2D187B88-94BD-4A39-AC97-F8F8B9363301}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@ -289,6 +291,18 @@ Global
{61F72E92-B3AE-4A10-B838-44F80AED40AE}.Release|Mixed Platforms.Build.0 = Release|Any CPU
{61F72E92-B3AE-4A10-B838-44F80AED40AE}.Release|x86.ActiveCfg = Release|Any CPU
{61F72E92-B3AE-4A10-B838-44F80AED40AE}.Release|x86.Build.0 = Release|Any CPU
{2D187B88-94BD-4A39-AC97-F8F8B9363301}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{2D187B88-94BD-4A39-AC97-F8F8B9363301}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2D187B88-94BD-4A39-AC97-F8F8B9363301}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
{2D187B88-94BD-4A39-AC97-F8F8B9363301}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU
{2D187B88-94BD-4A39-AC97-F8F8B9363301}.Debug|x86.ActiveCfg = Debug|Any CPU
{2D187B88-94BD-4A39-AC97-F8F8B9363301}.Debug|x86.Build.0 = Debug|Any CPU
{2D187B88-94BD-4A39-AC97-F8F8B9363301}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2D187B88-94BD-4A39-AC97-F8F8B9363301}.Release|Any CPU.Build.0 = Release|Any CPU
{2D187B88-94BD-4A39-AC97-F8F8B9363301}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
{2D187B88-94BD-4A39-AC97-F8F8B9363301}.Release|Mixed Platforms.Build.0 = Release|Any CPU
{2D187B88-94BD-4A39-AC97-F8F8B9363301}.Release|x86.ActiveCfg = Release|Any CPU
{2D187B88-94BD-4A39-AC97-F8F8B9363301}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
@ -315,5 +329,6 @@ Global
{1D0764B4-1DEB-4232-A714-D4B7E846918A} = {982F09D8-621E-4872-BA7B-BBDEA47D1EFD}
{E5FACCD4-6327-43AA-80A9-AE6F4A3BFE6A} = {A5A15F1C-885A-452A-A731-B0173DDBD913}
{61F72E92-B3AE-4A10-B838-44F80AED40AE} = {F31FF137-390C-49BF-A3BD-7C6ED3597C21}
{2D187B88-94BD-4A39-AC97-F8F8B9363301} = {F31FF137-390C-49BF-A3BD-7C6ED3597C21}
EndGlobalSection
EndGlobal

View File

@ -0,0 +1,216 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using System.IO;
using Microsoft.Framework.WebEncoders;
using Microsoft.Framework.WebEncoders.Testing;
using Xunit;
namespace Microsoft.AspNet.Html.Abstractions.Test
{
public class HtmlContentBuilderExtensionsTest
{
[Fact]
public void Builder_AppendLine_Empty()
{
// Arrange
var builder = new TestHtmlContentBuilder();
// Act
builder.AppendLine();
// Assert
Assert.Collection(
builder.Entries,
entry => Assert.Equal(Environment.NewLine, HtmlContentToString(entry)));
}
[Fact]
public void Builder_AppendLine_String()
{
// Arrange
var builder = new TestHtmlContentBuilder();
// Act
builder.AppendLine("Hi");
// Assert
Assert.Collection(
builder.Entries,
entry => Assert.Equal("Hi", Assert.IsType<UnencodedString>(entry).Value),
entry => Assert.Equal(Environment.NewLine, HtmlContentToString(entry)));
}
[Fact]
public void Builder_AppendLine_IHtmlContent()
{
// Arrange
var builder = new TestHtmlContentBuilder();
var content = new OtherHtmlContent("Hi");
// Act
builder.AppendLine(content);
// Assert
Assert.Collection(
builder.Entries,
entry => Assert.Same(content, entry),
entry => Assert.Equal(Environment.NewLine, HtmlContentToString(entry)));
}
[Fact]
public void Builder_AppendLineEncoded_String()
{
// Arrange
var builder = new TestHtmlContentBuilder();
// Act
builder.AppendLineEncoded("Hi");
// Assert
Assert.Collection(
builder.Entries,
entry => Assert.Equal("Hi", Assert.IsType<EncodedString>(entry).Value),
entry => Assert.Equal(Environment.NewLine, HtmlContentToString(entry)));
}
[Fact]
public void Builder_SetContent_String()
{
// Arrange
var builder = new TestHtmlContentBuilder();
builder.Append("Existing Content. Will be Cleared.");
// Act
builder.SetContent("Hi");
// Assert
Assert.Collection(
builder.Entries,
entry => Assert.Equal("Hi", Assert.IsType<UnencodedString>(entry).Value));
}
[Fact]
public void Builder_SetContent_IHtmlContent()
{
// Arrange
var builder = new TestHtmlContentBuilder();
builder.Append("Existing Content. Will be Cleared.");
var content = new OtherHtmlContent("Hi");
// Act
builder.SetContent(content);
// Assert
Assert.Collection(
builder.Entries,
entry => Assert.Same(content, entry));
}
[Fact]
public void Builder_SetContentEncoded_String()
{
// Arrange
var builder = new TestHtmlContentBuilder();
builder.Append("Existing Content. Will be Cleared.");
// Act
builder.SetContentEncoded("Hi");
// Assert
Assert.Collection(
builder.Entries,
entry => Assert.Equal("Hi", Assert.IsType<EncodedString>(entry).Value));
}
private static string HtmlContentToString(IHtmlContent content)
{
using (var writer = new StringWriter())
{
content.WriteTo(writer, new CommonTestEncoder());
return writer.ToString();
}
}
private class TestHtmlContentBuilder : IHtmlContentBuilder
{
public List<IHtmlContent> Entries { get; } = new List<IHtmlContent>();
public IHtmlContentBuilder Append(string unencoded)
{
Entries.Add(new UnencodedString(unencoded));
return this;
}
public IHtmlContentBuilder Append(IHtmlContent content)
{
Entries.Add(content);
return this;
}
public IHtmlContentBuilder AppendEncoded(string encoded)
{
Entries.Add(new EncodedString(encoded));
return this;
}
public void Clear()
{
Entries.Clear();
}
public void WriteTo(TextWriter writer, IHtmlEncoder encoder)
{
throw new NotImplementedException();
}
}
private class EncodedString : IHtmlContent
{
public EncodedString(string value)
{
Value = value;
}
public string Value { get; }
public void WriteTo(TextWriter writer, IHtmlEncoder encoder)
{
throw new NotImplementedException();
}
}
private class UnencodedString : IHtmlContent
{
public UnencodedString(string value)
{
Value = value;
}
public string Value { get; }
public void WriteTo(TextWriter writer, IHtmlEncoder encoder)
{
throw new NotImplementedException();
}
}
private class OtherHtmlContent : IHtmlContent
{
public OtherHtmlContent(string value)
{
Value = value;
}
public string Value { get; }
public void WriteTo(TextWriter writer, IHtmlEncoder encoder)
{
throw new NotImplementedException();
}
}
}
}

View File

@ -0,0 +1,21 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">14.0</VisualStudioVersion>
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
</PropertyGroup>
<Import Project="$(VSToolsPath)\DNX\Microsoft.DNX.Props" Condition="'$(VSToolsPath)' != ''" />
<PropertyGroup Label="Globals">
<ProjectGuid>2d187b88-94bd-4a39-ac97-f8f8b9363301</ProjectGuid>
<RootNamespace>Microsoft.AspNet.Html.Abstractions.Test</RootNamespace>
<BaseIntermediateOutputPath Condition="'$(BaseIntermediateOutputPath)'=='' ">..\..\artifacts\obj\$(MSBuildProjectName)</BaseIntermediateOutputPath>
<OutputPath Condition="'$(OutputPath)'=='' ">..\..\artifacts\bin\$(MSBuildProjectName)\</OutputPath>
</PropertyGroup>
<PropertyGroup>
<SchemaVersion>2.0</SchemaVersion>
</PropertyGroup>
<ItemGroup>
<Service Include="{82a7f48d-3b50-4b1e-b82e-3ada8210c358}" />
</ItemGroup>
<Import Project="$(VSToolsPath)\DNX\Microsoft.DNX.targets" Condition="'$(VSToolsPath)' != ''" />
</Project>

View File

@ -0,0 +1,15 @@
{
"dependencies": {
"Microsoft.AspNet.Html.Abstractions": "1.0.0-*",
"Microsoft.AspNet.Testing": "1.0.0-*",
"Microsoft.Framework.WebEncoders.Testing": "1.0.0-*",
"xunit.runner.aspnet": "2.0.0-aspnet-*"
},
"commands": {
"test": "xunit.runner.aspnet"
},
"frameworks": {
"dnx451": { },
"dnxcore50": { }
}
}