diff --git a/src/EPPlus.Extensions.Tests/EPPlus.Extensions.Tests.csproj b/src/EPPlus.Extensions.Tests/EPPlus.Extensions.Tests.csproj index 3c5b751..da728db 100644 --- a/src/EPPlus.Extensions.Tests/EPPlus.Extensions.Tests.csproj +++ b/src/EPPlus.Extensions.Tests/EPPlus.Extensions.Tests.csproj @@ -1,10 +1,10 @@ - + Debug AnyCPU - 2ccc31a1-9647-43ec-8179-acc7ab005ff2 + {2CCC31A1-9647-43EC-8179-ACC7AB005FF2} Library Properties EPPlus.Extensions.Tests @@ -30,25 +30,49 @@ 4 - - - - - - - - - - - - - - + + ..\packages\EPPlus.3.1.3.3\lib\net35\EPPlus.dll + True + + + ..\packages\Fixie.1.0.2\lib\net45\Fixie.dll + True + + + ..\packages\Shouldly.2.8.2\lib\net451\Shouldly.dll + True + + + + + + + + + - + + + + PreserveNewest + + + + PreserveNewest + + + + + {03756be7-6cc0-4d3e-80cf-3f7db49bc77a} + EPPlus.Extensions + + + + + - - + \ No newline at end of file diff --git a/src/EPPlus.Extensions.Tests/ExtensionTests.cs b/src/EPPlus.Extensions.Tests/ExtensionTests.cs new file mode 100644 index 0000000..e47fd63 --- /dev/null +++ b/src/EPPlus.Extensions.Tests/ExtensionTests.cs @@ -0,0 +1,138 @@ +using System; +using System.IO; +using System.Reflection; +using OfficeOpenXml; +using Shouldly; + +namespace EPPlus.Extensions.Tests +{ + public class ExtensionTests + { + public void ToDataSetSimple_ShouldHandleHeaderRows_WhenSpecified() + { + var package = GetMarvelPackage(); + + var result = package.ToDataSet(true); + + result.Tables[0].Rows.Count.ShouldBe(10); + } + + public void ToDataSetSimple_ShouldHandleHeaderRows_WhenNotSpecified() + { + var package = GetMarvelPackage(); + + var result = package.ToDataSet(false); + + result.Tables[0].Rows.Count.ShouldBe(11); + } + + public void ToDataSet_ShouldThrowException_WhenHeaderRowIsLessThanZero() + { + var package = new ExcelPackage(); + + var exception = Should.Throw(() => package.ToDataSet(-1)); + exception.ParamName.ShouldBe("headerRow"); + } + + public void ToDataSet_ShouldReturnOneTable_WhenOneSheet() + { + var package = GetStatesPackage(); + + var result = package.ToDataSet(0); + + result.Tables.Count.ShouldBe(1); + } + + public void ToDataSet_ShouldReturnTwoTables_WhenTwoSheets() + { + var package = GetMarvelPackage(); + + var result = package.ToDataSet(0); + + result.Tables.Count.ShouldBe(2); + } + + public void ToDataSet_ShouldHandleHeaderRows_WhenSetToZero() + { + var package = GetStatesPackage(); + + var result = package.ToDataSet(0); + + result.Tables[0].Rows.Count.ShouldBe(50); + } + + public void ToDataSet_ShouldHandleHeaderRows_WhenSetToOne() + { + var package = GetStatesPackage(); + + var result = package.ToDataSet(1); + + result.Tables[0].Rows.Count.ShouldBe(49); + } + + public void ToDataSet_ShouldHandleHeaderRows_WhenSetToTen() + { + var package = GetStatesPackage(); + + var result = package.ToDataSet(10); + + result.Tables[0].Rows.Count.ShouldBe(40); + } + + public void ToDataSet_ShouldNameColumnsWithHeaderValues_WhenHeaderValuesExist() + { + var package = GetMarvelPackage(); + + var result = package.ToDataSet(1); + + result.Tables[0].Columns[0].ColumnName.ShouldBe("First Name"); + result.Tables[0].Columns[1].ColumnName.ShouldBe("Last Name"); + result.Tables[0].Columns[2].ColumnName.ShouldBe("Alter Ego"); + } + + public void ToDataSet_ShouldUseGenericColumnNames_WhenHeaderValuesDoNotExist() + { + var package = GetStatesPackage(); + + var result = package.ToDataSet(0); + + result.Tables[0].Columns[0].ColumnName.ShouldBe("Column 1"); + } + + public void ToDataSet_ShouldAddColumns_ForEachSourceColumn() + { + var package = GetMarvelPackage(); + + var result = package.ToDataSet(0); + + result.Tables[0].Columns.Count.ShouldBe(3); + } + + public void ToDataSet_ShouldAddRows_ForEachSourceRow() + { + var package = GetStatesPackage(); + + var result = package.ToDataSet(0); + + result.Tables[0].Rows.Count.ShouldBe(50); + result.Tables[0].Rows[0][0].ToString().ShouldBe("Alabama"); + result.Tables[0].Rows[49][0].ToString().ShouldBe("Wyoming"); + } + + private static ExcelPackage GetMarvelPackage() + { + var path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), @"Marvel.xlsx"); + var file = new FileInfo(path); + var package = new ExcelPackage(file); + return package; + } + + private static ExcelPackage GetStatesPackage() + { + var path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), @"States.xlsx"); + var file = new FileInfo(path); + var package = new ExcelPackage(file); + return package; + } + } +} \ No newline at end of file diff --git a/src/EPPlus.Extensions.Tests/Marvel.xlsx b/src/EPPlus.Extensions.Tests/Marvel.xlsx new file mode 100644 index 0000000..4cdb7b7 Binary files /dev/null and b/src/EPPlus.Extensions.Tests/Marvel.xlsx differ diff --git a/src/EPPlus.Extensions.Tests/States.xlsx b/src/EPPlus.Extensions.Tests/States.xlsx new file mode 100644 index 0000000..4ebc17c Binary files /dev/null and b/src/EPPlus.Extensions.Tests/States.xlsx differ diff --git a/src/EPPlus.Extensions.Tests/packages.config b/src/EPPlus.Extensions.Tests/packages.config index 1506b15..3c3a05f 100644 --- a/src/EPPlus.Extensions.Tests/packages.config +++ b/src/EPPlus.Extensions.Tests/packages.config @@ -1,5 +1,6 @@  + \ No newline at end of file diff --git a/src/EPPlus.Extensions.sln b/src/EPPlus.Extensions.sln index 5137ffc..5d4d6ea 100644 --- a/src/EPPlus.Extensions.sln +++ b/src/EPPlus.Extensions.sln @@ -1,10 +1,12 @@  Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio 14 -VisualStudioVersion = 14.0.24720.0 +VisualStudioVersion = 14.0.25420.1 MinimumVisualStudioVersion = 10.0.40219.1 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EPPlus.Extensions", "EPPlus.Extensions\EPPlus.Extensions.csproj", "{03756BE7-6CC0-4D3E-80CF-3F7DB49BC77A}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EPPlus.Extensions.Tests", "EPPlus.Extensions.Tests\EPPlus.Extensions.Tests.csproj", "{2CCC31A1-9647-43EC-8179-ACC7AB005FF2}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -15,6 +17,10 @@ Global {03756BE7-6CC0-4D3E-80CF-3F7DB49BC77A}.Debug|Any CPU.Build.0 = Debug|Any CPU {03756BE7-6CC0-4D3E-80CF-3F7DB49BC77A}.Release|Any CPU.ActiveCfg = Release|Any CPU {03756BE7-6CC0-4D3E-80CF-3F7DB49BC77A}.Release|Any CPU.Build.0 = Release|Any CPU + {2CCC31A1-9647-43EC-8179-ACC7AB005FF2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {2CCC31A1-9647-43EC-8179-ACC7AB005FF2}.Debug|Any CPU.Build.0 = Debug|Any CPU + {2CCC31A1-9647-43EC-8179-ACC7AB005FF2}.Release|Any CPU.ActiveCfg = Release|Any CPU + {2CCC31A1-9647-43EC-8179-ACC7AB005FF2}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/src/EPPlus.Extensions/EPPlusExtensions.cs b/src/EPPlus.Extensions/EPPlusExtensions.cs index dd18749..7f2090e 100644 --- a/src/EPPlus.Extensions/EPPlusExtensions.cs +++ b/src/EPPlus.Extensions/EPPlusExtensions.cs @@ -31,7 +31,7 @@ public static DataSet ToDataSet(this ExcelPackage package, int headerRow = 0) { if (headerRow < 0) { - throw new ArgumentException("headerRow must be 0 or greater."); + throw new ArgumentOutOfRangeException(nameof(headerRow), headerRow, "Must be 0 or greater."); } var result = new DataSet();