public void Traverse2_Types() { var instance = new Traverse2Types <InnerClass>(); var trv = Traverse2.Create(instance); Assert.AreEqual( 100, trv.Field("IntField").GetValue <int>() ); Assert.AreEqual( "hello", trv.Field("StringField").GetValue <string>() ); var boolArray = trv.Field("ListOfBoolField").GetValue <IEnumerable <bool> >().ToArray(); Assert.AreEqual(true, boolArray[0]); Assert.AreEqual(false, boolArray[1]); var mixed = trv.Field("MixedField").GetValue <Dictionary <InnerClass, List <string> > >(); var key = trv.Field("key").GetValue <InnerClass>(); _ = mixed.TryGetValue(key, out var value); Assert.AreEqual("world", value.First()); var trvEmpty = Traverse2.Create(instance).Type("FooBar"); TestTraverse2_Basics.AssertIsEmpty(trvEmpty); }
public void Traverse2_Method_Static() { var trv = Traverse2.Create(typeof(Traverse2Methods_Static)); var mtrv = trv.Method("StaticMethod", new object[] { 6, 7 }); Assert.AreEqual(42, mtrv.GetValue <int>()); }
public void Traverse2_SilentFailures() { var trv1 = new Traverse2(null); AssertIsEmpty(trv1); trv1 = Traverse2.Create(null); AssertIsEmpty(trv1); var trv2 = trv1.Type("FooBar"); AssertIsEmpty(trv2); var trv3 = Traverse2.Create <FooBar>().Field("field"); AssertIsEmpty(trv3); var trv4 = Traverse2.CreateWithType(typeof(FooBar).FullName).Field("field"); AssertIsEmpty(trv4); var trv5 = new Traverse2(new FooBar()).Field("field"); AssertIsEmpty(trv5.Method("", new object[0])); AssertIsEmpty(trv5.Method("", new Type[0], new object[0])); }
public void Test_Traverse2_Create_Instance_ToString() { var instance = new Traverse2Fields_AccessModifiers(Traverse2Fields.testStrings); var trv = Traverse2.Create(instance); Assert.AreEqual(instance.ToString(), trv.ToString()); }
public void Traverse2_Field_ToString() { var instance = new Traverse2Fields_AccessModifiers(Traverse2Fields.testStrings); var trv = Traverse2.Create(instance).Field(Traverse2Fields.fieldNames[0]); Assert.AreEqual(Traverse2Fields.testStrings[0], trv.ToString()); }
public void Traverse2_Property_ToString() { var instance = new Traverse2Properties_AccessModifiers(Traverse2Properties.testStrings); var trv = Traverse2.Create(instance).Property(Traverse2Properties.propertyNames[0]); Assert.AreEqual(Traverse2Properties.testStrings[0], trv.ToString()); }
public void Traverse2T_Create_With_Null() { var trv = new Traverse2 <int>(Traverse2.Create(null)); Assert.AreEqual(0, trv.Value); trv.Value = 100; Assert.AreEqual(0, trv.Value); }
public void Traverse2_Method_Overloads() { var instance = new Traverse2Methods_Overloads(); var trv = Traverse2.Create(instance); var mtrv1 = trv.Method("SomeMethod", new Type[] { typeof(string), typeof(bool) }); Assert.AreEqual(true, mtrv1.GetValue <bool>("test", false)); Assert.AreEqual(false, mtrv1.GetValue <bool>("test", true)); }
public void Traverse2_Method_VariableArguments() { var trv = Traverse2.Create(typeof(Traverse2Methods_VarArgs)); Assert.AreEqual(30, trv.Method("Test1", 10, 20).GetValue <int>()); Assert.AreEqual(60, trv.Method("Test2", 10, 20, 30).GetValue <int>()); // Calling varargs methods directly won't work. Use parameter array instead // Assert.AreEqual(60, trv.Method("Test3", 100, 10, 20, 30).GetValue<int>()); Assert.AreEqual(6000, trv.Method("Test3", 100, new int[] { 10, 20, 30 }).GetValue <int>()); }
public void Traverse2_Method_OutParameters() { var trv = Traverse2.Create(typeof(Traverse2Methods_Parameter)); string result = null; var parameters = new object[] { result }; var types = new Type[] { typeof(string).MakeByRefType() }; var mtrv1 = trv.Method("WithOutParameter", types, parameters); Assert.AreEqual("ok", mtrv1.GetValue <string>()); Assert.AreEqual("hello", parameters[0]); }
public void Traverse2_Property_Static() { var instance = new Traverse2_BaseClass(); var trv1 = Traverse2.Create(instance).Property("StaticProperty"); Assert.AreEqual("test1", trv1.GetValue()); var trv2 = Traverse2.Create(typeof(Traverse2Properties_Static)).Property("StaticProperty"); Assert.AreEqual("test2", trv2.GetValue()); }
public void Traverse2_Field_Static() { var instance = new Traverse2_BaseClass(); var trv1 = Traverse2.Create(instance).Field("staticField"); Assert.AreEqual("test1", trv1.GetValue()); var trv2 = Traverse2.Create(typeof(Traverse2Fields_Static)).Field("staticField"); Assert.AreEqual("test2", trv2.GetValue()); }
public void Traverse2_Property_GetValue() { var instance = new Traverse2Properties_AccessModifiers(Traverse2Properties.testStrings); var trv = Traverse2.Create(instance); for (var i = 0; i < Traverse2Properties.testStrings.Length; i++) { var name = Traverse2Properties.propertyNames[i]; var ptrv = trv.Property(name); Assert.NotNull(ptrv); Assert.AreEqual(Traverse2Properties.testStrings[i], ptrv.GetValue()); Assert.AreEqual(Traverse2Properties.testStrings[i], ptrv.GetValue <string>()); } }
public void Traverse2_InnerInstance() { var instance = new Traverse2NestedTypes(null); var trv1 = Traverse2.Create(instance); var field1 = trv1.Field("innerInstance").Field("inner2").Field("field"); _ = field1.SetValue("somevalue"); var trv2 = Traverse2.Create(instance); var field2 = trv2.Field("innerInstance").Field("inner2").Field("field"); Assert.AreEqual("somevalue", field2.GetValue()); }
public void Traverse2_Static_Field_Instance_Field() { var extra = new Traverse2_ExtraClass("test1"); Assert.AreEqual("test1", Traverse2.Create(extra).Field("someString").GetValue()); Assert.AreEqual("test2", Traverse2Fields_Static.extraClassInstance.someString, "direct"); var trv = Traverse2.Create(typeof(Traverse2Fields_Static)); var trv2 = trv.Field("extraClassInstance"); Assert.AreEqual(typeof(Traverse2_ExtraClass), trv2.GetValue().GetType()); Assert.AreEqual("test2", trv2.Field("someString").GetValue(), "Traverse2"); }
public void Traverse2_Method_Instance() { var instance = new Traverse2Methods_Instance(); var trv = Traverse2.Create(instance); instance.Method1_called = false; var mtrv1 = trv.Method("Method1"); Assert.AreEqual(null, mtrv1.GetValue()); Assert.AreEqual(true, instance.Method1_called); var mtrv2 = trv.Method("Method2", new object[] { "arg" }); Assert.AreEqual("argarg", mtrv2.GetValue()); }
public void Traverse2_Field_GetValue() { var instance = new Traverse2Fields_AccessModifiers(Traverse2Fields.testStrings); var trv = Traverse2.Create(instance); for (var i = 0; i < Traverse2Fields.testStrings.Length; i++) { var name = Traverse2Fields.fieldNames[i]; var ftrv = trv.Field(name); Assert.NotNull(ftrv); Assert.AreEqual(Traverse2Fields.testStrings[i], ftrv.GetValue()); Assert.AreEqual(Traverse2Fields.testStrings[i], ftrv.GetValue <string>()); } }
public void Traverse2_Create_With_Null() { var trv = Traverse2.Create(null); Assert.NotNull(trv); Assert.Null(trv.ToString()); // field access var ftrv = trv.Field("foo"); Assert.NotNull(ftrv); Assert.Null(ftrv.GetValue()); Assert.Null(ftrv.ToString()); Assert.AreEqual(0, ftrv.GetValue <int>()); Assert.AreSame(ftrv, ftrv.SetValue(123)); // property access var ptrv = trv.Property("foo"); Assert.NotNull(ptrv); Assert.Null(ptrv.GetValue()); Assert.Null(ptrv.ToString()); Assert.Null(ptrv.GetValue <string>()); Assert.AreSame(ptrv, ptrv.SetValue("test")); // method access var mtrv = trv.Method("zee"); Assert.NotNull(mtrv); Assert.Null(mtrv.GetValue()); Assert.Null(mtrv.ToString()); Assert.AreEqual(0, mtrv.GetValue <float>()); Assert.AreSame(mtrv, mtrv.SetValue(null)); }
public void Traverse2_InnerStatic() { var trv1 = Traverse2.Create(typeof(Traverse2NestedTypes)); var field1 = trv1.Field("innerStatic").Field("inner2").Field("field"); _ = field1.SetValue("somevalue1"); var trv2 = Traverse2.Create(typeof(Traverse2NestedTypes)); var field2 = trv2.Field("innerStatic").Field("inner2").Field("field"); Assert.AreEqual("somevalue1", field2.GetValue()); _ = new Traverse2NestedTypes("somevalue2"); var value = Traverse2 .Create(typeof(Traverse2NestedTypes)) .Type("InnerStaticClass1") .Type("InnerStaticClass2") .Field("field") .GetValue <string>(); Assert.AreEqual("somevalue2", value); }
public void Traverse2_Field_SetValue() { var instance = new Traverse2Fields_AccessModifiers(Traverse2Fields.testStrings); var trv = Traverse2.Create(instance); for (var i = 0; i < Traverse2Fields.testStrings.Length; i++) { var newValue = "newvalue" + i; // before Assert.AreEqual(Traverse2Fields.testStrings[i], instance.GetTestField(i)); var name = Traverse2Fields.fieldNames[i]; var ftrv = trv.Field(name); _ = ftrv.SetValue(newValue); // after Assert.AreEqual(newValue, instance.GetTestField(i)); Assert.AreEqual(newValue, ftrv.GetValue()); Assert.AreEqual(newValue, ftrv.GetValue <string>()); } }
public void Traverse2_Property_SetValue() { var instance = new Traverse2Properties_AccessModifiers(Traverse2Properties.testStrings); var trv = Traverse2.Create(instance); for (var i = 0; i < Traverse2Properties.testStrings.Length - 1; i++) { var newValue = "newvalue" + i; // before Assert.AreEqual(Traverse2Properties.testStrings[i], instance.GetTestProperty(i)); var name = Traverse2Properties.propertyNames[i]; var ptrv = trv.Property(name); Assert.NotNull(ptrv); _ = ptrv.SetValue(newValue); // after Assert.AreEqual(newValue, instance.GetTestProperty(i)); Assert.AreEqual(newValue, ptrv.GetValue()); Assert.AreEqual(newValue, ptrv.GetValue <string>()); } }
public void Traverse2_Instance_Field_Static_Field() { var instance = new Traverse2_ExtraClass("test3"); Assert.AreEqual(typeof(Traverse2_BaseClass), instance.baseClass.GetType()); var trv1 = Traverse2.Create(instance); Assert.NotNull(trv1, "trv1"); var trv2 = trv1.Field("baseClass"); Assert.NotNull(trv2, "trv2"); var val = trv2.GetValue(); Assert.NotNull(val, "val"); Assert.AreEqual(typeof(Traverse2_BaseClass), val.GetType()); var trv3 = trv2.Field("baseField"); Assert.NotNull(trv3, "trv3"); Assert.AreEqual("base-field", trv3.GetValue()); }
public void OutputRuntimeInfo() { var runtimeInformationType = AccessTools2.TypeByName("System.Runtime.InteropServices.RuntimeInformation"); var executingAssembly = Assembly.GetExecutingAssembly(); TestTools.Log("Environment.OSVersion: " + Environment.OSVersion); TestTools.Log("RuntimeInformation.OSDescription: " + GetProperty(runtimeInformationType, "OSDescription")); TestTools.Log("IntPtr.Size: " + IntPtr.Size); TestTools.Log("Environment.Is64BitProcess: " + GetProperty(typeof(Environment), "Is64BitProcess")); TestTools.Log("Environment.Is64BitOperatingSystem: " + GetProperty(typeof(Environment), "Is64BitOperatingSystem")); TestTools.Log("RuntimeInformation.ProcessArchitecture: " + GetProperty(runtimeInformationType, "ProcessArchitecture")); TestTools.Log("RuntimeInformation.OSArchitecture: " + GetProperty(runtimeInformationType, "OSArchitecture")); TestTools.Log("RuntimeInformation.FrameworkDescription: " + GetProperty(runtimeInformationType, "FrameworkDescription")); TestTools.Log("Mono.Runtime.DisplayName: " + CallGetter(Type.GetType("Mono.Runtime"), "GetDisplayName")); TestTools.Log("RuntimeEnvironment.RuntimeDirectory: " + RuntimeEnvironment.GetRuntimeDirectory()); TestTools.Log("RuntimeEnvironment.SystemVersion: " + RuntimeEnvironment.GetSystemVersion()); TestTools.Log("Environment.Version: " + Environment.Version); TestTools.Log("Core Assembly: " + typeof(object).Assembly); TestTools.Log("Executing Assembly's ImageRuntimeVersion: " + executingAssembly.ImageRuntimeVersion); TestTools.Log("Executing Assembly's TargetFrameworkAttribute: " + (executingAssembly.GetCustomAttributes(true) .Where(attr => attr.GetType().Name is "TargetFrameworkAttribute") .Select(attr => Traverse2.Create(attr).Property("FrameworkName").GetValue <string>()) .FirstOrDefault() ?? "null")); }