Ejemplo n.º 1
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);
        }
        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);
        }