Ejemplo n.º 1
0
        public static IMessageBox Message([NotNull] this IMessageBox definition, [CanBeNull] IResult result)
        {
            if (definition == null)
            {
                throw new ArgumentNullException(nameof(definition));
            }

            return(definition.Message(result?.ErrorDescription));
        }
Ejemplo n.º 2
0
        public static IMessageBox Message([NotNull] this IMessageBox definition, [NotNull] Exception exception)
        {
            if (definition == null)
            {
                throw new ArgumentNullException(nameof(definition));
            }

            if (exception == null)
            {
                throw new ArgumentNullException(nameof(exception));
            }

            return(definition.Message(exception.GetOriginalMessage()));
        }
Ejemplo n.º 3
0
        private void AddTestModule(IVBProject project, Declaration stubSource)
        {
            if (project == null || project.IsWrappingNullReference)
            {
                return;
            }

            var settings = _settings.Create();

            AddReferenceIfNeeded(project, settings);

            try
            {
                using (var components = project.VBComponents)
                    using (var component = components.Add(ComponentType.StandardModule))
                        using (var module = component.CodeModule)
                        {
                            component.Name = GetNextTestModuleName(project);

                            // Test modules always have appropriate options so remove any pre-generated code.
                            if (module.CountOfLines > 0)
                            {
                                module.DeleteLines(1, module.CountOfLines);
                            }

                            if (stubSource != null)
                            {
                                var code = GetNewTestModuleCode(component, GetDeclarationsToStub(stubSource).ToList());
                                module.AddFromString(code);
                            }
                            else
                            {
                                module.AddFromString(GetNewTestModuleCode(component));
                            }

                            component.Activate();
                        }
            }
            catch (Exception ex)
            {
                _messageBox.Message(TestExplorer.Command_AddTestModule_Error);
                Logger.Warn("Unable to add test module. An exception was thrown.");
                Logger.Warn(ex);
            }
        }
Ejemplo n.º 4
0
        private void ExecuteInternal(IVBProject project, Declaration projectDeclaration)
        {
            if (project == null || project.IsWrappingNullReference)
            {
                return;
            }

            var settings = _configLoader.LoadConfiguration().UserSettings.UnitTestSettings;

            if (settings.BindingMode == BindingMode.EarlyBinding)
            {
                // FIXME: Push the actual adding of TestModules into UnitTesting, which sidesteps VBEInteraction being inaccessble here
                _interaction.EnsureProjectReferencesUnitTesting(project);
            }

            try
            {
                using (var components = project.VBComponents)
                {
                    using (var component = components.Add(ComponentType.StandardModule))
                    {
                        using (var module = component.CodeModule)
                        {
                            component.Name = GetNextTestModuleName(project);

                            var hasOptionExplicit = false;
                            if (module.CountOfLines > 0 && module.CountOfDeclarationLines > 0)
                            {
                                hasOptionExplicit = module.GetLines(1, module.CountOfDeclarationLines)
                                                    .Contains("Option Explicit");
                            }

                            var options = string.Concat(hasOptionExplicit ? string.Empty : "Option Explicit\r\n",
                                                        "Option Private Module\r\n\r\n");

                            if (projectDeclaration != null)
                            {
                                var moduleCodeBuilder  = new StringBuilder();
                                var declarationsToStub = GetDeclarationsToStub(projectDeclaration);

                                foreach (var declaration in declarationsToStub)
                                {
                                    var name = string.Empty;

                                    switch (declaration.DeclarationType)
                                    {
                                    case DeclarationType.Procedure:
                                    case DeclarationType.Function:
                                        name = declaration.IdentifierName;
                                        break;

                                    case DeclarationType.PropertyGet:
                                        name = $"Get{declaration.IdentifierName}";
                                        break;

                                    case DeclarationType.PropertyLet:
                                        name = $"Let{declaration.IdentifierName}";
                                        break;

                                    case DeclarationType.PropertySet:
                                        name = $"Set{declaration.IdentifierName}";
                                        break;
                                    }

                                    var stub = AddTestMethodCommand.TestMethodTemplate.Replace(
                                        AddTestMethodCommand.NamePlaceholder, $"{name}_Test");
                                    moduleCodeBuilder.AppendLine(stub);
                                }

                                module.AddFromString(options + GetTestModule(settings) + moduleCodeBuilder);
                            }
                            else
                            {
                                var defaultTestMethod = settings.DefaultTestStubInNewModule
                                    ? AddTestMethodCommand.TestMethodTemplate.Replace(
                                    AddTestMethodCommand.NamePlaceholder,
                                    "TestMethod1")
                                    : string.Empty;

                                module.AddFromString(options + GetTestModule(settings) + defaultTestMethod);
                            }
                        }

                        component.Activate();
                    }
                }
            }
            catch (Exception ex)
            {
                _messageBox.Message(TestExplorer.Command_AddTestModule_Error);
                Logger.Warn("Unable to add test module. An exception was thrown.");
                Logger.Warn(ex);
            }

            _state.OnParseRequested(this);
        }