• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 namespace Google.Protobuf.Compatibility
38 {
39     public class TypeExtensionsTest
40     {
41         public class DerivedList : List<string> { }
42         public string PublicProperty { get; set; }
43         private string PrivateProperty { get; set; }
44 
PublicMethod()45         public void PublicMethod()
46         {
47         }
48 
PrivateMethod()49         private void PrivateMethod()
50         {
51         }
52 
53         [Test]
54         [TestCase(typeof(int), true)]
55         [TestCase(typeof(int?), true)]
56         [TestCase(typeof(Nullable<>), true)]
57         [TestCase(typeof(WireFormat.WireType), true)]
58         [TestCase(typeof(string), false)]
59         [TestCase(typeof(object), false)]
60         [TestCase(typeof(Enum), false)]
61         [TestCase(typeof(ValueType), false)]
62         [TestCase(typeof(TypeExtensionsTest), false)]
63         [TestCase(typeof(Action), false)]
64         [TestCase(typeof(Action<>), false)]
65         [TestCase(typeof(IDisposable), false)]
IsValueType(Type type, bool expected)66         public void IsValueType(Type type, bool expected)
67         {
68             Assert.AreEqual(expected, TypeExtensions.IsValueType(type));
69         }
70 
71         [Test]
72         [TestCase(typeof(object), typeof(string), true)]
73         [TestCase(typeof(object), typeof(int), true)]
74         [TestCase(typeof(string), typeof(string), true)]
75         [TestCase(typeof(string), typeof(object), false)]
76         [TestCase(typeof(string), typeof(int), false)]
77         [TestCase(typeof(int), typeof(int), true)]
78         [TestCase(typeof(ValueType), typeof(int), true)]
79         [TestCase(typeof(long), typeof(int), false)] //
IsAssignableFrom(Type target, Type argument, bool expected)80         public void IsAssignableFrom(Type target, Type argument, bool expected)
81         {
82             Assert.AreEqual(expected, TypeExtensions.IsAssignableFrom(target, argument));
83         }
84 
85         [Test]
86         [TestCase(typeof(DerivedList), "Count")] // Go up the type hierarchy
87         [TestCase(typeof(List<string>), "Count")]
88         [TestCase(typeof(List<>), "Count")]
89         [TestCase(typeof(TypeExtensionsTest), "PublicProperty")]
GetProperty_Success(Type type, string name)90         public void GetProperty_Success(Type type, string name)
91         {
92             var property = TypeExtensions.GetProperty(type, name);
93             Assert.IsNotNull(property);
94             Assert.AreEqual(name, property.Name);
95         }
96 
97         [Test]
98         [TestCase(typeof(TypeExtensionsTest), "PrivateProperty")]
99         [TestCase(typeof(TypeExtensionsTest), "Garbage")]
GetProperty_NoSuchProperty(Type type, string name)100         public void GetProperty_NoSuchProperty(Type type, string name)
101         {
102             var property = TypeExtensions.GetProperty(type, name);
103             Assert.IsNull(property);
104         }
105 
106         [Test]
107         [TestCase(typeof(DerivedList), "RemoveAt")] // Go up the type hierarchy
108         [TestCase(typeof(List<>), "RemoveAt")]
109         [TestCase(typeof(TypeExtensionsTest), "PublicMethod")]
GetMethod_Success(Type type, string name)110         public void GetMethod_Success(Type type, string name)
111         {
112             var method = TypeExtensions.GetMethod(type, name);
113             Assert.IsNotNull(method);
114             Assert.AreEqual(name, method.Name);
115         }
116 
117         [Test]
118         [TestCase(typeof(TypeExtensionsTest), "PrivateMethod")]
119         [TestCase(typeof(TypeExtensionsTest), "GarbageMethod")]
GetMethod_NoSuchMethod(Type type, string name)120         public void GetMethod_NoSuchMethod(Type type, string name)
121         {
122             var method = TypeExtensions.GetMethod(type, name);
123             Assert.IsNull(method);
124         }
125 
126         [Test]
127         [TestCase(typeof(List<string>), "IndexOf")]
GetMethod_Ambiguous(Type type, string name)128         public void GetMethod_Ambiguous(Type type, string name)
129         {
130             Assert.Throws<AmbiguousMatchException>(() => TypeExtensions.GetMethod(type, name));
131         }
132     }
133 }
134