diff options
author | Bojan Rajkovic <bojan.rajkovic@xamarin.com> | 2012-12-07 00:18:06 +0400 |
---|---|---|
committer | Bojan Rajkovic <bojan.rajkovic@xamarin.com> | 2012-12-07 00:33:42 +0400 |
commit | c48b14ab6a629531a968659d2b327d8ff832a2fb (patch) | |
tree | 8321673cc99a4e74c4276b1897a421e7e1016790 /Src/Newtonsoft.Json.Tests/Utilities | |
parent | 96879894b4e21c34aa39a013681b8bfbf07b6409 (diff) |
Merge from upstream.
Diffstat (limited to 'Src/Newtonsoft.Json.Tests/Utilities')
3 files changed, 256 insertions, 200 deletions
diff --git a/Src/Newtonsoft.Json.Tests/Utilities/DynamicReflectionDelegateFactoryTests.cs b/Src/Newtonsoft.Json.Tests/Utilities/DynamicReflectionDelegateFactoryTests.cs index 384f71a..4cf32aa 100644 --- a/Src/Newtonsoft.Json.Tests/Utilities/DynamicReflectionDelegateFactoryTests.cs +++ b/Src/Newtonsoft.Json.Tests/Utilities/DynamicReflectionDelegateFactoryTests.cs @@ -1,165 +1,165 @@ -#region License
-// Copyright (c) 2007 James Newton-King
-//
-// Permission is hereby granted, free of charge, to any person
-// obtaining a copy of this software and associated documentation
-// files (the "Software"), to deal in the Software without
-// restriction, including without limitation the rights to use,
-// copy, modify, merge, publish, distribute, sublicense, and/or sell
-// copies of the Software, and to permit persons to whom the
-// Software is furnished to do so, subject to the following
-// conditions:
-//
-// The above copyright notice and this permission notice shall be
-// included in all copies or substantial portions of the Software.
-//
-// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
-// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
-// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
-// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
-// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
-// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
-// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
-// OTHER DEALINGS IN THE SOFTWARE.
-#endregion
-
-#if !PocketPC && !SILVERLIGHT
-using System;
-using System.Reflection;
-#if !NETFX_CORE
-using NUnit.Framework;
-#else
-using Microsoft.VisualStudio.TestTools.UnitTesting;
-using TestFixture = Microsoft.VisualStudio.TestTools.UnitTesting.TestClassAttribute;
-using Test = Microsoft.VisualStudio.TestTools.UnitTesting.TestMethodAttribute;
-#endif
-using Newtonsoft.Json.Serialization;
-using Newtonsoft.Json.Utilities;
-using Newtonsoft.Json.Tests.TestObjects;
-using Newtonsoft.Json.Tests.Serialization;
-
-namespace Newtonsoft.Json.Tests.Utilities
-{
- [TestFixture]
- public class DynamicReflectionDelegateFactoryTests : TestFixtureBase
- {
- [Test]
- public void CreateGetWithBadObjectTarget()
- {
- ExceptionAssert.Throws<InvalidCastException>("Unable to cast object of type 'Newtonsoft.Json.Tests.TestObjects.Person' to type 'Newtonsoft.Json.Tests.TestObjects.Movie'.",
- () =>
- {
- Person p = new Person();
- p.Name = "Hi";
-
- Func<object, object> setter = DynamicReflectionDelegateFactory.Instance.CreateGet<object>(typeof(Movie).GetProperty("Name"));
-
- setter(p);
- });
- }
-
- [Test]
- public void CreateSetWithBadObjectTarget()
- {
- ExceptionAssert.Throws<InvalidCastException>("Unable to cast object of type 'Newtonsoft.Json.Tests.TestObjects.Person' to type 'Newtonsoft.Json.Tests.TestObjects.Movie'.",
- () =>
- {
- Person p = new Person();
- Movie m = new Movie();
-
- Action<object, object> setter = DynamicReflectionDelegateFactory.Instance.CreateSet<object>(typeof(Movie).GetProperty("Name"));
-
- setter(m, "Hi");
-
- Assert.AreEqual(m.Name, "Hi");
-
- setter(p, "Hi");
- });
- }
-
- [Test]
- public void CreateSetWithBadTarget()
- {
- ExceptionAssert.Throws<InvalidCastException>("Specified cast is not valid.",
- () =>
- {
- object structTest = new StructTest();
-
- Action<object, object> setter = DynamicReflectionDelegateFactory.Instance.CreateSet<object>(typeof(StructTest).GetProperty("StringProperty"));
-
- setter(structTest, "Hi");
-
- Assert.AreEqual("Hi", ((StructTest)structTest).StringProperty);
-
- setter(new TimeSpan(), "Hi");
- });
- }
-
- [Test]
- public void CreateSetWithBadObjectValue()
- {
- ExceptionAssert.Throws<InvalidCastException>("Unable to cast object of type 'System.Version' to type 'System.String'.",
- () =>
- {
- Movie m = new Movie();
-
- Action<object, object> setter = DynamicReflectionDelegateFactory.Instance.CreateSet<object>(typeof(Movie).GetProperty("Name"));
-
- setter(m, new Version("1.1.1.1"));
- });
- }
-
- [Test]
- public void CreateStaticMethodCall()
- {
- MethodInfo castMethodInfo = typeof(JsonSerializerTest.DictionaryKey).GetMethod("op_Implicit", new[] { typeof(string) });
-
- Assert.IsNotNull(castMethodInfo);
-
- MethodCall<object, object> call = DynamicReflectionDelegateFactory.Instance.CreateMethodCall<object>(castMethodInfo);
-
- object result = call(null, "First!");
- Assert.IsNotNull(result);
-
- JsonSerializerTest.DictionaryKey key = (JsonSerializerTest.DictionaryKey) result;
- Assert.AreEqual("First!", key.Value);
- }
-
- //[Test]
- //public void sdfsdf()
- //{
- // string name = "MyAssembly";
- // string filename = name + ".dll";
-
- // AssemblyName s = new AssemblyName(name);
-
- // AssemblyBuilder assemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(s, AssemblyBuilderAccess.RunAndSave);
- // ModuleBuilder moduleBuilder = assemblyBuilder.DefineDynamicModule(filename, filename);
- // TypeBuilder typeBuilder = moduleBuilder.DefineType("MyType", TypeAttributes.Class, typeof(object));
- // MethodBuilder methodBuilder = typeBuilder.DefineMethod("TestSet", MethodAttributes.Public | MethodAttributes.Static, typeof(void), new[] { typeof(object), typeof(object) });
-
- // DynamicReflectionDelegateFactory.GenerateCreateSetFieldIL(typeof(ClassWithGuid).GetField("GuidField"), methodBuilder.GetILGenerator());
- // typeBuilder.CreateType();
- // assemblyBuilder.Save(filename);
- //}
-
- //[Test]
- //public void sdfsdf1()
- //{
- // string name = "MyAssembly1";
- // string filename = name + ".dll";
-
- // AssemblyName s = new AssemblyName(name);
-
- // AssemblyBuilder assemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(s, AssemblyBuilderAccess.RunAndSave);
- // ModuleBuilder moduleBuilder = assemblyBuilder.DefineDynamicModule(filename, filename);
- // TypeBuilder typeBuilder = moduleBuilder.DefineType("MyType", TypeAttributes.Class, typeof(object));
- // MethodBuilder methodBuilder = typeBuilder.DefineMethod("TestSet", MethodAttributes.Public | MethodAttributes.Static, typeof(void), new[] { typeof(object), typeof(object) });
-
- // DynamicReflectionDelegateFactory.GenerateCreateSetPropertyIL(typeof(Car).GetProperty("Model"), methodBuilder.GetILGenerator());
- // typeBuilder.CreateType();
- // assemblyBuilder.Save(filename);
- //}
- }
-}
+#region License +// Copyright (c) 2007 James Newton-King +// +// Permission is hereby granted, free of charge, to any person +// obtaining a copy of this software and associated documentation +// files (the "Software"), to deal in the Software without +// restriction, including without limitation the rights to use, +// copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the +// Software is furnished to do so, subject to the following +// conditions: +// +// The above copyright notice and this permission notice shall be +// included in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES +// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT +// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +// OTHER DEALINGS IN THE SOFTWARE. +#endregion + +#if !(SILVERLIGHT || PORTABLE || NETFX_CORE) +using System; +using System.Reflection; +#if !NETFX_CORE +using NUnit.Framework; +#else +using Microsoft.VisualStudio.TestPlatform.UnitTestFramework; +using TestFixture = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestClassAttribute; +using Test = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestMethodAttribute; +#endif +using Newtonsoft.Json.Serialization; +using Newtonsoft.Json.Utilities; +using Newtonsoft.Json.Tests.TestObjects; +using Newtonsoft.Json.Tests.Serialization; + +namespace Newtonsoft.Json.Tests.Utilities +{ + [TestFixture] + public class DynamicReflectionDelegateFactoryTests : TestFixtureBase + { + [Test] + public void CreateGetWithBadObjectTarget() + { + ExceptionAssert.Throws<InvalidCastException>("Unable to cast object of type 'Newtonsoft.Json.Tests.TestObjects.Person' to type 'Newtonsoft.Json.Tests.TestObjects.Movie'.", + () => + { + Person p = new Person(); + p.Name = "Hi"; + + Func<object, object> setter = DynamicReflectionDelegateFactory.Instance.CreateGet<object>(typeof(Movie).GetProperty("Name")); + + setter(p); + }); + } + + [Test] + public void CreateSetWithBadObjectTarget() + { + ExceptionAssert.Throws<InvalidCastException>("Unable to cast object of type 'Newtonsoft.Json.Tests.TestObjects.Person' to type 'Newtonsoft.Json.Tests.TestObjects.Movie'.", + () => + { + Person p = new Person(); + Movie m = new Movie(); + + Action<object, object> setter = DynamicReflectionDelegateFactory.Instance.CreateSet<object>(typeof(Movie).GetProperty("Name")); + + setter(m, "Hi"); + + Assert.AreEqual(m.Name, "Hi"); + + setter(p, "Hi"); + }); + } + + [Test] + public void CreateSetWithBadTarget() + { + ExceptionAssert.Throws<InvalidCastException>("Specified cast is not valid.", + () => + { + object structTest = new StructTest(); + + Action<object, object> setter = DynamicReflectionDelegateFactory.Instance.CreateSet<object>(typeof(StructTest).GetProperty("StringProperty")); + + setter(structTest, "Hi"); + + Assert.AreEqual("Hi", ((StructTest)structTest).StringProperty); + + setter(new TimeSpan(), "Hi"); + }); + } + + [Test] + public void CreateSetWithBadObjectValue() + { + ExceptionAssert.Throws<InvalidCastException>("Unable to cast object of type 'System.Version' to type 'System.String'.", + () => + { + Movie m = new Movie(); + + Action<object, object> setter = DynamicReflectionDelegateFactory.Instance.CreateSet<object>(typeof(Movie).GetProperty("Name")); + + setter(m, new Version("1.1.1.1")); + }); + } + + [Test] + public void CreateStaticMethodCall() + { + MethodInfo castMethodInfo = typeof(JsonSerializerTest.DictionaryKey).GetMethod("op_Implicit", new[] { typeof(string) }); + + Assert.IsNotNull(castMethodInfo); + + MethodCall<object, object> call = DynamicReflectionDelegateFactory.Instance.CreateMethodCall<object>(castMethodInfo); + + object result = call(null, "First!"); + Assert.IsNotNull(result); + + JsonSerializerTest.DictionaryKey key = (JsonSerializerTest.DictionaryKey) result; + Assert.AreEqual("First!", key.Value); + } + + //[Test] + //public void sdfsdf() + //{ + // string name = "MyAssembly"; + // string filename = name + ".dll"; + + // AssemblyName s = new AssemblyName(name); + + // AssemblyBuilder assemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(s, AssemblyBuilderAccess.RunAndSave); + // ModuleBuilder moduleBuilder = assemblyBuilder.DefineDynamicModule(filename, filename); + // TypeBuilder typeBuilder = moduleBuilder.DefineType("MyType", TypeAttributes.Class, typeof(object)); + // MethodBuilder methodBuilder = typeBuilder.DefineMethod("TestSet", MethodAttributes.Public | MethodAttributes.Static, typeof(void), new[] { typeof(object), typeof(object) }); + + // DynamicReflectionDelegateFactory.GenerateCreateSetFieldIL(typeof(ClassWithGuid).GetField("GuidField"), methodBuilder.GetILGenerator()); + // typeBuilder.CreateType(); + // assemblyBuilder.Save(filename); + //} + + //[Test] + //public void sdfsdf1() + //{ + // string name = "MyAssembly1"; + // string filename = name + ".dll"; + + // AssemblyName s = new AssemblyName(name); + + // AssemblyBuilder assemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(s, AssemblyBuilderAccess.RunAndSave); + // ModuleBuilder moduleBuilder = assemblyBuilder.DefineDynamicModule(filename, filename); + // TypeBuilder typeBuilder = moduleBuilder.DefineType("MyType", TypeAttributes.Class, typeof(object)); + // MethodBuilder methodBuilder = typeBuilder.DefineMethod("TestSet", MethodAttributes.Public | MethodAttributes.Static, typeof(void), new[] { typeof(object), typeof(object) }); + + // DynamicReflectionDelegateFactory.GenerateCreateSetPropertyIL(typeof(Car).GetProperty("Model"), methodBuilder.GetILGenerator()); + // typeBuilder.CreateType(); + // assemblyBuilder.Save(filename); + //} + } +} #endif
\ No newline at end of file diff --git a/Src/Newtonsoft.Json.Tests/Utilities/ReflectionUtilsTests.cs b/Src/Newtonsoft.Json.Tests/Utilities/ReflectionUtilsTests.cs index d0e3cfa..f8510d3 100644 --- a/Src/Newtonsoft.Json.Tests/Utilities/ReflectionUtilsTests.cs +++ b/Src/Newtonsoft.Json.Tests/Utilities/ReflectionUtilsTests.cs @@ -1,36 +1,63 @@ -using System;
-using System.Collections.Generic;
-using System.Runtime.Serialization.Formatters;
-#if !NETFX_CORE
-using NUnit.Framework;
-#else
-using Microsoft.VisualStudio.TestTools.UnitTesting;
-using TestFixture = Microsoft.VisualStudio.TestTools.UnitTesting.TestClassAttribute;
-using Test = Microsoft.VisualStudio.TestTools.UnitTesting.TestMethodAttribute;
-#endif
-using Newtonsoft.Json.Utilities;
-
-namespace Newtonsoft.Json.Tests.Utilities
-{
- [TestFixture]
- public class ReflectionUtilsTests : TestFixtureBase
- {
- [Test]
- public void GetTypeNameSimpleForGenericTypes()
- {
- string typeName;
-
- typeName = ReflectionUtils.GetTypeName(typeof(IList<Type>), FormatterAssemblyStyle.Simple);
- Assert.AreEqual("System.Collections.Generic.IList`1[[System.Type, mscorlib]], mscorlib", typeName);
-
- typeName = ReflectionUtils.GetTypeName(typeof(IDictionary<IList<Type>, IList<Type>>), FormatterAssemblyStyle.Simple);
- Assert.AreEqual("System.Collections.Generic.IDictionary`2[[System.Collections.Generic.IList`1[[System.Type, mscorlib]], mscorlib],[System.Collections.Generic.IList`1[[System.Type, mscorlib]], mscorlib]], mscorlib", typeName);
-
- typeName = ReflectionUtils.GetTypeName(typeof(IList<>), FormatterAssemblyStyle.Simple);
- Assert.AreEqual("System.Collections.Generic.IList`1, mscorlib", typeName);
-
- typeName = ReflectionUtils.GetTypeName(typeof(IDictionary<,>), FormatterAssemblyStyle.Simple);
- Assert.AreEqual("System.Collections.Generic.IDictionary`2, mscorlib", typeName);
- }
- }
-}
\ No newline at end of file +#region License +// Copyright (c) 2007 James Newton-King +// +// Permission is hereby granted, free of charge, to any person +// obtaining a copy of this software and associated documentation +// files (the "Software"), to deal in the Software without +// restriction, including without limitation the rights to use, +// copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the +// Software is furnished to do so, subject to the following +// conditions: +// +// The above copyright notice and this permission notice shall be +// included in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES +// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT +// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +// OTHER DEALINGS IN THE SOFTWARE. +#endregion + +#if !PORTABLE +using global::System; +using global::System.Collections.Generic; +using global::System.Runtime.Serialization.Formatters; +#if !NETFX_CORE +using global::NUnit.Framework; +#else +using global::Microsoft.VisualStudio.TestPlatform.UnitTestFramework; +using TestFixture = global::Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestClassAttribute; +using Test = global::Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestMethodAttribute; +#endif +using global::Newtonsoft.Json.Utilities; + +namespace Newtonsoft.Json.Tests.Utilities +{ + [TestFixture] + public class ReflectionUtilsTests : TestFixtureBase + { + [Test] + public void GetTypeNameSimpleForGenericTypes() + { + string typeName; + + typeName = ReflectionUtils.GetTypeName(typeof(IList<Type>), FormatterAssemblyStyle.Simple); + Assert.AreEqual("System.Collections.Generic.IList`1[[System.Type, mscorlib]], mscorlib", typeName); + + typeName = ReflectionUtils.GetTypeName(typeof(IDictionary<IList<Type>, IList<Type>>), FormatterAssemblyStyle.Simple); + Assert.AreEqual("System.Collections.Generic.IDictionary`2[[System.Collections.Generic.IList`1[[System.Type, mscorlib]], mscorlib],[System.Collections.Generic.IList`1[[System.Type, mscorlib]], mscorlib]], mscorlib", typeName); + + typeName = ReflectionUtils.GetTypeName(typeof(IList<>), FormatterAssemblyStyle.Simple); + Assert.AreEqual("System.Collections.Generic.IList`1, mscorlib", typeName); + + typeName = ReflectionUtils.GetTypeName(typeof(IDictionary<,>), FormatterAssemblyStyle.Simple); + Assert.AreEqual("System.Collections.Generic.IDictionary`2, mscorlib", typeName); + } + } +} +#endif
\ No newline at end of file diff --git a/Src/Newtonsoft.Json.Tests/Utilities/StringUtilsTests.cs b/Src/Newtonsoft.Json.Tests/Utilities/StringUtilsTests.cs new file mode 100644 index 0000000..6ff4555 --- /dev/null +++ b/Src/Newtonsoft.Json.Tests/Utilities/StringUtilsTests.cs @@ -0,0 +1,29 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using NUnit.Framework; +using Newtonsoft.Json.Utilities; + +namespace Newtonsoft.Json.Tests.Utilities +{ + [TestFixture] + public class StringUtilsTests : TestFixtureBase + { + [Test] + public void ToCamelCaseTest() + { + Assert.AreEqual("urlValue", StringUtils.ToCamelCase("URLValue")); + Assert.AreEqual("url", StringUtils.ToCamelCase("URL")); + Assert.AreEqual("id", StringUtils.ToCamelCase("ID")); + Assert.AreEqual("i", StringUtils.ToCamelCase("I")); + Assert.AreEqual("", StringUtils.ToCamelCase("")); + Assert.AreEqual(null, StringUtils.ToCamelCase(null)); + Assert.AreEqual("iPhone", StringUtils.ToCamelCase("iPhone")); + Assert.AreEqual("person", StringUtils.ToCamelCase("Person")); + Assert.AreEqual("iPhone", StringUtils.ToCamelCase("IPhone")); + Assert.AreEqual("i Phone", StringUtils.ToCamelCase("I Phone")); + Assert.AreEqual(" IPhone", StringUtils.ToCamelCase(" IPhone")); + } + } +}
\ No newline at end of file |