/// <summary> /// Initializes a new instance of the <see cref="ValidatedConstructorPropertyAssignmentTestScenario{T}"/> class. /// </summary> /// <param name="id">The identifier of the scenario.</param> /// <param name="propertyName">The name of the property that is assigned a value by the constructor.</param> /// <param name="systemUnderTestExpectedPropertyValueFunc">A func that returns the object to test and the expected value of the property being tested.</param> /// <param name="compareActualToExpectedUsing">Specifies how to compare the actual property value to the expected property value.</param> public ValidatedConstructorPropertyAssignmentTestScenario( string id, string propertyName, Func <SystemUnderTestExpectedPropertyValue <T> > systemUnderTestExpectedPropertyValueFunc, CompareActualToExpectedUsing compareActualToExpectedUsing) { new { id }.AsTest().Must().NotBeNullNorWhiteSpace(); new { propertyName }.AsTest().Must().NotBeNullNorWhiteSpace(id); T systemUnderTest = null; PropertyInfo property = null; object expectedPropertyValue = null; if ((propertyName != ConstructorPropertyAssignmentTestScenario.ForceGeneratedTestsToPassAndWriteMyOwnScenarioPropertyName) && (propertyName != ConstructorPropertyAssignmentTestScenario.NoPropertiesAssignedInConstructorScenarioPropertyName)) { new { systemUnderTestExpectedPropertyValueFunc }.AsTest().Must().NotBeNull(id); var systemUnderTestExpectedPropertyValue = systemUnderTestExpectedPropertyValueFunc(); systemUnderTest = systemUnderTestExpectedPropertyValue.SystemUnderTest; new { systemUnderTest }.AsTest().Must().NotBeNull(id); expectedPropertyValue = systemUnderTestExpectedPropertyValue.ExpectedPropertyValue; property = typeof(T).GetPropertyFiltered(propertyName, MemberRelationships.DeclaredOrInherited, MemberOwners.Instance, MemberAccessModifiers.Public, throwIfNotFound: false); new { property }.AsTest().Must().NotBeNull(id); if (compareActualToExpectedUsing == CompareActualToExpectedUsing.DefaultStrategy) { if (expectedPropertyValue == null) { compareActualToExpectedUsing = CompareActualToExpectedUsing.ReferenceEquality; } else if (expectedPropertyValue.GetType().IsValueType) { compareActualToExpectedUsing = CompareActualToExpectedUsing.ValueEquality; } else { compareActualToExpectedUsing = CompareActualToExpectedUsing.ReferenceEquality; } } } this.Id = id; this.PropertyName = propertyName; this.SystemUnderTest = systemUnderTest; this.ExpectedPropertyValue = expectedPropertyValue; this.Property = property; this.CompareActualToExpectedUsing = compareActualToExpectedUsing; }
/// <summary> /// Initializes a new instance of the <see cref="ValidatedConstructorPropertyAssignmentTestScenario{T}"/> class. /// </summary> /// <param name="id">The identifier of the scenario.</param> /// <param name="propertyName">The name of the property that is assigned a value by the constructor.</param> /// <param name="systemUnderTestExpectedPropertyValueFunc">A func that returns the object to test and the expected value of the property being tested.</param> /// <param name="compareActualToExpectedUsing">Specifies how to compare the actual property value to the expected property value.</param> public ValidatedConstructorPropertyAssignmentTestScenario( string id, string propertyName, Func <SystemUnderTestExpectedPropertyValue <T> > systemUnderTestExpectedPropertyValueFunc, CompareActualToExpectedUsing compareActualToExpectedUsing) { new { id }.AsTest().Must().NotBeNullNorWhiteSpace(); new { propertyName }.AsTest().Must().NotBeNullNorWhiteSpace(id); T systemUnderTest = null; PropertyInfo property = null; object expectedPropertyValue = null; if ((propertyName != ConstructorPropertyAssignmentTestScenario.ForceGeneratedTestsToPassAndWriteMyOwnScenarioPropertyName) && (propertyName != ConstructorPropertyAssignmentTestScenario.NoPropertiesAssignedInConstructorScenarioPropertyName)) { new { systemUnderTestExpectedPropertyValueFunc }.AsTest().Must().NotBeNull(id); var systemUnderTestExpectedPropertyValue = systemUnderTestExpectedPropertyValueFunc(); systemUnderTest = systemUnderTestExpectedPropertyValue.SystemUnderTest; new { systemUnderTest }.AsTest().Must().NotBeNull(id); expectedPropertyValue = systemUnderTestExpectedPropertyValue.ExpectedPropertyValue; property = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance).SingleOrDefault(_ => _.Name == propertyName); new { property }.AsTest().Must().NotBeNull(id); if (compareActualToExpectedUsing == CompareActualToExpectedUsing.DefaultStrategy) { if (expectedPropertyValue == null) { compareActualToExpectedUsing = CompareActualToExpectedUsing.ReferenceEquality; } else if (compareActualToExpectedUsing.GetType().IsValueType) { compareActualToExpectedUsing = CompareActualToExpectedUsing.ValueEquality; } else { compareActualToExpectedUsing = CompareActualToExpectedUsing.ReferenceEquality; } } } this.Id = id; this.PropertyName = propertyName; this.SystemUnderTest = systemUnderTest; this.ExpectedPropertyValue = expectedPropertyValue; this.Property = property; this.CompareActualToExpectedUsing = compareActualToExpectedUsing; }