public TestDataSetAttribute(Type declaringType, string propertyName, TestDataVariations testDataVariations = TestCommon.TestDataVariations.All)
 {
     DeclaringType = declaringType;
     PropertyName = propertyName;
     TestDataVariations = testDataVariations;
     ExtraDataSets = new List<Tuple<Type, string>>();
 }
 public TestDataSetAttribute(Type declaringType, string propertyName,
                             Type declaringType1, string propertyName1,
                             TestDataVariations testDataVariations = TestCommon.TestDataVariations.All)
     : this(declaringType, propertyName, testDataVariations)
 {
     ExtraDataSets = new List<Tuple<Type, string>> { Tuple.Create(declaringType1, propertyName1) };
 }
Exemple #3
0
        /// <summary>
        /// Gets test data for the given test data variation or returns null if the <see cref="TestData"/> instance
        /// doesn't support the given variation.
        /// </summary>
        /// <param name="variation">The test data variation with which to create the related test data.</param>
        /// <returns>Test data of the type specified by the <see cref="TestData.GetAsTypeOrNull"/> method.</returns>
        public object GetAsTestDataOrNull(TestDataVariations variation)
        {
            TestDataVariationProvider testDataVariation = null;

            if (this.registeredTestDataVariations.TryGetValue(variation, out testDataVariation))
            {
                return(testDataVariation.TestDataProvider());
            }

            return(null);
        }
Exemple #4
0
        /// <summary>
        /// Gets the related type for the given test data variation or returns null if the <see cref="TestData"/> instance
        /// doesn't support the given variation.
        /// </summary>
        /// <param name="variation">The test data variation with which to create the related <see cref="Type"/>.</param>
        /// <returns>The related <see cref="Type"/> for the <see cref="TestData.Type"/> as given by the test data variation.</returns>
        /// <example>
        /// For example, if the given <see cref="TestData"/> was created for <see cref="string"/> test data and the varation parameter
        /// was <see cref="TestDataVariations.AsList"/> then the returned type would be <see cref="List<string>"/>.
        /// </example>
        public Type GetAsTypeOrNull(TestDataVariations variation)
        {
            TestDataVariationProvider testDataVariation = null;

            if (this.registeredTestDataVariations.TryGetValue(variation, out testDataVariation))
            {
                return(testDataVariation.Type);
            }

            return(null);
        }
 public TestDataSetAttribute(Type declaringType, string propertyName,
                             Type declaringType1, string propertyName1,
                             Type declaringType2, string propertyName2,
                             Type declaringType3, string propertyName3,
                             TestDataVariations testDataVariations = TestCommon.TestDataVariations.All)
     : this(declaringType, propertyName, testDataVariations)
 {
     ExtraDataSets = new List <Tuple <Type, string> > {
         Tuple.Create(declaringType1, propertyName1), Tuple.Create(declaringType2, propertyName2), Tuple.Create(declaringType3, propertyName3)
     };
 }
        private static IEnumerable <object[]> TryGetDataSetFromTestDataCollection(
            Type declaringType,
            string propertyName,
            TestDataVariations variations
            )
        {
            object propertyValue = GetTestDataPropertyValue(declaringType, propertyName);

            IEnumerable <TestData> testDataCollection = propertyValue as IEnumerable <TestData>;

            return(testDataCollection == null
              ? null
              : GetDataSetFromTestDataCollection(testDataCollection, variations));
        }
        /// <summary>
        /// Creates an instance of the generic <see cref="HttpResponseMessage"/> for every value
        /// in the given <paramref name="testDataCollection"/> and invokes the <paramref name="codeUnderTest"/>.
        /// </summary>
        /// <param name="testDataCollection">The collection of test data.</param>
        /// <param name="flags">The test variations.</param>
        /// <param name="codeUnderTest">The code to invoke with each <see cref="HttpResponseMessage"/>.</param>
        public static void ExecuteForEachHttpResponseMessage(IEnumerable<TestData> testDataCollection, TestDataVariations flags, Action<HttpResponseMessage, Type, object> codeUnderTest)
        {
            Assert.IsNotNull(testDataCollection, "testDataCollection cannot be null.");
            Assert.IsNotNull(codeUnderTest, "codeUnderTest cannot be null.");

            TestDataAssert.Execute(
                testDataCollection,
                flags,
                "Failed in ExecuteForEachHttpResponseMessage.",
                (type, obj) =>
                {
                    Type convertType = obj == null ? type : obj.GetType();
                    HttpResponseMessage response =
                        (HttpResponseMessage)GenericTypeAssert.InvokeConstructor(
                            typeof(HttpResponseMessage<>),
                            convertType,
                            new Type[] { convertType },
                            new object[] { obj });

                    codeUnderTest(response, type, obj);
                });
        }
Exemple #8
0
 /// <summary>
 /// Allows derived classes to register a <paramref name="testDataProvider "/> <see cref="Func<>"/> that will
 /// provide test data for a given variation.
 /// </summary>
 /// <param name="variation">The variation with which to register the <paramref name="testDataProvider "/>r.</param>
 /// <param name="type">The type of the test data created by the <paramref name="testDataProvider "/></param>
 /// <param name="testDataProvider">A <see cref="Func<>"/> that will provide test data.</param>
 protected void RegisterTestDataVariation(TestDataVariations variation, Type type, Func <object> testDataProvider)
 {
     this.registeredTestDataVariations.Add(variation, new TestDataVariationProvider(type, testDataProvider));
 }
 private static IEnumerable<object[]> GetDataSetFromTestDataCollection(IEnumerable<TestData> testDataCollection, TestDataVariations variations)
 {
     foreach (TestData testdataInstance in testDataCollection)
     {
         foreach (TestDataVariations variation in testdataInstance.GetSupportedTestDataVariations())
         {
             if ((variation & variations) == variation)
             {
                 Type variationType = testdataInstance.GetAsTypeOrNull(variation);
                 object testData = testdataInstance.GetAsTestDataOrNull(variation);
                 if (AsSingleInstances(variation))
                 {
                     foreach (object obj in (IEnumerable)testData)
                     {
                         yield return new object[] { variationType, obj };
                     }
                 }
                 else
                 {
                     yield return new object[] { variationType, testData };
                 }
             }
         }
     }
 }
 // The base data set(first one) can either be a TestDataSet or a TestDataSetCollection
 private static IEnumerable <object[]> GetBaseDataSet(Type declaringType, string propertyName, TestDataVariations variations)
 {
     return(TryGetDataSetFromTestDataCollection(declaringType, propertyName, variations) ?? GetDataSet(declaringType, propertyName));
 }
 // The base data set(first one) can either be a TestDataSet or a TestDataSetCollection
 private static IEnumerable<object[]> GetBaseDataSet(Type declaringType, string propertyName, TestDataVariations variations)
 {
     return TryGetDataSetFromTestDataCollection(declaringType, propertyName, variations) ?? GetDataSet(declaringType, propertyName);
 }
        private static IEnumerable<object[]> TryGetDataSetFromTestDataCollection(Type declaringType, string propertyName, TestDataVariations variations)
        {
            object propertyValue = GetTestDataPropertyValue(declaringType, propertyName);

            IEnumerable<TestData> testDataCollection = propertyValue as IEnumerable<TestData>;

            return testDataCollection == null ? null : GetDataSetFromTestDataCollection(testDataCollection, variations);
        }
Exemple #13
0
 /// <summary>
 /// Allows derived classes to register a <paramref name="testDataProvider "/> <see cref="Func<>"/> that will 
 /// provide test data for a given variation.
 /// </summary>
 /// <param name="variation">The variation with which to register the <paramref name="testDataProvider "/>r.</param>
 /// <param name="type">The type of the test data created by the <paramref name="testDataProvider "/></param>
 /// <param name="testDataProvider">A <see cref="Func<>"/> that will provide test data.</param>
 protected void RegisterTestDataVariation(TestDataVariations variation, Type type, Func<object> testDataProvider)
 {
     this.registeredTestDataVariations.Add(variation, new TestDataVariationProvider(type, testDataProvider));
 }
 private static IEnumerable <object[]> GetDataSetFromTestDataCollection(IEnumerable <TestData> testDataCollection, TestDataVariations variations)
 {
     foreach (TestData testdataInstance in testDataCollection)
     {
         foreach (TestDataVariations variation in testdataInstance.GetSupportedTestDataVariations())
         {
             if ((variation & variations) == variation)
             {
                 Type   variationType = testdataInstance.GetAsTypeOrNull(variation);
                 object testData      = testdataInstance.GetAsTestDataOrNull(variation);
                 if (AsSingleInstances(variation))
                 {
                     foreach (object obj in (IEnumerable)testData)
                     {
                         yield return(new object[] { variationType, obj });
                     }
                 }
                 else
                 {
                     yield return(new object[] { variationType, testData });
                 }
             }
         }
     }
 }
Exemple #15
0
        /// <summary>
        /// Gets test data for the given test data variation or returns null if the <see cref="TestData"/> instance
        /// doesn't support the given variation.
        /// </summary>
        /// <param name="variation">The test data variation with which to create the related test data.</param>
        /// <returns>Test data of the type specified by the <see cref="TestData.GetAsTypeOrNull"/> method.</returns>
        public object GetAsTestDataOrNull(TestDataVariations variation)
        {
            TestDataVariationProvider testDataVariation = null;
            if (this.registeredTestDataVariations.TryGetValue(variation, out testDataVariation))
            {
                return testDataVariation.TestDataProvider();
            }

            return null;
        }
Exemple #16
0
        /// <summary>
        /// Creates an instance of the generic <see cref="ObjectContent"/> for every value
        /// in the given <paramref name="testDataCollection"/> and invokes the <paramref name="codeUnderTest"/>.
        /// </summary>
        /// <param name="testDataCollection">The collection of test data.</param>
        /// <param name="flags">The test variations.</param>
        /// <param name="codeUnderTest">The code to invoke with each <see cref="ObjectContent"/>.</param>
        public static void ExecuteForEachObjectContent(IEnumerable<TestData> testDataCollection, TestDataVariations flags, Action<ObjectContent, Type, object> codeUnderTest)
        {
            Assert.IsNotNull(testDataCollection, "testDataCollection cannot be null.");
            Assert.IsNotNull(codeUnderTest, "codeUnderTest cannot be null.");

            TestDataAssert.Execute(
                testDataCollection,
                flags,
                "Failed in ExecuteForEachObjectContent.",
                (type, obj) =>
                {
                    Type convertType = obj == null ? type : obj.GetType();
                    ObjectContent objectContent =
                        (ObjectContent)GenericTypeAssert.InvokeConstructor(
                            typeof(ObjectContent<>),
                            convertType,
                            new Type[] { convertType },
                            new object[] { obj });

                    codeUnderTest(objectContent, type, obj);
                });
        }
Exemple #17
0
        public static void Execute(IEnumerable<TestData> testDataCollection, TestDataVariations flags, string messageOnFail, Action<Type, object> codeUnderTest)
        {
            if (testDataCollection == null)
            {
                throw new ArgumentNullException("testData");
            }

            if (codeUnderTest == null)
            {
                throw new ArgumentNullException("codeUnderTest");
            }

            foreach (TestData testdataInstance in testDataCollection)
            {
                foreach (TestDataVariations variation in testdataInstance.GetSupportedTestDataVariations())
                {
                    if ((variation & flags) == variation)
                    {
                        Type variationType = testdataInstance.GetAsTypeOrNull(variation);
                        object testData = testdataInstance.GetAsTestDataOrNull(variation);
                        if (AsSingleInstances(variation))
                        {
                            foreach (object obj in (IEnumerable)testData)
                            {
                                ExecuteCodeUnderTest(variationType, obj, messageOnFail, codeUnderTest);
                            }
                        }
                        else
                        {
                            ExecuteCodeUnderTest(variationType, testData, messageOnFail, codeUnderTest);
                        }
                    }
                }
            }
        }
Exemple #18
0
        /// <summary>
        /// Creates an instance of the generic <see cref="HttpRequestMessage"/> for every value
        /// in the given <paramref name="testDataCollection"/> and invokes the <paramref name="codeUnderTest"/>.
        /// </summary>
        /// <param name="testDataCollection">The collection of test data.</param>
        /// <param name="flags">The test variations.</param>
        /// <param name="codeUnderTest">The code to invoke with each <see cref="HttpRequestMessage"/>.</param>
        public static void ExecuteForEachHttpRequestMessage(IEnumerable <TestData> testDataCollection, TestDataVariations flags, Action <HttpRequestMessage, Type, object> codeUnderTest)
        {
            Assert.IsNotNull(testDataCollection, "testDataCollection cannot be null.");
            Assert.IsNotNull(codeUnderTest, "codeUnderTest cannot be null.");

            TestDataAssert.Execute(
                testDataCollection,
                flags,
                "Failed in ExecuteForEachHttpRequestMessage.",
                (type, obj) =>
            {
                Type convertType           = obj == null ? type : obj.GetType();
                HttpRequestMessage request =
                    (HttpRequestMessage)GenericTypeAssert.InvokeConstructor(
                        typeof(HttpRequestMessage <>),
                        convertType,
                        new Type[] { convertType },
                        new object[] { obj });

                codeUnderTest(request, type, obj);
            });
        }
Exemple #19
0
        /// <summary>
        /// Creates an instance of the generic <see cref="ObjectContent"/> for every value
        /// in the given <paramref name="testDataCollection"/> and invokes the <paramref name="codeUnderTest"/>.
        /// </summary>
        /// <param name="testDataCollection">The collection of test data.</param>
        /// <param name="flags">The test variations.</param>
        /// <param name="codeUnderTest">The code to invoke with each <see cref="ObjectContent"/>.</param>
        public static void ExecuteForEachObjectContent(IEnumerable <TestData> testDataCollection, TestDataVariations flags, Action <ObjectContent, Type, object> codeUnderTest)
        {
            Assert.IsNotNull(testDataCollection, "testDataCollection cannot be null.");
            Assert.IsNotNull(codeUnderTest, "codeUnderTest cannot be null.");

            TestDataAssert.Execute(
                testDataCollection,
                flags,
                "Failed in ExecuteForEachObjectContent.",
                (type, obj) =>
            {
                Type convertType            = obj == null ? type : obj.GetType();
                ObjectContent objectContent =
                    (ObjectContent)GenericTypeAssert.InvokeConstructor(
                        typeof(ObjectContent <>),
                        convertType,
                        new Type[] { convertType },
                        new object[] { obj });

                codeUnderTest(objectContent, type, obj);
            });
        }
Exemple #20
0
 private static bool AsSingleInstances(TestDataVariations variation)
 {
     return variation == TestDataVariations.AsInstance ||
            variation == TestDataVariations.AsNullable ||
            variation == TestDataVariations.AsDerivedType ||
            variation == TestDataVariations.AsKnownType ||
            variation == TestDataVariations.AsDataMember ||
            variation == TestDataVariations.AsXmlElementProperty;
 }
Exemple #21
0
        /// <summary>
        /// Gets the related type for the given test data variation or returns null if the <see cref="TestData"/> instance
        /// doesn't support the given variation.
        /// </summary>
        /// <param name="variation">The test data variation with which to create the related <see cref="Type"/>.</param>
        /// <returns>The related <see cref="Type"/> for the <see cref="TestData.Type"/> as given by the test data variation.</returns>
        /// <example>
        /// For example, if the given <see cref="TestData"/> was created for <see cref="string"/> test data and the varation parameter
        /// was <see cref="TestDataVariations.AsList"/> then the returned type would be <see cref="List<string>"/>.
        /// </example>
        public Type GetAsTypeOrNull(TestDataVariations variation)
        {
            TestDataVariationProvider testDataVariation = null;
            if (this.registeredTestDataVariations.TryGetValue(variation, out testDataVariation))
            {
                return testDataVariation.Type;
            }

            return null;
        }