public void ShouldNotHaveParamBlockIfScriptHasNoParametersDefined()
        {
            var           ast        = GenerateAst(scriptWithoutParameters);
            ParamBlockAst paramBlock = null;
            bool          result     = PowerShellParseUtilities.HasParamBlock(ast, out paramBlock);

            Assert.AreEqual <bool>(false, result);
        }
        private ParamBlockAst GenerateParamBlockAst(string script)
        {
            var           ast        = GenerateAst(script);
            ParamBlockAst paramBlock = null;

            if (PowerShellParseUtilities.HasParamBlock(ast, out paramBlock))
            {
                return(paramBlock);
            }
            return(null);
        }
Exemple #3
0
        public void ShouldDetectInputErrors()
        {
            var model = new ParameterEditorModel(
                new ObservableCollection <ScriptParameterViewModel>()
            {
                new ScriptParameterViewModel(new ScriptParameter()
                {
                    Name         = "StringType",
                    Type         = DataTypeConstants.StringType,
                    DefaultValue = null
                }),
                new ScriptParameterViewModel(new ScriptParameter()
                {
                    Name         = "CharType",
                    Type         = DataTypeConstants.CharType,
                    DefaultValue = null
                }),
                new ScriptParameterViewModel(new ScriptParameter()
                {
                    Name         = "ByteType",
                    Type         = DataTypeConstants.ByteType,
                    DefaultValue = null
                }),
                new ScriptParameterViewModel(new ScriptParameter()
                {
                    Name         = "IntType",
                    Type         = DataTypeConstants.Int32Type,
                    DefaultValue = null
                })
            },

                PowerShellParseUtilities.GenerateCommonParameters());

            var viewModel = new ParameterEditorViewModel(model);

            model.Parameters[1].Value = "MoreThanOneChar";
            Assert.AreEqual <bool>(false, viewModel.IsSaveButtonEnabled);

            model.Parameters[1].Value = "C";
            Assert.AreEqual <bool>(true, viewModel.IsSaveButtonEnabled);

            model.Parameters[2].Value = (byte)122;
            Assert.AreEqual <bool>(true, viewModel.IsSaveButtonEnabled);

            model.Parameters[2].Value = -1;
            Assert.AreEqual <bool>(false, viewModel.IsSaveButtonEnabled);

            model.Parameters[2].Value = "StringShouldNotWork";
            Assert.AreEqual <bool>(false, viewModel.IsSaveButtonEnabled);
        }
        private static bool?ShowParameterEditor(ParamBlockAst paramBlockAst, out string scriptArgs)
        {
            scriptArgs = String.Empty;
            var model     = PowerShellParseUtilities.ParseParameters(paramBlockAst);
            var viewModel = new ParameterEditorViewModel(model);
            var view      = new ParameterEditorView(viewModel);

            bool?wasOkClicked = view.ShowModal();

            if (wasOkClicked != true)
            {
                return(wasOkClicked);
            }

            scriptArgs = GenerateScripArgsFromModel(model);
            return(wasOkClicked);
        }
        public static ParamBlockAst GetScriptParameters(IVsEditorAdaptersFactoryService adaptersFactory, IVsTextManager textManager)
        {
            IVsTextView   vsTextView;
            ParamBlockAst paramBlock = null;

            //Returns the active or previously active view.
            //
            // Parameters:
            //   fMustHaveFocus:
            //     [in] If true, then the current UI active view is returned. If false, then
            //     the last active view is returned, regardless of whether this view is currently
            //     UI active.
            //
            //   pBuffer:
            //     [in] Pass null for pBuffer to get the previously active code view, regardless
            //     of the text buffer that it was associated with. If you pass in a valid pointer
            //     to a buffer, then you are returned the last active view for that particular
            //     buffer.
            //
            //   ppView:
            //     [out] Pointer to the Microsoft.VisualStudio.TextManager.Interop.IVsTextView
            //     interface.
            textManager.GetActiveView(1, null, out vsTextView);
            if (vsTextView == null)
            {
                return(null);
            }

            IVsTextLines textLines;

            vsTextView.GetBuffer(out textLines);
            ITextBuffer textBuffer = adaptersFactory.GetDataBuffer(textLines as IVsTextBuffer);
            Ast         scriptAst;

            if (!textBuffer.Properties.TryGetProperty <Ast>(BufferProperties.Ast, out scriptAst))
            {
                return(null);
            }

            PowerShellParseUtilities.HasParamBlock(scriptAst, out paramBlock);
            return(paramBlock);
        }
Exemple #6
0
        public void ShouldCollapseParameterSetNamesWhenThereIsNone()
        {
            var model = new ParameterEditorModel(
                new ObservableCollection <ScriptParameterViewModel>()
            {
                new ScriptParameterViewModel(new ScriptParameter()
                {
                    Name         = "StringType",
                    Type         = DataTypeConstants.StringType,
                    DefaultValue = null
                }),
                new ScriptParameterViewModel(new ScriptParameter()
                {
                    Name         = "CharType",
                    Type         = DataTypeConstants.CharType,
                    DefaultValue = null
                }),
                new ScriptParameterViewModel(new ScriptParameter()
                {
                    Name         = "ByteType",
                    Type         = DataTypeConstants.ByteType,
                    DefaultValue = null
                }),
                new ScriptParameterViewModel(new ScriptParameter()
                {
                    Name         = "IntType",
                    Type         = DataTypeConstants.Int32Type,
                    DefaultValue = null
                })
            },

                PowerShellParseUtilities.GenerateCommonParameters());

            var viewModel = new ParameterEditorViewModel(model);

            Assert.AreEqual <string>(null, viewModel.SelectedParameterSetName);
            Assert.AreEqual <IList <string> >(null, viewModel.ParameterSetNames);
            Assert.AreEqual <bool>(false, viewModel.HasParameterSets);
        }
        public void ShouldParseParametersCorrectly()
        {
            ParamBlockAst paramBlock = GenerateParamBlockAst(scriptWithParameters);
            var           model      = PowerShellParseUtilities.ParseParameters(paramBlock);

            var expectedModel = new ParameterEditorModel(
                new ObservableCollection <ScriptParameterViewModel>()
            {
                new ScriptParameterViewModel(new ScriptParameter()
                {
                    Name         = "StringType",
                    Type         = DataTypeConstants.StringType,
                    DefaultValue = null
                }),
                new ScriptParameterViewModel(new ScriptParameter()
                {
                    Name         = "CharType",
                    Type         = DataTypeConstants.CharType,
                    DefaultValue = null
                }),
                new ScriptParameterViewModel(new ScriptParameter()
                {
                    Name         = "ByteType",
                    Type         = DataTypeConstants.ByteType,
                    DefaultValue = null
                }),
                new ScriptParameterViewModel(new ScriptParameter()
                {
                    Name         = "IntType",
                    Type         = DataTypeConstants.Int32Type,
                    DefaultValue = null
                })
            },

                PowerShellParseUtilities.GenerateCommonParameters());

            Assert.AreEqual <bool>(true, CompareParameterEditorModels(expectedModel, model));
        }
Exemple #8
0
        public void ShouldGeneratedCorrectScriptArgsBasedOnPartiallyGivenValues()
        {
            var model = new ParameterEditorModel(
                new ObservableCollection <ScriptParameterViewModel>()
            {
                new ScriptParameterViewModel(new ScriptParameter()
                {
                    Name         = "StringType",
                    Type         = DataTypeConstants.StringType,
                    DefaultValue = null
                }),
                new ScriptParameterViewModel(new ScriptParameter()
                {
                    Name         = "CharType",
                    Type         = DataTypeConstants.CharType,
                    DefaultValue = "C"
                }),
                new ScriptParameterViewModel(new ScriptParameter()
                {
                    Name         = "ByteType",
                    Type         = DataTypeConstants.ByteType,
                    DefaultValue = null
                }),
                new ScriptParameterViewModel(new ScriptParameter()
                {
                    Name         = "IntType",
                    Type         = DataTypeConstants.Int32Type,
                    DefaultValue = 1111
                })
            },

                PowerShellParseUtilities.GenerateCommonParameters());

            string actualScriptArgs   = ParameterEditorHelper.GenerateScripArgsFromModel(model);
            string expectedScriptArgs = " -CharType \"C\" -IntType 1111";

            Assert.AreEqual <string>(expectedScriptArgs, actualScriptArgs);
        }
Exemple #9
0
        public void ShouldGeneratedEmptyScriptArgsWhenNoValueIsGiven()
        {
            var model = new ParameterEditorModel(
                new ObservableCollection <ScriptParameterViewModel>()
            {
                new ScriptParameterViewModel(new ScriptParameter()
                {
                    Name         = "StringType",
                    Type         = DataTypeConstants.StringType,
                    DefaultValue = null
                }),
                new ScriptParameterViewModel(new ScriptParameter()
                {
                    Name         = "CharType",
                    Type         = DataTypeConstants.CharType,
                    DefaultValue = null
                }),
                new ScriptParameterViewModel(new ScriptParameter()
                {
                    Name         = "ByteType",
                    Type         = DataTypeConstants.ByteType,
                    DefaultValue = null
                }),
                new ScriptParameterViewModel(new ScriptParameter()
                {
                    Name         = "IntType",
                    Type         = DataTypeConstants.Int32Type,
                    DefaultValue = null
                })
            },

                PowerShellParseUtilities.GenerateCommonParameters());

            string actualScriptArgs = ParameterEditorHelper.GenerateScripArgsFromModel(model);

            Assert.AreEqual <string>(String.Empty, actualScriptArgs);
        }
Exemple #10
0
        public void ShouldDetectMantoryParametersNotSet()
        {
            var model = new ParameterEditorModel(
                new ObservableCollection <ScriptParameterViewModel>()
            {
                new ScriptParameterViewModel(new ScriptParameter()
                {
                    Name         = "StringType",
                    Type         = DataTypeConstants.StringType,
                    DefaultValue = null,
                    IsMandatory  = true
                }),
                new ScriptParameterViewModel(new ScriptParameter()
                {
                    Name         = "CharType",
                    Type         = DataTypeConstants.CharType,
                    DefaultValue = null,
                    IsMandatory  = true
                }),
                new ScriptParameterViewModel(new ScriptParameter()
                {
                    Name         = "ByteType",
                    Type         = DataTypeConstants.ByteType,
                    DefaultValue = null,
                    IsMandatory  = true
                }),
                new ScriptParameterViewModel(new ScriptParameter()
                {
                    Name         = "IntType",
                    Type         = DataTypeConstants.Int32Type,
                    DefaultValue = null,
                    IsMandatory  = true
                }),
                new ScriptParameterViewModel(new ScriptParameter()
                {
                    Name         = "BoolType",
                    Type         = DataTypeConstants.BoolType,
                    DefaultValue = null,
                    IsMandatory  = true
                })
            },

                PowerShellParseUtilities.GenerateCommonParameters());

            var viewModel = new ParameterEditorViewModel(model);

            model.Parameters[0].Value = null;
            Assert.AreEqual <bool>(true, viewModel.Parameters[0].HasValidationResult);
            Assert.AreEqual <bool>(true, viewModel.Parameters[0].ValidationResult.IsWarning);

            model.Parameters[0].Value = string.Empty;
            Assert.AreEqual <bool>(true, viewModel.Parameters[1].HasValidationResult);
            Assert.AreEqual <bool>(true, viewModel.Parameters[1].ValidationResult.IsWarning);

            model.Parameters[1].Value = null;
            Assert.AreEqual <bool>(true, viewModel.Parameters[1].HasValidationResult);
            Assert.AreEqual <bool>(true, viewModel.Parameters[1].ValidationResult.IsWarning);

            model.Parameters[2].Value = null;
            Assert.AreEqual <bool>(true, viewModel.Parameters[2].HasValidationResult);
            Assert.AreEqual <bool>(true, viewModel.Parameters[2].ValidationResult.IsWarning);

            model.Parameters[3].Value = null;
            Assert.AreEqual <bool>(true, viewModel.Parameters[3].HasValidationResult);
            Assert.AreEqual <bool>(true, viewModel.Parameters[3].ValidationResult.IsWarning);

            model.Parameters[4].Value = null;
            Assert.AreEqual <bool>(true, viewModel.Parameters[4].HasValidationResult);
            Assert.AreEqual <bool>(true, viewModel.Parameters[4].ValidationResult.IsWarning);
        }
Exemple #11
0
        public void ShouldUpdateParametersWithParameterSetChanged()
        {
            var model = new ParameterEditorModel(
                new ObservableCollection <ScriptParameterViewModel>()
            {
                new ScriptParameterViewModel(new ScriptParameter()
                {
                    Name         = "StringType",
                    Type         = DataTypeConstants.StringType,
                    DefaultValue = null
                }),
                new ScriptParameterViewModel(new ScriptParameter()
                {
                    Name         = "CharType",
                    Type         = DataTypeConstants.CharType,
                    DefaultValue = "c"
                })
            },

                PowerShellParseUtilities.GenerateCommonParameters(),

                new Dictionary <string, IList <ScriptParameterViewModel> >()
            {
                { "Set1", new List <ScriptParameterViewModel>()
                  {
                      new ScriptParameterViewModel(new ScriptParameter()
                        {
                            Name             = "LongType",
                            Type             = DataTypeConstants.Int64Type,
                            DefaultValue     = 100000,
                            ParameterSetName = "Set1"
                        }),
                      new ScriptParameterViewModel(new ScriptParameter()
                        {
                            Name             = "BoolType",
                            Type             = DataTypeConstants.BoolType,
                            DefaultValue     = null,
                            ParameterSetName = "Set1"
                        })
                  } },

                { "Set2", new List <ScriptParameterViewModel>()
                  {
                      new ScriptParameterViewModel(new ScriptParameter()
                        {
                            Name             = "SwitchType",
                            Type             = DataTypeConstants.SwitchType,
                            ParameterSetName = "Set2",
                        }),
                      new ScriptParameterViewModel(new ScriptParameter()
                        {
                            Name             = "DecimalType",
                            Type             = DataTypeConstants.DecimalType,
                            ParameterSetName = "Set2"
                        })
                  } }
            },

                new List <string>()
            {
                "Set1", "Set2"
            },

                "Set1");

            var viewModel = new ParameterEditorViewModel(model);

            viewModel.SelectedParameterSetName = "Set2";

            var expectedParameters = new ObservableCollection <ScriptParameterViewModel>()
            {
                new ScriptParameterViewModel(new ScriptParameter()
                {
                    Name         = "StringType",
                    Type         = DataTypeConstants.StringType,
                    DefaultValue = null
                }),
                new ScriptParameterViewModel(new ScriptParameter()
                {
                    Name         = "CharType",
                    Type         = DataTypeConstants.CharType,
                    DefaultValue = "c"
                }),
                new ScriptParameterViewModel(new ScriptParameter()
                {
                    Name             = "SwitchType",
                    Type             = DataTypeConstants.SwitchType,
                    ParameterSetName = "Set2",
                }),
                new ScriptParameterViewModel(new ScriptParameter()
                {
                    Name             = "DecimalType",
                    Type             = DataTypeConstants.DecimalType,
                    ParameterSetName = "Set2"
                })
            };

            var  equalityComparer = new ScriptParameterViewModelEqualityComparer();
            bool equal            = Enumerable.SequenceEqual <ScriptParameterViewModel>(expectedParameters, model.Parameters, equalityComparer);

            // Parameters should inclulde the parameters within default Set.
            Assert.AreEqual <bool>(true, equal);
        }
        public void ShouldParseParameterSetNamesCorrectly()
        {
            ParamBlockAst paramBlock = GenerateParamBlockAst(scriptWithParameterSets);
            var           model      = PowerShellParseUtilities.ParseParameters(paramBlock);

            var expectedModel = new ParameterEditorModel(
                new ObservableCollection <ScriptParameterViewModel>()
            {
                new ScriptParameterViewModel(new ScriptParameter()
                {
                    Name         = "StringType",
                    Type         = DataTypeConstants.StringType,
                    DefaultValue = null
                }),
                new ScriptParameterViewModel(new ScriptParameter()
                {
                    Name         = "CharType",
                    Type         = DataTypeConstants.CharType,
                    DefaultValue = "c"
                })
            },

                PowerShellParseUtilities.GenerateCommonParameters(),

                new Dictionary <string, IList <ScriptParameterViewModel> >()
            {
                { "Set1", new List <ScriptParameterViewModel>()
                  {
                      new ScriptParameterViewModel(new ScriptParameter()
                        {
                            Name             = "LongType",
                            Type             = DataTypeConstants.Int64Type,
                            DefaultValue     = 100000,
                            ParameterSetName = "Set1"
                        }),
                      new ScriptParameterViewModel(new ScriptParameter()
                        {
                            Name             = "BoolType",
                            Type             = DataTypeConstants.BoolType,
                            DefaultValue     = null,
                            ParameterSetName = "Set1"
                        })
                  } },

                { "Set2", new List <ScriptParameterViewModel>()
                  {
                      new ScriptParameterViewModel(new ScriptParameter()
                        {
                            Name             = "SwitchType",
                            Type             = DataTypeConstants.SwitchType,
                            ParameterSetName = "Set2",
                        }),
                      new ScriptParameterViewModel(new ScriptParameter()
                        {
                            Name             = "DecimalType",
                            Type             = DataTypeConstants.DecimalType,
                            ParameterSetName = "Set2"
                        })
                  } }
            },

                new List <string>()
            {
                "Set1", "Set2"
            },

                "Set1");

            Assert.AreEqual <bool>(true, CompareParameterEditorModels(expectedModel, model));
        }