public void Convert_StringToString_CorrectValue() { //Build the SUT var tc = new TypeConverter(); //Call the method to test var actual = tc.Convert("My God", typeof(string)); //Assertion Assert.That(actual, Is.InstanceOf<string>()); Assert.That(actual, Is.EqualTo("My God")); }
public void Convert_StringToEnum_CorrectValue() { //Build the SUT var tc = new TypeConverter(); //Call the method to test var actual = tc.Convert("Beta", typeof(NBi.Testing.Unit.Core.Assemblies.Resource.Enumeration)); //Assertion Assert.That(actual, Is.InstanceOf<NBi.Testing.Unit.Core.Assemblies.Resource.Enumeration>()); Assert.That(actual, Is.EqualTo(NBi.Testing.Unit.Core.Assemblies.Resource.Enumeration.Beta)); }
public void Convert_StringToDateTime_YMDHMCorrectValue() { //Build the SUT var tc = new TypeConverter(); //Call the method to test var actual = tc.Convert("2012-05-10 10:15", typeof(DateTime)); //Assertion Assert.That(actual, Is.InstanceOf<DateTime>()); Assert.That(actual, Is.EqualTo(new DateTime(2012, 05, 10, 10, 15, 0))); }
public void Convert_StringToDecimal_CorrectValue() { //Build the SUT var tc = new TypeConverter(); //Call the method to test var actual = tc.Convert("10", typeof(decimal)); //Assertion Assert.That(actual, Is.InstanceOf<decimal>()); Assert.That(actual, Is.EqualTo(10)); }
/// <summary> /// Execute the static method of the a type. Let you specify the value of the parameters of the method called. /// </summary> /// <param name="target">The type on which you want to apply a method</param> /// <param name="methodName">The name of the static method</param> /// <param name="parameters">The pair of names and values for each parameter of the method</param> /// <returns></returns> public object ExecuteStatic(Type type, string methodName, IDictionary<string, object> parameters) { var flags = BindingFlags.IgnoreCase | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static; MethodInfo methodInfo = type.GetMethod(methodName, flags); if (methodInfo == null) throw new ArgumentException(string.Format("Method named '{0}' not found in type '{1}'", methodName, type), "methodName"); var paramList = new List<object>(); foreach (ParameterInfo paramInfo in methodInfo.GetParameters()) { var converter = new TypeConverter(); var value = converter.Convert(parameters[paramInfo.Name], paramInfo.ParameterType); paramList.Add(value); } var result = methodInfo.Invoke(null, paramList.ToArray()); return result; }