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/
6 // Redistribution and use in source and binary forms, with or without
7 // modification, are permitted provided that the following conditions are
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
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.
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.
32 using NUnit.Framework;
34 using System.Collections.Generic;
35 using System.Reflection;
38 namespace Google.Protobuf.Compatibility
40 public class TypeExtensionsTest
42 public class DerivedList : List<string> { }
43 public string PublicProperty { get; set; }
44 private string PrivateProperty { get; set; }
46 public void PublicMethod()
50 private void PrivateMethod()
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)] //
63 public void IsAssignableFrom(Type target, Type argument, bool expected)
65 Assert.AreEqual(expected, TypeExtensions.IsAssignableFrom(target, argument));
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")]
73 public void GetProperty_Success(Type type, string name)
75 var property = TypeExtensions.GetProperty(type, name);
76 Assert.IsNotNull(property);
77 Assert.AreEqual(name, property.Name);
81 [TestCase(typeof(TypeExtensionsTest), "PrivateProperty")]
82 [TestCase(typeof(TypeExtensionsTest), "Garbage")]
83 public void GetProperty_NoSuchProperty(Type type, string name)
85 var property = TypeExtensions.GetProperty(type, name);
86 Assert.IsNull(property);
90 [TestCase(typeof(DerivedList), "RemoveAt")] // Go up the type hierarchy
91 [TestCase(typeof(List<>), "RemoveAt")]
92 [TestCase(typeof(TypeExtensionsTest), "PublicMethod")]
93 public void GetMethod_Success(Type type, string name)
95 var method = TypeExtensions.GetMethod(type, name);
96 Assert.IsNotNull(method);
97 Assert.AreEqual(name, method.Name);
101 [TestCase(typeof(TypeExtensionsTest), "PrivateMethod")]
102 [TestCase(typeof(TypeExtensionsTest), "GarbageMethod")]
103 public void GetMethod_NoSuchMethod(Type type, string name)
105 var method = TypeExtensions.GetMethod(type, name);
106 Assert.IsNull(method);
110 [TestCase(typeof(List<string>), "IndexOf")]
111 public void GetMethod_Ambiguous(Type type, string name)
113 Assert.Throws<AmbiguousMatchException>(() => TypeExtensions.GetMethod(type, name));