|
| 1 | +// <copyright file="TestReflection.cs" company="Google Inc."> |
| 2 | +// Copyright (C) 2019 Google Inc. All Rights Reserved. |
| 3 | +// |
| 4 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +// you may not use this file except in compliance with the License. |
| 6 | +// You may obtain a copy of the License at |
| 7 | +// |
| 8 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +// |
| 10 | +// Unless required by applicable law or agreed to in writing, software |
| 11 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +// See the License for the specific language governing permissions and |
| 14 | +// limitations under the License. |
| 15 | +// </copyright> |
| 16 | + |
| 17 | +using System; |
| 18 | +using System.Collections.Generic; |
| 19 | +using System.Collections; |
| 20 | +using System.IO; |
| 21 | + |
| 22 | +using Google; |
| 23 | + |
| 24 | +// Class used to test method invocation. |
| 25 | +public class Greeter { |
| 26 | + private string name; |
| 27 | + |
| 28 | + public Greeter(string name) { |
| 29 | + this.name = name; |
| 30 | + } |
| 31 | + |
| 32 | + public static string GenericHello() { |
| 33 | + return "Hello"; |
| 34 | + } |
| 35 | + |
| 36 | + public static string GenericHelloWithCustomerName(string customerName) { |
| 37 | + return String.Format("{0} {1}", GenericHello(), customerName); |
| 38 | + } |
| 39 | + |
| 40 | + public static string GenericHelloWithCustomerName(int customerId) { |
| 41 | + return String.Format("{0} customer #{1}", GenericHello(), customerId); |
| 42 | + } |
| 43 | + |
| 44 | + public static string GenericHelloWithPronoun(string pronoun = "There") { |
| 45 | + return String.Format("{0} {1}", GenericHello(), pronoun); |
| 46 | + } |
| 47 | + |
| 48 | + public static string GenericHelloWithCustomerNameAndPronoun(string customerName, |
| 49 | + string pronoun = "There") { |
| 50 | + return String.Format("{0} {1}", GenericHelloWithPronoun(pronoun: pronoun), customerName); |
| 51 | + } |
| 52 | + |
| 53 | + private string MyNameIs() { |
| 54 | + return String.Format(", my name is {0}", name); |
| 55 | + } |
| 56 | + |
| 57 | + public string Hello() { |
| 58 | + return Greeter.GenericHello() + MyNameIs(); |
| 59 | + } |
| 60 | + |
| 61 | + public string HelloWithCustomerName(string customerName) { |
| 62 | + return Greeter.GenericHelloWithCustomerName(customerName) + MyNameIs(); |
| 63 | + } |
| 64 | + |
| 65 | + public string HelloWithCustomerNameAndPronoun(string customerName, |
| 66 | + string pronoun = "There") { |
| 67 | + return Greeter.GenericHelloWithCustomerNameAndPronoun(customerName, |
| 68 | + pronoun: pronoun) + MyNameIs(); |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +[UnityEditor.InitializeOnLoad] |
| 73 | +public class TestReflection { |
| 74 | + |
| 75 | + /// <summary> |
| 76 | + /// Test all reflection methods. |
| 77 | + /// </summary> |
| 78 | + static TestReflection() { |
| 79 | + // Disable stack traces for more condensed logs. |
| 80 | + UnityEngine.Application.stackTraceLogType = UnityEngine.StackTraceLogType.None; |
| 81 | + |
| 82 | + // Run tests. |
| 83 | + var failures = new List<string>(); |
| 84 | + foreach (var test in new Func<bool>[] { |
| 85 | + TestFindClassWithAssemblyName, |
| 86 | + TestFindClassWithoutAssemblyName, |
| 87 | + TestInvokeStaticMethodWithNoArgs, |
| 88 | + TestInvokeStaticMethodWithStringArg, |
| 89 | + TestInvokeStaticMethodWithIntArg, |
| 90 | + TestInvokeStaticMethodWithNamedArgDefault, |
| 91 | + TestInvokeStaticMethodWithNamedArg, |
| 92 | + TestInvokeStaticMethodWithArgAndNamedArgDefault, |
| 93 | + TestInvokeStaticMethodWithArgAndNamedArg, |
| 94 | + TestInvokeInstanceMethodWithNoArgs, |
| 95 | + TestInvokeInstanceMethodWithNamedArgDefault, |
| 96 | + TestInvokeInstanceMethodWithNamedArg |
| 97 | + }) { |
| 98 | + var testName = test.Method.Name; |
| 99 | + Exception exception = null; |
| 100 | + bool succeeded = false; |
| 101 | + try { |
| 102 | + UnityEngine.Debug.Log(String.Format("Running test {0}", testName)); |
| 103 | + succeeded = test(); |
| 104 | + } catch (Exception ex) { |
| 105 | + exception = ex; |
| 106 | + succeeded = false; |
| 107 | + } |
| 108 | + if (succeeded) { |
| 109 | + UnityEngine.Debug.Log(String.Format("{0}: PASSED", testName)); |
| 110 | + } else { |
| 111 | + UnityEngine.Debug.LogError(String.Format("{0} ({1}): FAILED", testName, exception)); |
| 112 | + failures.Add(testName); |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + if (failures.Count > 0) { |
| 117 | + UnityEngine.Debug.Log("Test failed"); |
| 118 | + foreach (var testName in failures) { |
| 119 | + UnityEngine.Debug.Log(String.Format("{0}: FAILED", testName)); |
| 120 | + } |
| 121 | + UnityEditor.EditorApplication.Exit(1); |
| 122 | + } |
| 123 | + UnityEngine.Debug.Log("Test passed"); |
| 124 | + UnityEditor.EditorApplication.Exit(0); |
| 125 | + } |
| 126 | + |
| 127 | + // Test searching for a class when specifying the assembly name. |
| 128 | + static bool TestFindClassWithAssemblyName() { |
| 129 | + var expectedType = typeof(UnityEditor.EditorApplication); |
| 130 | + var foundType = VersionHandler.FindClass("UnityEditor", "UnityEditor.EditorApplication"); |
| 131 | + if (expectedType != foundType) { |
| 132 | + UnityEngine.Debug.LogError(String.Format("Unexpected type {0} vs {1}", foundType, |
| 133 | + expectedType)); |
| 134 | + return false; |
| 135 | + } |
| 136 | + return true; |
| 137 | + } |
| 138 | + |
| 139 | + // Test searching for a class without specifying the assembly name. |
| 140 | + static bool TestFindClassWithoutAssemblyName() { |
| 141 | + var expectedType = typeof(UnityEditor.EditorApplication); |
| 142 | + var foundType = VersionHandler.FindClass(null, "UnityEditor.EditorApplication"); |
| 143 | + if (expectedType != foundType) { |
| 144 | + UnityEngine.Debug.LogError(String.Format("Unexpected type {0} vs {1}", foundType, |
| 145 | + expectedType)); |
| 146 | + return false; |
| 147 | + } |
| 148 | + return true; |
| 149 | + } |
| 150 | + |
| 151 | + static bool CheckValue(string expected, string value) { |
| 152 | + if (value != expected) { |
| 153 | + UnityEngine.Debug.LogError(String.Format("Unexpected value {0} vs {1}", value, |
| 154 | + expected)); |
| 155 | + return false; |
| 156 | + } |
| 157 | + return true; |
| 158 | + } |
| 159 | + |
| 160 | + // Invoke a static method with no arguments. |
| 161 | + static bool TestInvokeStaticMethodWithNoArgs() { |
| 162 | + return CheckValue("Hello", |
| 163 | + (string)VersionHandler.InvokeStaticMethod(typeof(Greeter), "GenericHello", |
| 164 | + null, null)); |
| 165 | + } |
| 166 | + |
| 167 | + // Invoke an overloaded static method with a string arg. |
| 168 | + static bool TestInvokeStaticMethodWithStringArg() { |
| 169 | + return CheckValue("Hello Jane", |
| 170 | + (string)VersionHandler.InvokeStaticMethod(typeof(Greeter), |
| 171 | + "GenericHelloWithCustomerName", |
| 172 | + new object[] { "Jane" }, null)); |
| 173 | + } |
| 174 | + |
| 175 | + // Invoke an overloaded static method with an int arg. |
| 176 | + static bool TestInvokeStaticMethodWithIntArg() { |
| 177 | + return CheckValue("Hello customer #1337", |
| 178 | + (string)VersionHandler.InvokeStaticMethod(typeof(Greeter), |
| 179 | + "GenericHelloWithCustomerName", |
| 180 | + new object[] { 1337 }, null)); |
| 181 | + } |
| 182 | + |
| 183 | + // Invoke a static method with a default value of a named arg. |
| 184 | + static bool TestInvokeStaticMethodWithNamedArgDefault() { |
| 185 | + return CheckValue("Hello There", |
| 186 | + (string)VersionHandler.InvokeStaticMethod(typeof(Greeter), |
| 187 | + "GenericHelloWithPronoun", |
| 188 | + null, null)); |
| 189 | + } |
| 190 | + |
| 191 | + // Invoke a static method with a named arg. |
| 192 | + static bool TestInvokeStaticMethodWithNamedArg() { |
| 193 | + return CheckValue("Hello Miss", |
| 194 | + (string)VersionHandler.InvokeStaticMethod( |
| 195 | + typeof(Greeter), "GenericHelloWithPronoun", |
| 196 | + null, new Dictionary<string, object> { { "pronoun", "Miss" } })); |
| 197 | + } |
| 198 | + |
| 199 | + // Invoke a static method with a positional and default value for a named arg. |
| 200 | + static bool TestInvokeStaticMethodWithArgAndNamedArgDefault() { |
| 201 | + return CheckValue("Hello There Bob", |
| 202 | + (string)VersionHandler.InvokeStaticMethod( |
| 203 | + typeof(Greeter), "GenericHelloWithCustomerNameAndPronoun", |
| 204 | + new object[] { "Bob" }, null)); |
| 205 | + } |
| 206 | + |
| 207 | + // Invoke a static method with a positional and named arg. |
| 208 | + static bool TestInvokeStaticMethodWithArgAndNamedArg() { |
| 209 | + return CheckValue("Hello Mrs Smith", |
| 210 | + (string)VersionHandler.InvokeStaticMethod( |
| 211 | + typeof(Greeter), "GenericHelloWithCustomerNameAndPronoun", |
| 212 | + new object[] { "Smith" }, |
| 213 | + new Dictionary<string, object> { { "pronoun", "Mrs" } } )); |
| 214 | + } |
| 215 | + |
| 216 | + // Invoke an instance method with no args. |
| 217 | + static bool TestInvokeInstanceMethodWithNoArgs() { |
| 218 | + return CheckValue("Hello, my name is Sam", |
| 219 | + (string)VersionHandler.InvokeInstanceMethod(new Greeter("Sam"), "Hello", |
| 220 | + null, null)); |
| 221 | + } |
| 222 | + |
| 223 | + // Invoke an instance method with an default value for a named argument. |
| 224 | + static bool TestInvokeInstanceMethodWithNamedArgDefault() { |
| 225 | + return CheckValue("Hello There Helen, my name is Sam", |
| 226 | + (string)VersionHandler.InvokeInstanceMethod( |
| 227 | + new Greeter("Sam"), "HelloWithCustomerNameAndPronoun", |
| 228 | + new object[] { "Helen" }, null)); |
| 229 | + } |
| 230 | + |
| 231 | + // Invoke an instance method with a named argument. |
| 232 | + static bool TestInvokeInstanceMethodWithNamedArg() { |
| 233 | + return CheckValue("Hello Mrs Smith, my name is Sam", |
| 234 | + (string)VersionHandler.InvokeInstanceMethod( |
| 235 | + new Greeter("Sam"), "HelloWithCustomerNameAndPronoun", |
| 236 | + new object[] { "Smith" }, |
| 237 | + new Dictionary<string, object> { { "pronoun", "Mrs" } })); |
| 238 | + } |
| 239 | +} |
0 commit comments