Skip to content

Commit 3db2062

Browse files
Added / adapted test cases
1 parent 25b2899 commit 3db2062

File tree

8 files changed

+125
-32
lines changed

8 files changed

+125
-32
lines changed

MigrationGuide.md

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -279,15 +279,22 @@ fill3.ForegroundColor = Color.CreateTheme(Theme.ColorSchemeElement.Accent1, 0.5f
279279
Fill fill4 = "FF00FF00"; // Implicit conversion to SrgbColor as foreground
280280
281281
// Implicit creation of fill from a int (indexed color):
282-
Fill fill5 = 64; // Implicit conversion to IndexedColor as foreground
282+
Fill fill5 = IndexedColor.Value.Red2; // Implicit conversion to IndexedColor as foreground
283+
284+
// Implicit creation of fill from a int (indexed color):
285+
Fill fill6 = 64; // Implicit conversion to IndexedColor as foreground, ising the index number
283286
284287
// Implicit creation of specific color from a string (ARGB):
285-
Fill fill6 = new Fill();
286-
fill6.ForegroundColor = "FF00FF00"; // Implicit conversion to SrgbColor
288+
Fill fill7 = new Fill();
289+
fill7.ForegroundColor = "FF00FF00"; // Implicit conversion to SrgbColor
290+
291+
// Implicit creation of fill from a int (indexed color):
292+
Fill fill8 = new Fill();
293+
Fill fill8.BackgroundColor = IndexedColor.Value.Black; // Implicit conversion to IndexedColor as background
287294
288295
// Implicit creation of specific color from a int (color index):
289-
Fill fill7 = new Fill();
290-
fill7.BackgroundColor = 63; // Implicit conversion to IndexedColor
296+
Fill fill9 = new Fill();
297+
fill9.BackgroundColor = 63; // Implicit conversion to IndexedColor
291298
```
292299

293300

NanoXLSX.Core/Colors/Color.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -323,13 +323,23 @@ public static implicit operator Color(string rgbValue)
323323
/// <summary>
324324
/// Implicit conversion from index number to Color.
325325
/// </summary>
326-
/// <param name="colorIndex">Index (<see cref="IndexedColor.ColorValue"/>)</param>
326+
/// <param name="colorIndex">Numeric value of the color index (<see cref="IndexedColor.ColorValue"/>)</param>
327327
/// \remark <remarks>The resulting color value will be of the type <see cref="IndexedColor"/>, if valid</remarks>
328328
public static implicit operator Color(int colorIndex)
329329
{
330330
return CreateIndexed(colorIndex);
331331
}
332332

333+
/// <summary>
334+
/// Implicit conversion from index number to Color.
335+
/// </summary>
336+
/// <param name="colorIndex">Index (<see cref="IndexedColor.ColorValue"/>)</param>
337+
/// \remark <remarks>The resulting color value will be of the type <see cref="IndexedColor"/>, if valid</remarks>
338+
public static implicit operator Color(IndexedColor.Value colorIndex)
339+
{
340+
return CreateIndexed(colorIndex);
341+
}
342+
333343

334344
/// <summary>
335345
/// Determines whether the specified object is equal to the current object

NanoXLSX.Core/Styles/Fill.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,6 @@ public Fill(string foreground, string background)
137137
{
138138
BackgroundColor = Color.CreateRgb(background);
139139
ForegroundColor = Color.CreateRgb(foreground);
140-
// IndexedColor = DefaultIndexedColor;
141140
PatternFill = PatternValue.Solid;
142141
}
143142

@@ -267,7 +266,19 @@ public static implicit operator Fill(string value)
267266
}
268267

269268
/// <summary>
270-
/// Implicit operator to create a Fill object from an indexed color value as foreground color with <see cref="FillType.FillColor"/>
269+
/// Implicit operator to create a Fill object from an indexed color value (<see cref="IndexedColor.Value"/>) as foreground color with <see cref="FillType.FillColor"/>
270+
/// </summary>
271+
/// <param name="index">Color index (0 to 65)</param>
272+
public static implicit operator Fill(IndexedColor.Value index)
273+
{
274+
Fill fill = new Fill();
275+
fill.PatternFill = PatternValue.Solid;
276+
fill.ForegroundColor = Color.CreateIndexed(index);
277+
return fill;
278+
}
279+
280+
/// <summary>
281+
/// Implicit operator to create a Fill object from an indexed color index (numeric) as foreground color with <see cref="FillType.FillColor"/>
271282
/// </summary>
272283
/// <param name="index">Color index (0 to 65)</param>
273284
public static implicit operator Fill(int index)

NanoXLSX.Writer-Reader.Test/Workbooks/WorkbookTest.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -583,7 +583,7 @@ public void AddMruColorTest()
583583
Assert.Equal("FFAABBCC", workbook.GetMruColors()[0]);
584584
}
585585

586-
[Fact(DisplayName = "Test of the AddMruColor function for color objects")]
586+
[Fact(DisplayName = "Test of the AddMruColor function for ARGB values")]
587587
public void AddMruColorTest2()
588588
{
589589
Workbook workbook = new Workbook();
@@ -594,6 +594,16 @@ public void AddMruColorTest2()
594594
Assert.Equal("FFAABBCC", workbook.GetMruColors()[0]);
595595
}
596596

597+
[Fact(DisplayName = "Test of the AddMruColor function for indexed values")]
598+
public void AddMruColorTest3()
599+
{
600+
Workbook workbook = new Workbook();
601+
Assert.Empty(workbook.GetMruColors());
602+
Color color = "AABBCC"; // implicit conversion
603+
workbook.AddMruColor(color);
604+
Assert.Equal(1, workbook.GetMruColors().Count);
605+
Assert.Equal("FFAABBCC", workbook.GetMruColors()[0]);
606+
}
597607

598608
[Theory(DisplayName = "Test of the failing AddMruColor function when adding an invalid color value")]
599609
[InlineData(null)]

NanoXLSX.Writer-Reader.Test/Workbooks/WorkbookWriteReadTest.cs

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public void StringProcessingTest()
3030
Assert.Equal("Text1", givenWorkbook.CurrentWorksheet.Cells["A5"].Value.ToString());
3131
}
3232

33-
[Fact(DisplayName = "Test of the (virtual) 'MruColors' property when writing and reading a workbook")]
33+
[Fact(DisplayName = "Test of the (virtual) 'MruColors' property on a ARGB value, when writing and reading a workbook")]
3434
public void ReadMruColorsTest()
3535
{
3636
Workbook workbook = new Workbook();
@@ -46,8 +46,24 @@ public void ReadMruColorsTest()
4646
Assert.Equal("FF" + color2, mruColors[1].GetArgbValue());
4747
}
4848

49-
[Fact(DisplayName = "Test of the (virtual) 'MruColors' property when writing and reading a workbook, neglecting the default color")]
49+
50+
[Fact(DisplayName = "Test of the (virtual) 'MruColors' property on a indexed color, when writing and reading a workbook")]
5051
public void ReadMruColorsTest2()
52+
{
53+
Workbook workbook = new Workbook();
54+
workbook.AddMruColor(IndexedColor.Value.Blue4);
55+
workbook.AddMruColor(IndexedColor.Value.StrongYellow);
56+
Workbook givenWorkbook = TestUtils.WriteAndReadWorkbook(workbook);
57+
List<Color> mruColors = ((List<Color>)givenWorkbook.GetMruColors());
58+
mruColors.Sort();
59+
Assert.Equal(2, mruColors.Count);
60+
Assert.Equal(IndexedColor.GetArgbValue(IndexedColor.Value.Blue4), mruColors[0].GetArgbValue());
61+
Assert.Equal(IndexedColor.GetArgbValue(IndexedColor.Value.StrongYellow), mruColors[1].GetArgbValue());
62+
}
63+
64+
65+
[Fact(DisplayName = "Test of the (virtual) 'MruColors' property when writing and reading a workbook, neglecting the default color")]
66+
public void ReadMruColorsTest3()
5167
{
5268
Workbook workbook = new Workbook();
5369
string color1 = "AACC00";
@@ -61,6 +77,17 @@ public void ReadMruColorsTest2()
6177
Assert.Equal("FF" + color1, mruColors[0].GetArgbValue());
6278
}
6379

80+
[Fact(DisplayName = "Test of the (virtual) 'MruColors' property when writing and reading a workbook, neglecting an undefined color")]
81+
public void ReadMruColorsTest4()
82+
{
83+
Workbook workbook = new Workbook();
84+
Color color = Color.CreateNone();
85+
workbook.AddMruColor(color);
86+
Workbook givenWorkbook = TestUtils.WriteAndReadWorkbook(workbook);
87+
List<Color> mruColors = ((List<Color>)givenWorkbook.GetMruColors());
88+
Assert.Empty(mruColors);
89+
}
90+
6491
[Theory(DisplayName = "Test of the 'Hidden' property when writing and reading a workbook")]
6592
[InlineData(true)]
6693
[InlineData(false)]

NanoXLSX.Writer/Internal/Writers/ColorWriter.cs

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,10 @@ internal class ColorWriter : IColorWriter
2626
/// </summary>
2727
/// <param name="color">Color instance</param>
2828
/// <returns>Enumeration of the attributes, describing the color instance</returns>
29+
/// \remark <remarks>The passed color instance may never be null. Such cases have to be handled earlier</remarks>
2930
public IEnumerable<XmlAttribute> GetAttributes(Color color)
3031
{
3132
List<XmlAttribute> attributes = new List<XmlAttribute>();
32-
if (color == null)
33-
{
34-
return attributes;
35-
}
3633
string name = GetAttributeName(color);
3734
if (name != null)
3835
{
@@ -51,12 +48,9 @@ public IEnumerable<XmlAttribute> GetAttributes(Color color)
5148
/// </summary>
5249
/// <param name="color">Color instance</param>
5350
/// <returns>Attribute name</returns>
51+
/// \remark <remarks>The passed color instance may never be null. Such cases have to be handled earlier</remarks>
5452
public string GetAttributeName(Color color)
5553
{
56-
if (color == null)
57-
{
58-
return null;
59-
}
6054
switch (color.Type)
6155
{
6256
case Color.ColorType.Auto:
@@ -79,27 +73,29 @@ public string GetAttributeName(Color color)
7973
/// </summary>
8074
/// <param name="color">Color instance</param>
8175
/// <returns>Attribute value</returns>
76+
/// \remark <remarks>The passed color instance may never be null. Such cases have to be handled earlier</remarks>
8277
public string GetAttributeValue(Color color)
8378
{
84-
if (color == null)
85-
{
86-
return null;
87-
}
79+
string value = null;
8880
switch (color.Type)
8981
{
9082
case Color.ColorType.Auto:
91-
return "1";
83+
value = "1";
84+
break;
9285
case Color.ColorType.Rgb:
93-
return color.RgbColor.StringValue;
86+
value = color.RgbColor.StringValue;
87+
break;
9488
case Color.ColorType.Indexed:
95-
return color.IndexedColor.StringValue;
89+
value = color.IndexedColor.StringValue;
90+
break;
9691
case Color.ColorType.Theme:
97-
return color.ThemeColor.StringValue;
92+
value = color.ThemeColor.StringValue;
93+
break;
9894
case Color.ColorType.System:
99-
return color.SystemColor.StringValue;
100-
default:
101-
return null;
95+
value = color.SystemColor.StringValue;
96+
break;
10297
}
98+
return value;
10399
}
104100

105101
/// <summary>

NanoXlsx.Core.Test/Colors/ColorTest.cs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,12 +206,27 @@ public void ImplicitRgbConversionTest(string givrnRgb, string expectedRgb)
206206
Assert.Equal(expectedRgb, color.GetArgbValue(), ignoreCase: true);
207207
}
208208

209+
[Theory(DisplayName = "Test of the implicit operator function, when using a value of IndexedColor.Value")]
210+
[InlineData(IndexedColor.Value.Black)]
211+
[InlineData(IndexedColor.Value.Black0)]
212+
[InlineData(IndexedColor.Value.Cyan)]
213+
[InlineData(IndexedColor.Value.SystemBackground)]
214+
[InlineData(IndexedColor.Value.SystemForeground)]
215+
public void ImplicitIndexedConversionTest(IndexedColor.Value index)
216+
{
217+
Color color = index;
218+
Assert.Equal(Color.ColorType.Indexed, color.Type);
219+
Assert.NotNull(color.GetArgbValue());
220+
Assert.Equal(index, color.IndexedColor.ColorValue);
221+
}
222+
223+
209224
[Theory(DisplayName = "Test of the implicit operator function, when using an int")]
210225
[InlineData(5)]
211226
[InlineData(0)]
212227
[InlineData(22)]
213228
[InlineData(65)]
214-
public void ImplicitIndexedConversionTest(int index)
229+
public void ImplicitIndexedConversionTest2(int index)
215230
{
216231
Color color = index;
217232
Assert.Equal(Color.ColorType.Indexed, color.Type);

NanoXlsx.Core.Test/Styles/FillTest.cs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -333,13 +333,30 @@ public void ImplicitOperatorTest(string value)
333333
Assert.Equal(fill.BackgroundColor, expectedFill.BackgroundColor);
334334
}
335335

336+
337+
[Theory(DisplayName = "Test of the implicit operator (create indexed color by enum value)")]
338+
[InlineData(IndexedColor.Value.Red)]
339+
[InlineData(IndexedColor.Value.Black0)]
340+
[InlineData(IndexedColor.Value.Black)]
341+
[InlineData(IndexedColor.Value.SystemForeground)]
342+
[InlineData(IndexedColor.Value.SystemBackground)]
343+
public void ImplicitOperatorTest3(IndexedColor.Value value)
344+
{
345+
Fill fill = value;
346+
Fill expectedFill = new Fill();
347+
expectedFill.ForegroundColor = Color.CreateIndexed(value);
348+
expectedFill.PatternFill = PatternValue.Solid;
349+
Assert.Equal(fill.ForegroundColor, expectedFill.ForegroundColor);
350+
Assert.Equal(fill.BackgroundColor, expectedFill.BackgroundColor);
351+
}
352+
336353
[Theory(DisplayName = "Test of the implicit operator (create indexed color by int)")]
337354
[InlineData(10, IndexedColor.Value.Red)]
338355
[InlineData(0, IndexedColor.Value.Black0)]
339356
[InlineData(8, IndexedColor.Value.Black)]
340357
[InlineData(64, IndexedColor.Value.SystemForeground)]
341358
[InlineData(65, IndexedColor.Value.SystemBackground)]
342-
public void ImplicitOperatorTest3(int givenValue, IndexedColor.Value expectedValue)
359+
public void ImplicitOperatorTest4(int givenValue, IndexedColor.Value expectedValue)
343360
{
344361
Fill fill = givenValue;
345362
Fill expectedFill = new Fill();

0 commit comments

Comments
 (0)