// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Collections.Generic;
using System.Linq;
using Moq;
using Xunit;
namespace Microsoft.AspNet.Mvc.Rendering
{
///
/// Tests the 's link generation methods.
///
public class HtmlHelperLinkGenerationTest
{
public static IEnumerable ActionLinkGenerationData
{
get
{
yield return new object[] {
"Details", "Product", new { isprint = "true", showreviews = "true" }, "https", "www.contoso.com", "h1",
new { p1 = "p1-value" } };
yield return new object[] {
"Details", "Product", new { isprint = "true", showreviews = "true" }, "https", "www.contoso.com", null, null };
yield return new object[] {
"Details", "Product", new { isprint = "true", showreviews = "true" }, "https", null, null, null };
yield return new object[] {
"Details", "Product", new { isprint = "true", showreviews = "true" }, null, null, null, null };
yield return new object[] {
"Details", "Product", null, null, null, null, null };
yield return new object[] {
null, null, null, null, null, null, null };
}
}
[Theory]
[MemberData(nameof(ActionLinkGenerationData))]
public void ActionLink_GeneratesLink_WithExpectedValues(
string action,
string controller,
object routeValues,
string protocol,
string hostname,
string fragment,
object htmlAttributes)
{
//Arrange
string expectedLink = string.Format(@"Details ",
protocol,
hostname,
controller,
action,
GetRouteValuesAsString(routeValues),
fragment,
GetHtmlAttributesAsString(htmlAttributes));
var urlHelper = new Mock();
urlHelper.Setup(
h => h.Action(
It.IsAny(),
It.IsAny(),
It.IsAny(),
It.IsAny(),
It.IsAny(),
It.IsAny()))
.Returns(
(actn, cntrlr, rvalues, prtcl, hname, frgmt) =>
string.Format("{0}{1}{2}{3}{4}{5}",
prtcl,
hname,
cntrlr,
actn,
GetRouteValuesAsString(rvalues),
frgmt));
var htmlHelper = DefaultTemplatesUtilities.GetHtmlHelper(urlHelper.Object);
// Act
var actualLink = htmlHelper.ActionLink(
linkText: "Details",
actionName: action,
controllerName: controller,
protocol: protocol,
hostname: hostname,
fragment: fragment,
routeValues: routeValues,
htmlAttributes: htmlAttributes).ToString();
// Assert
Assert.Equal(expectedLink, actualLink);
}
public static IEnumerable RouteLinkGenerationData
{
get
{
yield return new object[] {
"default", new { isprint = "true", showreviews = "true" }, "https", "www.contoso.com", "h1",
new { p1 = "p1-value" } };
yield return new object[] {
"default", new { isprint = "true", showreviews = "true" }, "https", "www.contoso.com", null, null };
yield return new object[] {
"default", new { isprint = "true", showreviews = "true" }, "https", null, null, null };
yield return new object[] {
"default", new { isprint = "true", showreviews = "true" }, null, null, null, null };
yield return new object[] {
"default", null, null, null, null, null };
}
}
[Theory]
[MemberData(nameof(RouteLinkGenerationData))]
public void RouteLink_GeneratesLink_WithExpectedValues(
string routeName,
object routeValues,
string protocol,
string hostname,
string fragment,
object htmlAttributes)
{
//Arrange
string expectedLink = string.Format(@"Details ",
protocol,
hostname,
GetRouteValuesAsString(routeValues),
fragment,
GetHtmlAttributesAsString(htmlAttributes));
var urlHelper = new Mock();
urlHelper
.Setup(
h => h.RouteUrl(
It.IsAny(),
It.IsAny(),
It.IsAny(),
It.IsAny(),
It.IsAny()))
.Returns(
(rname, rvalues, prtcl, hname, frgmt) =>
string.Format("{0}{1}{2}{3}",
prtcl,
hname,
GetRouteValuesAsString(rvalues),
frgmt));
var htmlHelper = DefaultTemplatesUtilities.GetHtmlHelper(urlHelper.Object);
// Act
var actualLink = htmlHelper.RouteLink(
linkText: "Details",
routeName: routeName,
protocol: protocol,
hostName: hostname,
fragment: fragment,
routeValues: routeValues,
htmlAttributes: htmlAttributes).ToString();
// Assert
Assert.Equal(expectedLink, actualLink);
}
private string GetRouteValuesAsString(object routeValues)
{
var dict = TypeHelper.ObjectToDictionary(routeValues);
return string.Join(string.Empty, dict.Select(kvp => string.Format("{0}={1}", kvp.Key, kvp.Value.ToString())));
}
private string GetHtmlAttributesAsString(object routeValues)
{
var dict = TypeHelper.ObjectToDictionary(routeValues);
return string.Join(string.Empty, dict.Select(kvp => string.Format(" {0}=\"{1}\"", kvp.Key, kvp.Value.ToString())));
}
}
}