Files
acceptance-tests
data
debian
docs
external
Newtonsoft.Json
api-doc-tools
api-snapshot
aspnetwebstack
packages
src
test
Microsoft.TestCommon
Microsoft.Web.Helpers.Test
Microsoft.Web.Http.Data.Test
Microsoft.Web.Mvc.Test
Microsoft.Web.WebPages.OAuth.Test
SPA.Test
System.Json.Test.Integration
System.Json.Test.Unit
System.Net.Http.Formatting.Test.Integration
System.Net.Http.Formatting.Test.Unit
System.Web.Helpers.Test
System.Web.Http.Integration.Test
System.Web.Http.SelfHost.Test
System.Web.Http.Test
Common
Controllers
Dispatcher
Filters
Hosting
Internal
Metadata
ModelBinding
Properties
Query
Routing
Services
Tracing
Util
Validation
ValueProviders
AuthorizeAttributeTest.cs
DictionaryExtensionsTest.cs
HttpRequestMessageExtensionsTest.cs
HttpResponseExceptionTest.cs
HttpResponseMessageExtensionsTest.cs
HttpRouteCollectionExtensionsTest.cs
HttpServerTest.cs
QueryableAttributeTest.cs
System.Web.Http.Test.csproj
packages.config
System.Web.Http.WebHost.Test
System.Web.Mvc.Test
System.Web.Razor.Test
System.Web.WebPages.Administration.Test
System.Web.WebPages.Deployment.Test
System.Web.WebPages.Razor.Test
System.Web.WebPages.Test
WebMatrix.Data.Test
WebMatrix.WebData.Test
Settings.StyleCop
tools
.gitattributes
.gitignore
License.txt
README.md
Runtime.msbuild
Runtime.sln
Runtime.xunit
Settings.StyleCop
build.cmd
binary-reference-assemblies
bockbuild
boringssl
cecil
cecil-legacy
corefx
corert
helix-binaries
ikdasm
ikvm
illinker-test-assets
linker
llvm
nuget-buildtasks
nunit-lite
roslyn-binaries
rx
xunit-binaries
how-to-bump-roslyn-binaries.md
ikvm-native
libgc
llvm
m4
man
mcs
mk
mono
msvc
po
runtime
samples
scripts
support
tools
COPYING.LIB
LICENSE
Makefile.am
Makefile.in
NEWS
README.md
acinclude.m4
aclocal.m4
autogen.sh
code_of_conduct.md
compile
config.guess
config.h.in
config.rpath
config.sub
configure.REMOVED.git-id
configure.ac.REMOVED.git-id
depcomp
install-sh
ltmain.sh.REMOVED.git-id
missing
mkinstalldirs
mono-uninstalled.pc.in
test-driver
winconfig.h
linux-packaging-mono/external/aspnetwebstack/test/System.Web.Http.Test/DictionaryExtensionsTest.cs
Jo Shields a575963da9 Imported Upstream version 3.6.0
Former-commit-id: da6be194a6b1221998fc28233f2503bd61dd9d14
2014-08-13 10:39:27 +01:00

154 lines
4.7 KiB
C#

// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.
using System.Collections.Generic;
using System.Linq;
using System.Net;
using Microsoft.TestCommon;
using Xunit;
using Xunit.Extensions;
using Assert = Microsoft.TestCommon.AssertEx;
namespace System.Web.Http
{
public class DictionaryExtensionsTest
{
[Fact]
public void IsCorrectType()
{
Assert.Type.HasProperties(typeof(DictionaryExtensions), TypeAssert.TypeProperties.IsStatic | TypeAssert.TypeProperties.IsClass);
}
[Fact]
public void TryGetValueThrowsOnNullCollection()
{
string value;
Assert.ThrowsArgumentNull(() => DictionaryExtensions.TryGetValue<string>(null, String.Empty, out value), "collection");
}
[Fact]
public void TryGetValueThrowsOnNullKey()
{
IDictionary<string, object> dict = new Dictionary<string, object>();
string value;
Assert.ThrowsArgumentNull(() => dict.TryGetValue<string>(null, out value), "key");
}
public static TheoryDataSet<object> DictionaryValues
{
get
{
return new TheoryDataSet<object>
{
"test",
new string[] { "A", "B", "C" },
8,
new List<int> {1, 2, 3},
1D,
(IEnumerable<double>)new List<double> { 1D, 2D, 3D },
new Uri("http://some.host"),
Guid.NewGuid(),
HttpStatusCode.NotImplemented,
new HttpStatusCode[] { HttpStatusCode.Accepted, HttpStatusCode.Ambiguous, HttpStatusCode.BadGateway }
};
}
}
[Fact]
public void TryGetValueReturnsFalse()
{
// Arrange
IDictionary<string, object> dict = new Dictionary<string, object>();
// Act
string resultValue = null;
bool result = dict.TryGetValue("notfound", out resultValue);
// Assert
Assert.False(result);
Assert.Null(resultValue);
}
[Theory]
[PropertyData("DictionaryValues")]
public void TryGetValueReturnsTrue<T>(T value)
{
// Arrange
IDictionary<string, object> dict = new Dictionary<string, object>()
{
{ "key", value }
};
// Act
T resultValue;
bool result = DictionaryExtensions.TryGetValue(dict, "key", out resultValue);
// Assert
Assert.True(result);
Assert.Equal(typeof(T), resultValue.GetType());
Assert.Equal(value, resultValue);
}
[Fact]
public void GetValueThrowsOnNullCollection()
{
Assert.ThrowsArgumentNull(() => DictionaryExtensions.GetValue<string>(null, String.Empty), "collection");
}
[Fact]
public void GetValueThrowsOnNullKey()
{
IDictionary<string, object> dict = new Dictionary<string, object>();
Assert.ThrowsArgumentNull(() => dict.GetValue<string>(null), "key");
}
[Fact]
public void GetValueThrowsOnNotFound()
{
IDictionary<string, object> dict = new Dictionary<string, object>();
Assert.Throws<InvalidOperationException>(() => dict.GetValue<string>("notfound"));
}
[Theory]
[PropertyData("DictionaryValues")]
public void GetValueReturnsValue<T>(T value)
{
// Arrange
IDictionary<string, object> dict = new Dictionary<string, object>()
{
{ "key", value }
};
// Act
T resultValue = DictionaryExtensions.GetValue<T>(dict, "key");
// Assert
Assert.Equal(typeof(T), resultValue.GetType());
Assert.Equal(value, resultValue);
}
[Fact]
public void FindKeysWithPrefixRecognizesRootChilden()
{
// Arrange
IDictionary<string, int> dict = new Dictionary<string, int>()
{
{ "[0]", 1 },
{ "Name", 2 },
{ "Address.Street", 3 },
{ "", 4 }
};
// Act
List<int> results = DictionaryExtensions.FindKeysWithPrefix<int>(dict, "").Select(kvp => kvp.Value).ToList();
// Assert
Assert.Equal(4, results.Count);
Assert.Contains(1, results);
Assert.Contains(2, results);
Assert.Contains(3, results);
Assert.Contains(4, results);
}
}
}