Imported Upstream version 3.6.0

Former-commit-id: da6be194a6b1221998fc28233f2503bd61dd9d14
This commit is contained in:
Jo Shields
2014-08-13 10:39:27 +01:00
commit a575963da9
50588 changed files with 8155799 additions and 0 deletions

View File

@@ -0,0 +1,31 @@
// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using System.Linq;
namespace Microsoft.Web.UnitTestUtil
{
public static class AnonymousObject
{
public static string Inspect(object obj)
{
if (obj == null)
{
return "(null)";
}
object[] args = Enumerable.Empty<Object>().ToArray();
IEnumerable<string> values = obj.GetType()
.GetProperties()
.Select(prop => String.Format("{0}: {1}", prop.Name, prop.GetValue(obj, args)));
if (!values.Any())
{
return "(no properties)";
}
return "{ " + values.Aggregate((left, right) => left + ", " + right) + " }";
}
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,17 @@
// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.
using System.Collections.Generic;
using Moq;
namespace System.Web.Mvc.Test
{
public static class HttpContextHelpers
{
public static Mock<HttpContextBase> GetMockHttpContext()
{
Mock<HttpContextBase> mockContext = new Mock<HttpContextBase>();
mockContext.Setup(m => m.Items).Returns(new Dictionary<object, object>());
return mockContext;
}
}
}

View File

@@ -0,0 +1,176 @@
// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.
using System;
using System.Collections;
using System.IO;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using Moq;
namespace Microsoft.Web.UnitTestUtil
{
public static class MvcHelper
{
public const string AppPathModifier = "/$(SESSION)";
public static HtmlHelper<object> GetHtmlHelper()
{
HttpContextBase httpcontext = GetHttpContext("/app/", null, null);
RouteCollection rt = new RouteCollection();
rt.Add(new Route("{controller}/{action}/{id}", null) { Defaults = new RouteValueDictionary(new { id = "defaultid" }) });
rt.Add("namedroute", new Route("named/{controller}/{action}/{id}", null) { Defaults = new RouteValueDictionary(new { id = "defaultid" }) });
RouteData rd = new RouteData();
rd.Values.Add("controller", "home");
rd.Values.Add("action", "oldaction");
ViewDataDictionary vdd = new ViewDataDictionary();
ViewContext viewContext = new ViewContext()
{
HttpContext = httpcontext,
RouteData = rd,
ViewData = vdd
};
Mock<IViewDataContainer> mockVdc = new Mock<IViewDataContainer>();
mockVdc.Setup(vdc => vdc.ViewData).Returns(vdd);
HtmlHelper<object> htmlHelper = new HtmlHelper<object>(viewContext, mockVdc.Object, rt);
return htmlHelper;
}
public static HtmlHelper GetHtmlHelper(string protocol, int port)
{
HttpContextBase httpcontext = GetHttpContext("/app/", null, null, protocol, port);
RouteCollection rt = new RouteCollection();
rt.Add(new Route("{controller}/{action}/{id}", null) { Defaults = new RouteValueDictionary(new { id = "defaultid" }) });
rt.Add("namedroute", new Route("named/{controller}/{action}/{id}", null) { Defaults = new RouteValueDictionary(new { id = "defaultid" }) });
RouteData rd = new RouteData();
rd.Values.Add("controller", "home");
rd.Values.Add("action", "oldaction");
ViewDataDictionary vdd = new ViewDataDictionary();
Mock<ViewContext> mockViewContext = new Mock<ViewContext>();
mockViewContext.Setup(c => c.HttpContext).Returns(httpcontext);
mockViewContext.Setup(c => c.RouteData).Returns(rd);
mockViewContext.Setup(c => c.ViewData).Returns(vdd);
Mock<IViewDataContainer> mockVdc = new Mock<IViewDataContainer>();
mockVdc.Setup(vdc => vdc.ViewData).Returns(vdd);
HtmlHelper htmlHelper = new HtmlHelper(mockViewContext.Object, mockVdc.Object, rt);
return htmlHelper;
}
public static HtmlHelper GetHtmlHelper(ViewDataDictionary viewData)
{
Mock<ViewContext> mockViewContext = new Mock<ViewContext>() { CallBase = true };
mockViewContext.Setup(c => c.ViewData).Returns(viewData);
mockViewContext.Setup(c => c.HttpContext.Items).Returns(new Hashtable());
IViewDataContainer container = GetViewDataContainer(viewData);
return new HtmlHelper(mockViewContext.Object, container);
}
public static HtmlHelper<TModel> GetHtmlHelper<TModel>(ViewDataDictionary<TModel> viewData)
{
Mock<ViewContext> mockViewContext = new Mock<ViewContext>() { CallBase = true };
mockViewContext.Setup(c => c.ViewData).Returns(viewData);
mockViewContext.Setup(c => c.HttpContext.Items).Returns(new Hashtable());
IViewDataContainer container = GetViewDataContainer(viewData);
return new HtmlHelper<TModel>(mockViewContext.Object, container);
}
public static HtmlHelper GetHtmlHelperWithPath(ViewDataDictionary viewData)
{
return GetHtmlHelperWithPath(viewData, "/");
}
public static HtmlHelper GetHtmlHelperWithPath(ViewDataDictionary viewData, string appPath)
{
ViewContext viewContext = GetViewContextWithPath(appPath, viewData);
Mock<IViewDataContainer> mockContainer = new Mock<IViewDataContainer>();
mockContainer.Setup(c => c.ViewData).Returns(viewData);
IViewDataContainer container = mockContainer.Object;
return new HtmlHelper(viewContext, container, new RouteCollection());
}
public static HtmlHelper<TModel> GetHtmlHelperWithPath<TModel>(ViewDataDictionary<TModel> viewData, string appPath)
{
ViewContext viewContext = GetViewContextWithPath(appPath, viewData);
Mock<IViewDataContainer> mockContainer = new Mock<IViewDataContainer>();
mockContainer.Setup(c => c.ViewData).Returns(viewData);
IViewDataContainer container = mockContainer.Object;
return new HtmlHelper<TModel>(viewContext, container, new RouteCollection());
}
public static HtmlHelper<TModel> GetHtmlHelperWithPath<TModel>(ViewDataDictionary<TModel> viewData)
{
return GetHtmlHelperWithPath(viewData, "/");
}
public static HttpContextBase GetHttpContext(string appPath, string requestPath, string httpMethod, string protocol, int port)
{
Mock<HttpContextBase> mockHttpContext = new Mock<HttpContextBase>();
if (!String.IsNullOrEmpty(appPath))
{
mockHttpContext.Setup(o => o.Request.ApplicationPath).Returns(appPath);
}
if (!String.IsNullOrEmpty(requestPath))
{
mockHttpContext.Setup(o => o.Request.AppRelativeCurrentExecutionFilePath).Returns(requestPath);
}
Uri uri;
if (port >= 0)
{
uri = new Uri(protocol + "://localhost" + ":" + Convert.ToString(port));
}
else
{
uri = new Uri(protocol + "://localhost");
}
mockHttpContext.Setup(o => o.Request.Url).Returns(uri);
mockHttpContext.Setup(o => o.Request.PathInfo).Returns(String.Empty);
if (!String.IsNullOrEmpty(httpMethod))
{
mockHttpContext.Setup(o => o.Request.HttpMethod).Returns(httpMethod);
}
mockHttpContext.Setup(o => o.Session).Returns((HttpSessionStateBase)null);
mockHttpContext.Setup(o => o.Response.ApplyAppPathModifier(It.IsAny<string>())).Returns<string>(r => AppPathModifier + r);
mockHttpContext.Setup(o => o.Items).Returns(new Hashtable());
return mockHttpContext.Object;
}
public static HttpContextBase GetHttpContext(string appPath, string requestPath, string httpMethod)
{
return GetHttpContext(appPath, requestPath, httpMethod, Uri.UriSchemeHttp.ToString(), -1);
}
public static ViewContext GetViewContextWithPath(string appPath, ViewDataDictionary viewData)
{
HttpContextBase httpContext = GetHttpContext(appPath, "/request", "GET");
Mock<ViewContext> mockViewContext = new Mock<ViewContext>() { DefaultValue = DefaultValue.Mock };
mockViewContext.Setup(c => c.HttpContext).Returns(httpContext);
mockViewContext.Setup(c => c.ViewData).Returns(viewData);
mockViewContext.Setup(c => c.Writer).Returns(new StringWriter());
return mockViewContext.Object;
}
public static ViewContext GetViewContextWithPath(ViewDataDictionary viewData)
{
return GetViewContextWithPath("/", viewData);
}
public static IViewDataContainer GetViewDataContainer(ViewDataDictionary viewData)
{
Mock<IViewDataContainer> mockContainer = new Mock<IViewDataContainer>();
mockContainer.Setup(c => c.ViewData).Returns(viewData);
return mockContainer.Object;
}
}
}

View File

@@ -0,0 +1,9 @@
// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.
namespace System.Web.Mvc.Test
{
public class Resolver<T> : IResolver<T>
{
public T Current { get; set; }
}
}

View File

@@ -0,0 +1,75 @@
// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Web.Mvc;
namespace Microsoft.Web.UnitTestUtil
{
// just a simple value provider used for unit testing
public sealed class SimpleValueProvider : Dictionary<string, object>, IValueProvider
{
private readonly CultureInfo _culture;
public SimpleValueProvider()
: this(null)
{
}
public SimpleValueProvider(CultureInfo culture)
: base(StringComparer.OrdinalIgnoreCase)
{
_culture = culture ?? CultureInfo.InvariantCulture;
}
// copied from ValueProviderUtil
public bool ContainsPrefix(string prefix)
{
foreach (string key in Keys)
{
if (key != null)
{
if (prefix.Length == 0)
{
return true; // shortcut - non-null key matches empty prefix
}
if (key.StartsWith(prefix, StringComparison.OrdinalIgnoreCase))
{
if (key.Length == prefix.Length)
{
return true; // exact match
}
else
{
switch (key[prefix.Length])
{
case '.': // known separator characters
case '[':
return true;
}
}
}
}
}
return false; // nothing found
}
public ValueProviderResult GetValue(string key)
{
object rawValue;
if (TryGetValue(key, out rawValue))
{
return new ValueProviderResult(rawValue, Convert.ToString(rawValue, _culture), _culture);
}
else
{
// value not found
return null;
}
}
}
}

View File

@@ -0,0 +1,16 @@
// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.
using System.Web.Mvc;
namespace Microsoft.Web.UnitTestUtil
{
public class SimpleViewDataContainer : IViewDataContainer
{
public SimpleViewDataContainer(ViewDataDictionary viewData)
{
ViewData = viewData;
}
public ViewDataDictionary ViewData { get; set; }
}
}