Skip to content

Commit 87ccd0a

Browse files
powercodeiSazonov
authored andcommitted
Only display properties with values for MeasureInfo (#7104)
* Adding support for built-in List formats to include ItemSelectionCondition * Adding ItemSelectionCondition to GenericMeasureInfo * Adding tests for MeasureInfo format-list * Adding positive tests
1 parent 1bfe96d commit 87ccd0a

File tree

5 files changed

+101
-29
lines changed

5 files changed

+101
-29
lines changed

src/System.Management.Automation/FormatAndOutput/DefaultFormatters/PowerShellCore_format_ps1xml.cs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,14 @@ internal static IEnumerable<ExtendedTypeDefinition> GetFormatData()
191191
"System.Management.Automation.Job",
192192
ViewsOf_System_Management_Automation_Job());
193193

194+
yield return new ExtendedTypeDefinition(
195+
"Microsoft.PowerShell.Commands.TextMeasureInfo",
196+
ViewsOf_Deserialized_Microsoft_PowerShell_Commands_TextMeasureInfo());
197+
198+
yield return new ExtendedTypeDefinition(
199+
"Microsoft.PowerShell.Commands.GenericMeasureInfo",
200+
ViewsOf_Deserialized_Microsoft_PowerShell_Commands_GenericMeasureInfo());
201+
194202
yield return new ExtendedTypeDefinition(
195203
"Deserialized.Microsoft.PowerShell.Commands.TextMeasureInfo",
196204
ViewsOf_Deserialized_Microsoft_PowerShell_Commands_TextMeasureInfo());
@@ -1070,11 +1078,11 @@ private static IEnumerable<FormatViewDefinition> ViewsOf_Deserialized_Microsoft_
10701078
ListControl.Create()
10711079
.StartEntry()
10721080
.AddItemProperty(@"Count")
1073-
.AddItemProperty(@"Average")
1074-
.AddItemProperty(@"Sum")
1075-
.AddItemProperty(@"Maximum")
1076-
.AddItemProperty(@"Minimum")
1077-
.AddItemProperty(@"Property")
1081+
.AddItemPropertyIfSet(@"Average")
1082+
.AddItemPropertyIfSet(@"Sum")
1083+
.AddItemPropertyIfSet(@"Maximum")
1084+
.AddItemPropertyIfSet(@"Minimum")
1085+
.AddItemPropertyIfSet(@"Property")
10781086
.EndEntry()
10791087
.EndList());
10801088
}

src/System.Management.Automation/FormatAndOutput/common/DisplayDatabase/displayDescriptionData.cs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -788,7 +788,27 @@ public DisplayEntry(string value, DisplayEntryValueType type)
788788
ValueType = type;
789789
}
790790

791-
/// <summary/>
791+
/// <summary>
792+
/// Creates a DisplayEntry for the given scriptblock.
793+
/// </summary>
794+
/// <param name="scriptblock">The content of the scriptblock.</param>
795+
/// <returns>A <see cref="DisplayEntry"/> for the <paramref name="scriptblock"/>.</returns>
796+
public static DisplayEntry CreateScriptBlockEntry(string scriptblock)
797+
{
798+
return new DisplayEntry(scriptblock, DisplayEntryValueType.ScriptBlock);
799+
}
800+
801+
/// <summary>
802+
/// Creates a DisplayEntry for the given property name.
803+
/// </summary>
804+
/// <param name="property">The name of the property.</param>
805+
/// <returns>A <see cref="DisplayEntry"/> for the <paramref name="property"/>.</returns>
806+
public static DisplayEntry CreatePropertyEntry(string property)
807+
{
808+
return new DisplayEntry(property, DisplayEntryValueType.Property);
809+
}
810+
811+
/// <inheritdoc />
792812
public override string ToString()
793813
{
794814
return (ValueType == DisplayEntryValueType.Property ? "property: " : "script: ") + Value;

src/System.Management.Automation/FormatAndOutput/common/DisplayDatabase/displayDescriptionData_List.cs

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -370,31 +370,57 @@ internal ListEntryBuilder(ListControlBuilder listBuilder, ListControlEntry listE
370370
_listEntry = listEntry;
371371
}
372372

373-
private ListEntryBuilder AddItem(string value, string label, DisplayEntryValueType kind, string format)
373+
private ListEntryBuilder AddItem(string value, string label, DisplayEntryValueType kind, string format, DisplayEntry itemSelectionCondition)
374374
{
375375
if (string.IsNullOrEmpty(value))
376+
{
376377
throw PSTraceSource.NewArgumentNullException("property");
378+
}
377379

378380
_listEntry.Items.Add(new ListControlEntryItem
379381
{
380382
DisplayEntry = new DisplayEntry(value, kind),
381383
Label = label,
382-
FormatString = format
384+
FormatString = format,
385+
ItemSelectionCondition = itemSelectionCondition,
383386
});
384387

385388
return this;
386389
}
387390

388-
/// <summary></summary>
389-
public ListEntryBuilder AddItemScriptBlock(string scriptBlock, string label = null, string format = null)
391+
/// <summary>Adds a scriptblock list entry.</summary>
392+
/// <param name="scriptBlock">The content of the scriptblock to add.</param>
393+
/// <param name="label">A label for the entry.</param>
394+
/// <param name="format">A format string for the scriptblock result.</param>
395+
/// <param name="itemSelectionCondition">A condition that has to be met for the entry to be displayed.</param>
396+
/// <returns>A reference to this instance after the append operation has completed.</returns>
397+
public ListEntryBuilder AddItemScriptBlock(string scriptBlock, string label = null, string format = null, DisplayEntry itemSelectionCondition = null)
390398
{
391-
return AddItem(scriptBlock, label, DisplayEntryValueType.ScriptBlock, format);
399+
return AddItem(scriptBlock, label, DisplayEntryValueType.ScriptBlock, format, itemSelectionCondition);
392400
}
393401

394-
/// <summary></summary>
395-
public ListEntryBuilder AddItemProperty(string property, string label = null, string format = null)
402+
/// <summary>Adds a property list entry.</summary>
403+
/// <param name="property">The property to add.</param>
404+
/// <param name="label">A label for the entry.</param>
405+
/// <param name="format">A format string for the property value.</param>
406+
/// <param name="itemSelectionCondition">A condition that has to be met for the entry to be displayed.</param>
407+
/// <returns>A reference to this instance after the append operation has completed.</returns>
408+
public ListEntryBuilder AddItemProperty(string property, string label = null, string format = null, DisplayEntry itemSelectionCondition = null)
409+
{
410+
return AddItem(property, label, DisplayEntryValueType.Property, format, itemSelectionCondition);
411+
}
412+
413+
/// <summary>
414+
/// Adds a property that is displayed if it the property has a value.
415+
/// </summary>
416+
/// <param name="property">The property to add.</param>
417+
/// <param name="label">A label for the entry.</param>
418+
/// <param name="format">A format string for the property value.</param>
419+
/// <returns>A reference to this instance after the append operation has completed.</returns>
420+
public ListEntryBuilder AddItemPropertyIfSet(string property, string label = null, string format = null)
396421
{
397-
return AddItem(property, label, DisplayEntryValueType.Property, format);
422+
var itemSelectionCondition = DisplayEntry.CreatePropertyEntry(property);
423+
return AddItem(property, label, DisplayEntryValueType.Property, format, itemSelectionCondition);
398424
}
399425

400426
/// <summary></summary>

src/System.Management.Automation/FormatAndOutput/common/DisplayDatabase/typeDataXmlLoader.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -883,6 +883,11 @@ private void LoadListControlItemDefinitionsFromObjectModel(ListControlEntryDefin
883883
fpt.expression = expression;
884884
fpt.fieldFormattingDirective.formatString = listItem.FormatString;
885885
lvid.formatTokenList.Add(fpt);
886+
if (listItem.ItemSelectionCondition != null)
887+
{
888+
var conditionToken = LoadExpressionFromObjectModel(listItem.ItemSelectionCondition, viewIndex, typeName);
889+
lvid.conditionToken = conditionToken;
890+
}
886891
}
887892

888893
if (!String.IsNullOrEmpty(listItem.Label))

test/powershell/Modules/Microsoft.PowerShell.Utility/object.tests.ps1

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ Describe "Object cmdlets" -Tags "CI" {
88
}
99

1010
It "AsString returns a string" {
11-
$processes = Get-Process | Group-Object -Property ProcessName -AsHashTable -AsString
12-
$result = $processes.Keys | ForEach-Object {$_.GetType()}
13-
$result[0].Name | Should -Be "String"
11+
$processes = Get-Process | Group-Object -Property ProcessName -AsHashTable -AsString
12+
$result = $processes.Keys | ForEach-Object {$_.GetType()}
13+
$result[0].Name | Should -Be "String"
1414
}
1515
}
1616

@@ -45,18 +45,18 @@ Describe "Object cmdlets" -Tags "CI" {
4545
$secondObject | Add-Member -NotePropertyName Header -NotePropertyValue $secondValue
4646

4747
$testCases = @(
48-
@{ data = @("abc","ABC","Def"); min = "abc"; max = "Def"},
48+
@{ data = @("abc", "ABC", "Def"); min = "abc"; max = "Def"},
4949
@{ data = @([datetime]::Today, [datetime]::Today.AddDays(-1)); min = ([datetime]::Today.AddDays(-1)).ToString() ; max = [datetime]::Today.ToString() }
50-
@{ data = @(1,2,3,"ABC"); min = 1; max = "ABC"},
51-
@{ data = @(4,2,3,"ABC",1); min = 1; max = "ABC"},
52-
@{ data = @(4,2,3,"ABC",1,"DEF"); min = 1; max = "DEF"},
53-
@{ data = @("111 Test","19"); min = "111 Test"; max = "19"},
50+
@{ data = @(1, 2, 3, "ABC"); min = 1; max = "ABC"},
51+
@{ data = @(4, 2, 3, "ABC", 1); min = 1; max = "ABC"},
52+
@{ data = @(4, 2, 3, "ABC", 1, "DEF"); min = 1; max = "DEF"},
53+
@{ data = @("111 Test", "19"); min = "111 Test"; max = "19"},
5454
@{ data = @("19", "111 Test"); min = "111 Test"; max = "19"},
55-
@{ data = @("111 Test",19); min = "111 Test"; max = 19},
55+
@{ data = @("111 Test", 19); min = "111 Test"; max = 19},
5656
@{ data = @(19, "111 Test"); min = "111 Test"; max = 19},
57-
@{ data = @(100,2,3, "A", 1); min = 1; max = "A"},
58-
@{ data = @(4,2,3, "ABC", 1, "DEF"); min = 1; max = "DEF"},
59-
@{ data = @("abc",[Datetime]::Today,"def"); min = [Datetime]::Today.ToString(); max = "def"}
57+
@{ data = @(100, 2, 3, "A", 1); min = 1; max = "A"},
58+
@{ data = @(4, 2, 3, "ABC", 1, "DEF"); min = 1; max = "DEF"},
59+
@{ data = @("abc", [Datetime]::Today, "def"); min = [Datetime]::Today.ToString(); max = "def"}
6060
)
6161
}
6262

@@ -85,18 +85,31 @@ Describe "Object cmdlets" -Tags "CI" {
8585
}
8686

8787
It 'returns a GenericMeasureInfoObject' {
88-
$gmi = 1,2,3 | measure-object -max -min
88+
$gmi = 1, 2, 3 | measure-object -max -min
8989
$gmi | Should -BeOfType Microsoft.PowerShell.Commands.GenericMeasureInfo
9090
}
9191

9292
It 'should return correct error for non-numeric input' {
93-
$gmi = "abc",[Datetime]::Now | measure -sum -max -ErrorVariable err -ErrorAction silentlycontinue
93+
$gmi = "abc", [Datetime]::Now | measure -sum -max -ErrorVariable err -ErrorAction silentlycontinue
9494
$err | ForEach-Object { $_.FullyQualifiedErrorId | Should -Be 'NonNumericInputObject,Microsoft.PowerShell.Commands.MeasureObjectCommand' }
9595
}
9696

9797
It 'should have the correct count' {
98-
$gmi = "abc",[Datetime]::Now | measure -sum -max -ErrorVariable err -ErrorAction silentlycontinue
98+
$gmi = "abc", [Datetime]::Now | measure -sum -max -ErrorVariable err -ErrorAction silentlycontinue
9999
$gmi.Count | Should -Be 2
100100
}
101+
102+
It 'should only display fields that are set' {
103+
$text = 1, 2, 3 | Measure-Object -Sum -Average | Format-List | Out-String
104+
$text -match "min|max" | Should -BeFalse
105+
$text -match 'Sum' | Should -BeTrue
106+
$text -match 'Average' | Should -BeTrue
107+
108+
$text = 1, 2, 3 | Measure-Object -Minimum -Maximum | Format-List | Out-String
109+
$text -match "min" | Should -BeTrue
110+
$text -match "max" | Should -BeTrue
111+
$text -match 'Average' | Should -BeFalse
112+
$text -match 'Sum' | Should -BeFalse
113+
}
101114
}
102115
}

0 commit comments

Comments
 (0)