-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathValidationErrorTest.cs
More file actions
673 lines (532 loc) · 18.4 KB
/
ValidationErrorTest.cs
File metadata and controls
673 lines (532 loc) · 18.4 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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
using LogicBuilder.Workflow.ComponentModel.Compiler;
using System;
using System.Collections;
using System.Globalization;
namespace LogicBuilder.Workflow.Tests.ComponentModel.Compiler
{
public class ValidationErrorTest
{
#region Constructor Tests
[Fact]
public void Constructor_TwoParameters_CreatesValidationError()
{
// Arrange
string errorText = "Test error message";
int errorNumber = 100;
// Act
var error = new ValidationError(errorText, errorNumber);
// Assert
Assert.Equal(errorText, error.ErrorText);
Assert.Equal(errorNumber, error.ErrorNumber);
Assert.False(error.IsWarning);
Assert.Null(error.PropertyName);
}
[Fact]
public void Constructor_ThreeParameters_CreatesValidationError()
{
// Arrange
string errorText = "Test error message";
int errorNumber = 100;
bool isWarning = true;
// Act
var error = new ValidationError(errorText, errorNumber, isWarning);
// Assert
Assert.Equal(errorText, error.ErrorText);
Assert.Equal(errorNumber, error.ErrorNumber);
Assert.True(error.IsWarning);
Assert.Null(error.PropertyName);
}
[Fact]
public void Constructor_FourParameters_CreatesValidationError()
{
// Arrange
string errorText = "Test error message";
int errorNumber = 100;
bool isWarning = false;
string propertyName = "TestProperty";
// Act
var error = new ValidationError(errorText, errorNumber, isWarning, propertyName);
// Assert
Assert.Equal(errorText, error.ErrorText);
Assert.Equal(errorNumber, error.ErrorNumber);
Assert.False(error.IsWarning);
Assert.Equal(propertyName, error.PropertyName);
}
[Fact]
public void Constructor_WithNullErrorText_CreatesValidationError()
{
// Arrange
string? errorText = null;
int errorNumber = 100;
// Act
var error = new ValidationError(errorText, errorNumber);
// Assert
Assert.Null(error.ErrorText);
Assert.Equal(errorNumber, error.ErrorNumber);
}
[Fact]
public void Constructor_WithEmptyErrorText_CreatesValidationError()
{
// Arrange
string errorText = string.Empty;
int errorNumber = 100;
// Act
var error = new ValidationError(errorText, errorNumber);
// Assert
Assert.Equal(string.Empty, error.ErrorText);
Assert.Equal(errorNumber, error.ErrorNumber);
}
[Fact]
public void Constructor_WithNullPropertyName_CreatesValidationError()
{
// Arrange
string errorText = "Test error";
int errorNumber = 100;
string? propertyName = null;
// Act
var error = new ValidationError(errorText, errorNumber, false, propertyName);
// Assert
Assert.Null(error.PropertyName);
}
[Fact]
public void Constructor_WithZeroErrorNumber_CreatesValidationError()
{
// Arrange
string errorText = "Test error";
int errorNumber = 0;
// Act
var error = new ValidationError(errorText, errorNumber);
// Assert
Assert.Equal(0, error.ErrorNumber);
}
[Fact]
public void Constructor_WithNegativeErrorNumber_CreatesValidationError()
{
// Arrange
string errorText = "Test error";
int errorNumber = -1;
// Act
var error = new ValidationError(errorText, errorNumber);
// Assert
Assert.Equal(-1, error.ErrorNumber);
}
#endregion
#region Property Tests
[Fact]
public void ErrorText_ReturnsCorrectValue()
{
// Arrange
string expectedText = "This is an error message";
var error = new ValidationError(expectedText, 200);
// Act
string actualText = error.ErrorText;
// Assert
Assert.Equal(expectedText, actualText);
}
[Fact]
public void ErrorNumber_ReturnsCorrectValue()
{
// Arrange
int expectedNumber = 300;
var error = new ValidationError("Error", expectedNumber);
// Act
int actualNumber = error.ErrorNumber;
// Assert
Assert.Equal(expectedNumber, actualNumber);
}
[Fact]
public void IsWarning_ReturnsFalse_WhenNotSetAsWarning()
{
// Arrange
var error = new ValidationError("Error", 100, false);
// Act
bool isWarning = error.IsWarning;
// Assert
Assert.False(isWarning);
}
[Fact]
public void IsWarning_ReturnsTrue_WhenSetAsWarning()
{
// Arrange
var error = new ValidationError("Warning", 100, true);
// Act
bool isWarning = error.IsWarning;
// Assert
Assert.True(isWarning);
}
[Fact]
public void PropertyName_CanBeSet()
{
// Arrange
var error = new ValidationError("Error", 100);
string expectedName = "MyProperty";
// Act
error.PropertyName = expectedName;
// Assert
Assert.Equal(expectedName, error.PropertyName);
}
[Fact]
public void PropertyName_CanBeSetToNull()
{
// Arrange
var error = new ValidationError("Error", 100, false, "InitialProperty")
{
// Act
PropertyName = null
};
// Assert
Assert.Null(error.PropertyName);
}
[Fact]
public void PropertyName_CanBeChanged()
{
// Arrange
var error = new ValidationError("Error", 100, false, "FirstProperty");
string newName = "SecondProperty";
// Act
error.PropertyName = newName;
// Assert
Assert.Equal(newName, error.PropertyName);
}
#endregion
#region UserData Tests
[Fact]
public void UserData_IsNotNull()
{
// Arrange
var error = new ValidationError("Error", 100);
// Act
IDictionary userData = error.UserData;
// Assert
Assert.NotNull(userData);
}
[Fact]
public void UserData_IsEmptyInitially()
{
// Arrange
var error = new ValidationError("Error", 100);
// Act
IDictionary userData = error.UserData;
// Assert
Assert.Empty(userData);
}
[Fact]
public void UserData_CanStoreValues()
{
// Arrange
var error = new ValidationError("Error", 100);
string key = "TestKey";
string value = "TestValue";
// Act
error.UserData[key] = value;
// Assert
Assert.Equal(value, error.UserData[key]);
}
[Fact]
public void UserData_CanStoreMultipleValues()
{
// Arrange
var error = new ValidationError("Error", 100);
// Act
error.UserData["Key1"] = "Value1";
error.UserData["Key2"] = 42;
error.UserData["Key3"] = true;
// Assert
Assert.Equal("Value1", error.UserData["Key1"]);
Assert.Equal(42, error.UserData["Key2"]);
Assert.True((bool)error.UserData["Key3"]!);
Assert.Equal(3, error.UserData.Count);
}
[Fact]
public void UserData_ReturnsSameInstance()
{
// Arrange
var error = new ValidationError("Error", 100);
// Act
IDictionary userData1 = error.UserData;
IDictionary userData2 = error.UserData;
// Assert
Assert.Same(userData1, userData2);
}
[Fact]
public void UserData_CanRemoveValues()
{
// Arrange
var error = new ValidationError("Error", 100);
string key = "TestKey";
error.UserData[key] = "TestValue";
// Act
error.UserData.Remove(key);
// Assert
Assert.False(error.UserData.Contains(key));
}
[Fact]
public void UserData_CanClearAllValues()
{
// Arrange
var error = new ValidationError("Error", 100);
error.UserData["Key1"] = "Value1";
error.UserData["Key2"] = "Value2";
// Act
error.UserData.Clear();
// Assert
Assert.Empty(error.UserData);
}
#endregion
#region GetNotSetValidationError Tests
[Fact]
public void GetNotSetValidationError_CreatesValidationError()
{
// Arrange
string propertyName = "TestProperty";
// Act
ValidationError error = ValidationError.GetNotSetValidationError(propertyName);
// Assert
Assert.NotNull(error);
}
[Fact]
public void GetNotSetValidationError_SetsPropertyName()
{
// Arrange
string propertyName = "TestProperty";
// Act
ValidationError error = ValidationError.GetNotSetValidationError(propertyName);
// Assert
Assert.Equal(propertyName, error.PropertyName);
}
[Fact]
public void GetNotSetValidationError_SetsCorrectErrorNumber()
{
// Arrange
string propertyName = "TestProperty";
// Act
ValidationError error = ValidationError.GetNotSetValidationError(propertyName);
// Assert
Assert.Equal(0x116, error.ErrorNumber);
}
[Fact]
public void GetNotSetValidationError_IsNotWarning()
{
// Arrange
string propertyName = "TestProperty";
// Act
ValidationError error = ValidationError.GetNotSetValidationError(propertyName);
// Assert
Assert.False(error.IsWarning);
}
[Fact]
public void GetNotSetValidationError_ErrorTextContainsPropertyName()
{
// Arrange
string propertyName = "TestProperty";
// Act
ValidationError error = ValidationError.GetNotSetValidationError(propertyName);
// Assert
Assert.NotNull(error.ErrorText);
Assert.Contains(propertyName, error.ErrorText);
}
[Fact]
public void GetNotSetValidationError_WithNullPropertyName_CreatesValidationError()
{
// Arrange
string? propertyName = null;
// Act
ValidationError error = ValidationError.GetNotSetValidationError(propertyName);
// Assert
Assert.NotNull(error);
Assert.Null(error.PropertyName);
}
[Fact]
public void GetNotSetValidationError_WithEmptyPropertyName_CreatesValidationError()
{
// Arrange
string propertyName = string.Empty;
// Act
ValidationError error = ValidationError.GetNotSetValidationError(propertyName);
// Assert
Assert.NotNull(error);
Assert.Equal(string.Empty, error.PropertyName);
}
#endregion
#region ToString Tests
[Fact]
public void ToString_ReturnsErrorFormat_WhenIsWarningIsFalse()
{
// Arrange
var error = new ValidationError("Test error", 100, false);
// Act
string result = error.ToString();
// Assert
Assert.Contains("error", result);
Assert.Contains("100", result);
Assert.Contains("Test error", result);
}
[Fact]
public void ToString_ReturnsWarningFormat_WhenIsWarningIsTrue()
{
// Arrange
var error = new ValidationError("Test warning", 200, true);
// Act
string result = error.ToString();
// Assert
Assert.Contains("warning", result);
Assert.Contains("200", result);
Assert.Contains("Test warning", result);
}
[Fact]
public void ToString_IncludesErrorNumber()
{
// Arrange
var error = new ValidationError("Error message", 12345);
// Act
string result = error.ToString();
// Assert
Assert.Contains("12345", result);
}
[Fact]
public void ToString_IncludesErrorText()
{
// Arrange
string errorText = "This is a specific error message";
var error = new ValidationError(errorText, 100);
// Act
string result = error.ToString();
// Assert
Assert.Contains(errorText, result);
}
[Fact]
public void ToString_UsesInvariantCulture()
{
// Arrange
var error = new ValidationError("Error", 999);
var currentCulture = CultureInfo.CurrentCulture;
try
{
// Act - change culture to verify invariant culture is used
CultureInfo.CurrentCulture = new CultureInfo("fr-FR");
string result = error.ToString();
// Assert - the format should not be affected by culture
Assert.Contains("error 999: Error", result);
}
finally
{
CultureInfo.CurrentCulture = currentCulture;
}
}
[Fact]
public void ToString_WithNullErrorText_DoesNotThrow()
{
// Arrange
var error = new ValidationError(null, 100);
// Act
Exception? exception = Record.Exception(() => error.ToString());
// Assert
Assert.Null(exception);
}
[Fact]
public void ToString_WithEmptyErrorText_ReturnsValidFormat()
{
// Arrange
var error = new ValidationError(string.Empty, 100);
// Act
string result = error.ToString();
// Assert
Assert.Contains("error", result);
Assert.Contains("100", result);
}
[Theory]
[InlineData(0, false, "error 0:")]
[InlineData(100, false, "error 100:")]
[InlineData(200, true, "warning 200:")]
[InlineData(-1, false, "error -1:")]
public void ToString_ReturnsExpectedFormat(int errorNumber, bool isWarning, string expectedPrefix)
{
// Arrange
var error = new ValidationError("Message", errorNumber, isWarning);
// Act
string result = error.ToString();
// Assert
Assert.StartsWith(expectedPrefix, result);
}
#endregion
#region Serialization Tests
[Fact]
public void ValidationError_DoesNotHaveSerializableAttribute()
{
// Arrange
var type = typeof(ValidationError);
// Act
var attributes = type.GetCustomAttributes(typeof(SerializableAttribute), false);
// Assert
Assert.Empty(attributes);
}
#endregion
#region Integration Tests
[Fact]
public void ValidationError_CanBeUsedInCollection()
{
// Arrange
var errors = new System.Collections.Generic.List<ValidationError>();
var error1 = new ValidationError("Error 1", 1);
var error2 = new ValidationError("Error 2", 2);
// Act
errors.Add(error1);
errors.Add(error2);
// Assert
Assert.Equal(2, errors.Count);
Assert.Contains(error1, errors);
Assert.Contains(error2, errors);
}
[Fact]
public void ValidationError_PropertiesAreIndependent()
{
// Arrange
var error1 = new ValidationError("Error 1", 1, false, "Property1");
var error2 = new ValidationError("Error 2", 2, true, "Property2");
// Act
error1.UserData["key"] = "value1";
error2.UserData["key"] = "value2";
// Assert
Assert.NotEqual(error1.ErrorText, error2.ErrorText);
Assert.NotEqual(error1.ErrorNumber, error2.ErrorNumber);
Assert.NotEqual(error1.IsWarning, error2.IsWarning);
Assert.NotEqual(error1.PropertyName, error2.PropertyName);
Assert.NotEqual(error1.UserData["key"], error2.UserData["key"]);
}
[Fact]
public void ValidationError_WithMultilineErrorText_HandlesCorrectly()
{
// Arrange
string multilineText = "Line 1\nLine 2\nLine 3";
var error = new ValidationError(multilineText, 100);
// Act
string errorText = error.ErrorText;
string toString = error.ToString();
// Assert
Assert.Equal(multilineText, errorText);
Assert.Contains(multilineText, toString);
}
[Fact]
public void ValidationError_WithSpecialCharacters_HandlesCorrectly()
{
// Arrange
string specialText = "Error with special chars: <>&\"'";
var error = new ValidationError(specialText, 100);
// Act
string result = error.ToString();
// Assert
Assert.Contains(specialText, result);
}
[Fact]
public void GetNotSetValidationError_CreatesDifferentInstances()
{
// Arrange & Act
ValidationError error1 = ValidationError.GetNotSetValidationError("Property1");
ValidationError error2 = ValidationError.GetNotSetValidationError("Property2");
// Assert
Assert.NotSame(error1, error2);
Assert.NotEqual(error1.PropertyName, error2.PropertyName);
}
#endregion
}
}