Ejemplo n.º 1
0
        /// <summary>
        /// Iterates through all supported types for <see cref="UnitTests.WhenIHaveA{T}()"/> and generates instances.  Then
        /// calls all <see cref="IRDMPSingleDatabaseObjectControl"/> user interfaces which match the instance (e.g.
        /// <see cref="CatalogueUI"/> for <see cref="Catalogue"/>.  Then calls the <paramref name="action"/> on the ui instance
        /// created.
        /// </summary>
        /// <param name="action"></param>
        protected void ForEachUI(Action <IRDMPSingleDatabaseObjectControl> action)
        {
            SetupMEF();

            var types = typeof(Catalogue).Assembly.GetTypes()
                        .Where(t => t != null && typeof(DatabaseEntity).IsAssignableFrom(t) && !t.IsAbstract && !t.IsInterface).ToArray();

            var uiTypes = typeof(CatalogueUI).Assembly.GetTypes()
                          .Where(t => t != null && typeof(IRDMPSingleDatabaseObjectControl).IsAssignableFrom(t) &&
                                 !t.IsAbstract && !t.IsInterface &&
                                 t.BaseType != null &&
                                 t.BaseType.BaseType != null &&
                                 t.BaseType.BaseType.GetGenericArguments().Any()).ToArray();

            var methods          = typeof(UITests).GetMethods(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
            var methodWhenIHaveA = methods.Single(m => m.Name.Equals("WhenIHaveA") && !m.GetParameters().Any());

            List <DatabaseEntity> objectsToTest = new List <DatabaseEntity>();

            foreach (Type t in types)
            {
                //ignore these types too
                if (SkipTheseTypes.Contains(t.Name) || t.Name.StartsWith("Spontaneous") ||
                    typeof(SpontaneousObject).IsAssignableFrom(t))
                {
                    continue;
                }

                //ensure that the method supports the Type
                var genericWhenIHaveA = methodWhenIHaveA.MakeGenericMethod(t);
                var instance          = (DatabaseEntity)genericWhenIHaveA.Invoke(this, null);

                objectsToTest.Add(instance);
            }

            //sets up all the child providers etc
            InitializeItemActivator();

            foreach (DatabaseEntity o in objectsToTest)
            {
                //foreach compatible UI
                foreach (var uiType in uiTypes.Where(a => a.BaseType.BaseType.GetGenericArguments()[0] == o.GetType()))
                {
                    //todo
                    var methodAndLaunch = methods.Single(m => m.Name.Equals("AndLaunch"));

                    //ensure that the method supports the Type
                    var genericAndLaunch = methodAndLaunch.MakeGenericMethod(uiType);

                    IRDMPSingleDatabaseObjectControl ui;

                    try
                    {
                        ui = (IRDMPSingleDatabaseObjectControl)genericAndLaunch.Invoke(this, new object[] { o, true });
                    }
                    catch (Exception ex)
                    {
                        throw new Exception("Failed to construct '" + uiType + "'.  Code to reproduce is:" + Environment.NewLine + ShowCode(o.GetType(), uiType), ex);
                    }


                    action(ui);
                    ClearResults();
                }
            }
        }
Ejemplo n.º 2
0
        public void TestAllSupported()
        {
            //load all DatabaseEntity types
            MEF mef = new MEF();

            mef.Setup(new SafeDirectoryCatalog(TestContext.CurrentContext.TestDirectory));

            var types = mef.GetAllTypes()
                        .Where(t => typeof(DatabaseEntity).IsAssignableFrom(t) && !t.IsAbstract && !t.IsInterface).ToArray();

            var methods = typeof(UnitTests).GetMethods(BindingFlags.NonPublic | BindingFlags.Instance);
            var method  = methods.Single(m => m.Name.Equals("WhenIHaveA") && !m.GetParameters().Any());

            List <Type> notSupported = new List <Type>();

            foreach (Type t in types)
            {
                //ignore these types too
                if (SkipTheseTypes.Contains(t.Name) || t.Name.StartsWith("Spontaneous") || typeof(SpontaneousObject).IsAssignableFrom(t))
                {
                    continue;
                }

                DatabaseEntity instance = null;

                try
                {
                    //ensure that the method supports the Type
                    var generic = method.MakeGenericMethod(t);
                    instance = (DatabaseEntity)generic.Invoke(this, null);
                }
                catch (TargetInvocationException exception)
                {
                    if (exception.InnerException is TestCaseNotWrittenYetException)
                    {
                        notSupported.Add(t);
                    }
                    else
                    {
                        throw;
                    }
                }

                //if the instance returned by MakeGenericMethod does not pass checks that's a dealbreaker!
                if (instance != null)
                {
                    try
                    {
                        //and that it returns an instance
                        Assert.IsNotNull(instance);
                        Assert.IsTrue(instance.Exists());
                        Assert.AreEqual(ChangeDescription.NoChanges, instance.HasLocalChanges().Evaluation, "Type was '" + t.Name + "'");
                    }
                    catch (Exception e)
                    {
                        throw new Exception("Implementation of WhenIHaveA<" + t.Name + "> is flawed", e);
                    }
                }
            }

            Assert.IsEmpty(notSupported, "The following Types were not supported by WhenIHaveA<T>:" + Environment.NewLine + string.Join(Environment.NewLine, notSupported.Select(t => t.Name)));
        }