1 #region Copyright notice and license 2 // Protocol Buffers - Google's data interchange format 3 // Copyright 2015 Google Inc. All rights reserved. 4 // https://developers.google.com/protocol-buffers/ 5 // 6 // Redistribution and use in source and binary forms, with or without 7 // modification, are permitted provided that the following conditions are 8 // met: 9 // 10 // * Redistributions of source code must retain the above copyright 11 // notice, this list of conditions and the following disclaimer. 12 // * Redistributions in binary form must reproduce the above 13 // copyright notice, this list of conditions and the following disclaimer 14 // in the documentation and/or other materials provided with the 15 // distribution. 16 // * Neither the name of Google Inc. nor the names of its 17 // contributors may be used to endorse or promote products derived from 18 // this software without specific prior written permission. 19 // 20 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 23 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 26 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 #endregion 32 using NUnit.Framework; 33 using System; 34 using System.Collections.Generic; 35 using System.Reflection; 36 37 #if !NET35 38 namespace Google.Protobuf.Compatibility 39 { 40 public class TypeExtensionsTest 41 { 42 public class DerivedList : List<string> { } 43 public string PublicProperty { get; set; } 44 private string PrivateProperty { get; set; } 45 PublicMethod()46 public void PublicMethod() 47 { 48 } 49 PrivateMethod()50 private void PrivateMethod() 51 { 52 } 53 54 [Test] 55 [TestCase(typeof(object), typeof(string), true)] 56 [TestCase(typeof(object), typeof(int), true)] 57 [TestCase(typeof(string), typeof(string), true)] 58 [TestCase(typeof(string), typeof(object), false)] 59 [TestCase(typeof(string), typeof(int), false)] 60 [TestCase(typeof(int), typeof(int), true)] 61 [TestCase(typeof(ValueType), typeof(int), true)] 62 [TestCase(typeof(long), typeof(int), false)] // IsAssignableFrom(Type target, Type argument, bool expected)63 public void IsAssignableFrom(Type target, Type argument, bool expected) 64 { 65 Assert.AreEqual(expected, TypeExtensions.IsAssignableFrom(target, argument)); 66 } 67 68 [Test] 69 [TestCase(typeof(DerivedList), "Count")] // Go up the type hierarchy 70 [TestCase(typeof(List<string>), "Count")] 71 [TestCase(typeof(List<>), "Count")] 72 [TestCase(typeof(TypeExtensionsTest), "PublicProperty")] GetProperty_Success(Type type, string name)73 public void GetProperty_Success(Type type, string name) 74 { 75 var property = TypeExtensions.GetProperty(type, name); 76 Assert.IsNotNull(property); 77 Assert.AreEqual(name, property.Name); 78 } 79 80 [Test] 81 [TestCase(typeof(TypeExtensionsTest), "PrivateProperty")] 82 [TestCase(typeof(TypeExtensionsTest), "Garbage")] GetProperty_NoSuchProperty(Type type, string name)83 public void GetProperty_NoSuchProperty(Type type, string name) 84 { 85 var property = TypeExtensions.GetProperty(type, name); 86 Assert.IsNull(property); 87 } 88 89 [Test] 90 [TestCase(typeof(DerivedList), "RemoveAt")] // Go up the type hierarchy 91 [TestCase(typeof(List<>), "RemoveAt")] 92 [TestCase(typeof(TypeExtensionsTest), "PublicMethod")] GetMethod_Success(Type type, string name)93 public void GetMethod_Success(Type type, string name) 94 { 95 var method = TypeExtensions.GetMethod(type, name); 96 Assert.IsNotNull(method); 97 Assert.AreEqual(name, method.Name); 98 } 99 100 [Test] 101 [TestCase(typeof(TypeExtensionsTest), "PrivateMethod")] 102 [TestCase(typeof(TypeExtensionsTest), "GarbageMethod")] GetMethod_NoSuchMethod(Type type, string name)103 public void GetMethod_NoSuchMethod(Type type, string name) 104 { 105 var method = TypeExtensions.GetMethod(type, name); 106 Assert.IsNull(method); 107 } 108 109 [Test] 110 [TestCase(typeof(List<string>), "IndexOf")] GetMethod_Ambiguous(Type type, string name)111 public void GetMethod_Ambiguous(Type type, string name) 112 { 113 Assert.Throws<AmbiguousMatchException>(() => TypeExtensions.GetMethod(type, name)); 114 } 115 } 116 } 117 #endif 118