Skip to content

Commit 91b5706

Browse files
Fixed core tests
1 parent e60c850 commit 91b5706

File tree

2 files changed

+150
-40
lines changed

2 files changed

+150
-40
lines changed

NanoXLSX.Core/Colors/Color.cs

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -392,43 +392,38 @@ public int CompareTo(object obj)
392392
{
393393
return typeCompare;
394394
}
395+
395396
// 2) Same type -> compare internal representation
396397
switch (Type)
397398
{
398399
case ColorType.None:
399400
return 0;
400-
case ColorType.Auto: // Auto has no value
401+
case ColorType.Auto:
401402
return 0;
402403
case ColorType.Rgb:
403404
return string.Compare(
404405
RgbColor?.StringValue,
405406
other.RgbColor?.StringValue,
406407
StringComparison.OrdinalIgnoreCase);
407408
case ColorType.Indexed:
408-
return string.Compare(
409-
IndexedColor?.StringValue,
410-
other.IndexedColor?.StringValue,
411-
StringComparison.Ordinal);
409+
// Numeric comparison of palette index
410+
return IndexedColor.ColorValue.CompareTo(other.IndexedColor.ColorValue);
412411
case ColorType.Theme:
413412
{
414-
int themeCompare = string.Compare(
415-
ThemeColor?.StringValue,
416-
other.ThemeColor?.StringValue,
417-
StringComparison.Ordinal);
418-
413+
// Numeric comparison of theme slot
414+
int themeCompare = ThemeColor.ColorValue.CompareTo(other.ThemeColor.ColorValue);
419415
if (themeCompare != 0)
420416
{
421417
return themeCompare;
422418
}
423-
// Same theme index -> compare tint
419+
// Same theme slot -> compare tint
424420
return Nullable.Compare(Tint, other.Tint);
425421
}
426422
case ColorType.System:
427-
return string.Compare(
428-
SystemColor?.StringValue,
429-
other.SystemColor?.StringValue,
430-
StringComparison.OrdinalIgnoreCase);
431-
default: // Defensive fallback
423+
// Enum-based comparison -> not string-based
424+
return SystemColor.ColorValue.CompareTo(other.SystemColor.ColorValue);
425+
default:
426+
// Defensive fallback —> should normally never happen
432427
return string.Compare(
433428
Value?.StringValue,
434429
other.Value?.StringValue,

NanoXlsx.Core.Test/Colors/ColorTest.cs

Lines changed: 139 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ public void ValueSrgbTest(string givenRgbValue, string expectedRgbValue)
279279
{
280280
Color color = Color.CreateRgb(givenRgbValue);
281281
Assert.True(color.Value is SrgbColor);
282-
Assert.Equal(expectedRgbValue, color.GetArgbValue());
282+
Assert.Equal(expectedRgbValue, color.RgbColor.ColorValue);
283283
}
284284

285285
[Theory(DisplayName = "Test of the Value property on indexed colors")]
@@ -291,7 +291,7 @@ public void ValueIndexedTest(IndexedColor.Value indexedValue)
291291
{
292292
Color color = Color.CreateIndexed(indexedValue);
293293
Assert.True(color.Value is IndexedColor);
294-
Assert.Equal(indexedValue, ((IndexedColor)color.Value).ColorValue);
294+
Assert.Equal(indexedValue, color.IndexedColor.ColorValue);
295295
}
296296

297297
[Theory(DisplayName = "Test of the Value property on system colors")]
@@ -303,7 +303,7 @@ public void ValueSystemTest(SystemColor.Value systemColor)
303303
{
304304
Color color = Color.CreateSystem(systemColor);
305305
Assert.True(color.Value is SystemColor);
306-
Assert.Equal(systemColor, ((SystemColor)color.Value).ColorValue);
306+
Assert.Equal(systemColor, color.SystemColor.ColorValue);
307307
}
308308

309309
[Theory(DisplayName = "Test of the Value property on theme colors")]
@@ -318,27 +318,59 @@ public void ValueThemeTest(Theme.ColorSchemeElement themeElement)
318318
Assert.Equal(themeElement, ((ThemeColor)color.Value).ColorValue);
319319
}
320320

321-
// ---------- GetArgbValue Tests ----------
321+
[Theory(DisplayName = "Test of the GetArgbValue function on a sRGB color")]
322+
[InlineData("000000", "FF000000")]
323+
[InlineData("FFFFFF", "FFFFFFFF")]
324+
[InlineData("123456", "FF123456")]
325+
[InlineData("FF000000", "FF000000")]
326+
[InlineData("FFFFFFFF", "FFFFFFFF")]
327+
[InlineData("FF234567", "FF234567")]
328+
public void GetArgbValueSRgbTest(string givenRgb, string expectedRgb)
329+
{
330+
var c = Color.CreateRgb(givenRgb);
331+
Assert.Equal(expectedRgb, c.GetArgbValue());
332+
}
322333

323-
[Fact(DisplayName = "GetArgbValueReturnsNullForThemeTest")]
334+
[Theory(DisplayName = "Test of the GetArgbValue function on a sRGB color")]
335+
[InlineData(IndexedColor.Value.Black0, "FF000000")]
336+
[InlineData(IndexedColor.Value.Black, "FF000000")]
337+
[InlineData(IndexedColor.Value.White, "FFFFFFFF")]
338+
[InlineData(IndexedColor.Value.StrongCyan, "FF00FFFF")]
339+
[InlineData(IndexedColor.Value.DarkMaroon, "FF800000")]
340+
[InlineData(IndexedColor.Value.Lavender, "FFCC99FF")]
341+
public void GetArgbValueIndexedTest(IndexedColor.Value givenIndex, string expectedRgb)
342+
{
343+
var c = Color.CreateIndexed(givenIndex);
344+
Assert.Equal(expectedRgb, c.GetArgbValue());
345+
}
346+
347+
348+
[Fact(DisplayName = "Test of the GetArgbValue function on a theme color")]
324349
public void GetArgbValueReturnsNullForThemeTest()
325350
{
326351
var c = Color.CreateTheme(Theme.ColorSchemeElement.Dark1);
327352

328353
Assert.Null(c.GetArgbValue());
329354
}
330355

331-
[Fact(DisplayName = "GetArgbValueReturnsNullForAutoTest")]
356+
[Fact(DisplayName = "Test of the GetArgbValue function on a system color")]
357+
public void GetArgbValueReturnsNullForSystemTest()
358+
{
359+
var c = Color.CreateSystem(SystemColor.Value.ActiveBorder);
360+
361+
Assert.Null(c.GetArgbValue());
362+
}
363+
364+
[Fact(DisplayName = "Test of the GetArgbValue function on a auto color")]
332365
public void GetArgbValueReturnsNullForAutoTest()
333366
{
334367
var c = Color.CreateAuto();
335368

336369
Assert.Null(c.GetArgbValue());
337370
}
338371

339-
// ---------- Equals Tests ----------
340372

341-
[Fact(DisplayName = "EqualsSameRgbValueTest")]
373+
[Fact(DisplayName = "Test of the Equals method on equality")]
342374
public void EqualsSameRgbValueTest()
343375
{
344376
var a = Color.CreateRgb("FFABCDEF");
@@ -348,7 +380,7 @@ public void EqualsSameRgbValueTest()
348380
Assert.True(a.Equals(b));
349381
}
350382

351-
[Fact(DisplayName = "EqualsDifferentRgbValueTest")]
383+
[Fact(DisplayName = "Test of the Equals method on inequality")]
352384
public void EqualsDifferentRgbValueTest()
353385
{
354386
var a = Color.CreateRgb("FFABCDEF");
@@ -357,7 +389,7 @@ public void EqualsDifferentRgbValueTest()
357389
Assert.NotEqual(a, b);
358390
}
359391

360-
[Fact(DisplayName = "EqualsDifferentTypeTest")]
392+
[Fact(DisplayName = "Test of the Equals method on inequality on different types")]
361393
public void EqualsDifferentTypeTest()
362394
{
363395
var a = Color.CreateRgb("FF000000");
@@ -366,9 +398,7 @@ public void EqualsDifferentTypeTest()
366398
Assert.NotEqual(a, b);
367399
}
368400

369-
// ---------- HashCode Tests ----------
370-
371-
[Fact(DisplayName = "GetHashCodeEqualObjectsTest")]
401+
[Fact(DisplayName = "Test of the GetHasCode method on equality")]
372402
public void GetHashCodeEqualObjectsTest()
373403
{
374404
var a = Color.CreateRgb("FF112233");
@@ -377,7 +407,7 @@ public void GetHashCodeEqualObjectsTest()
377407
Assert.Equal(a.GetHashCode(), b.GetHashCode());
378408
}
379409

380-
[Fact(DisplayName = "GetHashCodeDifferentObjectsTest")]
410+
[Fact(DisplayName = "Test of the GetHasCode method on inequality")]
381411
public void GetHashCodeDifferentObjectsTest()
382412
{
383413
var a = Color.CreateRgb("FF112233");
@@ -386,34 +416,55 @@ public void GetHashCodeDifferentObjectsTest()
386416
Assert.NotEqual(a.GetHashCode(), b.GetHashCode());
387417
}
388418

389-
// ---------- CompareTo Tests ----------
390419

391-
[Fact(DisplayName = "CompareToNullTest")]
420+
[Fact(DisplayName = "Test of the CompareTo method on null values")]
392421
public void CompareToNullTest()
393422
{
394423
var c = Color.CreateRgb("FF000000");
395424

396425
Assert.True(c.CompareTo(null) > 0);
397426
}
398427

399-
[Fact(DisplayName = "CompareToWrongTypeTest")]
428+
[Fact(DisplayName = "Test of the CompareTo method on different types")]
400429
public void CompareToWrongTypeTest()
401430
{
402431
var c = Color.CreateRgb("FF000000");
403432

404433
Assert.Throws<StyleException>(() => c.CompareTo("not a color"));
405434
}
406435

407-
[Fact(DisplayName = "CompareToSameRgbTest")]
408-
public void CompareToSameRgbTest()
436+
[Fact(DisplayName = "Test of the CompareTo method on two none color types")]
437+
public void CompareNoneColorTypeTest()
409438
{
410-
var a = Color.CreateRgb("FF000000");
411-
var b = Color.CreateRgb("FF000000");
439+
var a = Color.CreateNone();
440+
var b = Color.CreateNone();
441+
Assert.Equal(0, a.CompareTo(b));
442+
}
443+
444+
[Fact(DisplayName = "Test of the CompareTo method on two auto color types")]
445+
public void CompareAutoColorTypeTest()
446+
{
447+
var a = Color.CreateAuto();
448+
var b = Color.CreateAuto();
449+
Assert.Equal(0, a.CompareTo(b));
450+
}
451+
452+
[Theory(DisplayName = "Test of the CompareTo method on identical RGB/ARGB values")]
453+
[InlineData("000000")]
454+
[InlineData("FFFFFF")]
455+
[InlineData("AABBCC")]
456+
[InlineData("FF000000")]
457+
[InlineData("FFFFFFFF")]
458+
[InlineData("FFAABBCC")]
459+
public void CompareToSameRgbTest(string rgbValue)
460+
{
461+
var a = Color.CreateRgb(rgbValue);
462+
var b = Color.CreateRgb(rgbValue);
412463

413464
Assert.Equal(0, a.CompareTo(b));
414465
}
415466

416-
[Fact(DisplayName = "CompareToRgbOrderingTest")]
467+
[Fact(DisplayName = "Test of the CompareTo method on different sRGB values")]
417468
public void CompareToRgbOrderingTest()
418469
{
419470
var a = Color.CreateRgb("FF000000");
@@ -422,7 +473,7 @@ public void CompareToRgbOrderingTest()
422473
Assert.True(a.CompareTo(b) < 0);
423474
}
424475

425-
[Fact(DisplayName = "CompareToDifferentTypeOrderingTest")]
476+
[Fact(DisplayName = "Test of the CompareTo method on different color values if sRGB and indexes are compared")]
426477
public void CompareToDifferentTypeOrderingTest()
427478
{
428479
var rgb = Color.CreateRgb("FF000000");
@@ -431,13 +482,77 @@ public void CompareToDifferentTypeOrderingTest()
431482
Assert.NotEqual(0, rgb.CompareTo(indexed));
432483
}
433484

434-
[Fact(DisplayName = "CompareToThemeTintTest")]
485+
[Fact(DisplayName = "Test of the CompareTo method on different tint values")]
435486
public void CompareToThemeTintTest()
436487
{
437488
var a = Color.CreateTheme(Theme.ColorSchemeElement.Accent1, 0.1);
438489
var b = Color.CreateTheme(Theme.ColorSchemeElement.Accent1, 0.2);
439490

440491
Assert.True(a.CompareTo(b) < 0);
441492
}
493+
494+
[Fact(DisplayName = "Test of the CompareTo method on colors with different theme slots")]
495+
public void CompareToThemeDifferentThemeSlots()
496+
{
497+
var c1 = Color.CreateTheme(Theme.ColorSchemeElement.Dark1);
498+
var c2 = Color.CreateTheme(Theme.ColorSchemeElement.Accent1);
499+
500+
int result = c1.CompareTo(c2);
501+
502+
Assert.True(result < 0);
503+
}
504+
505+
[Fact(DisplayName = "Test of the CompareTo method on colors with same slot but different tint")]
506+
public void CompareToThemeSameSlotDifferentTint()
507+
{
508+
var c1 = Color.CreateTheme(Theme.ColorSchemeElement.Accent1, tint: -0.2);
509+
var c2 = Color.CreateTheme(Theme.ColorSchemeElement.Accent1, tint: 0.2);
510+
511+
int result = c1.CompareTo(c2);
512+
513+
Assert.True(result < 0);
514+
}
515+
516+
[Fact(DisplayName = "Test of the CompareTo method on System colors")]
517+
public void CompareToSystemColors()
518+
{
519+
var c1 = Color.CreateSystem(new SystemColor(SystemColor.Value.AppWorkspace));
520+
var c2 = Color.CreateSystem(new SystemColor(SystemColor.Value.Menu));
521+
522+
int result = c1.CompareTo(c2);
523+
524+
Assert.NotEqual(0, result);
525+
}
526+
527+
[Fact(DisplayName = "Test of the CompareTo method on a defensive fallback path")]
528+
public void CompareToDefensiveFallback()
529+
{
530+
var c1 = Color.CreateRgb("FF0000");
531+
var c2 = Color.CreateRgb("00FF00");
532+
533+
typeof(Color)
534+
.GetProperty(nameof(Color.Type))
535+
.SetValue(c1, (Color.ColorType)999);
536+
537+
typeof(Color)
538+
.GetProperty(nameof(Color.Type))
539+
.SetValue(c2, (Color.ColorType)999);
540+
541+
int result = c1.CompareTo(c2);
542+
543+
Assert.Equal(0, result);
544+
}
545+
546+
[Fact(DisplayName = "Test of the CompareTo method on indexed colors uses numeric index")]
547+
public void CompareToIndexedNumericComparison()
548+
{
549+
var c1 = Color.CreateIndexed(IndexedColor.Value.Black); // e.g. 8
550+
var c2 = Color.CreateIndexed(IndexedColor.Value.White); // e.g. 9
551+
552+
int result = c1.CompareTo(c2);
553+
554+
Assert.True(result < 0); // Both are invalid types - corner case
555+
}
556+
442557
}
443558
}

0 commit comments

Comments
 (0)