/// <summary> /// Checks initial value, sets and then checks value. Works with multiple properties. /// </summary> /// <param name="context"> <see cref="GetSetContext"/>, drives the test.</param> public static void GetSet(GetSetContext context) { Type type = context.Object.GetType(); foreach (KeyValuePair <string, List <object> > propertyKV in context.PropertyNamesAndSetGetValue) { PropertyInfo propertyInfo = type.GetProperty(propertyKV.Key); try { if (propertyInfo.GetMethod != null) { object initialValue = propertyInfo.GetValue(context.Object, null); if ((initialValue == null && propertyKV.Value[0] != null)) { context.Errors.Add(propertyKV.Key + ": initial value == null && expected != null, expect initial value: " + propertyKV.Value[0].ToString()); } else if (initialValue != null && propertyKV.Value[0] == null) { context.Errors.Add(propertyKV.Key + ": initial value != null && expected == null, initial value: " + initialValue.ToString()); } else if (initialValue != null && !initialValue.Equals(propertyKV.Value[0])) { context.Errors.Add(propertyKV.Key + ", initial value != expected. expected: " + propertyKV.Value[0].ToString() + ", was: " + initialValue.ToString()); } } if (propertyInfo.SetMethod != null) { for (int i = 1; i < propertyKV.Value.Count; i++) { propertyInfo.SetValue(context.Object, propertyKV.Value[i]); object getVal = propertyInfo.GetValue(context.Object, null); if ((getVal == null && propertyKV.Value[i] != null)) { context.Errors.Add(propertyKV.Key + "( " + i.ToString() + "), Get returned null, set was: " + propertyKV.Value[i].ToString()); } else if (getVal != null && propertyKV.Value[i] == null) { context.Errors.Add(propertyKV.Key + "( " + i.ToString() + "), Get not null, set was null, get was: " + getVal); } else if (getVal != null && !getVal.Equals(propertyKV.Value[i])) { context.Errors.Add(propertyKV.Key + "( " + i.ToString() + ") Set did not equal get: " + propertyKV.Value[i].ToString() + ", " + getVal + "."); } } } } catch (Exception ex) { context.Errors.Add(ex.ToString()); } } }
/// <summary> /// Sets a property, then checks it, checking for an expected exception. /// </summary> /// <param name="obj">object that has a 'setter'.</param> /// <param name="property">the name of the property.</param> /// <param name="propertyValue">value to set on the property.</param> /// <param name="expectedException">checks that exception is correct.</param> public static void SetGet(object obj, string property, object propertyValue, ExpectedException expectedException, GetSetContext context) { if (obj == null) { throw new TestException("obj == null"); } if (string.IsNullOrWhiteSpace(property)) { throw new TestException("string.IsNullOrWhiteSpace(property)"); } Type type = obj.GetType(); PropertyInfo propertyInfo = type.GetProperty(property); if (propertyInfo == null) { context.Errors.Add("'get is not found for property: '" + property + "', type: '" + type.ToString() + "'"); return; } if (!propertyInfo.CanWrite) { context.Errors.Add("can not write to property: '" + property + "', type: '" + type.ToString() + "'"); return; } var compareContext = new CompareContext(); try { propertyInfo.SetValue(obj, propertyValue); object retval = propertyInfo.GetValue(obj); IdentityComparer.AreEqual(propertyValue, retval, compareContext); expectedException.ProcessNoException(compareContext); } catch (Exception exception) { // look for InnerException as exception is a wrapped exception. expectedException.ProcessException(exception.InnerException, compareContext); } context.Errors.AddRange(compareContext.Diffs); }