Files
acceptance-tests
data
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
Common
Extensions
Properties
JsonArrayTest.cs
JsonDefaultTest.cs
JsonObjectTest.cs
JsonPrimitiveTest.cs
JsonTypeTest.cs
JsonValueDynamicMetaObjectTest.cs
JsonValueDynamicTest.cs
JsonValueLinqExtensionsTest.cs
JsonValueTest.cs
System.Json.Test.Unit.csproj
packages.config
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
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
ikdasm
ikvm
linker
nuget-buildtasks
nunit-lite
roslyn-binaries
rx
xunit-binaries
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.Json.Test.Unit/JsonDefaultTest.cs
Jo Shields a575963da9 Imported Upstream version 3.6.0
Former-commit-id: da6be194a6b1221998fc28233f2503bd61dd9d14
2014-08-13 10:39:27 +01:00

156 lines
6.7 KiB
C#

// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.
using System.IO;
using System.Runtime.Serialization.Json;
using Xunit;
namespace System.Json
{
public class JsonDefaultTest
{
const string IndexerNotSupportedMsgFormat = "'{0}' type indexer is not supported on JsonValue of 'JsonType.Default' type.";
const string OperationNotAllowedOnDefaultMsgFormat = "Operation not supported on JsonValue instances of 'JsonType.Default' type.";
[Fact]
public void PropertiesTest()
{
JsonValue target = AnyInstance.DefaultJsonValue;
Assert.Equal(JsonType.Default, target.JsonType);
Assert.Equal(0, target.Count);
Assert.Equal(false, target.ContainsKey("hello"));
Assert.Equal(false, target.ContainsKey(String.Empty));
}
[Fact]
public void SaveTest()
{
JsonValue target = AnyInstance.DefaultJsonValue;
using (MemoryStream ms = new MemoryStream())
{
ExceptionHelper.Throws<InvalidOperationException>(() => target.Save(ms), "Operation not supported on JsonValue instances of 'JsonType.Default' type.");
}
}
[Fact]
public void ToStringTest()
{
JsonValue target;
target = AnyInstance.DefaultJsonValue;
Assert.Equal(target.ToString(), "Default");
}
[Fact(Skip = "See bug #228569 in CSDMain")]
public void ReadAsTests()
{
JsonValue target = AnyInstance.DefaultJsonValue;
string typeName = target.GetType().FullName;
string errorMsgFormat = "Cannot read '{0}' as '{1}' type.";
ExceptionHelper.Throws<NotSupportedException>(delegate { target.ReadAs(typeof(bool)); }, String.Format(errorMsgFormat, typeName, typeof(bool)));
ExceptionHelper.Throws<NotSupportedException>(delegate { target.ReadAs(typeof(string)); }, String.Format(errorMsgFormat, typeName, typeof(string)));
ExceptionHelper.Throws<NotSupportedException>(delegate { target.ReadAs(typeof(JsonObject)); }, String.Format(errorMsgFormat, typeName, typeof(JsonObject)));
ExceptionHelper.Throws<NotSupportedException>(delegate { target.ReadAs<bool>(); }, String.Format(errorMsgFormat, typeName, typeof(bool)));
ExceptionHelper.Throws<NotSupportedException>(delegate { target.ReadAs<string>(); }, String.Format(errorMsgFormat, typeName, typeof(string)));
ExceptionHelper.Throws<NotSupportedException>(delegate { target.ReadAs<JsonObject>(); }, String.Format(errorMsgFormat, typeName, typeof(JsonObject)));
bool boolValue;
string stringValue;
JsonObject objValue;
object value;
Assert.False(target.TryReadAs(typeof(bool), out value), "TryReadAs expected to return false");
Assert.Null(value);
Assert.False(target.TryReadAs(typeof(string), out value), "TryReadAs expected to return false");
Assert.Null(value);
Assert.False(target.TryReadAs(typeof(JsonObject), out value), "TryReadAs expected to return false");
Assert.Null(value);
Assert.False(target.TryReadAs<bool>(out boolValue), "TryReadAs expected to return false");
Assert.False(boolValue);
Assert.False(target.TryReadAs<string>(out stringValue), "TryReadAs expected to return false");
Assert.Null(stringValue);
Assert.False(target.TryReadAs<JsonObject>(out objValue), "TryReadAs expected to return false");
Assert.Null(objValue);
}
[Fact]
public void ItemTests()
{
JsonValue target = AnyInstance.DefaultJsonValue;
ExceptionHelper.Throws<InvalidOperationException>(delegate { var v = target["MissingProperty"]; }, String.Format(IndexerNotSupportedMsgFormat, typeof(string).FullName));
ExceptionHelper.Throws<InvalidOperationException>(delegate { target["NewProperty"] = AnyInstance.AnyJsonValue1; }, String.Format(IndexerNotSupportedMsgFormat, typeof(string).FullName));
}
[Fact]
public void DynamicItemTests()
{
dynamic target = AnyInstance.DefaultJsonValue;
var getByKey = target["SomeKey"];
Assert.Same(getByKey, AnyInstance.DefaultJsonValue);
var getByIndex = target[10];
Assert.Same(getByIndex, AnyInstance.DefaultJsonValue);
ExceptionHelper.Throws<InvalidOperationException>(delegate { target["SomeKey"] = AnyInstance.AnyJsonObject; }, String.Format(IndexerNotSupportedMsgFormat, typeof(string).FullName));
ExceptionHelper.Throws<InvalidOperationException>(delegate { target[10] = AnyInstance.AnyJsonObject; }, String.Format(IndexerNotSupportedMsgFormat, typeof(int).FullName));
}
[Fact(Skip = "See bug #228569 in CSDMain")]
public void InvalidAssignmentValueTest()
{
JsonValue target;
JsonValue value = AnyInstance.DefaultJsonValue;
target = AnyInstance.AnyJsonArray;
ExceptionHelper.Throws<ArgumentException>(delegate { target[0] = value; }, OperationNotAllowedOnDefaultMsgFormat);
target = AnyInstance.AnyJsonObject;
ExceptionHelper.Throws<ArgumentException>(delegate { target["key"] = value; }, OperationNotAllowedOnDefaultMsgFormat);
}
[Fact]
public void DefaultConcatTest()
{
JsonValue jv = JsonValueExtensions.CreateFrom(AnyInstance.AnyPerson);
dynamic target = JsonValueExtensions.CreateFrom(AnyInstance.AnyPerson);
Person person = AnyInstance.AnyPerson;
Assert.Equal(JsonType.Default, target.Friends[100000].Name.JsonType);
Assert.Equal(JsonType.Default, target.Friends[0].Age.Minutes.JsonType);
JsonValue jv1 = target.MissingProperty as JsonValue;
Assert.NotNull(jv1);
JsonValue jv2 = target.MissingProperty1.MissingProperty2 as JsonValue;
Assert.NotNull(jv2);
Assert.Same(jv1, jv2);
Assert.Same(target.Person.Name.MissingProperty, AnyInstance.DefaultJsonValue);
}
[Fact]
public void CastingDefaultValueTest()
{
JsonValue jv = AnyInstance.DefaultJsonValue;
dynamic d = jv;
ExceptionHelper.Throws<InvalidCastException>(delegate { float p = (float)d; });
ExceptionHelper.Throws<InvalidCastException>(delegate { byte p = (byte)d; });
ExceptionHelper.Throws<InvalidCastException>(delegate { int p = (int)d; });
Assert.Null((string)d);
}
}
}