-
Notifications
You must be signed in to change notification settings - Fork 323
Expand file tree
/
Copy pathResizingTest.cs
More file actions
186 lines (163 loc) · 6.64 KB
/
ResizingTest.cs
File metadata and controls
186 lines (163 loc) · 6.64 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
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using Microsoft.PowerShell;
using Newtonsoft.Json;
using Xunit;
namespace Test.Resizing
{
#pragma warning disable 0649
/// <summary>
/// This class is initialized by JSON deserialization.
/// </summary>
internal sealed class LogicalToPhysicalLineTestData
{
public string Name;
public string Line;
public bool IsFirstLogicalLine;
public List<LogicalToPhysicalLineTestContext> Context;
}
/// <summary>
/// This class is initialized by JSON deserialization.
/// </summary>
internal sealed class LogicalToPhysicalLineTestContext
{
public int BufferWidth;
public int InitialX;
public int LineCount;
public int LastLineLen;
}
/// <summary>
/// This class is initialized by JSON deserialization.
/// </summary>
internal sealed class ResizingTestData
{
public string Name;
public List<string> Lines;
public int OldBufferWidth;
public int NewBufferWidth;
public List<ResizingTestContext> Context;
}
/// <summary>
/// This class is initialized by JSON deserialization.
/// </summary>
internal sealed class ResizingTestContext
{
public Point OldInitial;
public Point OldCursor;
public Point NewInitial;
public Point NewCursor;
public RenderOffset Offset;
internal sealed class RenderOffset
{
public int LineIndex;
public int CharIndex;
}
}
#pragma warning restore 0649
}
namespace Test
{
using Test.Resizing;
public partial class ReadLine
{
private static List<ResizingTestData> s_resizingTestData;
private void InitializeTestData()
{
if (s_resizingTestData is null)
{
string path = Path.Combine("assets", "resizing", "renderdata-to-cursor-point.json");
string text = File.ReadAllText(path);
s_resizingTestData = JsonConvert.DeserializeObject<List<ResizingTestData>>(text);
}
}
private PSConsoleReadLine GetPSConsoleReadLineSingleton()
{
return (PSConsoleReadLine)typeof(PSConsoleReadLine)
.GetField("_singleton", BindingFlags.Static | BindingFlags.NonPublic).GetValue(null);
}
[Fact]
public void ConvertPointToRenderDataOffset_ShouldWork()
{
InitializeTestData();
PSConsoleReadLine instance = GetPSConsoleReadLineSingleton();
foreach (ResizingTestData test in s_resizingTestData)
{
RenderData renderData = new()
{
lines = new RenderedLineData[test.Lines.Count],
bufferWidth = test.OldBufferWidth
};
for (int i = 0; i < test.Lines.Count; i++)
{
renderData.lines[i] = new RenderedLineData(test.Lines[i], isFirstLogicalLine: i == 0);
}
for (int j = 0; j < test.Context.Count; j++)
{
ResizingTestContext context = test.Context[j];
renderData.cursorLeft = context.OldCursor.X;
renderData.cursorTop = context.OldCursor.Y;
RenderDataOffset offset = instance.ConvertPointToRenderDataOffset(context.OldInitial.X, context.OldInitial.Y, renderData);
Assert.True(
context.Offset.LineIndex == offset.LogicalLineIndex &&
context.Offset.CharIndex == offset.VisibleCharIndex,
$"{test.Name}-context_{j}: calculated offset is not what's expected [line: {offset.LogicalLineIndex}, char: {offset.VisibleCharIndex}]");
}
}
}
[Fact]
public void ConvertRenderDataOffsetToPoint_ShouldWork()
{
InitializeTestData();
PSConsoleReadLine instance = GetPSConsoleReadLineSingleton();
foreach (ResizingTestData test in s_resizingTestData)
{
RenderData renderData = new()
{
lines = new RenderedLineData[test.Lines.Count],
bufferWidth = test.OldBufferWidth
};
for (int i = 0; i < test.Lines.Count; i++)
{
renderData.lines[i] = new RenderedLineData(test.Lines[i], isFirstLogicalLine: i == 0);
}
for (int j = 0; j < test.Context.Count; j++)
{
ResizingTestContext context = test.Context[j];
if (context.Offset.LineIndex != -1)
{
renderData.cursorLeft = context.OldCursor.X;
renderData.cursorTop = context.OldCursor.Y;
var offset = new RenderDataOffset(context.Offset.LineIndex, context.Offset.CharIndex);
Point newCursor = instance.ConvertRenderDataOffsetToPoint(context.NewInitial.X, context.NewInitial.Y, test.NewBufferWidth, renderData, offset);
Assert.True(
context.NewCursor.X == newCursor.X &&
context.NewCursor.Y == newCursor.Y,
$"{test.Name}-context_{j}: calculated new cursor is not what's expected [X: {newCursor.X}, Y: {newCursor.Y}]");
}
}
}
}
[Fact]
public void PhysicalLineCountMethod_ShouldWork()
{
var path = Path.Combine("assets", "resizing", "physical-line-count.json");
var text = File.ReadAllText(path);
var testDataList = JsonConvert.DeserializeObject<List<LogicalToPhysicalLineTestData>>(text);
foreach (LogicalToPhysicalLineTestData test in testDataList)
{
RenderedLineData lineData = new(test.Line, test.IsFirstLogicalLine);
for (int i = 0; i < test.Context.Count; i++)
{
LogicalToPhysicalLineTestContext context = test.Context[i];
int lineCount = lineData.PhysicalLineCount(context.BufferWidth, context.InitialX, out int lastLinelen);
Assert.True(
context.LineCount == lineCount &&
context.LastLineLen == lastLinelen,
$"{test.Name}-context_{i}: calculated physical line count or length of last physical line is not what's expected [count: {lineCount}, lastLen: {lastLinelen}]");
}
}
}
}
}