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); }
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)); }
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); }
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); }
/// <summary> /// Try to parse a Param block and form them into a model containing parameters related data. /// </summary> /// <param name="paramBlockAst">The targeting Param block.</param> /// <returns>A model containing parameters related data.</returns> public static ParameterEditorModel ParseParameters(ParamBlockAst paramBlockAst) { ObservableCollection <ScriptParameterViewModel> scriptParameters = new ObservableCollection <ScriptParameterViewModel>(); IDictionary <string, IList <ScriptParameterViewModel> > parameterSetToParametersDict = new Dictionary <string, IList <ScriptParameterViewModel> >(); IList <string> parameterSetNames = new List <string>(); if (paramBlockAst != null) { // First, filter all parameter types not supported yet. var parametersList = paramBlockAst.Parameters. Where(p => !DataTypeConstants.UnsupportedDataTypes.Contains(p.StaticType.FullName)). ToList(); foreach (var p in parametersList) { HashSet <object> allowedValues = new HashSet <object>(); bool isParameterSetDefined = false; string parameterSetName = null; bool isMandatory = false; foreach (var a in p.Attributes) { // Find if there defines attribute ValidateSet if (a.TypeName.FullName.Equals(ValidateSetTypeName, StringComparison.OrdinalIgnoreCase)) { foreach (StringConstantExpressionAst pa in ((AttributeAst)a).PositionalArguments) { allowedValues.Add(pa.Value); } } // Find if there defines attribute ParameterNameSet // Find if there defines attribute Mandatory if (a is AttributeAst) { foreach (var arg in ((AttributeAst)a).NamedArguments) { if (!isParameterSetDefined) { isParameterSetDefined = arg.ArgumentName.Equals(ParameterSetArgumentName, StringComparison.OrdinalIgnoreCase); if (isParameterSetDefined) { parameterSetName = ((StringConstantExpressionAst)arg.Argument).Value; } } if (!isMandatory) { bool isMandatoryDefined = arg.ArgumentName.Equals(MandatoryArgumentName, StringComparison.OrdinalIgnoreCase); if (isMandatoryDefined) { if (arg.Argument is VariableExpressionAst) { isMandatory = ((VariableExpressionAst)arg.Argument).VariablePath.UserPath.Equals("true", StringComparison.OrdinalIgnoreCase); } if (arg.Argument is ConstantExpressionAst) { isMandatory = Convert.ToBoolean(((ConstantExpressionAst)arg.Argument).Value); } } } } } } // Get parameter type string type = p.StaticType.FullName; if (type.EndsWith(DataTypeConstants.ArrayType, StringComparison.OrdinalIgnoreCase)) { type = DataTypeConstants.ArrayType; } // Get parameter name string name = p.Name.VariablePath.UserPath; // Get paramter default value, null it is if there is none specified object defaultValue = null; if (p.DefaultValue != null) { if (p.DefaultValue is ConstantExpressionAst) { defaultValue = ((ConstantExpressionAst)p.DefaultValue).Value; } else if (p.DefaultValue is VariableExpressionAst) { defaultValue = ((VariableExpressionAst)p.DefaultValue).VariablePath.UserPath; } } // Construct a ScriptParameterViewModel based on whether a parameter set is found ScriptParameterViewModel newViewModel = null; if (isParameterSetDefined && parameterSetName != null) { newViewModel = new ScriptParameterViewModel(new ScriptParameter(name, type, defaultValue, allowedValues, parameterSetName, isMandatory)); IList <ScriptParameterViewModel> existingSets; if (parameterSetToParametersDict.TryGetValue(parameterSetName, out existingSets)) { existingSets.Add(newViewModel); } else { parameterSetNames.Add(parameterSetName); parameterSetToParametersDict.Add(parameterSetName, new List <ScriptParameterViewModel>() { newViewModel }); } } else { newViewModel = new ScriptParameterViewModel(new ScriptParameter(name, type, defaultValue, allowedValues, isMandatory)); scriptParameters.Add(newViewModel); } } } // Construct the actual model ParameterEditorModel model = new ParameterEditorModel(scriptParameters, GenerateCommonParameters(), parameterSetToParametersDict.Count > 0 ? parameterSetToParametersDict : null, parameterSetNames.Count > 0 ? parameterSetNames : null, parameterSetNames.FirstOrDefault()); return(model); }
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); }
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)); }
private bool CompareParameterEditorModels(ParameterEditorModel model1, ParameterEditorModel model2) { if (model1 == model2) { return(true); } if ((model1 == null && model2 != null) || (model1 != null && model2 == null) || (model1.ParameterSetToParametersDict == null && model2.ParameterSetToParametersDict != null) || (model1.ParameterSetToParametersDict != null && model2.ParameterSetToParametersDict == null)) { return(false); } if (model1.Parameters.Count != model2.Parameters.Count || model1.CommonParameters.Count != model2.CommonParameters.Count || (model1.ParameterSetToParametersDict != null && model2.ParameterSetToParametersDict != null && model1.ParameterSetToParametersDict.Count != model2.ParameterSetToParametersDict.Count)) { return(false); } // Compare Parameters and CommonParameters int parametersCount = model1.Parameters.Count; var equalityComparer = new ScriptParameterViewModelEqualityComparer(); bool equal = Enumerable.SequenceEqual <ScriptParameterViewModel>(model1.Parameters, model2.Parameters, equalityComparer) && Enumerable.SequenceEqual <ScriptParameterViewModel>(model1.CommonParameters, model2.CommonParameters, equalityComparer); if (!equal) { return(false); } if (model1.SelectedParameterSetName != null) { // Compare SelectedParameterSetName and ParameterSetNames equal = equal && String.Equals(model1.SelectedParameterSetName, model2.SelectedParameterSetName, StringComparison.Ordinal) && Enumerable.SequenceEqual <string>(model1.ParameterSetNames, model2.ParameterSetNames, StringComparer.Ordinal); if (!equal) { return(false); } // Compare ParameterSetToParametersDict foreach (var set in model1.ParameterSetNames) { equal = equal && Enumerable.SequenceEqual <ScriptParameterViewModel>(model1.ParameterSetToParametersDict[set], model2.ParameterSetToParametersDict[set], equalityComparer); if (!equal) { return(false); } } } return(equal); }