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,95 @@
// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.
using System.Collections.Generic;
using System.Web.WebPages.Scope;
using Moq;
using Xunit;
using Assert = Microsoft.TestCommon.AssertEx;
namespace System.Web.WebPages.Test
{
public class AspNetRequestStorageProvider
{
[Fact]
public void AspNetStorageProviderReturnsApplicationStateBeforeAppStart()
{
// Arrange
var provider = GetProvider(() => false);
// Act and Assert
Assert.NotNull(provider.ApplicationScope);
Assert.NotNull(provider.GlobalScope);
Assert.Equal(provider.ApplicationScope, provider.GlobalScope);
}
[Fact]
public void AspNetStorageProviderThrowsWhenAccessingRequestScopeBeforeAppStart()
{
// Arrange
var provider = GetProvider(() => false);
// Act and Assert
Assert.Throws<InvalidOperationException>(
() => { var x = provider.RequestScope; },
"RequestScope cannot be created when _AppStart is executing.");
}
[Fact]
public void AspNetStorageProviderThrowsWhenAssigningScopeBeforeAppStart()
{
// Arrange
var provider = GetProvider(() => false);
// Act and Assert
Assert.Throws<InvalidOperationException>(
() => { provider.CurrentScope = new ScopeStorageDictionary(); },
"Storage scopes cannot be created when _AppStart is executing.");
}
[Fact]
public void AspNetStorageProviderReturnsRequestScopeAfterAppStart()
{
// Arrange
var provider = GetProvider();
// Act and Assert
Assert.NotNull(provider.RequestScope);
Assert.Equal(provider.RequestScope, provider.CurrentScope);
}
[Fact]
public void AspNetStorageRetrievesRequestScopeAfterSettingAnonymousScopes()
{
// Arrange
var provider = GetProvider();
// Act
var requestScope = provider.RequestScope;
var Scope = new ScopeStorageDictionary();
provider.CurrentScope = Scope;
Assert.Equal(provider.CurrentScope, Scope);
Assert.Equal(provider.RequestScope, requestScope);
}
[Fact]
public void AspNetStorageUsesApplicationScopeAsGlobalScope()
{
// Arrange
var provider = GetProvider();
// Act and Assert
Assert.Equal(provider.GlobalScope, provider.ApplicationScope);
}
private AspNetRequestScopeStorageProvider GetProvider(Func<bool> appStartExecuted = null)
{
Mock<HttpContextBase> context = new Mock<HttpContextBase>();
context.Setup(c => c.Items).Returns(new Dictionary<object, object>());
appStartExecuted = appStartExecuted ?? (() => true);
return new AspNetRequestScopeStorageProvider(context.Object, appStartExecuted);
}
}
}

View File

@@ -0,0 +1,172 @@
// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.
using System.Collections.Generic;
using System.Web.WebPages.Scope;
using Xunit;
namespace System.Web.WebPages.Test
{
public class ScopeStorageDictionaryTest
{
[Fact]
public void ScopeStorageDictionaryLooksUpLocalValuesFirst()
{
// Arrange
var stateStorage = GetChainedStorageStateDictionary();
// Act and Assert
Assert.Equal(stateStorage["f"], "f2");
}
[Fact]
public void ScopeStorageDictionaryOverridesParentValuesWithLocalValues()
{
// Arrange
var stateStorage = GetChainedStorageStateDictionary();
// Act and Assert
Assert.Equal(stateStorage["a"], "a2");
Assert.Equal(stateStorage["d"], "d2");
}
[Fact]
public void ScopeStorageDictionaryLooksUpParentValuesWhenNotFoundLocally()
{
// Arrange
var stateStorage = GetChainedStorageStateDictionary();
// Act and Assert
Assert.Equal(stateStorage["c"], "c0");
Assert.Equal(stateStorage["b"], "b1");
}
[Fact]
public void ScopeStorageDictionaryTreatsNullAsOrdinaryValues()
{
// Arrange
var stateStorage = GetChainedStorageStateDictionary();
stateStorage["b"] = null;
// Act and Assert
Assert.Null(stateStorage["b"]);
}
[Fact]
public void ContainsKeyReturnsTrueIfItContainsKey()
{
// Arrange
var scopeStorage = GetChainedStorageStateDictionary();
// Act and Assert
Assert.True(scopeStorage.ContainsKey("f"));
}
[Fact]
public void ContainsKeyReturnsTrueIfBaseContainsKey()
{
// Arrange
var scopeStorage = GetChainedStorageStateDictionary();
// Act and Assert
Assert.True(scopeStorage.ContainsKey("e"));
}
[Fact]
public void ContainsKeyReturnsFalseIfItDoesNotContainKeyAndBaseIsNull()
{
// Arrange
var scopeStorage = new ScopeStorageDictionary() { { "foo", "bar" } };
// Act and Assert
Assert.False(scopeStorage.ContainsKey("baz"));
}
[Fact]
public void CountReturnsCountFromCurrentAndBaseScope()
{
// Arrange
var scopeStorage = GetChainedStorageStateDictionary();
// Act and Assert
Assert.Equal(6, scopeStorage.Count);
}
[Fact]
public void ScopeStorageDictionaryGetsValuesFromCurrentAndBaseScope()
{
// Arrange
var scopeStorage = GetChainedStorageStateDictionary();
// Act and Assert
Assert.Equal(scopeStorage["a"], "a2");
Assert.Equal(scopeStorage["b"], "b1");
Assert.Equal(scopeStorage["c"], "c0");
Assert.Equal(scopeStorage["d"], "d2");
Assert.Equal(scopeStorage["e"], "e1");
Assert.Equal(scopeStorage["f"], "f2");
}
[Fact]
public void ClearRemovesAllItemsFromCurrentScope()
{
// Arrange
var dictionary = new ScopeStorageDictionary { { "foo", "bar" }, { "foo2", "bar2" } };
// Act
dictionary.Clear();
// Assert
Assert.Equal(0, dictionary.Count);
}
[Fact]
public void ScopeStorageDictionaryIsNotReadOnly()
{
// Arrange
var dictionary = new ScopeStorageDictionary();
// Act and Assert
Assert.False(dictionary.IsReadOnly);
}
[Fact]
public void CopyToCopiesItemsToArrayAtSpecifiedIndex()
{
// Arrange
var dictionary = GetChainedStorageStateDictionary();
var array = new KeyValuePair<object, object>[8];
// Act
dictionary.CopyTo(array, 2);
// Assert
Assert.Equal(array[2].Key, "a");
Assert.Equal(array[2].Value, "a2");
Assert.Equal(array[4].Key, "f");
Assert.Equal(array[4].Value, "f2");
Assert.Equal(array[7].Key, "c");
Assert.Equal(array[7].Value, "c0");
}
private ScopeStorageDictionary GetChainedStorageStateDictionary()
{
var root = new ScopeStorageDictionary();
root["a"] = "a0";
root["b"] = "b0";
root["c"] = "c0";
var firstGen = new ScopeStorageDictionary(baseScope: root);
firstGen["a"] = "a1";
firstGen["b"] = "b1";
firstGen["d"] = "d1";
firstGen["e"] = "e1";
var secondGen = new ScopeStorageDictionary(baseScope: firstGen);
secondGen["a"] = "a2";
secondGen["d"] = "d2";
secondGen["f"] = "f2";
return secondGen;
}
}
}

View File

@@ -0,0 +1,50 @@
// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.
using System.Collections.Generic;
using System.Web.WebPages.Scope;
using Xunit;
namespace System.Web.WebPages.Test
{
public class ScopeStorageKeyComparerTest
{
[Fact]
public void ScopeStorageComparerPerformsCaseInsensitiveOrdinalComparisonForStrings()
{
// Arrange
var dictionary = new Dictionary<object, object>(ScopeStorageComparer.Instance) { { "foo", "bar" } };
// Act and Assert
Assert.Equal(dictionary["foo"], "bar");
Assert.Equal(dictionary["foo"], dictionary["FOo"]);
}
[Fact]
public void ScopeStorageComparerPerformsRegularComparisonForOtherTypes()
{
// Arrange
var stateStorage = new Dictionary<object, object> { { 4, "4-value" }, { new Person { ID = 10 }, "person-value" } };
// Act and Assert
Assert.Equal(stateStorage[4], "4-value");
Assert.Equal(stateStorage[(int)8 / 2], stateStorage[4]);
Assert.Equal(stateStorage[new Person { ID = 10 }], "person-value");
}
private class Person
{
public int ID { get; set; }
public override bool Equals(object o)
{
var other = o as Person;
return (other != null) && (other.ID == ID);
}
public override int GetHashCode()
{
return ID;
}
}
}
}

View File

@@ -0,0 +1,80 @@
// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Web.WebPages.Scope;
using Xunit;
using Assert = Microsoft.TestCommon.AssertEx;
namespace System.Web.WebPages.Test
{
public class WebConfigScopeStorageTest
{
[Fact]
public void WebConfigScopeStorageReturnsConfigValue()
{
// Arrange
var stateStorage = GetWebConfigScopeStorage();
// Assert
Assert.Equal(stateStorage["foo1"], "bar1");
Assert.Equal(stateStorage["foo2"], "bar2");
}
[Fact]
public void WebConfigScopeStoragePerformsCaseInsensitiveKeyCompares()
{
// Arrange
var stateStorage = GetWebConfigScopeStorage();
// Assert
Assert.Equal(stateStorage["FOO1"], "bar1");
Assert.Equal(stateStorage["FoO2"], "bar2");
}
[Fact]
public void WebConfigScopeStorageThrowsWhenWriting()
{
// Arrange
var stateStorage = GetWebConfigScopeStorage();
// Act and Assert
Assert.Throws<NotSupportedException>(() => stateStorage["foo"] = "some value", "Storage scope is read only.");
Assert.Throws<NotSupportedException>(() => stateStorage.Add("foo", "value"), "Storage scope is read only.");
Assert.Throws<NotSupportedException>(() => stateStorage.Remove("foo"), "Storage scope is read only.");
Assert.Throws<NotSupportedException>(() => stateStorage.Clear(), "Storage scope is read only.");
Assert.Throws<NotSupportedException>(() => stateStorage.Remove(new KeyValuePair<object, object>("foo", "bar")), "Storage scope is read only.");
}
[Fact]
public void WebConfigStateAllowsEnumeratingOverConfigItems()
{
// Arrange
var dictionary = new Dictionary<string, string> { { "a", "b" }, { "c", "d" }, { "x12", "y34" } };
var stateStorage = GetWebConfigScopeStorage(dictionary);
// Act and Assert
Assert.True(dictionary.All(item => item.Value == stateStorage[item.Key] as string));
}
private WebConfigScopeDictionary GetWebConfigScopeStorage(IDictionary<string, string> values = null)
{
NameValueCollection collection = new NameValueCollection();
if (values == null)
{
collection.Add("foo1", "bar1");
collection.Add("foo2", "bar2");
}
else
{
foreach (var item in values)
{
collection.Add(item.Key, item.Value);
}
}
return new WebConfigScopeDictionary(collection);
}
}
}