diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ConvertFrom-StringData.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ConvertFrom-StringData.cs index 9ad5a1c0c95..cd6716b484b 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ConvertFrom-StringData.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ConvertFrom-StringData.cs @@ -19,7 +19,7 @@ public sealed class ConvertFromStringDataCommand : PSCmdlet /// /// The list of properties to display - /// These take the form of an MshExpression + /// These take the form of an PSPropertyExpression /// /// [Parameter(Mandatory = true, Position = 0, ValueFromPipeline = true)] diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/FormatAndOutput/OutGridView/ExpressionColumnInfo.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/FormatAndOutput/OutGridView/ExpressionColumnInfo.cs index e56e2b4d2e1..4dc99189e99 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/FormatAndOutput/OutGridView/ExpressionColumnInfo.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/FormatAndOutput/OutGridView/ExpressionColumnInfo.cs @@ -10,9 +10,9 @@ namespace Microsoft.PowerShell.Commands internal class ExpressionColumnInfo : ColumnInfo { - private MshExpression _expression; + private PSPropertyExpression _expression; - internal ExpressionColumnInfo(string staleObjectPropertyName, string displayName, MshExpression expression) + internal ExpressionColumnInfo(string staleObjectPropertyName, string displayName, PSPropertyExpression expression) : base(staleObjectPropertyName, displayName) { _expression = expression; @@ -20,7 +20,7 @@ internal ExpressionColumnInfo(string staleObjectPropertyName, string displayName internal override Object GetValue(PSObject liveObject) { - List resList = _expression.GetValues(liveObject); + List resList = _expression.GetValues(liveObject); if (resList.Count == 0) { @@ -28,7 +28,7 @@ internal override Object GetValue(PSObject liveObject) } // Only first element is used. - MshExpressionResult result = resList[0]; + PSPropertyExpressionResult result = resList[0]; if (result.Exception != null) { return null; @@ -38,4 +38,4 @@ internal override Object GetValue(PSObject liveObject) return objectResult == null ? String.Empty : ColumnInfo.LimitString(objectResult.ToString()); } } -} \ No newline at end of file +} diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/FormatAndOutput/OutGridView/OutGridViewCommand.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/FormatAndOutput/OutGridView/OutGridViewCommand.cs index 1461f226017..9afbf51de09 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/FormatAndOutput/OutGridView/OutGridViewCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/FormatAndOutput/OutGridView/OutGridViewCommand.cs @@ -44,7 +44,7 @@ public class OutGridViewCommand : PSCmdlet, IDisposable private const string DataNotQualifiedForGridView = "DataNotQualifiedForGridView"; private const string RemotingNotSupported = "RemotingNotSupported"; private TypeInfoDataBase _typeInfoDataBase; - private MshExpressionFactory _expressionFactory; + private PSPropertyExpressionFactory _expressionFactory; private OutWindowProxy _windowProxy; private GridHeader _gridHeader; @@ -110,7 +110,7 @@ public SwitchParameter PassThru protected override void BeginProcessing() { // Set up the ExpressionFactory - _expressionFactory = new MshExpressionFactory(); + _expressionFactory = new PSPropertyExpressionFactory(); // If the value of the Title parameter is valid, use it as a window's title. if (this.Title != null) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/FormatAndOutput/OutGridView/TableView.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/FormatAndOutput/OutGridView/TableView.cs index f828d1324de..9adc9d44f40 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/FormatAndOutput/OutGridView/TableView.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/FormatAndOutput/OutGridView/TableView.cs @@ -13,11 +13,11 @@ namespace Microsoft.PowerShell.Commands internal class TableView { - private MshExpressionFactory _expressionFactory; + private PSPropertyExpressionFactory _expressionFactory; private TypeInfoDataBase _typeInfoDatabase; private FormatErrorManager _errorManager; - internal void Initialize(MshExpressionFactory expressionFactory, + internal void Initialize(PSPropertyExpressionFactory expressionFactory, TypeInfoDataBase db) { _expressionFactory = expressionFactory; @@ -78,7 +78,7 @@ internal HeaderInfo GenerateHeaderInfo(PSObject input, TableControlBody tableBod } if (fpt.expression.isScriptBlock) { - MshExpression ex = _expressionFactory.CreateFromExpressionToken(fpt.expression); + PSPropertyExpression ex = _expressionFactory.CreateFromExpressionToken(fpt.expression); // Using the displayName as a propertyName for a stale PSObject. const string LastWriteTimePropertyName = "LastWriteTime"; @@ -132,7 +132,7 @@ internal HeaderInfo GenerateHeaderInfo(PSObject input, OutGridViewCommand parent if (PSObjectHelper.ShouldShowComputerNameProperty(input)) { activeAssociationList.Add(new MshResolvedExpressionParameterAssociation(null, - new MshExpression(RemotingConstants.ComputerNameNoteProperty))); + new PSPropertyExpression(RemotingConstants.ComputerNameNoteProperty))); } } else diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Measure-Object.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Measure-Object.cs index 894eda444d0..08c9f5cb0cd 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Measure-Object.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Measure-Object.cs @@ -82,8 +82,7 @@ public GenericMeasureInfo() /// Class output by Measure-Object. /// /// - /// This class is created for fixing "Measure-Object -MAX -MIN should work with ANYTHING that supports CompareTo" - /// bug (Win8:343911). + /// This class is created to make 'Measure-Object -MAX -MIN' work with ANYTHING that supports 'CompareTo'. /// GenericMeasureInfo class is shipped with PowerShell V2. Fixing this bug requires, changing the type of /// Maximum and Minimum properties which would be a breaking change. Hence created a new class to not /// have an appcompat issues with PS V2. @@ -271,7 +270,7 @@ public MeasureObjectCommand() /// [ValidateNotNullOrEmpty] [Parameter(Position = 0)] - public string[] Property { get; set; } = null; + public PSPropertyExpression[] Property { get; set; } = null; #endregion Common parameters in both sets @@ -486,10 +485,9 @@ private void AnalyzeObjectProperties(PSObject inObj) // First iterate over the user-specified list of // properties... - foreach (string p in Property) + foreach (var expression in Property) { - MshExpression expression = new MshExpression(p); - List resolvedNames = expression.ResolveNames(inObj); + List resolvedNames = expression.ResolveNames(inObj); if (resolvedNames == null || resolvedNames.Count == 0) { // Insert a blank entry so we can track @@ -506,7 +504,7 @@ private void AnalyzeObjectProperties(PSObject inObj) // Each property value can potentially refer // to multiple properties via globbing. Iterate over // the actual property names. - foreach (MshExpression resolvedName in resolvedNames) + foreach (PSPropertyExpression resolvedName in resolvedNames) { string propertyName = resolvedName.ToString(); // skip duplicated properties @@ -515,7 +513,7 @@ private void AnalyzeObjectProperties(PSObject inObj) continue; } - List tempExprRes = resolvedName.GetValues(inObj); + List tempExprRes = resolvedName.GetValues(inObj); if (tempExprRes == null || tempExprRes.Count == 0) { // Shouldn't happen - would somehow mean @@ -573,7 +571,7 @@ private void AnalyzeValue(string propertyName, object objValue) AnalyzeNumber(numValue, stat); } - // Win8:343911 Measure-Object -MAX -MIN should work with ANYTHING that supports CompareTo + // Measure-Object -MAX -MIN should work with ANYTHING that supports CompareTo if (_measureMin) { stat.min = Compare(objValue, stat.min, true); diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/OrderObjectBase.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/OrderObjectBase.cs index afed3c1e6fc..a83de507313 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/OrderObjectBase.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/OrderObjectBase.cs @@ -199,7 +199,7 @@ internal List MshParameterList // a string array and allows wildcard. // Yes, the Cmdlet is needed. It's used to get the TerminatingErrorContext, WriteError and WriteDebug. - #region process MshExpression and MshParameter + #region process PSPropertyExpression and MshParameter private static void ProcessExpressionParameter( List inputObjects, @@ -245,7 +245,7 @@ internal void ProcessExpressionParameter( foreach (MshParameter unexpandedParameter in _unexpandedParameterList) { - MshExpression mshExpression = (MshExpression)unexpandedParameter.GetEntry(FormatParameterDefinitionKeys.ExpressionEntryKey); + PSPropertyExpression mshExpression = (PSPropertyExpression)unexpandedParameter.GetEntry(FormatParameterDefinitionKeys.ExpressionEntryKey); if (!mshExpression.HasWildCardCharacters) // this special cases 1) script blocks and 2) wildcard-less strings { _mshParameterList.Add(unexpandedParameter); @@ -274,14 +274,14 @@ private static List ExpandExpressions(List inputObjects, { foreach (MshParameter unexpandedParameter in unexpandedParameterList) { - MshExpression ex = (MshExpression)unexpandedParameter.GetEntry(FormatParameterDefinitionKeys.ExpressionEntryKey); + PSPropertyExpression ex = (PSPropertyExpression)unexpandedParameter.GetEntry(FormatParameterDefinitionKeys.ExpressionEntryKey); if (!ex.HasWildCardCharacters) // this special cases 1) script blocks and 2) wildcard-less strings { expandedParameterList.Add(unexpandedParameter); } else { - SortedDictionary expandedPropertyNames = new SortedDictionary(StringComparer.OrdinalIgnoreCase); + SortedDictionary expandedPropertyNames = new SortedDictionary(StringComparer.OrdinalIgnoreCase); if (inputObjects != null) { foreach (object inputObject in inputObjects) @@ -291,14 +291,14 @@ private static List ExpandExpressions(List inputObjects, continue; } - foreach (MshExpression resolvedName in ex.ResolveNames(PSObject.AsPSObject(inputObject))) + foreach (PSPropertyExpression resolvedName in ex.ResolveNames(PSObject.AsPSObject(inputObject))) { expandedPropertyNames[resolvedName.ToString()] = resolvedName; } } } - foreach (MshExpression expandedExpression in expandedPropertyNames.Values) + foreach (PSPropertyExpression expandedExpression in expandedPropertyNames.Values) { MshParameter expandedParameter = new MshParameter(); expandedParameter.hash = (Hashtable)unexpandedParameter.hash.Clone(); @@ -321,20 +321,20 @@ private static void ExpandExpressions(PSObject inputObject, List U { foreach (MshParameter unexpandedParameter in UnexpandedParametersWithWildCardPattern) { - MshExpression ex = (MshExpression)unexpandedParameter.GetEntry(FormatParameterDefinitionKeys.ExpressionEntryKey); + PSPropertyExpression ex = (PSPropertyExpression)unexpandedParameter.GetEntry(FormatParameterDefinitionKeys.ExpressionEntryKey); - SortedDictionary expandedPropertyNames = new SortedDictionary(StringComparer.OrdinalIgnoreCase); + SortedDictionary expandedPropertyNames = new SortedDictionary(StringComparer.OrdinalIgnoreCase); if (inputObject == null) { continue; } - foreach (MshExpression resolvedName in ex.ResolveNames(PSObject.AsPSObject(inputObject))) + foreach (PSPropertyExpression resolvedName in ex.ResolveNames(PSObject.AsPSObject(inputObject))) { expandedPropertyNames[resolvedName.ToString()] = resolvedName; } - foreach (MshExpression expandedExpression in expandedPropertyNames.Values) + foreach (PSPropertyExpression expandedExpression in expandedPropertyNames.Values) { MshParameter expandedParameter = new MshParameter(); expandedParameter.hash = (Hashtable)unexpandedParameter.hash.Clone(); @@ -364,7 +364,7 @@ internal static string[] GetDefaultKeyPropertySet(PSObject mshObj) return props; } - #endregion process MshExpression and MshParameter + #endregion process PSPropertyExpression and MshParameter internal static List CreateOrderMatrix( PSCmdlet cmdlet, @@ -569,10 +569,10 @@ private static void EvaluateSortingExpression( ref bool comparable) { // NOTE: we assume globbing was not allowed in input - MshExpression ex = p.GetEntry(FormatParameterDefinitionKeys.ExpressionEntryKey) as MshExpression; + PSPropertyExpression ex = p.GetEntry(FormatParameterDefinitionKeys.ExpressionEntryKey) as PSPropertyExpression; // get the values, but do not expand aliases - List expressionResults = ex.GetValues(inputObject, false, true); + List expressionResults = ex.GetValues(inputObject, false, true); if (expressionResults.Count == 0) { @@ -584,7 +584,7 @@ private static void EvaluateSortingExpression( } propertyNotFoundMsg = null; // we obtained some results, enter them into the list - foreach (MshExpressionResult r in expressionResults) + foreach (PSPropertyExpressionResult r in expressionResults) { if (r.Exception == null) { diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/convert-HTML.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/convert-HTML.cs index b8e167e0c1d..ddbbe6c17f4 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/convert-HTML.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/convert-HTML.cs @@ -43,7 +43,7 @@ public PSObject InputObject /// /// The list of properties to display - /// These take the form of an MshExpression + /// These take the form of a PSPropertyExpression /// /// [Parameter(Position = 0)] @@ -342,9 +342,9 @@ private void InitializeResolvedNameMshParameters() string label = p.GetEntry(ConvertHTMLParameterDefinitionKeys.LabelEntryKey) as string; string alignment = p.GetEntry(ConvertHTMLParameterDefinitionKeys.AlignmentEntryKey) as string; string width = p.GetEntry(ConvertHTMLParameterDefinitionKeys.WidthEntryKey) as string; - MshExpression ex = p.GetEntry(FormatParameterDefinitionKeys.ExpressionEntryKey) as MshExpression; - List resolvedNames = ex.ResolveNames(_inputObject); - foreach (MshExpression resolvedName in resolvedNames) + PSPropertyExpression ex = p.GetEntry(FormatParameterDefinitionKeys.ExpressionEntryKey) as PSPropertyExpression; + List resolvedNames = ex.ResolveNames(_inputObject); + foreach (PSPropertyExpression resolvedName in resolvedNames) { Hashtable ht = CreateAuxPropertyHT(label, alignment, width); ht.Add(FormatParameterDefinitionKeys.ExpressionEntryKey, resolvedName.ToString()); @@ -567,7 +567,7 @@ private void WritePropertyName(StringBuilder Listtag, MshParameter p) } else { - MshExpression ex = p.GetEntry(FormatParameterDefinitionKeys.ExpressionEntryKey) as MshExpression; + PSPropertyExpression ex = p.GetEntry(FormatParameterDefinitionKeys.ExpressionEntryKey) as PSPropertyExpression; Listtag.Append(ex.ToString()); } } @@ -577,11 +577,11 @@ private void WritePropertyName(StringBuilder Listtag, MshParameter p) /// private void WritePropertyValue(StringBuilder Listtag, MshParameter p) { - MshExpression exValue = p.GetEntry(FormatParameterDefinitionKeys.ExpressionEntryKey) as MshExpression; + PSPropertyExpression exValue = p.GetEntry(FormatParameterDefinitionKeys.ExpressionEntryKey) as PSPropertyExpression; // get the value of the property - List resultList = exValue.GetValues(_inputObject); - foreach (MshExpressionResult result in resultList) + List resultList = exValue.GetValues(_inputObject); + foreach (PSPropertyExpressionResult result in resultList) { // create comma sep list for multiple results if (result.Result != null) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/select-object.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/select-object.cs index c11fd593cfa..eeab0052255 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/select-object.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/select-object.cs @@ -12,15 +12,15 @@ namespace Microsoft.PowerShell.Commands { /// - /// helper class to do wildcard matching on MshExpressions + /// helper class to do wildcard matching on PSPropertyExpressions /// - internal sealed class MshExpressionFilter + internal sealed class PSPropertyExpressionFilter { /// /// construct the class, using an array of patterns /// /// array of pattern strings to use - internal MshExpressionFilter(string[] wildcardPatternsStrings) + internal PSPropertyExpressionFilter(string[] wildcardPatternsStrings) { if (wildcardPatternsStrings == null) { @@ -38,9 +38,9 @@ internal MshExpressionFilter(string[] wildcardPatternsStrings) /// try to match the expression against the array of wildcard patterns. /// the first match shortcircuits the search /// - /// MshExpression to test against + /// PSPropertyExpression to test against /// true if there is a match, else false - internal bool IsMatch(MshExpression expression) + internal bool IsMatch(PSPropertyExpression expression) { for (int k = 0; k < _wildcardPatterns.Length; k++) { @@ -290,7 +290,7 @@ public PSObject StreamingDequeue() /// private List _expandMshParameterList; - private MshExpressionFilter _exclusionFilter; + private PSPropertyExpressionFilter _exclusionFilter; private class UniquePSObjectHelper { @@ -328,7 +328,7 @@ private void ProcessExpressionParameter() if (ExcludeProperty != null) { - _exclusionFilter = new MshExpressionFilter(ExcludeProperty); + _exclusionFilter = new PSPropertyExpressionFilter(ExcludeProperty); // ExcludeProperty implies -Property * for better UX if ((Property == null) || (Property.Length == 0)) { @@ -394,15 +394,15 @@ private void ProcessParameter(MshParameter p, PSObject inputObject, List expressionResults = new List(); - foreach (MshExpression resolvedName in ex.ResolveNames(inputObject)) + PSPropertyExpression ex = p.GetEntry(FormatParameterDefinitionKeys.ExpressionEntryKey) as PSPropertyExpression; + List expressionResults = new List(); + foreach (PSPropertyExpression resolvedName in ex.ResolveNames(inputObject)) { if (_exclusionFilter == null || !_exclusionFilter.IsMatch(resolvedName)) { - List tempExprResults = resolvedName.GetValues(inputObject); + List tempExprResults = resolvedName.GetValues(inputObject); if (tempExprResults == null) continue; - foreach (MshExpressionResult mshExpRes in tempExprResults) + foreach (PSPropertyExpressionResult mshExpRes in tempExprResults) { expressionResults.Add(mshExpRes); } @@ -413,7 +413,7 @@ private void ProcessParameter(MshParameter p, PSObject inputObject, List matchedProperties) { - MshExpression ex = p.GetEntry(FormatParameterDefinitionKeys.ExpressionEntryKey) as MshExpression; - List expressionResults = ex.GetValues(inputObject); + PSPropertyExpression ex = p.GetEntry(FormatParameterDefinitionKeys.ExpressionEntryKey) as PSPropertyExpression; + List expressionResults = ex.GetValues(inputObject); if (expressionResults.Count == 0) { @@ -484,7 +484,7 @@ private void ProcessExpandParameter(MshParameter p, PSObject inputObject, throw new SelectObjectException(errorRecord); } - MshExpressionResult r = expressionResults[0]; + PSPropertyExpressionResult r = expressionResults[0]; if (r.Exception == null) { // ignore the property value if it's null diff --git a/src/System.Management.Automation/FormatAndOutput/common/BaseFormattingCommand.cs b/src/System.Management.Automation/FormatAndOutput/common/BaseFormattingCommand.cs index 136a91cb59c..5eed701982b 100644 --- a/src/System.Management.Automation/FormatAndOutput/common/BaseFormattingCommand.cs +++ b/src/System.Management.Automation/FormatAndOutput/common/BaseFormattingCommand.cs @@ -82,7 +82,7 @@ internal override void BeginProcessing() // Get the Format Enumeration Limit. _enumerationLimit = InnerFormatShapeCommand.FormatEnumerationLimit(); - _expressionFactory = new MshExpressionFactory(); + _expressionFactory = new PSPropertyExpressionFactory(); _formatObjectDeserializer = new FormatObjectDeserializer(this.TerminatingErrorContext); } @@ -500,7 +500,7 @@ internal ScriptBlock CreateScriptBlock(string scriptText) return scriptBlock; } - private MshExpressionFactory _expressionFactory; + private PSPropertyExpressionFactory _expressionFactory; #endregion private FormatObjectDeserializer _formatObjectDeserializer; diff --git a/src/System.Management.Automation/FormatAndOutput/common/BaseFormattingCommandParameters.cs b/src/System.Management.Automation/FormatAndOutput/common/BaseFormattingCommandParameters.cs index 34d71e3199d..9d34035cea7 100644 --- a/src/System.Management.Automation/FormatAndOutput/common/BaseFormattingCommandParameters.cs +++ b/src/System.Management.Automation/FormatAndOutput/common/BaseFormattingCommandParameters.cs @@ -173,7 +173,7 @@ internal override object Verify(object val, ScriptBlock sb = val as ScriptBlock; if (sb != null) { - MshExpression ex = new MshExpression(sb); + PSPropertyExpression ex = new PSPropertyExpression(sb); return ex; } @@ -184,7 +184,7 @@ internal override object Verify(object val, { ProcessEmptyStringError(originalParameterWasHashTable, invocationContext); } - MshExpression ex = new MshExpression(s); + PSPropertyExpression ex = new PSPropertyExpression(s); if (_noGlobbing) { if (ex.HasWildCardCharacters) diff --git a/src/System.Management.Automation/FormatAndOutput/common/DisplayDatabase/XmlLoaderBase.cs b/src/System.Management.Automation/FormatAndOutput/common/DisplayDatabase/XmlLoaderBase.cs index a27319cd65f..b13076fb1a0 100644 --- a/src/System.Management.Automation/FormatAndOutput/common/DisplayDatabase/XmlLoaderBase.cs +++ b/src/System.Management.Automation/FormatAndOutput/common/DisplayDatabase/XmlLoaderBase.cs @@ -702,7 +702,7 @@ protected DatabaseLoadingInfo LoadingInfo } } - protected MshExpressionFactory expressionFactory; + protected PSPropertyExpressionFactory expressionFactory; protected DisplayResourceManagerCache displayResourceManagerCache; diff --git a/src/System.Management.Automation/FormatAndOutput/common/DisplayDatabase/displayDescriptionData.cs b/src/System.Management.Automation/FormatAndOutput/common/DisplayDatabase/displayDescriptionData.cs index 49feabe131b..fb0672ea2f1 100644 --- a/src/System.Management.Automation/FormatAndOutput/common/DisplayDatabase/displayDescriptionData.cs +++ b/src/System.Management.Automation/FormatAndOutput/common/DisplayDatabase/displayDescriptionData.cs @@ -170,7 +170,7 @@ internal bool ShowErrorsInFormattedOutput /// /// string to display in the formatted display (e.g. cell in a table) - /// when the evaluation of an MshExpression fails + /// when the evaluation of a PSPropertyExpression fails /// internal string errorStringInFormattedOutput = "#ERR"; diff --git a/src/System.Management.Automation/FormatAndOutput/common/DisplayDatabase/typeDataManager.cs b/src/System.Management.Automation/FormatAndOutput/common/DisplayDatabase/typeDataManager.cs index 1bc4f8bbb0a..39e597b6dae 100644 --- a/src/System.Management.Automation/FormatAndOutput/common/DisplayDatabase/typeDataManager.cs +++ b/src/System.Management.Automation/FormatAndOutput/common/DisplayDatabase/typeDataManager.cs @@ -89,7 +89,7 @@ internal TypeInfoDataBaseManager( _formatFileList.Add(formatFile); } - MshExpressionFactory expressionFactory = new MshExpressionFactory(); + PSPropertyExpressionFactory expressionFactory = new PSPropertyExpressionFactory(); List logEntries = null; // load the files @@ -207,7 +207,7 @@ internal void AddFormatData(IEnumerable formatData, bool } } - MshExpressionFactory expressionFactory = new MshExpressionFactory(); + PSPropertyExpressionFactory expressionFactory = new PSPropertyExpressionFactory(); List logEntries = null; // load the formatting data @@ -290,7 +290,7 @@ bool preValidated throw PSTraceSource.NewInvalidOperationException(FormatAndOutXmlLoadingStrings.SharedFormatTableCannotBeUpdated); } - MshExpressionFactory expressionFactory = new MshExpressionFactory(); + PSPropertyExpressionFactory expressionFactory = new PSPropertyExpressionFactory(); List logEntries = null; LoadFromFile(mshsnapins, expressionFactory, false, authorizationManager, host, preValidated, out logEntries); } @@ -316,7 +316,7 @@ bool preValidated /// true if we had a successful load internal bool LoadFromFile( Collection files, - MshExpressionFactory expressionFactory, + PSPropertyExpressionFactory expressionFactory, bool acceptLoadingErrors, AuthorizationManager authorizationManager, PSHost host, @@ -377,7 +377,7 @@ internal bool LoadFromFile( /// a database instance loaded from file(s) private static TypeInfoDataBase LoadFromFileHelper( Collection files, - MshExpressionFactory expressionFactory, + PSPropertyExpressionFactory expressionFactory, AuthorizationManager authorizationManager, PSHost host, bool preValidated, @@ -445,7 +445,7 @@ private static TypeInfoDataBase LoadFromFileHelper( private static void LoadFormatDataHelper( ExtendedTypeDefinition formatData, - MshExpressionFactory expressionFactory, List logEntries, ref bool success, + PSPropertyExpressionFactory expressionFactory, List logEntries, ref bool success, PSSnapInTypeAndFormatErrors file, TypeInfoDataBase db, bool isBuiltInFormatData, bool isForHelp) @@ -481,7 +481,7 @@ private static Tuple GetBuiltin(bool isForHelp, TypeGenerat private static bool ProcessBuiltin( PSSnapInTypeAndFormatErrors file, TypeInfoDataBase db, - MshExpressionFactory expressionFactory, + PSPropertyExpressionFactory expressionFactory, List logEntries, ref bool success) { @@ -517,7 +517,7 @@ private static bool ProcessBuiltin( private static void ProcessBuiltinFormatViewDefinitions( IEnumerable views, TypeInfoDataBase db, - MshExpressionFactory expressionFactory, + PSPropertyExpressionFactory expressionFactory, PSSnapInTypeAndFormatErrors file, List logEntries, bool isForHelp, diff --git a/src/System.Management.Automation/FormatAndOutput/common/DisplayDatabase/typeDataQuery.cs b/src/System.Management.Automation/FormatAndOutput/common/DisplayDatabase/typeDataQuery.cs index f5127d028f2..c46069de461 100644 --- a/src/System.Management.Automation/FormatAndOutput/common/DisplayDatabase/typeDataQuery.cs +++ b/src/System.Management.Automation/FormatAndOutput/common/DisplayDatabase/typeDataQuery.cs @@ -13,10 +13,10 @@ namespace Microsoft.PowerShell.Commands.Internal.Format { internal static class DisplayCondition { - internal static bool Evaluate(PSObject obj, MshExpression ex, out MshExpressionResult expressionResult) + internal static bool Evaluate(PSObject obj, PSPropertyExpression ex, out PSPropertyExpressionResult expressionResult) { expressionResult = null; - List res = ex.GetValues(obj); + List res = ex.GetValues(obj); if (res.Count == 0) return false; if (res[0].Exception != null) @@ -84,7 +84,7 @@ internal static void ResetTracer() } #endregion tracer - internal TypeMatch(MshExpressionFactory expressionFactory, TypeInfoDataBase db, Collection typeNames) + internal TypeMatch(PSPropertyExpressionFactory expressionFactory, TypeInfoDataBase db, Collection typeNames) { _expressionFactory = expressionFactory; _db = db; @@ -92,7 +92,7 @@ internal TypeMatch(MshExpressionFactory expressionFactory, TypeInfoDataBase db, _useInheritance = true; } - internal TypeMatch(MshExpressionFactory expressionFactory, TypeInfoDataBase db, Collection typeNames, bool useInheritance) + internal TypeMatch(PSPropertyExpressionFactory expressionFactory, TypeInfoDataBase db, Collection typeNames, bool useInheritance) { _expressionFactory = expressionFactory; _db = db; @@ -130,7 +130,7 @@ private int ComputeBestMatch(AppliesTo appliesTo, PSObject currentObject) int best = BestMatchIndexUndefined; foreach (TypeOrGroupReference r in appliesTo.referenceList) { - MshExpression ex = null; + PSPropertyExpression ex = null; if (r.conditionToken != null) { ex = _expressionFactory.CreateFromExpressionToken(r.conditionToken); @@ -170,7 +170,7 @@ private int ComputeBestMatch(AppliesTo appliesTo, PSObject currentObject) return best; } - private int ComputeBestMatchInGroup(TypeGroupDefinition tgd, PSObject currentObject, MshExpression ex) + private int ComputeBestMatchInGroup(TypeGroupDefinition tgd, PSObject currentObject, PSPropertyExpression ex) { int best = BestMatchIndexUndefined; int k = 0; @@ -189,7 +189,7 @@ private int ComputeBestMatchInGroup(TypeGroupDefinition tgd, PSObject currentObj return best; } - private int MatchTypeIndex(string typeName, PSObject currentObject, MshExpression ex) + private int MatchTypeIndex(string typeName, PSObject currentObject, PSPropertyExpression ex) { if (string.IsNullOrEmpty(typeName)) return BestMatchIndexUndefined; @@ -208,12 +208,12 @@ private int MatchTypeIndex(string typeName, PSObject currentObject, MshExpressio return BestMatchIndexUndefined; } - private bool MatchCondition(PSObject currentObject, MshExpression ex) + private bool MatchCondition(PSObject currentObject, PSPropertyExpression ex) { if (ex == null) return true; - MshExpressionResult expressionResult; + PSPropertyExpressionResult expressionResult; bool retVal = DisplayCondition.Evaluate(currentObject, ex, out expressionResult); if (expressionResult != null && expressionResult.Exception != null) { @@ -222,12 +222,12 @@ private bool MatchCondition(PSObject currentObject, MshExpression ex) return retVal; } - private MshExpressionFactory _expressionFactory; + private PSPropertyExpressionFactory _expressionFactory; private TypeInfoDataBase _db; private Collection _typeNameHierarchy; private bool _useInheritance; - private List _failedResultsList = new List(); + private List _failedResultsList = new List(); private int _bestMatchIndex = BestMatchIndexUndefined; private TypeMatchItem _bestMatchItem; @@ -264,7 +264,7 @@ internal static void ResetTracer() } #endregion tracer - internal static EnumerableExpansion GetEnumerableExpansionFromType(MshExpressionFactory expressionFactory, TypeInfoDataBase db, Collection typeNames) + internal static EnumerableExpansion GetEnumerableExpansionFromType(PSPropertyExpressionFactory expressionFactory, TypeInfoDataBase db, Collection typeNames) { TypeMatch match = new TypeMatch(expressionFactory, db, typeNames); foreach (EnumerableExpansionDirective expansionDirective in db.defaultSettingsSection.enumerableExpansionDirectiveList) @@ -292,7 +292,7 @@ internal static EnumerableExpansion GetEnumerableExpansionFromType(MshExpression } } - internal static FormatShape GetShapeFromType(MshExpressionFactory expressionFactory, TypeInfoDataBase db, Collection typeNames) + internal static FormatShape GetShapeFromType(PSPropertyExpressionFactory expressionFactory, TypeInfoDataBase db, Collection typeNames) { ShapeSelectionDirectives shapeDirectives = db.defaultSettingsSection.shapeSelectionDirectives; @@ -330,7 +330,7 @@ internal static FormatShape GetShapeFromPropertyCount(TypeInfoDataBase db, int p return FormatShape.List; } - internal static ViewDefinition GetViewByShapeAndType(MshExpressionFactory expressionFactory, TypeInfoDataBase db, + internal static ViewDefinition GetViewByShapeAndType(PSPropertyExpressionFactory expressionFactory, TypeInfoDataBase db, FormatShape shape, Collection typeNames, string viewName) { if (shape == FormatShape.Undefined) @@ -363,7 +363,7 @@ internal static ViewDefinition GetViewByShapeAndType(MshExpressionFactory expres return GetView(expressionFactory, db, t, typeNames, viewName); } - internal static ViewDefinition GetOutOfBandView(MshExpressionFactory expressionFactory, + internal static ViewDefinition GetOutOfBandView(PSPropertyExpressionFactory expressionFactory, TypeInfoDataBase db, Collection typeNames) { TypeMatch match = new TypeMatch(expressionFactory, db, typeNames); @@ -394,7 +394,7 @@ internal static ViewDefinition GetOutOfBandView(MshExpressionFactory expressionF return result; } - private static ViewDefinition GetView(MshExpressionFactory expressionFactory, TypeInfoDataBase db, System.Type mainControlType, Collection typeNames, string viewName) + private static ViewDefinition GetView(PSPropertyExpressionFactory expressionFactory, TypeInfoDataBase db, System.Type mainControlType, Collection typeNames, string viewName) { TypeMatch match = new TypeMatch(expressionFactory, db, typeNames); foreach (ViewDefinition vd in db.viewDefinitionsSection.viewDefinitionList) @@ -501,7 +501,7 @@ private static ViewDefinition GetBestMatch(TypeMatch match) return bestMatchedVD; } - private static ViewDefinition GetDefaultView(MshExpressionFactory expressionFactory, TypeInfoDataBase db, Collection typeNames) + private static ViewDefinition GetDefaultView(PSPropertyExpressionFactory expressionFactory, TypeInfoDataBase db, Collection typeNames) { TypeMatch match = new TypeMatch(expressionFactory, db, typeNames); diff --git a/src/System.Management.Automation/FormatAndOutput/common/DisplayDatabase/typeDataXmlLoader.cs b/src/System.Management.Automation/FormatAndOutput/common/DisplayDatabase/typeDataXmlLoader.cs index 2a7a2bccbf1..736bfa1bf2e 100644 --- a/src/System.Management.Automation/FormatAndOutput/common/DisplayDatabase/typeDataXmlLoader.cs +++ b/src/System.Management.Automation/FormatAndOutput/common/DisplayDatabase/typeDataXmlLoader.cs @@ -185,7 +185,7 @@ private static class XMLStringValues internal bool LoadXmlFile( XmlFileLoadInfo info, TypeInfoDataBase db, - MshExpressionFactory expressionFactory, + PSPropertyExpressionFactory expressionFactory, AuthorizationManager authorizationManager, PSHost host, bool preValidated) @@ -279,7 +279,7 @@ internal bool LoadXmlFile( internal bool LoadFormattingData( ExtendedTypeDefinition typeDefinition, TypeInfoDataBase db, - MshExpressionFactory expressionFactory, + PSPropertyExpressionFactory expressionFactory, bool isBuiltInFormatData, bool isForHelp) { diff --git a/src/System.Management.Automation/FormatAndOutput/common/DisplayDatabase/typeDataXmlLoader_List.cs b/src/System.Management.Automation/FormatAndOutput/common/DisplayDatabase/typeDataXmlLoader_List.cs index c303ba16d60..61c9fc9672d 100644 --- a/src/System.Management.Automation/FormatAndOutput/common/DisplayDatabase/typeDataXmlLoader_List.cs +++ b/src/System.Management.Automation/FormatAndOutput/common/DisplayDatabase/typeDataXmlLoader_List.cs @@ -258,7 +258,7 @@ private ListControlItemDefinition LoadListControlItemDefinition(XmlNode property // add condition lvid.conditionToken = condition; - // add either the text token or the MshExpression with optional format string + // add either the text token or the PSPropertyExpression with optional format string if (match.TextToken != null) { lvid.formatTokenList.Add(match.TextToken); diff --git a/src/System.Management.Automation/FormatAndOutput/common/DisplayDatabase/typeDataXmlLoader_Table.cs b/src/System.Management.Automation/FormatAndOutput/common/DisplayDatabase/typeDataXmlLoader_Table.cs index a582dfbfba9..2fab1b82175 100644 --- a/src/System.Management.Automation/FormatAndOutput/common/DisplayDatabase/typeDataXmlLoader_Table.cs +++ b/src/System.Management.Automation/FormatAndOutput/common/DisplayDatabase/typeDataXmlLoader_Table.cs @@ -469,7 +469,7 @@ private TableRowItemDefinition LoadColumnEntry(XmlNode columnEntryNode, int inde } // finally build the item to return - // add either the text token or the MshExpression with optional format string + // add either the text token or the PSPropertyExpression with optional format string if (match.TextToken != null) { rid.formatTokenList.Add(match.TextToken); diff --git a/src/System.Management.Automation/FormatAndOutput/common/DisplayDatabase/typeDataXmlLoader_Wide.cs b/src/System.Management.Automation/FormatAndOutput/common/DisplayDatabase/typeDataXmlLoader_Wide.cs index 9151bfb65e2..116353e2307 100644 --- a/src/System.Management.Automation/FormatAndOutput/common/DisplayDatabase/typeDataXmlLoader_Wide.cs +++ b/src/System.Management.Automation/FormatAndOutput/common/DisplayDatabase/typeDataXmlLoader_Wide.cs @@ -221,7 +221,7 @@ private List LoadPropertyEntry(XmlNode propertyEntryNode) // finally build the item to return List formatTokenList = new List(); - // add either the text token or the MshExpression with optional format string + // add either the text token or the PSPropertyExpression with optional format string if (match.TextToken != null) { formatTokenList.Add(match.TextToken); diff --git a/src/System.Management.Automation/FormatAndOutput/common/FormatGroupManager.cs b/src/System.Management.Automation/FormatAndOutput/common/FormatGroupManager.cs index 6ba01682f1d..e4cf1d7f70f 100644 --- a/src/System.Management.Automation/FormatAndOutput/common/FormatGroupManager.cs +++ b/src/System.Management.Automation/FormatAndOutput/common/FormatGroupManager.cs @@ -20,7 +20,7 @@ internal sealed class GroupingInfoManager /// /// name of the grouping property /// display name of the property - internal void Initialize(MshExpression groupingExpression, string displayLabel) + internal void Initialize(PSPropertyExpression groupingExpression, string displayLabel) { _groupingKeyExpression = groupingExpression; _label = displayLabel; @@ -51,7 +51,7 @@ internal bool UpdateGroupingKeyValue(PSObject so) if (_groupingKeyExpression == null) return false; - List results = _groupingKeyExpression.GetValues(so); + List results = _groupingKeyExpression.GetValues(so); // if we have more that one match, we have to select the first one if (results.Count > 0 && results[0].Exception == null) @@ -117,7 +117,7 @@ private static bool IsEqual(object first, object second) /// /// name of the current grouping key /// - private MshExpression _groupingKeyExpression = null; + private PSPropertyExpression _groupingKeyExpression = null; /// /// the current value of the grouping key diff --git a/src/System.Management.Automation/FormatAndOutput/common/FormatViewGenerator.cs b/src/System.Management.Automation/FormatAndOutput/common/FormatViewGenerator.cs index 969f468376b..83a3fba3a5f 100644 --- a/src/System.Management.Automation/FormatAndOutput/common/FormatViewGenerator.cs +++ b/src/System.Management.Automation/FormatAndOutput/common/FormatViewGenerator.cs @@ -14,7 +14,7 @@ namespace Microsoft.PowerShell.Commands.Internal.Format internal abstract class ViewGenerator { internal virtual void Initialize(TerminatingErrorContext terminatingErrorContext, - MshExpressionFactory mshExpressionFactory, + PSPropertyExpressionFactory mshExpressionFactory, TypeInfoDataBase db, ViewDefinition view, FormattingCommandLineParameters formatParameters) @@ -35,7 +35,7 @@ internal virtual void Initialize(TerminatingErrorContext terminatingErrorContext } internal virtual void Initialize(TerminatingErrorContext terminatingErrorContext, - MshExpressionFactory mshExpressionFactory, + PSPropertyExpressionFactory mshExpressionFactory, PSObject so, TypeInfoDataBase db, FormattingCommandLineParameters formatParameters) @@ -94,7 +94,7 @@ private void InitializeGroupBy() if (parameters != null && parameters.groupByParameter != null) { // get the expression to use - MshExpression groupingKeyExpression = parameters.groupByParameter.GetEntry(FormatParameterDefinitionKeys.ExpressionEntryKey) as MshExpression; + PSPropertyExpression groupingKeyExpression = parameters.groupByParameter.GetEntry(FormatParameterDefinitionKeys.ExpressionEntryKey) as PSPropertyExpression; // set the label string label = null; @@ -121,7 +121,7 @@ private void InitializeGroupBy() return; } - MshExpression ex = this.expressionFactory.CreateFromExpressionToken(gb.startGroup.expression, this.dataBaseInfo.view.loadingInfo); + PSPropertyExpression ex = this.expressionFactory.CreateFromExpressionToken(gb.startGroup.expression, this.dataBaseInfo.view.loadingInfo); _groupingManager = new GroupingInfoManager(); _groupingManager.Initialize(ex, null); @@ -323,22 +323,22 @@ protected class DataBaseInfo protected FormattingCommandLineParameters parameters; - protected MshExpressionFactory expressionFactory; + protected PSPropertyExpressionFactory expressionFactory; protected DataBaseInfo dataBaseInfo = new DataBaseInfo(); protected List activeAssociationList = null; protected FormattingCommandLineParameters inputParameters = null; - protected string GetExpressionDisplayValue(PSObject so, int enumerationLimit, MshExpression ex, + protected string GetExpressionDisplayValue(PSObject so, int enumerationLimit, PSPropertyExpression ex, FieldFormattingDirective directive) { - MshExpressionResult resolvedExpression; + PSPropertyExpressionResult resolvedExpression; return GetExpressionDisplayValue(so, enumerationLimit, ex, directive, out resolvedExpression); } - protected string GetExpressionDisplayValue(PSObject so, int enumerationLimit, MshExpression ex, - FieldFormattingDirective directive, out MshExpressionResult expressionResult) + protected string GetExpressionDisplayValue(PSObject so, int enumerationLimit, PSPropertyExpression ex, + FieldFormattingDirective directive, out PSPropertyExpressionResult expressionResult) { StringFormatError formatErrorObject = null; if (_errorManager.DisplayFormatErrorString) @@ -356,7 +356,7 @@ protected string GetExpressionDisplayValue(PSObject so, int enumerationLimit, Ms // we obtained a result, check if there is an error if (expressionResult.Exception != null) { - _errorManager.LogMshExpressionFailedResult(expressionResult, so); + _errorManager.LogPSPropertyExpressionFailedResult(expressionResult, so); if (_errorManager.DisplayErrorStrings) { retVal = _errorManager.ErrorString; @@ -381,13 +381,13 @@ protected bool EvaluateDisplayCondition(PSObject so, ExpressionToken conditionTo if (conditionToken == null) return true; - MshExpression ex = this.expressionFactory.CreateFromExpressionToken(conditionToken, this.dataBaseInfo.view.loadingInfo); - MshExpressionResult expressionResult; + PSPropertyExpression ex = this.expressionFactory.CreateFromExpressionToken(conditionToken, this.dataBaseInfo.view.loadingInfo); + PSPropertyExpressionResult expressionResult; bool retVal = DisplayCondition.Evaluate(so, ex, out expressionResult); if (expressionResult != null && expressionResult.Exception != null) { - _errorManager.LogMshExpressionFailedResult(expressionResult, so); + _errorManager.LogPSPropertyExpressionFailedResult(expressionResult, so); } return retVal; } @@ -403,11 +403,11 @@ internal FormatErrorManager ErrorManager protected FormatPropertyField GenerateFormatPropertyField(List formatTokenList, PSObject so, int enumerationLimit) { - MshExpressionResult result; + PSPropertyExpressionResult result; return GenerateFormatPropertyField(formatTokenList, so, enumerationLimit, out result); } - protected FormatPropertyField GenerateFormatPropertyField(List formatTokenList, PSObject so, int enumerationLimit, out MshExpressionResult result) + protected FormatPropertyField GenerateFormatPropertyField(List formatTokenList, PSObject so, int enumerationLimit, out PSPropertyExpressionResult result) { result = null; FormatPropertyField fpf = new FormatPropertyField(); @@ -417,7 +417,7 @@ protected FormatPropertyField GenerateFormatPropertyField(List form FieldPropertyToken fpt = token as FieldPropertyToken; if (fpt != null) { - MshExpression ex = this.expressionFactory.CreateFromExpressionToken(fpt.expression, this.dataBaseInfo.view.loadingInfo); + PSPropertyExpression ex = this.expressionFactory.CreateFromExpressionToken(fpt.expression, this.dataBaseInfo.view.loadingInfo); fpf.propertyValue = this.GetExpressionDisplayValue(so, enumerationLimit, ex, fpt.fieldFormattingDirective, out result); } else diff --git a/src/System.Management.Automation/FormatAndOutput/common/FormatViewGenerator_Complex.cs b/src/System.Management.Automation/FormatAndOutput/common/FormatViewGenerator_Complex.cs index b7d537347c1..77b4f658613 100644 --- a/src/System.Management.Automation/FormatAndOutput/common/FormatViewGenerator_Complex.cs +++ b/src/System.Management.Automation/FormatAndOutput/common/FormatViewGenerator_Complex.cs @@ -12,7 +12,7 @@ namespace Microsoft.PowerShell.Commands.Internal.Format { internal sealed class ComplexViewGenerator : ViewGenerator { - internal override void Initialize(TerminatingErrorContext errorContext, MshExpressionFactory expressionFactory, + internal override void Initialize(TerminatingErrorContext errorContext, PSPropertyExpressionFactory expressionFactory, PSObject so, TypeInfoDataBase db, FormattingCommandLineParameters parameters) { base.Initialize(errorContext, expressionFactory, so, db, parameters); @@ -73,7 +73,7 @@ internal sealed class ComplexControlGenerator { internal ComplexControlGenerator(TypeInfoDataBase dataBase, DatabaseLoadingInfo loadingInfo, - MshExpressionFactory expressionFactory, + PSPropertyExpressionFactory expressionFactory, List controlDefinitionList, FormatErrorManager resultErrorManager, int enumerationLimit, @@ -259,14 +259,14 @@ private void ExecuteFormatTokenList(TraversalInfo level, } else { - MshExpression ex = _expressionFactory.CreateFromExpressionToken(cpt.expression, _loadingInfo); - List resultList = ex.GetValues(so); + PSPropertyExpression ex = _expressionFactory.CreateFromExpressionToken(cpt.expression, _loadingInfo); + List resultList = ex.GetValues(so); if (resultList.Count > 0) { val = resultList[0].Result; if (resultList[0].Exception != null) { - _errorManager.LogMshExpressionFailedResult(resultList[0], so); + _errorManager.LogPSPropertyExpressionFailedResult(resultList[0], so); } } } @@ -361,20 +361,20 @@ private bool EvaluateDisplayCondition(PSObject so, ExpressionToken conditionToke if (conditionToken == null) return true; - MshExpression ex = _expressionFactory.CreateFromExpressionToken(conditionToken, _loadingInfo); - MshExpressionResult expressionResult; + PSPropertyExpression ex = _expressionFactory.CreateFromExpressionToken(conditionToken, _loadingInfo); + PSPropertyExpressionResult expressionResult; bool retVal = DisplayCondition.Evaluate(so, ex, out expressionResult); if (expressionResult != null && expressionResult.Exception != null) { - _errorManager.LogMshExpressionFailedResult(expressionResult, so); + _errorManager.LogPSPropertyExpressionFailedResult(expressionResult, so); } return retVal; } private TypeInfoDataBase _db; private DatabaseLoadingInfo _loadingInfo; - private MshExpressionFactory _expressionFactory; + private PSPropertyExpressionFactory _expressionFactory; private List _controlDefinitionList; private FormatErrorManager _errorManager; private TerminatingErrorContext _errorContext; @@ -409,7 +409,7 @@ internal TraversalInfo NextLevel /// internal sealed class ComplexViewObjectBrowser { - internal ComplexViewObjectBrowser(FormatErrorManager resultErrorManager, MshExpressionFactory mshExpressionFactory, int enumerationLimit) + internal ComplexViewObjectBrowser(FormatErrorManager resultErrorManager, PSPropertyExpressionFactory mshExpressionFactory, int enumerationLimit) { _errorManager = resultErrorManager; _expressionFactory = mshExpressionFactory; @@ -536,14 +536,14 @@ private void ProcessActiveAssociationList(PSObject so, formatValueList.Add(ftf); // compute the value of the entry - List resList = a.ResolvedExpression.GetValues(so); + List resList = a.ResolvedExpression.GetValues(so); object val = null; if (resList.Count >= 1) { - MshExpressionResult result = resList[0]; + PSPropertyExpressionResult result = resList[0]; if (result.Exception != null) { - _errorManager.LogMshExpressionFailedResult(result, so); + _errorManager.LogPSPropertyExpressionFailedResult(result, so); if (_errorManager.DisplayErrorStrings) { val = _errorManager.ErrorString; @@ -760,7 +760,7 @@ private List AddIndentationLevel(List formatValueList) private FormatErrorManager _errorManager; - private MshExpressionFactory _expressionFactory; + private PSPropertyExpressionFactory _expressionFactory; private int _enumerationLimit; } diff --git a/src/System.Management.Automation/FormatAndOutput/common/FormatViewGenerator_List.cs b/src/System.Management.Automation/FormatAndOutput/common/FormatViewGenerator_List.cs index 04f3a0fe06d..afdf7c421fb 100644 --- a/src/System.Management.Automation/FormatAndOutput/common/FormatViewGenerator_List.cs +++ b/src/System.Management.Automation/FormatAndOutput/common/FormatViewGenerator_List.cs @@ -13,7 +13,7 @@ internal sealed class ListViewGenerator : ViewGenerator // tableBody to use for this instance of the ViewGenerator; private ListControlBody _listBody; - internal override void Initialize(TerminatingErrorContext terminatingErrorContext, MshExpressionFactory mshExpressionFactory, TypeInfoDataBase db, ViewDefinition view, FormattingCommandLineParameters formatParameters) + internal override void Initialize(TerminatingErrorContext terminatingErrorContext, PSPropertyExpressionFactory mshExpressionFactory, TypeInfoDataBase db, ViewDefinition view, FormattingCommandLineParameters formatParameters) { base.Initialize(terminatingErrorContext, mshExpressionFactory, db, view, formatParameters); if ((null != this.dataBaseInfo) && (null != this.dataBaseInfo.view)) @@ -22,7 +22,7 @@ internal override void Initialize(TerminatingErrorContext terminatingErrorContex } } - internal override void Initialize(TerminatingErrorContext errorContext, MshExpressionFactory expressionFactory, + internal override void Initialize(TerminatingErrorContext errorContext, PSPropertyExpressionFactory expressionFactory, PSObject so, TypeInfoDataBase db, FormattingCommandLineParameters parameters) { base.Initialize(errorContext, expressionFactory, so, db, parameters); @@ -94,7 +94,7 @@ private ListViewEntry GenerateListViewEntryFromDataBaseInfo(PSObject so, int enu continue; ListViewField lvf = new ListViewField(); - MshExpressionResult result; + PSPropertyExpressionResult result; lvf.formatPropertyField = GenerateFormatPropertyField(listItem.formatTokenList, so, enumerationLimit, out result); // we need now to provide a label @@ -112,14 +112,14 @@ private ListViewEntry GenerateListViewEntryFromDataBaseInfo(PSObject so, int enu { // we did fail getting a result (i.e. property does not exist on the object) - // we try to fall back and see if we have an un-resolved MshExpression + // we try to fall back and see if we have an un-resolved PSPropertyExpression FormatToken token = listItem.formatTokenList[0]; FieldPropertyToken fpt = token as FieldPropertyToken; if (fpt != null) { - MshExpression ex = this.expressionFactory.CreateFromExpressionToken(fpt.expression, this.dataBaseInfo.view.loadingInfo); + PSPropertyExpression ex = this.expressionFactory.CreateFromExpressionToken(fpt.expression, this.dataBaseInfo.view.loadingInfo); - // use the un-resolved MshExpression string as a label + // use the un-resolved PSPropertyExpression string as a label lvf.label = ex.ToString(); } else diff --git a/src/System.Management.Automation/FormatAndOutput/common/FormatViewGenerator_Table.cs b/src/System.Management.Automation/FormatAndOutput/common/FormatViewGenerator_Table.cs index ef0c983903e..16889a15949 100644 --- a/src/System.Management.Automation/FormatAndOutput/common/FormatViewGenerator_Table.cs +++ b/src/System.Management.Automation/FormatAndOutput/common/FormatViewGenerator_Table.cs @@ -14,7 +14,7 @@ internal sealed class TableViewGenerator : ViewGenerator // tableBody to use for this instance of the ViewGenerator; private TableControlBody _tableBody; - internal override void Initialize(TerminatingErrorContext terminatingErrorContext, MshExpressionFactory mshExpressionFactory, TypeInfoDataBase db, ViewDefinition view, FormattingCommandLineParameters formatParameters) + internal override void Initialize(TerminatingErrorContext terminatingErrorContext, PSPropertyExpressionFactory mshExpressionFactory, TypeInfoDataBase db, ViewDefinition view, FormattingCommandLineParameters formatParameters) { base.Initialize(terminatingErrorContext, mshExpressionFactory, db, view, formatParameters); if ((null != this.dataBaseInfo) && (null != this.dataBaseInfo.view)) @@ -23,7 +23,7 @@ internal override void Initialize(TerminatingErrorContext terminatingErrorContex } } - internal override void Initialize(TerminatingErrorContext errorContext, MshExpressionFactory expressionFactory, + internal override void Initialize(TerminatingErrorContext errorContext, PSPropertyExpressionFactory expressionFactory, PSObject so, TypeInfoDataBase db, FormattingCommandLineParameters parameters) { @@ -56,7 +56,7 @@ internal override void Initialize(TerminatingErrorContext errorContext, MshExpre if (PSObjectHelper.ShouldShowComputerNameProperty(so)) { activeAssociationList.Add(new MshResolvedExpressionParameterAssociation(null, - new MshExpression(RemotingConstants.ComputerNameNoteProperty))); + new PSPropertyExpression(RemotingConstants.ComputerNameNoteProperty))); } return; } @@ -293,9 +293,9 @@ private bool HideHeaders } } - private static int ComputeDefaultAlignment(PSObject so, MshExpression ex) + private static int ComputeDefaultAlignment(PSObject so, PSPropertyExpression ex) { - List rList = ex.GetValues(so); + List rList = ex.GetValues(so); if ((rList.Count == 0) || (rList[0].Exception != null)) return TextAlignment.Left; diff --git a/src/System.Management.Automation/FormatAndOutput/common/FormatViewGenerator_Wide.cs b/src/System.Management.Automation/FormatAndOutput/common/FormatViewGenerator_Wide.cs index 5f3af4cb9b1..b386bb7354e 100644 --- a/src/System.Management.Automation/FormatAndOutput/common/FormatViewGenerator_Wide.cs +++ b/src/System.Management.Automation/FormatAndOutput/common/FormatViewGenerator_Wide.cs @@ -9,7 +9,7 @@ namespace Microsoft.PowerShell.Commands.Internal.Format { internal sealed class WideViewGenerator : ViewGenerator { - internal override void Initialize(TerminatingErrorContext errorContext, MshExpressionFactory expressionFactory, + internal override void Initialize(TerminatingErrorContext errorContext, PSPropertyExpressionFactory expressionFactory, PSObject so, TypeInfoDataBase db, FormattingCommandLineParameters parameters) { base.Initialize(errorContext, expressionFactory, so, db, parameters); @@ -171,7 +171,7 @@ private void SetUpActiveProperty(PSObject so) // we did not get any properties: //try to get the display property of the object - MshExpression displayNameExpression = PSObjectHelper.GetDisplayNameExpression(so, this.expressionFactory); + PSPropertyExpression displayNameExpression = PSObjectHelper.GetDisplayNameExpression(so, this.expressionFactory); if (displayNameExpression != null) { this.activeAssociationList = new List(); diff --git a/src/System.Management.Automation/FormatAndOutput/common/FormatViewManager.cs b/src/System.Management.Automation/FormatAndOutput/common/FormatViewManager.cs index 9a62ce2318e..7ac0ae5d5d3 100644 --- a/src/System.Management.Automation/FormatAndOutput/common/FormatViewManager.cs +++ b/src/System.Management.Automation/FormatAndOutput/common/FormatViewManager.cs @@ -7,6 +7,7 @@ using System.Management.Automation; using System.Management.Automation.Internal; using System.Text; +using Microsoft.PowerShell.Commands; namespace Microsoft.PowerShell.Commands.Internal.Format { @@ -86,7 +87,7 @@ private static string PSObjectTypeName(PSObject so) } internal void Initialize(TerminatingErrorContext errorContext, - MshExpressionFactory expressionFactory, + PSPropertyExpressionFactory expressionFactory, TypeInfoDataBase db, PSObject so, FormatShape shape, @@ -360,7 +361,7 @@ internal ViewGenerator ViewGenerator private static ViewGenerator SelectViewGeneratorFromViewDefinition( TerminatingErrorContext errorContext, - MshExpressionFactory expressionFactory, + PSPropertyExpressionFactory expressionFactory, TypeInfoDataBase db, ViewDefinition view, FormattingCommandLineParameters parameters) @@ -390,7 +391,7 @@ private static ViewGenerator SelectViewGeneratorFromViewDefinition( private static ViewGenerator SelectViewGeneratorFromProperties(FormatShape shape, PSObject so, TerminatingErrorContext errorContext, - MshExpressionFactory expressionFactory, + PSPropertyExpressionFactory expressionFactory, TypeInfoDataBase db, FormattingCommandLineParameters parameters) { @@ -405,7 +406,7 @@ private static ViewGenerator SelectViewGeneratorFromProperties(FormatShape shape { // check if we can have a table: // we want to get the # of properties we are going to display - List expressionList = PSObjectHelper.GetDefaultPropertySet(so); + List expressionList = PSObjectHelper.GetDefaultPropertySet(so); if (expressionList.Count == 0) { // we failed to get anything from a property set @@ -513,7 +514,7 @@ internal static bool IsPropertyLessObject(PSObject so) return false; } - internal static FormatEntryData GenerateOutOfBandData(TerminatingErrorContext errorContext, MshExpressionFactory expressionFactory, + internal static FormatEntryData GenerateOutOfBandData(TerminatingErrorContext errorContext, PSPropertyExpressionFactory expressionFactory, TypeInfoDataBase db, PSObject so, int enumerationLimit, bool useToStringFallback, out List errors) { errors = null; @@ -550,7 +551,7 @@ internal static FormatEntryData GenerateOutOfBandData(TerminatingErrorContext er } // we must check we have enough properties for a list view - if (new MshExpression("*").ResolveNames(so).Count <= 0) + if (new PSPropertyExpression("*").ResolveNames(so).Count <= 0) { return null; } @@ -585,9 +586,9 @@ internal static FormatEntryData GenerateOutOfBandObjectAsToString(PSObject so) /// /// Helper class to manage the logging of errors resulting from - /// evaluations of MshExpression instances + /// evaluations of PSPropertyExpression instances /// - /// Depending on settings, it queues the failing MshExpressionResult + /// Depending on settings, it queues the failing PSPropertyExpressionResult /// instances and generates a list of out-of-band FormatEntryData /// objects to be sent to the output pipeline /// @@ -599,15 +600,15 @@ internal FormatErrorManager(FormatErrorPolicy formatErrorPolicy) } /// - /// log a failed evaluation of an MshExpression + /// log a failed evaluation of an PSPropertyExpression /// - /// MshExpressionResult containing the failed evaluation data - /// object used to evaluate the MshExpression - internal void LogMshExpressionFailedResult(MshExpressionResult result, object sourceObject) + /// PSPropertyExpressionResult containing the failed evaluation data + /// object used to evaluate the PSPropertyExpression + internal void LogPSPropertyExpressionFailedResult(PSPropertyExpressionResult result, object sourceObject) { if (!_formatErrorPolicy.ShowErrorsAsMessages) return; - MshExpressionError error = new MshExpressionError(); + PSPropertyExpressionError error = new PSPropertyExpressionError(); error.result = result; error.sourceObject = sourceObject; _formattingErrorList.Add(error); @@ -679,17 +680,17 @@ private static ErrorRecord GenerateErrorRecord(FormattingError error) { ErrorRecord errorRecord = null; string msg = null; - MshExpressionError mshExpressionError = error as MshExpressionError; - if (mshExpressionError != null) + PSPropertyExpressionError psPropertyExpressionError = error as PSPropertyExpressionError; + if (psPropertyExpressionError != null) { errorRecord = new ErrorRecord( - mshExpressionError.result.Exception, - "mshExpressionError", + psPropertyExpressionError.result.Exception, + "PSPropertyExpressionError", ErrorCategory.InvalidArgument, - mshExpressionError.sourceObject); + psPropertyExpressionError.sourceObject); - msg = StringUtil.Format(FormatAndOut_format_xxx.MshExpressionError, - mshExpressionError.result.ResolvedExpression.ToString()); + msg = StringUtil.Format(FormatAndOut_format_xxx.PSPropertyExpressionError, + psPropertyExpressionError.result.ResolvedExpression.ToString()); errorRecord.ErrorDetails = new ErrorDetails(msg); } @@ -712,7 +713,7 @@ private static ErrorRecord GenerateErrorRecord(FormattingError error) private FormatErrorPolicy _formatErrorPolicy; /// - /// current list of failed MsExpression evaluations + /// current list of failed PSPropertyExpression evaluations /// private List _formattingErrorList = new List(); } diff --git a/src/System.Management.Automation/FormatAndOutput/common/Utilities/MshObjectUtil.cs b/src/System.Management.Automation/FormatAndOutput/common/Utilities/MshObjectUtil.cs index c4f885112fc..bc159565621 100644 --- a/src/System.Management.Automation/FormatAndOutput/common/Utilities/MshObjectUtil.cs +++ b/src/System.Management.Automation/FormatAndOutput/common/Utilities/MshObjectUtil.cs @@ -115,12 +115,12 @@ internal static bool IsStreamType(PSObject so, string streamFlag) /// if not found, it uses some heuristics to get a "close" match /// /// shell object to process - /// expression factory to create MshExpression - /// resolved MshExpression; null if no match was found - internal static MshExpression GetDisplayNameExpression(PSObject target, MshExpressionFactory expressionFactory) + /// expression factory to create PSPropertyExpression + /// resolved PSPropertyExpression; null if no match was found + internal static PSPropertyExpression GetDisplayNameExpression(PSObject target, PSPropertyExpressionFactory expressionFactory) { // first try to get the expression from the object (types.ps1xml data) - MshExpression expressionFromObject = GetDefaultNameExpression(target); + PSPropertyExpression expressionFromObject = GetDefaultNameExpression(target); if (expressionFromObject != null) { return expressionFromObject; @@ -135,8 +135,8 @@ internal static MshExpression GetDisplayNameExpression(PSObject target, MshExpre // go over the patterns, looking for the first match foreach (string pattern in knownPatterns) { - MshExpression ex = new MshExpression(pattern); - List exprList = ex.ResolveNames(target); + PSPropertyExpression ex = new PSPropertyExpression(pattern); + List exprList = ex.ResolveNames(target); while ((exprList.Count > 0) && ( exprList[0].ToString().Equals(RemotingConstants.ComputerNameNoteProperty, StringComparison.OrdinalIgnoreCase) || @@ -162,17 +162,17 @@ internal static MshExpression GetDisplayNameExpression(PSObject target, MshExpre /// it gets the display name value /// /// shell object to process - /// expression factory to create MshExpression - /// MshExpressionResult if successful; null otherwise - internal static MshExpressionResult GetDisplayName(PSObject target, MshExpressionFactory expressionFactory) + /// expression factory to create PSPropertyExpression + /// PSPropertyExpressionResult if successful; null otherwise + internal static PSPropertyExpressionResult GetDisplayName(PSObject target, PSPropertyExpressionFactory expressionFactory) { // get the expression to evaluate - MshExpression ex = GetDisplayNameExpression(target, expressionFactory); + PSPropertyExpression ex = GetDisplayNameExpression(target, expressionFactory); if (ex == null) return null; // evaluate the expression - List resList = ex.GetValues(target); + List resList = ex.GetValues(target); if (resList.Count == 0 || resList[0].Exception != null) { @@ -203,9 +203,9 @@ internal static IEnumerable GetEnumerable(object obj) return LanguagePrimitives.GetEnumerable(obj); } - private static string GetSmartToStringDisplayName(object x, MshExpressionFactory expressionFactory) + private static string GetSmartToStringDisplayName(object x, PSPropertyExpressionFactory expressionFactory) { - MshExpressionResult r = PSObjectHelper.GetDisplayName(PSObjectHelper.AsPSObject(x), expressionFactory); + PSPropertyExpressionResult r = PSObjectHelper.GetDisplayName(PSObjectHelper.AsPSObject(x), expressionFactory); if ((r != null) && (r.Exception == null)) { return PSObjectHelper.AsPSObject(r.Result).ToString(); @@ -216,7 +216,7 @@ private static string GetSmartToStringDisplayName(object x, MshExpressionFactory } } - private static string GetObjectName(object x, MshExpressionFactory expressionFactory) + private static string GetObjectName(object x, PSPropertyExpressionFactory expressionFactory) { string objName; @@ -245,7 +245,7 @@ private static string GetObjectName(object x, MshExpressionFactory expressionFac } else { - MshExpressionResult r = PSObjectHelper.GetDisplayName(PSObjectHelper.AsPSObject(x), expressionFactory); + PSPropertyExpressionResult r = PSObjectHelper.GetDisplayName(PSObjectHelper.AsPSObject(x), expressionFactory); if ((r != null) && (r.Exception == null)) { objName = PSObjectHelper.AsPSObject(r.Result).ToString(); ; @@ -273,11 +273,11 @@ private static string GetObjectName(object x, MshExpressionFactory expressionFac /// It takes into account enumerations (use display name) /// /// shell object to process - /// expression factory to create MshExpression + /// expression factory to create PSPropertyExpression /// limit on IEnumerable enumeration /// stores errors during string conversion /// string representation - internal static string SmartToString(PSObject so, MshExpressionFactory expressionFactory, int enumerationLimit, StringFormatError formatErrorObject) + internal static string SmartToString(PSObject so, PSPropertyExpressionFactory expressionFactory, int enumerationLimit, StringFormatError formatErrorObject) { if (so == null) return string.Empty; @@ -385,10 +385,10 @@ internal static PSObject AsPSObject(object obj) /// object to format /// limit on IEnumerable enumeration /// formatting error object, if present - /// expression factory to create MshExpression + /// expression factory to create PSPropertyExpression /// string representation internal static string FormatField(FieldFormattingDirective directive, object val, int enumerationLimit, - StringFormatError formatErrorObject, MshExpressionFactory expressionFactory) + StringFormatError formatErrorObject, PSPropertyExpressionFactory expressionFactory) { PSObject so = PSObjectHelper.AsPSObject(val); if (directive != null && !string.IsNullOrEmpty(directive.formatString)) @@ -451,26 +451,26 @@ private static PSMemberSet MaskDeserializedAndGetStandardMembers(PSObject so) return members[TypeTable.PSStandardMembers] as PSMemberSet; } - private static List GetDefaultPropertySet(PSMemberSet standardMembersSet) + private static List GetDefaultPropertySet(PSMemberSet standardMembersSet) { if (null != standardMembersSet) { PSPropertySet defaultDisplayPropertySet = standardMembersSet.Members[TypeTable.DefaultDisplayPropertySet] as PSPropertySet; if (null != defaultDisplayPropertySet) { - List retVal = new List(); + List retVal = new List(); foreach (string prop in defaultDisplayPropertySet.ReferencedPropertyNames) { if (!string.IsNullOrEmpty(prop)) { - retVal.Add(new MshExpression(prop)); + retVal.Add(new PSPropertyExpression(prop)); } } return retVal; } } - return new List(); + return new List(); } /// @@ -478,9 +478,9 @@ private static List GetDefaultPropertySet(PSMemberSet standardMem /// /// shell object to process /// resolved expression; empty list if not found - internal static List GetDefaultPropertySet(PSObject so) + internal static List GetDefaultPropertySet(PSObject so) { - List retVal = GetDefaultPropertySet(so.PSStandardMembers); + List retVal = GetDefaultPropertySet(so.PSStandardMembers); if (retVal.Count == 0) { retVal = GetDefaultPropertySet(MaskDeserializedAndGetStandardMembers(so)); @@ -489,7 +489,7 @@ internal static List GetDefaultPropertySet(PSObject so) return retVal; } - private static MshExpression GetDefaultNameExpression(PSMemberSet standardMembersSet) + private static PSPropertyExpression GetDefaultNameExpression(PSMemberSet standardMembersSet) { if (null != standardMembersSet) { @@ -504,7 +504,7 @@ private static MshExpression GetDefaultNameExpression(PSMemberSet standardMember } else { - return new MshExpression(expressionString); + return new PSPropertyExpression(expressionString); } } } @@ -512,36 +512,36 @@ private static MshExpression GetDefaultNameExpression(PSMemberSet standardMember return null; } - private static MshExpression GetDefaultNameExpression(PSObject so) + private static PSPropertyExpression GetDefaultNameExpression(PSObject so) { - MshExpression retVal = GetDefaultNameExpression(so.PSStandardMembers) ?? + PSPropertyExpression retVal = GetDefaultNameExpression(so.PSStandardMembers) ?? GetDefaultNameExpression(MaskDeserializedAndGetStandardMembers(so)); return retVal; } /// - /// helper to retrieve the value of an MshExpression and to format it + /// helper to retrieve the value of an PSPropertyExpression and to format it /// /// shell object to process /// limit on IEnumerable enumeration /// expression to use for retrieval /// format directive to use for formatting /// - /// expression factory to create MshExpression + /// expression factory to create PSPropertyExpression /// not null if an error condition arose /// formatted string internal static string GetExpressionDisplayValue( PSObject so, int enumerationLimit, - MshExpression ex, + PSPropertyExpression ex, FieldFormattingDirective directive, StringFormatError formatErrorObject, - MshExpressionFactory expressionFactory, - out MshExpressionResult result) + PSPropertyExpressionFactory expressionFactory, + out PSPropertyExpressionResult result) { result = null; - List resList = ex.GetValues(so); + List resList = ex.GetValues(so); if (resList.Count == 0) { @@ -599,9 +599,9 @@ internal abstract class FormattingError internal object sourceObject; } - internal sealed class MshExpressionError : FormattingError + internal sealed class PSPropertyExpressionError : FormattingError { - internal MshExpressionResult result; + internal PSPropertyExpressionResult result; } internal sealed class StringFormatError : FormattingError @@ -613,9 +613,9 @@ internal sealed class StringFormatError : FormattingError internal delegate ScriptBlock CreateScriptBlockFromString(string scriptBlockString); /// - /// helper class to create MshExpression's from format.ps1xml data structures + /// helper class to create PSPropertyExpression's from format.ps1xml data structures /// - internal sealed class MshExpressionFactory + internal sealed class PSPropertyExpressionFactory { /// internal void VerifyScriptBlockText(string scriptText) @@ -629,7 +629,7 @@ internal void VerifyScriptBlockText(string scriptText) /// expression token to use /// constructed expression /// - internal MshExpression CreateFromExpressionToken(ExpressionToken et) + internal PSPropertyExpression CreateFromExpressionToken(ExpressionToken et) { return CreateFromExpressionToken(et, null); } @@ -641,14 +641,14 @@ internal MshExpression CreateFromExpressionToken(ExpressionToken et) /// The context from which the file was loaded /// constructed expression /// - internal MshExpression CreateFromExpressionToken(ExpressionToken et, DatabaseLoadingInfo loadingInfo) + internal PSPropertyExpression CreateFromExpressionToken(ExpressionToken et, DatabaseLoadingInfo loadingInfo) { if (et.isScriptBlock) { // we cache script blocks from expression tokens if (_expressionCache != null) { - MshExpression value; + PSPropertyExpression value; if (_expressionCache.TryGetValue(et, out value)) { // got a hit on the cache, just return @@ -657,7 +657,7 @@ internal MshExpression CreateFromExpressionToken(ExpressionToken et, DatabaseLoa } else { - _expressionCache = new Dictionary(); + _expressionCache = new Dictionary(); } bool isFullyTrusted = false; @@ -677,7 +677,7 @@ internal MshExpression CreateFromExpressionToken(ExpressionToken et, DatabaseLoa sb.LanguageMode = PSLanguageMode.FullLanguage; } - MshExpression ex = new MshExpression(sb); + PSPropertyExpression ex = new PSPropertyExpression(sb); _expressionCache.Add(et, ex); @@ -685,10 +685,10 @@ internal MshExpression CreateFromExpressionToken(ExpressionToken et, DatabaseLoa } // we do not cache if it is just a property name - return new MshExpression(et.expressionValue); + return new PSPropertyExpression(et.expressionValue); } - private Dictionary _expressionCache; + private Dictionary _expressionCache; } } diff --git a/src/System.Management.Automation/FormatAndOutput/common/Utilities/MshParameterAssociation.cs b/src/System.Management.Automation/FormatAndOutput/common/Utilities/MshParameterAssociation.cs index 09a0cbcbd93..b24a111ed0c 100644 --- a/src/System.Management.Automation/FormatAndOutput/common/Utilities/MshParameterAssociation.cs +++ b/src/System.Management.Automation/FormatAndOutput/common/Utilities/MshParameterAssociation.cs @@ -20,7 +20,7 @@ internal sealed class MshResolvedExpressionParameterAssociation "MshResolvedExpressionParameterAssociation"); #endregion tracer - internal MshResolvedExpressionParameterAssociation(MshParameter parameter, MshExpression expression) + internal MshResolvedExpressionParameterAssociation(MshParameter parameter, PSPropertyExpression expression) { if (expression == null) throw PSTraceSource.NewArgumentNullException("expression"); @@ -29,7 +29,7 @@ internal MshResolvedExpressionParameterAssociation(MshParameter parameter, MshEx ResolvedExpression = expression; } - internal MshExpression ResolvedExpression { get; } + internal PSPropertyExpression ResolvedExpression { get; } internal MshParameter OriginatingParameter { get; } } @@ -37,7 +37,7 @@ internal MshResolvedExpressionParameterAssociation(MshParameter parameter, MshEx internal static class AssociationManager { internal static List SetupActiveProperties(List rawMshParameterList, - PSObject target, MshExpressionFactory expressionFactory) + PSObject target, PSPropertyExpressionFactory expressionFactory) { // check if we received properties from the command line if (rawMshParameterList != null && rawMshParameterList.Count > 0) @@ -56,7 +56,7 @@ internal static List SetupActivePrope if (PSObjectHelper.ShouldShowComputerNameProperty(target)) { activeAssociationList.Add(new MshResolvedExpressionParameterAssociation(null, - new MshExpression(RemotingConstants.ComputerNameNoteProperty))); + new PSPropertyExpression(RemotingConstants.ComputerNameNoteProperty))); } return activeAssociationList; @@ -77,8 +77,8 @@ internal static List ExpandTableParam foreach (MshParameter par in parameters) { - MshExpression expression = par.GetEntry(FormatParameterDefinitionKeys.ExpressionEntryKey) as MshExpression; - List expandedExpressionList = expression.ResolveNames(target); + PSPropertyExpression expression = par.GetEntry(FormatParameterDefinitionKeys.ExpressionEntryKey) as PSPropertyExpression; + List expandedExpressionList = expression.ResolveNames(target); if (!expression.HasWildCardCharacters && expandedExpressionList.Count == 0) { @@ -86,7 +86,7 @@ internal static List ExpandTableParam retVal.Add(new MshResolvedExpressionParameterAssociation(par, expression)); } - foreach (MshExpression ex in expandedExpressionList) + foreach (PSPropertyExpression ex in expandedExpressionList) { retVal.Add(new MshResolvedExpressionParameterAssociation(par, ex)); } @@ -101,10 +101,10 @@ internal static List ExpandParameters foreach (MshParameter par in parameters) { - MshExpression expression = par.GetEntry(FormatParameterDefinitionKeys.ExpressionEntryKey) as MshExpression; - List expandedExpressionList = expression.ResolveNames(target); + PSPropertyExpression expression = par.GetEntry(FormatParameterDefinitionKeys.ExpressionEntryKey) as PSPropertyExpression; + List expandedExpressionList = expression.ResolveNames(target); - foreach (MshExpression ex in expandedExpressionList) + foreach (PSPropertyExpression ex in expandedExpressionList) { retVal.Add(new MshResolvedExpressionParameterAssociation(par, ex)); } @@ -113,12 +113,12 @@ internal static List ExpandParameters return retVal; } - internal static List ExpandDefaultPropertySet(PSObject target, MshExpressionFactory expressionFactory) + internal static List ExpandDefaultPropertySet(PSObject target, PSPropertyExpressionFactory expressionFactory) { List retVal = new List(); - List expandedExpressionList = PSObjectHelper.GetDefaultPropertySet(target); + List expandedExpressionList = PSObjectHelper.GetDefaultPropertySet(target); - foreach (MshExpression ex in expandedExpressionList) + foreach (PSPropertyExpression ex in expandedExpressionList) { retVal.Add(new MshResolvedExpressionParameterAssociation(null, ex)); } @@ -161,7 +161,7 @@ internal static List ExpandAll(PSObje if (!duplicatesFinder.ContainsKey(property)) { duplicatesFinder.Add(property, null); - MshExpression expr = new MshExpression(property, true); + PSPropertyExpression expr = new PSPropertyExpression(property, true); retVal.Add(new MshResolvedExpressionParameterAssociation(null, expr)); } } diff --git a/src/System.Management.Automation/FormatAndOutput/common/Utilities/Mshexpression.cs b/src/System.Management.Automation/FormatAndOutput/common/Utilities/Mshexpression.cs index d8851b2daed..f4841a843bc 100644 --- a/src/System.Management.Automation/FormatAndOutput/common/Utilities/Mshexpression.cs +++ b/src/System.Management.Automation/FormatAndOutput/common/Utilities/Mshexpression.cs @@ -10,47 +10,65 @@ using System.Collections.ObjectModel; using System.Runtime.CompilerServices; -namespace Microsoft.PowerShell.Commands.Internal.Format +namespace Microsoft.PowerShell.Commands { /// - /// class to hold results - /// NOTE: we should make it an PSObject eventually + /// class that represents the results from evaluating a PSPropertyExpression against an object. /// - internal class MshExpressionResult + public class PSPropertyExpressionResult { - internal MshExpressionResult(object res, MshExpression re, Exception e) + + /// + /// Create a property expression result containing the original object, matching property expression + /// and any exception generated during the match process. + /// + public PSPropertyExpressionResult(object res, PSPropertyExpression re, Exception e) { Result = res; ResolvedExpression = re; Exception = e; } - internal object Result { get; } = null; + /// + /// The value of the object property matched by this property expression. + /// + public object Result { get; } = null; - internal MshExpression ResolvedExpression { get; } = null; + /// + /// The original property expression fully resolved. + /// + public PSPropertyExpression ResolvedExpression { get; } = null; - internal Exception Exception { get; } = null; + /// + /// Any exception thrown while evaluating the expression. + /// + public Exception Exception { get; } = null; } - internal class MshExpression + /// + /// PSPropertyExpression class. This class is used to get the names and/or values of properties + /// on an object. A property expression can be constructed using either a wildcard expression string + /// or a scriptblock to use to get the property value. + /// + public class PSPropertyExpression { /// /// constructor /// /// expression /// - internal MshExpression(string s) + public PSPropertyExpression(string s) : this(s, false) { } /// - /// constructor + /// Create a property expression with a wildcard pattern. /// - /// expression + /// Property name pattern to match. /// true if no further attempts should be made to resolve wildcards /// - internal MshExpression(string s, bool isResolved) + public PSPropertyExpression(string s, bool isResolved) { if (string.IsNullOrEmpty(s)) { @@ -61,11 +79,11 @@ internal MshExpression(string s, bool isResolved) } /// - /// constructor + /// Create a property expression with a ScriptBlock /// - /// + /// ScriptBlock to evaluate when retrieving the property value from an object. /// - internal MshExpression(ScriptBlock scriptBlock) + public PSPropertyExpression(ScriptBlock scriptBlock) { if (scriptBlock == null) { @@ -74,8 +92,14 @@ internal MshExpression(ScriptBlock scriptBlock) Script = scriptBlock; } + /// + /// The ScriptBlock for this expression to use when matching. + /// public ScriptBlock Script { get; } = null; + /// + /// ToString() implementation for the property expression. + /// public override string ToString() { if (Script != null) @@ -84,12 +108,20 @@ public override string ToString() return _stringValue; } - internal List ResolveNames(PSObject target) + /// + /// Resolve the names matched by this the expression. + /// + /// The object to apply the expression against. + public List ResolveNames(PSObject target) { return ResolveNames(target, true); } - internal bool HasWildCardCharacters + /// + /// Indicates if the pattern has wildcard characters in it. If the supplied pattern was + /// a scriptblock, this will be false. + /// + public bool HasWildCardCharacters { get { @@ -99,9 +131,14 @@ internal bool HasWildCardCharacters } } - internal List ResolveNames(PSObject target, bool expand) + /// + /// Resolve the names matched by this the expression. + /// + /// The object to apply the expression against. + /// If the matched properties are property sets, expand them. + public List ResolveNames(PSObject target, bool expand) { - List retVal = new List(); + List retVal = new List(); if (_isResolved) { @@ -112,13 +149,17 @@ internal List ResolveNames(PSObject target, bool expand) if (Script != null) { // script block, just add it to the list and be done - MshExpression ex = new MshExpression(Script); + PSPropertyExpression ex = new PSPropertyExpression(Script); ex._isResolved = true; retVal.Add(ex); return retVal; } + // If the object passed in is a hashtable, then turn it into a PSCustomObject so + // that property expressions can work on it. + target = IfHashtableWrapAsPSCustomObject(target); + // we have a string value IEnumerable members = null; if (HasWildCardCharacters) @@ -195,7 +236,7 @@ internal List ResolveNames(PSObject target, bool expand) { if (!hash.ContainsKey(m.Name)) { - MshExpression ex = new MshExpression(m.Name); + PSPropertyExpression ex = new PSPropertyExpression(m.Name); ex._isResolved = true; retVal.Add(ex); @@ -206,30 +247,44 @@ internal List ResolveNames(PSObject target, bool expand) return retVal; } - internal List GetValues(PSObject target) + /// + /// Gets the values of the object properties matched by this expression. + /// + /// The object to match against. + public List GetValues(PSObject target) { return GetValues(target, true, true); } - internal List GetValues(PSObject target, bool expand, bool eatExceptions) + /// + /// Gets the values of the object properties matched by this expression. + /// + /// The object to match against. + /// If the matched properties are parameter sets, expand them. + /// If true, any exceptions that occur during the match process are ignored. + public List GetValues(PSObject target, bool expand, bool eatExceptions) { - List retVal = new List(); + List retVal = new List(); + + // If the object passed in is a hashtable, then turn it into a PSCustomObject so + // that property expressions can work on it. + target = IfHashtableWrapAsPSCustomObject(target); // process the script case if (Script != null) { - MshExpression scriptExpression = new MshExpression(Script); - MshExpressionResult r = scriptExpression.GetValue(target, eatExceptions); + PSPropertyExpression scriptExpression = new PSPropertyExpression(Script); + PSPropertyExpressionResult r = scriptExpression.GetValue(target, eatExceptions); retVal.Add(r); return retVal; } // process the expression - List resolvedExpressionList = this.ResolveNames(target, expand); + List resolvedExpressionList = this.ResolveNames(target, expand); - foreach (MshExpression re in resolvedExpressionList) + foreach (PSPropertyExpression re in resolvedExpressionList) { - MshExpressionResult r = re.GetValue(target, eatExceptions); + PSPropertyExpressionResult r = re.GetValue(target, eatExceptions); retVal.Add(r); } @@ -240,7 +295,7 @@ internal List GetValues(PSObject target, bool expand, bool private CallSite> _getValueDynamicSite; - private MshExpressionResult GetValue(PSObject target, bool eatExceptions) + private PSPropertyExpressionResult GetValue(PSObject target, bool eatExceptions) { try { @@ -270,13 +325,13 @@ private MshExpressionResult GetValue(PSObject target, bool eatExceptions) result = _getValueDynamicSite.Target.Invoke(_getValueDynamicSite, target); } - return new MshExpressionResult(result, this, null); + return new PSPropertyExpressionResult(result, this, null); } catch (RuntimeException e) { if (eatExceptions) { - return new MshExpressionResult(null, this, e); + return new PSPropertyExpressionResult(null, this, e); } else { @@ -284,6 +339,17 @@ private MshExpressionResult GetValue(PSObject target, bool eatExceptions) } } } + + private PSObject IfHashtableWrapAsPSCustomObject(PSObject target) + { + // If the object passed in is a hashtable, then turn it into a PSCustomObject so + // that property expressions can work on it. + if (PSObject.Base(target) is Hashtable targetAsHash) + { + target = (PSObject)(LanguagePrimitives.ConvertPSObjectToType(targetAsHash, typeof(PSObject), false, null, true)); + } + return target; + } // private members private string _stringValue; diff --git a/src/System.Management.Automation/engine/parser/TypeResolver.cs b/src/System.Management.Automation/engine/parser/TypeResolver.cs index 3af4609d2d1..c4a749abb7a 100644 --- a/src/System.Management.Automation/engine/parser/TypeResolver.cs +++ b/src/System.Management.Automation/engine/parser/TypeResolver.cs @@ -857,6 +857,7 @@ static TypeAccelerators() // Add additional utility types that are useful as type accelerators, but aren't // fundamentally "core language", or may be unsafe to expose to untrusted input. builtinTypeAccelerators.Add("scriptblock", typeof(ScriptBlock)); + builtinTypeAccelerators.Add("pspropertyexpression", typeof(PSPropertyExpression)); builtinTypeAccelerators.Add("psvariable", typeof(PSVariable)); builtinTypeAccelerators.Add("type", typeof(Type)); builtinTypeAccelerators.Add("psmoduleinfo", typeof(PSModuleInfo)); diff --git a/src/System.Management.Automation/resources/FormatAndOut_format_xxx.resx b/src/System.Management.Automation/resources/FormatAndOut_format_xxx.resx index def91beeec1..66cfdc8a25f 100644 --- a/src/System.Management.Automation/resources/FormatAndOut_format_xxx.resx +++ b/src/System.Management.Automation/resources/FormatAndOut_format_xxx.resx @@ -173,7 +173,7 @@ The {0} property is recursive. - + Failed to evaluate expression "{0}". diff --git a/test/powershell/Language/Parser/TypeAccelerator.Tests.ps1 b/test/powershell/Language/Parser/TypeAccelerator.Tests.ps1 index 0f5c4a1a106..2bbb0466893 100644 --- a/test/powershell/Language/Parser/TypeAccelerator.Tests.ps1 +++ b/test/powershell/Language/Parser/TypeAccelerator.Tests.ps1 @@ -117,7 +117,7 @@ Describe "Type accelerators" -Tags "CI" { @{ Accelerator = 'cimtype' Type = [Microsoft.Management.Infrastructure.CimType] - } + } @{ Accelerator = 'cimconverter' Type = [Microsoft.Management.Infrastructure.CimConverter] @@ -197,7 +197,7 @@ Describe "Type accelerators" -Tags "CI" { @{ Accelerator = 'SupportsWildcards' Type = [System.Management.Automation.SupportsWildcardsAttribute] - } + } @{ Accelerator = 'switch' Type = [System.Management.Automation.SwitchParameter] @@ -353,7 +353,7 @@ Describe "Type accelerators" -Tags "CI" { @{ Accelerator = 'psscriptmethod' Type = [System.Management.Automation.PSScriptMethod] - } + } @{ Accelerator = 'psscriptproperty' Type = [System.Management.Automation.PSScriptProperty] @@ -370,15 +370,19 @@ Describe "Type accelerators" -Tags "CI" { Accelerator = 'psvariableproperty' Type = [System.Management.Automation.PSVariableProperty] } + @{ + Accelerator = 'pspropertyexpression' + Type = [Microsoft.PowerShell.Commands.PSPropertyExpression] + } ) - + if ( $IsCoreCLR ) { - $totalAccelerators = 90 + $totalAccelerators = 91 } else { - $totalAccelerators = 94 + $totalAccelerators = 95 $extraFullPSAcceleratorTestCases = @( @{ @@ -413,13 +417,13 @@ Describe "Type accelerators" -Tags "CI" { param($Accelerator, $Type) $TypeAcceleratorsType::Get[$Accelerator] | Should -Be ($Type) } - + It 'Should have a type accelerator for non-dotnet-core type: ' -Skip:$IsCoreCLR -TestCases $extraFullPSAcceleratorTestCases { param($Accelerator, $Type) $TypeAcceleratorsType::Get[$Accelerator] | Should -Be ($Type) } } - + Context 'User Defined Accelerators' { BeforeAll { $TypeAcceleratorsType::Add('userDefinedAcceleratorType', [int]) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/Measure-Object.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/Measure-Object.Tests.ps1 index b676fc52723..b0155a79344 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Measure-Object.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Measure-Object.Tests.ps1 @@ -349,8 +349,290 @@ Describe "Measure-Object DRT basic functionality" -Tags "CI" { } } + It "Measure-Object with ScriptBlock properties should work" { + $result = 1..10 | Measure-Object -Sum -Average -Minimum -Maximum -Property {$_ * 10} + $result.Count | Should -Be 10 + $result.Average | Should -Be 55 + $result.Sum | Should -Be 550 + $result.Minimum | Should -Be 10 + $result.Maximum | Should -Be 100 + $result.Property | Should -Be '$_ * 10' + } + + It "Measure-Object with ScriptBlock properties should work with -word" { + $result = "a,b,c" | Measure-Object -Word {$_ -split ','} + $result.Words | Should -Be 3 + } + + It "Measure-Object ScriptBlock properties should be able to transform input" { + $map = @{ one = 1; two = 2; three = 3 } + $result = "one", "two", "three" | Measure-Object -Sum {$map[$_]} + $result.Sum | Should -Be 6 + } + + It "Measure-Object should handle hashtables as objects" { + $htables = @{foo = 1}, @{foo = 3}, @{foo = 10} + $result = $htables | Measure-Object -Sum fo* + $result.Sum | Should -Be 14 + } + + It "Measure-Object should handle hashtables as objects with ScriptBlock properties" { + $htables = @{foo = 1}, @{foo = 3}, @{foo = 10} + $result = $htables | Measure-Object -Sum {$_.foo * 10 } + $result.Sum | Should -Be 140 + } + + # + # Since PSPropertyExtression is now a public type, this function is used to test its + # operation as a parameter on a PowerShell function, independent of Measure-Object + # + function Test-PSPropertyExpression { + [CmdletBinding()] + param ( + [Parameter(Mandatory,Position=0)] + [PSPropertyExpression] + $pe, + [Parameter(ValueFromPipeline)] + $InputObject + ) + begin { $sum = 0} + process { $sum += $pe.GetValues($InputObject).result } + end { $sum } + } + + It "Test-PropertyExpression function with a wildcard property expression should sum numbers" { + $result = (1..10).foreach{@{value = $_}} | Test-PSPropertyExpression val* + $result | Should -Be 55 + } + + It "Test-PropertyExpression function with a scriptblock property expression should sum numbers" { + $result = 1..10 | Test-PSPropertyExpression {$_} + $result | Should -Be 55 + } + + It "Test-PropertyExpression function with a scriptblock property expression should be able to transform input" { + # Count the number of 'e's in the words. + $result = "one", "two", "three", "four", "five" | Test-PSPropertyExpression {($_.ToCharArray() -match 'e').Count} + $result | Should -Be 4 + } + It "Measure-Object with multiple lines should work"{ + $result = "123`n4" | Measure-Object -Line + $result.Lines | Should -Be 2 + } + + It "Measure-Object with ScriptBlock properties should work" { + $result = 1..10 | Measure-Object -Sum -Average -Minimum -Maximum -Property {$_ * 10} + $result.Count | Should -Be 10 + $result.Average | Should -Be 55 + $result.Sum | Should -Be 550 + $result.Minimum | Should -Be 10 + $result.Maximum | Should -Be 100 + $result.Property | Should -Be '$_ * 10' + } + + It "Measure-Object with ScriptBlock properties should work with -word" { + $result = "a,b,c", "d,e" | Measure-Object -Word {$_ -split ','} + $result.Words | Should -Be 5 + } + + It "Measure-Object ScriptBlock properties should be able to transform input" { + $map = @{ one = 1; two = 2; three = 3 } + $result = "one", "two", "three" | Measure-Object -Sum {$map[$_]} + $result.Sum | Should -Be 6 + } + + It "Measure-Object should handle hashtables as objects" { + $htables = @{foo = 1}, @{foo = 3}, @{foo = 10} + $result = $htables | Measure-Object -Sum fo* + $result.Sum | Should -Be 14 + } + + It "Measure-Object should handle hashtables as objects with ScriptBlock properties" { + $htables = @{foo = 1}, @{foo = 3}, @{foo = 10} + $result = $htables | Measure-Object -Sum {$_.foo * 10 } + $result.Sum | Should -Be 140 + } +} + +# Since PSPropertyExpression is now a public type, it can be tested +# directly, independent of the Measure-Object cmdlet +Describe "Directly test the PSPropertyExpression type" -Tags "CI" { + # this function is used to test the use of PSPropertyExpression + # as a parameter in script, + function Test-PSPropertyExpression { + [CmdletBinding()] + param ( + [Parameter(Mandatory,Position=0)] + [PSPropertyExpression] + $pe, + [Parameter(ValueFromPipeline)] + $InputObject + ) + begin { $sum = 0} + process { $sum += $pe.GetValues($InputObject).result } + end { $sum } + } + + It "Test-PropertyExpression function with a wildcard property expression should sum numbers" { + $result = (1..10).foreach{@{value = $_}} | Test-PSPropertyExpression val* + $result | Should -Be 55 + } + + It "Test-PropertyExpression function with a scriptblock property expression should sum numbers" { + $result = 1..10 | Test-PSPropertyExpression {$_} + $result | Should -Be 55 + } + + It "Test-PropertyExpression function with a scriptblock property expression should be able to transform input" { + # Count the number of 'e's in the words. + $result = "one", "two", "three", "four", "five" | Test-PSPropertyExpression {($_.ToCharArray() -match 'e').Count} + $result | Should -Be 4 + } + It "Measure-Object with multiple lines should work"{ $result = "123`n4" | Measure-Object -Line $result.Lines | Should -Be 2 } + + It "Measure-Object with ScriptBlock properties should work" { + $result = 1..10 | Measure-Object -Sum -Average -Minimum -Maximum -Property {$_ * 10} + $result.Count | Should -Be 10 + $result.Average | Should -Be 55 + $result.Sum | Should -Be 550 + $result.Minimum | Should -Be 10 + $result.Maximum | Should -Be 100 + $result.Property | Should -Be '$_ * 10' + } + + It "Measure-Object with ScriptBlock properties should work with -word" { + $result = "a,b,c", "d,e" | Measure-Object -Word {$_ -split ','} + $result.Words | Should -Be 5 + } + + It "Measure-Object ScriptBlock properties should be able to transform input" { + $map = @{ one = 1; two = 2; three = 3 } + $result = "one", "two", "three" | Measure-Object -Sum {$map[$_]} + $result.Sum | Should -Be 6 + } + + It "Measure-Object should handle hashtables as objects" { + $htables = @{foo = 1}, @{foo = 3}, @{foo = 10} + $result = $htables | Measure-Object -Sum fo* + $result.Sum | Should -Be 14 + } + + It "Measure-Object should handle hashtables as objects with ScriptBlock properties" { + $htables = @{foo = 1}, @{foo = 3}, @{foo = 10} + $result = $htables | Measure-Object -Sum {$_.foo * 10 } + $result.Sum | Should -Be 140 + } +} + +# Since PSPropertyExpression is now a public type, it can be tested +# directly, independent of the Measure-Object cmdlet +Describe "Directly test the PSPropertyExpression type" -Tags "CI" { + # this function is used to test the use of PSPropertyExpression + # as a parameter in script, + function Test-PSPropertyExpression { + [CmdletBinding()] + param ( + [Parameter(Mandatory,Position=0)] + [PSPropertyExpression] + $pe, + [Parameter(ValueFromPipeline)] + $InputObject + ) + begin { $sum = 0} + process { $sum += $pe.GetValues($InputObject).result } + end { $sum } + } + + It "Test-PropertyExpression function with a wildcard property expression should sum numbers" { + $result = (1..10).foreach{@{value = $_}} | Test-PSPropertyExpression val* + $result | Should -Be 55 + } + + It "Test-PropertyExpression function with a scriptblock property expression should sum numbers" { + $result = 1..10 | Test-PSPropertyExpression {$_} + $result | Should -Be 55 + } + + It "Test-PropertyExpression function with a scriptblock property expression should be able to transform input" { + # Count the number of 'e's in the words. + $result = "one", "two", "three", "four", "five" | Test-PSPropertyExpression {($_.ToCharArray() -match 'e').Count} + $result | Should -Be 4 + } + It "Measure-Object with multiple lines should work"{ + $result = "123`n4" | Measure-Object -Line + $result.Lines | Should -Be 2 + } + + It "Measure-Object with ScriptBlock properties should work" { + $result = 1..10 | Measure-Object -Sum -Average -Minimum -Maximum -Property {$_ * 10} + $result.Count | Should -Be 10 + $result.Average | Should -Be 55 + $result.Sum | Should -Be 550 + $result.Minimum | Should -Be 10 + $result.Maximum | Should -Be 100 + $result.Property | Should -Be '$_ * 10' + } + + It "Measure-Object with ScriptBlock properties should work with -word" { + $result = "a,b,c", "d,e" | Measure-Object -Word {$_ -split ','} + $result.Words | Should -Be 5 + } + + It "Measure-Object ScriptBlock properties should be able to transform input" { + $map = @{ one = 1; two = 2; three = 3 } + $result = "one", "two", "three" | Measure-Object -Sum {$map[$_]} + $result.Sum | Should -Be 6 + } + + It "Measure-Object should handle hashtables as objects" { + $htables = @{foo = 1}, @{foo = 3}, @{foo = 10} + $result = $htables | Measure-Object -Sum fo* + $result.Sum | Should -Be 14 + } + + It "Measure-Object should handle hashtables as objects with ScriptBlock properties" { + $htables = @{foo = 1}, @{foo = 3}, @{foo = 10} + $result = $htables | Measure-Object -Sum {$_.foo * 10 } + $result.Sum | Should -Be 140 + } +} + +# Since PSPropertyExpression is now a public type, it can be tested +# directly, independent of the Measure-Object cmdlet +Describe "Directly test the PSPropertyExpression type" -Tags "CI" { + # this function is used to test the use of PSPropertyExpression + # as a parameter in script, + function Test-PSPropertyExpression { + [CmdletBinding()] + param ( + [Parameter(Mandatory,Position=0)] + [PSPropertyExpression] + $pe, + [Parameter(ValueFromPipeline)] + $InputObject + ) + begin { $sum = 0} + process { $sum += $pe.GetValues($InputObject).result } + end { $sum } + } + + It "Test-PropertyExpression function with a wildcard property expression should sum numbers" { + $result = (1..10).foreach{@{value = $_}} | Test-PSPropertyExpression val* + $result | Should -Be 55 + } + + It "Test-PropertyExpression function with a scriptblock property expression should sum numbers" { + $result = 1..10 | Test-PSPropertyExpression {$_} + $result | Should -Be 55 + } + + It "Test-PropertyExpression function with a scriptblock property expression should be able to transform input" { + # Count the number of 'e's in the words. + $result = "one", "two", "three", "four", "five" | Test-PSPropertyExpression {($_.ToCharArray() -match 'e').Count} + $result | Should -Be 4 + } }