-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSampleTests.cs
More file actions
256 lines (217 loc) · 6.48 KB
/
SampleTests.cs
File metadata and controls
256 lines (217 loc) · 6.48 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
// Copyright (c) ktsu.dev
// All rights reserved.
// Licensed under the MIT license.
namespace ktsu.TUI.Test;
using ktsu.TUI.Core.Contracts;
using ktsu.TUI.Core.Elements.Primitives;
using ktsu.TUI.Core.Models;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
/// <summary>
/// Tests for basic TUI elements functionality
/// </summary>
[TestClass]
public sealed class UIElementTests
{
/// <summary>
/// Tests that TextElement properly sets and gets text property
/// </summary>
[TestMethod]
public void TextElementSetTextUpdatesTextProperty()
{
// Arrange
TextElement textElement = new();
string expectedText = "Hello World";
// Act
textElement.Text = expectedText;
// Assert
Assert.AreEqual(expectedText, textElement.Text);
}
/// <summary>
/// Tests that TextElement invalidates when text changes
/// </summary>
[TestMethod]
public void TextElementChangeTextTriggersInvalidation()
{
// Arrange
TextElement textElement = new("Initial text");
bool invalidated = false;
textElement.Invalidated += (sender, args) => invalidated = true;
// Act
textElement.Text = "New text";
// Assert
Assert.IsTrue(invalidated, "Changing text should trigger invalidation event");
}
/// <summary>
/// Tests that TextElement doesn't invalidate when setting same text
/// </summary>
[TestMethod]
public void TextElementSetSameTextDoesNotInvalidate()
{
// Arrange
string initialText = "Same text";
TextElement textElement = new(initialText);
bool invalidated = false;
textElement.Invalidated += (sender, args) => invalidated = true;
// Act
textElement.Text = initialText;
// Assert
Assert.IsFalse(invalidated, "Setting the same text value should not trigger invalidation");
}
/// <summary>
/// Tests that TextElement handles null text gracefully
/// </summary>
[TestMethod]
public void TextElementSetNullTextSetsEmptyString()
{
// Arrange
TextElement textElement = new()
{
// Act
Text = null!
};
// Assert
Assert.AreEqual(string.Empty, textElement.Text);
}
/// <summary>
/// Tests that TextElement initializes with correct default values
/// </summary>
[TestMethod]
public void TextElementDefaultConstructorInitializesCorrectly()
{
// Arrange & Act
TextElement textElement = new();
// Assert
Assert.AreEqual(string.Empty, textElement.Text);
Assert.AreEqual(HorizontalAlignment.Left, textElement.HorizontalAlignment);
Assert.AreEqual(VerticalAlignment.Top, textElement.VerticalAlignment);
Assert.IsFalse(textElement.WordWrap, "Default WordWrap should be false");
Assert.IsTrue(textElement.IsVisible, "Default IsVisible should be true");
}
/// <summary>
/// Tests that TextElement constructor with text parameter works correctly
/// </summary>
[TestMethod]
public void TextElementConstructorWithTextSetsTextCorrectly()
{
// Arrange
string expectedText = "Constructor text";
// Act
TextElement textElement = new(expectedText);
// Assert
Assert.AreEqual(expectedText, textElement.Text);
}
/// <summary>
/// Tests that TextElement constructor with text and style works correctly
/// </summary>
[TestMethod]
public void TextElementConstructorWithTextAndStyleSetsPropertiesCorrectly()
{
// Arrange
string expectedText = "Styled text";
TextStyle expectedStyle = new()
{ IsBold = true, Foreground = "red" };
// Act
TextElement textElement = new(expectedText, expectedStyle);
// Assert
Assert.AreEqual(expectedText, textElement.Text);
Assert.AreEqual(expectedStyle.IsBold, textElement.Style.IsBold);
Assert.AreEqual(expectedStyle.Foreground, textElement.Style.Foreground);
}
/// <summary>
/// Tests that Position property changes trigger invalidation
/// </summary>
[TestMethod]
public void UIElementChangePositionTriggersInvalidation()
{
// Arrange
TextElement textElement = new();
bool invalidated = false;
textElement.Invalidated += (sender, args) => invalidated = true;
// Act
textElement.Position = new Position(10, 20);
// Assert
Assert.IsTrue(invalidated, "Changing position should trigger invalidation event");
Assert.AreEqual(10, textElement.Position.X);
Assert.AreEqual(20, textElement.Position.Y);
}
/// <summary>
/// Tests that Dimensions property changes trigger invalidation
/// </summary>
[TestMethod]
public void UIElementChangeDimensionsTriggersInvalidation()
{
// Arrange
TextElement textElement = new();
bool invalidated = false;
textElement.Invalidated += (sender, args) => invalidated = true;
// Act
textElement.Dimensions = new Dimensions(100, 50);
// Assert
Assert.IsTrue(invalidated, "Changing dimensions should trigger invalidation event");
Assert.AreEqual(100, textElement.Dimensions.Width);
Assert.AreEqual(50, textElement.Dimensions.Height);
}
/// <summary>
/// Tests that Visibility property changes trigger invalidation
/// </summary>
[TestMethod]
public void UIElementChangeVisibilityTriggersInvalidation()
{
// Arrange
TextElement textElement = new();
bool invalidated = false;
textElement.Invalidated += (sender, args) => invalidated = true;
// Act
textElement.IsVisible = false;
// Assert
Assert.IsTrue(invalidated, "Changing visibility should trigger invalidation event");
Assert.IsFalse(textElement.IsVisible, "IsVisible should be false after setting it to false");
}
/// <summary>
/// Tests that Render doesn't execute when element is not visible
/// </summary>
[TestMethod]
public void UIElementRenderWhenNotVisibleDoesNotCallOnRender()
{
// Arrange
Mock<IConsoleProvider> mockProvider = new();
TextElement textElement = new("Test text")
{
IsVisible = false
};
// Act
textElement.Render(mockProvider.Object);
// Assert
mockProvider.VerifyNoOtherCalls();
}
/// <summary>
/// Tests that HandleInput returns false by default
/// </summary>
[TestMethod]
public void UIElementHandleInputReturnsFalseByDefault()
{
// Arrange
TextElement textElement = new();
InputResult input = new()
{ Key = ConsoleKey.Enter, Modifiers = InputModifiers.None };
// Act
bool result = textElement.HandleInput(input);
// Assert
Assert.IsFalse(result, "HandleInput should return false by default for non-interactive elements");
}
/// <summary>
/// Tests that Padding property is properly set and retrieved
/// </summary>
[TestMethod]
public void UIElementSetPaddingUpdatesPaddingProperty()
{
// Arrange
TextElement textElement = new();
Padding expectedPadding = new(1, 2, 3, 4);
// Act
textElement.Padding = expectedPadding;
// Assert
Assert.AreEqual(expectedPadding, textElement.Padding);
}
}