Skip to content

Commit 7065001

Browse files
committed
Add UnitTests
1 parent 362c6a5 commit 7065001

File tree

8 files changed

+196
-2
lines changed

8 files changed

+196
-2
lines changed

MergePDF.sln

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ VisualStudioVersion = 15.0.27130.2026
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MergePDF", "MergePDF\MergePDF.csproj", "{8BF6B775-F54A-4590-959B-FD6D5AEC1280}"
77
EndProject
8+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MergePDFTests", "MergePDFTests\MergePDFTests.csproj", "{116308E4-C514-461A-B222-AEC05F1DFEFC}"
9+
EndProject
810
Global
911
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1012
Debug|Any CPU = Debug|Any CPU
@@ -15,6 +17,10 @@ Global
1517
{8BF6B775-F54A-4590-959B-FD6D5AEC1280}.Debug|Any CPU.Build.0 = Debug|Any CPU
1618
{8BF6B775-F54A-4590-959B-FD6D5AEC1280}.Release|Any CPU.ActiveCfg = Release|Any CPU
1719
{8BF6B775-F54A-4590-959B-FD6D5AEC1280}.Release|Any CPU.Build.0 = Release|Any CPU
20+
{116308E4-C514-461A-B222-AEC05F1DFEFC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
21+
{116308E4-C514-461A-B222-AEC05F1DFEFC}.Debug|Any CPU.Build.0 = Debug|Any CPU
22+
{116308E4-C514-461A-B222-AEC05F1DFEFC}.Release|Any CPU.ActiveCfg = Release|Any CPU
23+
{116308E4-C514-461A-B222-AEC05F1DFEFC}.Release|Any CPU.Build.0 = Release|Any CPU
1824
EndGlobalSection
1925
GlobalSection(SolutionProperties) = preSolution
2026
HideSolutionNode = FALSE

MergePDF/PdfMerger.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,7 @@ public void MergeFiles(string outputFile = null)
5959
{
6060
foreach (var document in Documents)
6161
{
62-
document.Replace(@"\", "/");
63-
PdfDocument importPdf = PdfReader.Open(document, PdfDocumentOpenMode.Import);
62+
PdfDocument importPdf = PdfReader.Open(document.Replace(@"\", "/"), PdfDocumentOpenMode.Import);
6463
CopyPages(importPdf, outPdf);
6564
}
6665
var outputLocation = OutputFile.Replace(@"\", "/");

MergePDFTests/MergePDFTests.csproj

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="15.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<Import Project="..\packages\MSTest.TestAdapter.1.3.2\build\net45\MSTest.TestAdapter.props" Condition="Exists('..\packages\MSTest.TestAdapter.1.3.2\build\net45\MSTest.TestAdapter.props')" />
4+
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
5+
<PropertyGroup>
6+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
7+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
8+
<ProjectGuid>{116308E4-C514-461A-B222-AEC05F1DFEFC}</ProjectGuid>
9+
<OutputType>Library</OutputType>
10+
<AppDesignerFolder>Properties</AppDesignerFolder>
11+
<RootNamespace>MergePDFTests</RootNamespace>
12+
<AssemblyName>MergePDFTests</AssemblyName>
13+
<TargetFrameworkVersion>v4.7.2</TargetFrameworkVersion>
14+
<FileAlignment>512</FileAlignment>
15+
<ProjectTypeGuids>{3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
16+
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">15.0</VisualStudioVersion>
17+
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
18+
<ReferencePath>$(ProgramFiles)\Common Files\microsoft shared\VSTT\$(VisualStudioVersion)\UITestExtensionPackages</ReferencePath>
19+
<IsCodedUITest>False</IsCodedUITest>
20+
<TestProjectType>UnitTest</TestProjectType>
21+
<NuGetPackageImportStamp>
22+
</NuGetPackageImportStamp>
23+
</PropertyGroup>
24+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
25+
<DebugSymbols>true</DebugSymbols>
26+
<DebugType>full</DebugType>
27+
<Optimize>false</Optimize>
28+
<OutputPath>bin\Debug\</OutputPath>
29+
<DefineConstants>DEBUG;TRACE</DefineConstants>
30+
<ErrorReport>prompt</ErrorReport>
31+
<WarningLevel>4</WarningLevel>
32+
</PropertyGroup>
33+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
34+
<DebugType>pdbonly</DebugType>
35+
<Optimize>true</Optimize>
36+
<OutputPath>bin\Release\</OutputPath>
37+
<DefineConstants>TRACE</DefineConstants>
38+
<ErrorReport>prompt</ErrorReport>
39+
<WarningLevel>4</WarningLevel>
40+
</PropertyGroup>
41+
<ItemGroup>
42+
<Reference Include="Microsoft.VisualStudio.TestPlatform.TestFramework, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
43+
<HintPath>..\packages\MSTest.TestFramework.1.3.2\lib\net45\Microsoft.VisualStudio.TestPlatform.TestFramework.dll</HintPath>
44+
</Reference>
45+
<Reference Include="Microsoft.VisualStudio.TestPlatform.TestFramework.Extensions, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
46+
<HintPath>..\packages\MSTest.TestFramework.1.3.2\lib\net45\Microsoft.VisualStudio.TestPlatform.TestFramework.Extensions.dll</HintPath>
47+
</Reference>
48+
<Reference Include="PdfSharp, Version=1.50.4820.0, Culture=neutral, PublicKeyToken=f94615aa0424f9eb, processorArchitecture=MSIL">
49+
<HintPath>..\packages\PDFsharp.1.50.4820-RC1\lib\net20\PdfSharp.dll</HintPath>
50+
</Reference>
51+
<Reference Include="PdfSharp.Charting, Version=1.50.4820.0, Culture=neutral, PublicKeyToken=f94615aa0424f9eb, processorArchitecture=MSIL">
52+
<HintPath>..\packages\PDFsharp.1.50.4820-RC1\lib\net20\PdfSharp.Charting.dll</HintPath>
53+
</Reference>
54+
<Reference Include="System" />
55+
<Reference Include="System.Core" />
56+
<Reference Include="System.Drawing" />
57+
</ItemGroup>
58+
<ItemGroup>
59+
<Compile Include="UnitTests.cs" />
60+
<Compile Include="Properties\AssemblyInfo.cs" />
61+
</ItemGroup>
62+
<ItemGroup>
63+
<None Include="packages.config" />
64+
<None Include="TestFiles\test_1.pdf" />
65+
<None Include="TestFiles\test_2.pdf" />
66+
</ItemGroup>
67+
<ItemGroup>
68+
<ProjectReference Include="..\MergePDF\MergePDF.csproj">
69+
<Project>{8bf6b775-f54a-4590-959b-fd6d5aec1280}</Project>
70+
<Name>MergePDF</Name>
71+
</ProjectReference>
72+
</ItemGroup>
73+
<Import Project="$(VSToolsPath)\TeamTest\Microsoft.TestTools.targets" Condition="Exists('$(VSToolsPath)\TeamTest\Microsoft.TestTools.targets')" />
74+
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
75+
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
76+
<PropertyGroup>
77+
<ErrorText>This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
78+
</PropertyGroup>
79+
<Error Condition="!Exists('..\packages\MSTest.TestAdapter.1.3.2\build\net45\MSTest.TestAdapter.props')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\MSTest.TestAdapter.1.3.2\build\net45\MSTest.TestAdapter.props'))" />
80+
<Error Condition="!Exists('..\packages\MSTest.TestAdapter.1.3.2\build\net45\MSTest.TestAdapter.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\MSTest.TestAdapter.1.3.2\build\net45\MSTest.TestAdapter.targets'))" />
81+
</Target>
82+
<Import Project="..\packages\MSTest.TestAdapter.1.3.2\build\net45\MSTest.TestAdapter.targets" Condition="Exists('..\packages\MSTest.TestAdapter.1.3.2\build\net45\MSTest.TestAdapter.targets')" />
83+
</Project>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using System.Reflection;
2+
using System.Runtime.CompilerServices;
3+
using System.Runtime.InteropServices;
4+
5+
[assembly: AssemblyTitle("MergePDFTests")]
6+
[assembly: AssemblyDescription("")]
7+
[assembly: AssemblyConfiguration("")]
8+
[assembly: AssemblyCompany("")]
9+
[assembly: AssemblyProduct("MergePDFTests")]
10+
[assembly: AssemblyCopyright("Copyright © 2018")]
11+
[assembly: AssemblyTrademark("")]
12+
[assembly: AssemblyCulture("")]
13+
14+
[assembly: ComVisible(false)]
15+
16+
[assembly: Guid("116308e4-c514-461a-b222-aec05f1dfefc")]
17+
18+
// [assembly: AssemblyVersion("1.0.*")]
19+
[assembly: AssemblyVersion("1.0.0.0")]
20+
[assembly: AssemblyFileVersion("1.0.0.0")]

MergePDFTests/TestFiles/test_1.pdf

16.1 KB
Binary file not shown.

MergePDFTests/TestFiles/test_2.pdf

16.2 KB
Binary file not shown.

MergePDFTests/UnitTests.cs

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
using System;
2+
using Microsoft.VisualStudio.TestTools.UnitTesting;
3+
using MergePDF;
4+
using PdfSharp.Pdf.IO;
5+
6+
namespace MergePDFTests
7+
{
8+
[TestClass]
9+
public class UnitTests
10+
{
11+
private const string PDF_TEST_FILE_1 = @"..\..\TestFiles\test_1.pdf";
12+
private const string PDF_TEST_FILE_2 = @"..\..\TestFiles\test_2.pdf";
13+
private const string PDF_OUT = "out.pdf";
14+
15+
[TestMethod]
16+
public void Merge_with_no_files_Fail()
17+
{
18+
PdfMerger pdfMerger = new PdfMerger();
19+
Assert.ThrowsException<Exception>(() => pdfMerger.MergeFiles());
20+
}
21+
22+
[TestMethod]
23+
public void Merge_with_not_enough_files_Fail()
24+
{
25+
PdfMerger pdfMerger = new PdfMerger();
26+
pdfMerger.AddFile(PDF_TEST_FILE_1);
27+
28+
if (System.IO.File.Exists(PDF_OUT))
29+
System.IO.File.Delete(PDF_OUT);
30+
31+
Assert.ThrowsException<Exception>(() => pdfMerger.MergeFiles(PDF_OUT));
32+
}
33+
34+
[TestMethod]
35+
public void Merge_with_no_out_file_Fail()
36+
{
37+
PdfMerger pdfMerger = new PdfMerger();
38+
pdfMerger.AddFile(PDF_TEST_FILE_1);
39+
pdfMerger.AddFile(PDF_TEST_FILE_2);
40+
41+
if (System.IO.File.Exists(PDF_OUT))
42+
System.IO.File.Delete(PDF_OUT);
43+
44+
Assert.ThrowsException<Exception>(() => pdfMerger.MergeFiles());
45+
}
46+
47+
[TestMethod]
48+
public void Merge_files_Pass()
49+
{
50+
PdfMerger pdfMerger = new PdfMerger();
51+
pdfMerger.AddFile(PDF_TEST_FILE_1);
52+
pdfMerger.AddFile(PDF_TEST_FILE_2);
53+
54+
if (System.IO.File.Exists(PDF_OUT))
55+
System.IO.File.Delete(PDF_OUT);
56+
57+
pdfMerger.MergeFiles(PDF_OUT);
58+
59+
Assert.IsTrue(System.IO.File.Exists(PDF_OUT));
60+
}
61+
62+
[TestMethod]
63+
public void Merge_files_pageCount_verify_Pass()
64+
{
65+
PdfMerger pdfMerger = new PdfMerger();
66+
pdfMerger.AddFile(PDF_TEST_FILE_1);
67+
pdfMerger.AddFile(PDF_TEST_FILE_2);
68+
pdfMerger.AddFile(PDF_TEST_FILE_1);
69+
pdfMerger.AddFile(PDF_TEST_FILE_2);
70+
71+
if (System.IO.File.Exists(PDF_OUT))
72+
System.IO.File.Delete(PDF_OUT);
73+
74+
pdfMerger.MergeFiles(PDF_OUT);
75+
76+
var count = PdfReader.Open(PDF_OUT.Replace(@"\", "/"), PdfDocumentOpenMode.Import).PageCount;
77+
Assert.AreEqual(pdfMerger.Documents.Count, count);
78+
}
79+
}
80+
}

MergePDFTests/packages.config

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<packages>
3+
<package id="MSTest.TestAdapter" version="1.3.2" targetFramework="net472" />
4+
<package id="MSTest.TestFramework" version="1.3.2" targetFramework="net472" />
5+
<package id="PDFsharp" version="1.50.4820-RC1" targetFramework="net472" />
6+
</packages>

0 commit comments

Comments
 (0)