Skip to content

Commit 913a41f

Browse files
author
Fahad Adeel
committed
Added Image unit tests and modified the public method to internal for the worksheet.
1 parent 9cc2429 commit 913a41f

File tree

2 files changed

+23
-18
lines changed

2 files changed

+23
-18
lines changed

FileFormat.Cells/Worksheet.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ namespace FileFormat.Cells
99
{
1010
public sealed class Worksheet
1111
{
12-
private readonly WorksheetPart _worksheetPart;
13-
private readonly SheetData _sheetData;
12+
private WorksheetPart _worksheetPart;
13+
private SheetData _sheetData;
1414

1515
// New Cells property
1616
public CellIndexer Cells { get; }
1717

18-
public Worksheet(WorksheetPart worksheetPart, DocumentFormat.OpenXml.Spreadsheet.Worksheet worksheet)
18+
internal Worksheet(WorksheetPart worksheetPart, DocumentFormat.OpenXml.Spreadsheet.Worksheet worksheet)
1919
{
2020
_worksheetPart = worksheetPart ?? throw new ArgumentNullException(nameof(worksheetPart));
2121

@@ -314,7 +314,5 @@ public Cell this[string cellReference]
314314
}
315315
}
316316
}
317-
318-
319317
}
320318

FileFormat.Cells_Tests/UnitTest1.cs

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
public class UnitTest1
66
{
77

8-
string testFilePath = "/Users/fahadadeelqazi/Downloads/test_fahad_new_protected111.xlsx";
8+
string testFilePath = "/Users/fahadadeelqazi/Downloads/test_fahad_new_protected_image.xlsx";
99
private string imageInputPath = "/Users/fahadadeelqazi/Downloads/ImageCells.png";
1010
string outputDirectory = "/Users/fahadadeelqazi/Downloads/";
1111

@@ -294,44 +294,51 @@ public void Test_ExtractImages()
294294
{
295295
using (Workbook wb = new Workbook(testFilePath))
296296
{
297+
297298
var worksheet = wb.Worksheets[0];
298-
var images = worksheet.ExtractImages();
299+
var image = new Image(imageInputPath);
300+
301+
worksheet.AddImage(image, 6, 1, 8, 3);
302+
303+
wb.Save(testFilePath);
304+
305+
Workbook wb1 = new Workbook(testFilePath);
306+
307+
var worksheet1 = wb1.Worksheets[0];
308+
309+
var images = worksheet1.ExtractImages();
299310

300311
Assert.IsTrue(images.Any(), "No images extracted from the worksheet.");
301312

302313
if (!Directory.Exists(outputDirectory))
303314
Directory.CreateDirectory(outputDirectory);
304315

305-
foreach (var image in images)
316+
foreach (var extracted_image in images)
306317
{
307-
var outputFilePath = Path.Combine(outputDirectory, $"Image_{Guid.NewGuid()}.{image.Extension}");
318+
var outputFilePath = Path.Combine(outputDirectory, $"Image_{Guid.NewGuid()}.{extracted_image.Extension}");
308319

309320
using (var fileStream = File.Create(outputFilePath))
310321
{
311-
image.Data.CopyTo(fileStream);
322+
extracted_image.Data.CopyTo(fileStream);
312323
}
313324

314325
// Assert that the image has been saved correctly
315326
Assert.IsTrue(File.Exists(outputFilePath), $"Image not found at {outputFilePath}");
316327
}
317328
}
318-
}
319-
320-
321-
322329

323330

331+
}
324332

325333

326334

327335
[TestCleanup]
328336
public void Cleanup()
329337
{
330-
// Clean up test artifacts
331-
//if (File.Exists(testFilePath))
332-
//File.Delete(testFilePath);
338+
//Clean up test artifacts
339+
if (File.Exists(testFilePath))
340+
File.Delete(testFilePath);
333341

334-
335342
}
336343

337344
}

0 commit comments

Comments
 (0)