Repository navigation
Expand file tree
/
Copy pathtest_ProgressNode.cs
More file actions
331 lines (275 loc) · 14.1 KB
/
Copy pathtest_ProgressNode.cs
File metadata and controls
331 lines (275 loc) · 14.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System.Collections;
using System.Management.Automation;
using Microsoft.PowerShell;
using Xunit;
namespace PSTests.Parallel
{
/// <summary>
/// Tests for ProgressNode rendering with double-width unicode characters.
/// These tests verify the fix for Issue #21293 where progress bars with
/// double-width characters (Japanese, Chinese, Korean, emoji) exceeded maxWidth.
/// </summary>
public static class ProgressNodeTests
{
/// <summary>
/// Verify Issue #21293 scenario - the original bug report.
/// This test reproduces the exact scenario from the issue.
/// Tests with edge cases around standard terminal width (80 columns).
/// </summary>
/// <param name="maxWidth">The maximum width in buffer cells for the progress bar rendering.</param>
[Theory]
[InlineData(79)]
[InlineData(80)]
[InlineData(81)]
public static void ProgressBar_OriginalBugScenario_MustNotExceedMaxWidth(int maxWidth)
{
var rawUI = new TestProgressRawUI();
var record = new ProgressRecord(0, "My Status", "1/6 次の段階");
var node = new ProgressNode(1, record);
node.PercentComplete = 50;
node.SecondsRemaining = 120;
node.Style = ProgressNode.RenderStyle.Ansi;
var strCollection = new ArrayList();
node.Render(strCollection, 0, maxWidth, rawUI);
var output = strCollection[0] as string;
int actualWidth = ConsoleControl.LengthInBufferCells(output, 0, checkEscapeSequences: true);
Assert.True(
actualWidth <= maxWidth,
$"Issue #21293 (width {maxWidth}): Progress bar ({actualWidth} cells) exceeds maxWidth ({maxWidth}). Output: {output}");
}
/// <summary>
/// Verify that progress bar respects maxWidth for Japanese text.
/// </summary>
/// <param name="maxWidth">The maximum width in buffer cells for the progress bar rendering.</param>
[Theory]
[InlineData(79)]
[InlineData(80)]
[InlineData(81)]
public static void ProgressBar_WithDoubleWidthChars_MustNotExceedMaxWidth(int maxWidth)
{
var rawUI = new TestProgressRawUI();
var record = new ProgressRecord(0, "Activity", "日本語テキスト");
var node = new ProgressNode(1, record);
node.PercentComplete = 50;
node.Style = ProgressNode.RenderStyle.Ansi;
var strCollection = new ArrayList();
node.Render(strCollection, 0, maxWidth, rawUI);
var output = strCollection[0] as string;
int actualWidth = ConsoleControl.LengthInBufferCells(output, 0, checkEscapeSequences: true);
Assert.True(
actualWidth <= maxWidth,
$"Progress bar width {maxWidth}: ({actualWidth}) exceeds maxWidth ({maxWidth}). Output: {output}");
}
/// <summary>
/// Verify emoji handling with sufficient text length.
/// </summary>
/// <param name="maxWidth">The maximum width in buffer cells for the progress bar rendering.</param>
[Theory]
[InlineData(79)]
[InlineData(80)]
[InlineData(81)]
public static void ProgressBar_WithEmoji_MustNotExceedMaxWidth(int maxWidth)
{
var rawUI = new TestProgressRawUI();
var record = new ProgressRecord(0, "Upload", "📁ファイル転送中🔄処理中📊");
var node = new ProgressNode(1, record);
node.PercentComplete = 50;
node.Style = ProgressNode.RenderStyle.Ansi;
var strCollection = new ArrayList();
node.Render(strCollection, 0, maxWidth, rawUI);
var output = strCollection[0] as string;
int actualWidth = ConsoleControl.LengthInBufferCells(output, 0, checkEscapeSequences: true);
Assert.True(
actualWidth <= maxWidth,
$"Emoji progress bar width {maxWidth}: ({actualWidth} cells) exceeds maxWidth ({maxWidth}). Output: {output}");
}
/// <summary>
/// Test various lengths of double-width text.
/// All test cases have sufficient length to detect the bug.
/// </summary>
/// <param name="statusText">The status message displayed in the progress bar.</param>
/// <param name="maxWidth">The maximum width in buffer cells for the progress bar rendering.</param>
[Theory]
[InlineData("あいうえお", 79)]
[InlineData("あいうえお", 80)]
[InlineData("あいうえお", 81)]
[InlineData("データ処理中", 79)]
[InlineData("データ処理中", 80)]
[InlineData("データ処理中", 81)]
[InlineData("ファイルをアップロード中", 79)]
[InlineData("ファイルをアップロード中", 80)]
[InlineData("ファイルをアップロード中", 81)]
public static void ProgressBar_VariousDoubleWidthLengths_MustNotExceedMaxWidth(string statusText, int maxWidth)
{
var rawUI = new TestProgressRawUI();
var record = new ProgressRecord(0, "Test", statusText);
var node = new ProgressNode(1, record);
node.PercentComplete = 50;
node.Style = ProgressNode.RenderStyle.Ansi;
var strCollection = new ArrayList();
node.Render(strCollection, 0, maxWidth, rawUI);
var output = strCollection[0] as string;
int actualWidth = ConsoleControl.LengthInBufferCells(output, 0, checkEscapeSequences: true);
Assert.True(
actualWidth <= maxWidth,
$"Status '{statusText}' width {maxWidth}: resulted in width {actualWidth} exceeding maxWidth {maxWidth}. Output: {output}");
}
/// <summary>
/// Verify mixed ASCII and double-width characters.
/// </summary>
/// <param name="maxWidth">The maximum width in buffer cells for the progress bar rendering.</param>
[Theory]
[InlineData(79)]
[InlineData(80)]
[InlineData(81)]
public static void ProgressBar_MixedWidthText_MustNotExceedMaxWidth(int maxWidth)
{
var rawUI = new TestProgressRawUI();
var record = new ProgressRecord(0, "Process", "File_日本語_document.txt");
var node = new ProgressNode(1, record);
node.PercentComplete = 50;
node.Style = ProgressNode.RenderStyle.Ansi;
var strCollection = new ArrayList();
node.Render(strCollection, 0, maxWidth, rawUI);
var output = strCollection[0] as string;
int actualWidth = ConsoleControl.LengthInBufferCells(output, 0, checkEscapeSequences: true);
Assert.True(
actualWidth <= maxWidth,
$"Mixed-width text width {maxWidth}: resulted in width {actualWidth} exceeding maxWidth {maxWidth}. Output: {output}");
}
/// <summary>
/// Verify long double-width string handling and truncation.
/// </summary>
/// <param name="maxWidth">The maximum width in buffer cells for the progress bar rendering.</param>
[Theory]
[InlineData(79)]
[InlineData(80)]
[InlineData(81)]
public static void ProgressBar_LongDoubleWidthString_MustNotExceedMaxWidth(int maxWidth)
{
var rawUI = new TestProgressRawUI();
var record = new ProgressRecord(0, "処理", "ファイルを処理中です今しばらくお待ちください");
var node = new ProgressNode(1, record);
node.PercentComplete = 50;
node.Style = ProgressNode.RenderStyle.Ansi;
var strCollection = new ArrayList();
node.Render(strCollection, 0, maxWidth, rawUI);
var output = strCollection[0] as string;
int actualWidth = ConsoleControl.LengthInBufferCells(output, 0, checkEscapeSequences: true);
Assert.True(
actualWidth <= maxWidth,
$"Long double-width text width {maxWidth}: resulted in width {actualWidth} exceeding maxWidth {maxWidth}. Output: {output}");
}
/// <summary>
/// Verify that truncation respects character boundaries.
/// </summary>
/// <param name="maxWidth">The maximum width in buffer cells for the progress bar rendering.</param>
[Theory]
[InlineData(79)]
[InlineData(80)]
[InlineData(81)]
public static void ProgressBar_Truncation_MustRespectCharacterBoundaries(int maxWidth)
{
var rawUI = new TestProgressRawUI();
var record = new ProgressRecord(0, "Downloading", "ファイル処理中123456789012345678901234567890");
var node = new ProgressNode(1, record);
node.PercentComplete = 50;
node.Style = ProgressNode.RenderStyle.Ansi;
var strCollection = new ArrayList();
node.Render(strCollection, 0, maxWidth, rawUI);
var output = strCollection[0] as string;
int actualWidth = ConsoleControl.LengthInBufferCells(output, 0, checkEscapeSequences: true);
Assert.True(
actualWidth <= maxWidth,
$"Truncated progress bar width {maxWidth}: ({actualWidth} cells) exceeds maxWidth ({maxWidth}). Output: {output}");
}
/// <summary>
/// Test for Issue: Double closing bracket when truncating with double-width characters.
/// Verifies that statusPartDisplayWidth is calculated from actual statusPart length,
/// not from estimated value, to prevent progress bar from exceeding maxWidth.
/// </summary>
/// <param name="maxWidth">The maximum width in buffer cells for the progress bar rendering.</param>
[Theory]
[InlineData(79)]
[InlineData(80)]
[InlineData(81)]
public static void ProgressBar_LongStatusWithDoubleWidth_MustNotExceedMaxWidth(int maxWidth)
{
var rawUI = new TestProgressRawUI();
// Very long status with emojis and mixed width characters that will be truncated
var record = new ProgressRecord(0, "Long Text Test", "🚀🎯⚡💾✅ 絵文字も含めた超長文テストケース with emojis and very long text to test the truncation feature properly");
var node = new ProgressNode(1, record);
node.PercentComplete = 100;
node.Style = ProgressNode.RenderStyle.Ansi;
var strCollection = new ArrayList();
node.Render(strCollection, 0, maxWidth, rawUI);
var output = strCollection[0] as string;
int actualWidth = ConsoleControl.LengthInBufferCells(output, 0, checkEscapeSequences: true);
// The progress bar must not exceed maxWidth
Assert.True(
actualWidth <= maxWidth,
$"Progress bar with long truncated status width {maxWidth}: ({actualWidth} cells) exceeds maxWidth ({maxWidth}). Output: {output}");
// Verify no double closing brackets - count ']' occurrences at end
int closingBracketCount = 0;
for (int i = output.Length - 1; i >= 0 && output[i] == ']'; i--)
{
closingBracketCount++;
}
Assert.True(
closingBracketCount <= 1,
$"Found {closingBracketCount} consecutive closing brackets at end (width {maxWidth}), expected 1. Output: {output}");
}
/// <summary>
/// Test activityDisplayWidth bug with small maxWidth to amplify error.
/// Bug: After truncation, activityDisplayWidth is set to maxWidth/2 but actual width differs.
/// </summary>
/// <param name="maxWidth">The maximum width in buffer cells for the progress bar rendering.</param>
[Theory]
[InlineData(79)]
[InlineData(80)]
[InlineData(81)]
public static void ExtremeTest_ActivityWidthBug_SmallMaxWidth(int maxWidth)
{
var rawUI = new TestProgressRawUI();
string longActivity = "日本語日本語日本語日本語日本語"; // 20 cells
var record = new ProgressRecord(0, longActivity, "St");
var node = new ProgressNode(1, record);
node.PercentComplete = 50;
node.SecondsRemaining = 30;
node.Style = ProgressNode.RenderStyle.Ansi;
var strCollection = new ArrayList();
node.Render(strCollection, 0, maxWidth, rawUI);
var output = strCollection[0] as string;
int actualWidth = ConsoleControl.LengthInBufferCells(output, 0, checkEscapeSequences: true);
Assert.True(
actualWidth <= maxWidth,
$"maxWidth {maxWidth} test: width {actualWidth} exceeds {maxWidth}. Output: [{output}]");
}
/// <summary>
/// Test surrogate pair handling with strategically positioned emojis.
/// Bug: Substring(i,1) splits surrogate pairs causing width calculation errors.
/// </summary>
/// <param name="maxWidth">The maximum width in buffer cells for the progress bar rendering.</param>
[Theory]
[InlineData(79)]
[InlineData(80)]
[InlineData(81)]
public static void ExtremeTest_SurrogatePairAtVTBoundary(int maxWidth)
{
var rawUI = new TestProgressRawUI();
var record = new ProgressRecord(0, "A", "X🚀Y🚀Z🚀");
var node = new ProgressNode(1, record);
node.PercentComplete = 33;
node.Style = ProgressNode.RenderStyle.Ansi;
var strCollection = new ArrayList();
node.Render(strCollection, 0, maxWidth, rawUI);
var output = strCollection[0] as string;
int actualWidth = ConsoleControl.LengthInBufferCells(output, 0, checkEscapeSequences: true);
Assert.True(
actualWidth <= maxWidth,
$"Surrogate pair test width {maxWidth}: width {actualWidth} exceeds {maxWidth}. Output: [{output}]");
}
}
}