From a0acfc9a42b3df4c2242c5a61335f7307356087e Mon Sep 17 00:00:00 2001 From: Justin Grote Date: Mon, 24 Jan 2022 18:22:18 -0800 Subject: [PATCH 1/2] Variables: Dictionaries should be referenced by stringified key, not by index Fixes #1679 --- .../Services/DebugAdapter/Debugging/VariableDetails.cs | 9 ++------- .../Debugging/DebugServiceTests.cs | 8 ++++---- 2 files changed, 6 insertions(+), 11 deletions(-) diff --git a/src/PowerShellEditorServices/Services/DebugAdapter/Debugging/VariableDetails.cs b/src/PowerShellEditorServices/Services/DebugAdapter/Debugging/VariableDetails.cs index 739885883..cf68689d8 100644 --- a/src/PowerShellEditorServices/Services/DebugAdapter/Debugging/VariableDetails.cs +++ b/src/PowerShellEditorServices/Services/DebugAdapter/Debugging/VariableDetails.cs @@ -191,11 +191,7 @@ private static string GetValueStringAndType(object value, bool isExpandable, out { // For DictionaryEntry - display the key/value as the value. var entry = (DictionaryEntry)value; - valueString = - string.Format( - "[{0}, {1}]", - entry.Key, - GetValueStringAndType(entry.Value, GetIsExpandable(entry.Value), out typeName)); + valueString = GetValueStringAndType(entry.Value, GetIsExpandable(entry.Value), out typeName); } else { @@ -328,12 +324,11 @@ private VariableDetails[] GetChildren(object obj, ILogger logger) // function that defines parameters and has been passed parameters. // If you open the $PSBoundParameters variable node in this scenario and see nothing, // this code is broken. - int i = 0; foreach (DictionaryEntry entry in dictionary) { childVariables.Add( new VariableDetails( - "[" + i++ + "]", + "[" + entry.Key + "]", entry)); } } diff --git a/test/PowerShellEditorServices.Test/Debugging/DebugServiceTests.cs b/test/PowerShellEditorServices.Test/Debugging/DebugServiceTests.cs index d8507afba..5b610cc6e 100644 --- a/test/PowerShellEditorServices.Test/Debugging/DebugServiceTests.cs +++ b/test/PowerShellEditorServices.Test/Debugging/DebugServiceTests.cs @@ -719,13 +719,13 @@ await debugService.SetLineBreakpointsAsync( VariableDetailsBase[] childVars = debugService.GetVariables(var.Id); // 2 variables plus "Raw View" Assert.Equal(3, childVars.Length); - Assert.Equal("[0]", childVars[0].Name); - Assert.Equal("[1]", childVars[1].Name); + Assert.Equal("[firstChild]", childVars[0].Name); + Assert.Equal("[secondChild]", childVars[1].Name); var childVarStrs = new HashSet(childVars.Select(v => v.ValueString)); var expectedVars = new[] { - "[firstChild, \"Child\"]", - "[secondChild, 42]" + "\"Child\"", + "42" }; foreach (string expectedVar in expectedVars) From 0176fe19c4a529d4fc5853ee544c3b611b64cdf9 Mon Sep 17 00:00:00 2001 From: Justin Grote Date: Mon, 24 Jan 2022 19:06:01 -0800 Subject: [PATCH 2/2] Fix flaky test because hashtables are unordered --- .../Debugging/DebugServiceTests.cs | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/test/PowerShellEditorServices.Test/Debugging/DebugServiceTests.cs b/test/PowerShellEditorServices.Test/Debugging/DebugServiceTests.cs index 5b610cc6e..00996db96 100644 --- a/test/PowerShellEditorServices.Test/Debugging/DebugServiceTests.cs +++ b/test/PowerShellEditorServices.Test/Debugging/DebugServiceTests.cs @@ -719,19 +719,15 @@ await debugService.SetLineBreakpointsAsync( VariableDetailsBase[] childVars = debugService.GetVariables(var.Id); // 2 variables plus "Raw View" Assert.Equal(3, childVars.Length); - Assert.Equal("[firstChild]", childVars[0].Name); - Assert.Equal("[secondChild]", childVars[1].Name); - var childVarStrs = new HashSet(childVars.Select(v => v.ValueString)); - var expectedVars = new[] { - "\"Child\"", - "42" - }; + // Hashtables are unordered hence the Linq examination, examination by index is unreliable + VariableDetailsBase firstChild = Array.Find(childVars, v => v.Name == "[firstChild]"); + Assert.NotNull(firstChild); + Assert.Equal("\"Child\"", firstChild.ValueString); - foreach (string expectedVar in expectedVars) - { - Assert.Contains(expectedVar, childVarStrs); - } + VariableDetailsBase secondChild = Array.Find(childVars, v => v.Name == "[secondChild]"); + Assert.NotNull(secondChild); + Assert.Equal("42", secondChild.ValueString); } [Fact]