Skip to content

Commit 624fe30

Browse files
authored
Display DictionaryEntry as key/value pairs in debugger (#1680)
Dictionaries should be referenced by stringified key, not by index.
1 parent a4e68a1 commit 624fe30

File tree

2 files changed

+9
-18
lines changed

2 files changed

+9
-18
lines changed

src/PowerShellEditorServices/Services/DebugAdapter/Debugging/VariableDetails.cs

+2-7
Original file line numberDiff line numberDiff line change
@@ -191,11 +191,7 @@ private static string GetValueStringAndType(object value, bool isExpandable, out
191191
{
192192
// For DictionaryEntry - display the key/value as the value.
193193
var entry = (DictionaryEntry)value;
194-
valueString =
195-
string.Format(
196-
"[{0}, {1}]",
197-
entry.Key,
198-
GetValueStringAndType(entry.Value, GetIsExpandable(entry.Value), out typeName));
194+
valueString = GetValueStringAndType(entry.Value, GetIsExpandable(entry.Value), out typeName);
199195
}
200196
else
201197
{
@@ -328,12 +324,11 @@ private VariableDetails[] GetChildren(object obj, ILogger logger)
328324
// function that defines parameters and has been passed parameters.
329325
// If you open the $PSBoundParameters variable node in this scenario and see nothing,
330326
// this code is broken.
331-
int i = 0;
332327
foreach (DictionaryEntry entry in dictionary)
333328
{
334329
childVariables.Add(
335330
new VariableDetails(
336-
"[" + i++ + "]",
331+
"[" + entry.Key + "]",
337332
entry));
338333
}
339334
}

test/PowerShellEditorServices.Test/Debugging/DebugServiceTests.cs

+7-11
Original file line numberDiff line numberDiff line change
@@ -719,19 +719,15 @@ await debugService.SetLineBreakpointsAsync(
719719
VariableDetailsBase[] childVars = debugService.GetVariables(var.Id);
720720
// 2 variables plus "Raw View"
721721
Assert.Equal(3, childVars.Length);
722-
Assert.Equal("[0]", childVars[0].Name);
723-
Assert.Equal("[1]", childVars[1].Name);
724722

725-
var childVarStrs = new HashSet<string>(childVars.Select(v => v.ValueString));
726-
var expectedVars = new[] {
727-
"[firstChild, \"Child\"]",
728-
"[secondChild, 42]"
729-
};
723+
// Hashtables are unordered hence the Linq examination, examination by index is unreliable
724+
VariableDetailsBase firstChild = Array.Find(childVars, v => v.Name == "[firstChild]");
725+
Assert.NotNull(firstChild);
726+
Assert.Equal("\"Child\"", firstChild.ValueString);
730727

731-
foreach (string expectedVar in expectedVars)
732-
{
733-
Assert.Contains(expectedVar, childVarStrs);
734-
}
728+
VariableDetailsBase secondChild = Array.Find(childVars, v => v.Name == "[secondChild]");
729+
Assert.NotNull(secondChild);
730+
Assert.Equal("42", secondChild.ValueString);
735731
}
736732

737733
[Fact]

0 commit comments

Comments
 (0)