Procházet zdrojové kódy

Fixed 30 tests in terms of running properly without creating directories and copying files manually. Should run straight from source.

master
MadBoy_cp před 12 roky
rodič
revize
1f547264f9
3 změnil soubory, kde provedl 163 přidání a 90 odebrání
  1. 90
    90
      UnitTests/DocXUnitTests.cs
  2. 72
    0
      UnitTests/RelativeDirectory.cs
  3. 1
    0
      UnitTests/UnitTests.csproj

+ 90
- 90
UnitTests/DocXUnitTests.cs Zobrazit soubor

[TestClass] [TestClass]
public class DocXUnitTests public class DocXUnitTests
{ {
// Get the fullpath to the executing assembly.
string directory_executing_assembly;
string directory_documents;
string file_temp = "temp.docx";
private readonly string _directoryDocuments;
private const string FileTemp = "temp.docx";
private readonly string _directoryWithFiles;


const string package_part_document = "/word/document.xml"; const string package_part_document = "/word/document.xml";


public DocXUnitTests() public DocXUnitTests()
{
directory_executing_assembly = Assembly.GetExecutingAssembly().Location;

// The documents directory
List<string> steps = directory_executing_assembly.Split('\\').ToList();
{
string directoryExecutingAssembly = Assembly.GetExecutingAssembly().Location; // Get the fullpath to the executing assembly.
List<string> steps = directoryExecutingAssembly.Split('\\').ToList(); // The documents directory
steps.RemoveRange(steps.Count() - 2, 2); steps.RemoveRange(steps.Count() - 2, 2);
directory_documents = String.Join("\\", steps) + "\\documents\\";
Setup(directory_documents);
_directoryDocuments = String.Join("\\", steps) + "\\documents\\";
Setup(_directoryDocuments); // prepare temp documents directory
RelativeDirectory rd = new RelativeDirectory(); // prepares the files for testing
rd.Up(3);
_directoryWithFiles = rd.Path + @"\UnitTests\documents\";
} }
private static void Setup(string path) private static void Setup(string path)
{ {
{ {
{"COURT NAME","Fred Frump"}, {"COURT NAME","Fred Frump"},
{"Case Number","cr-md-2011-1234567"} {"Case Number","cr-md-2011-1234567"}
};
using (DocX replaceDoc = DocX.Load(directory_documents + "ReplaceTests.docx"))
};
using (DocX replaceDoc = DocX.Load(_directoryWithFiles + "ReplaceTests.docx"))
{ {
foreach (var t in replaceDoc.Tables) foreach (var t in replaceDoc.Tables)
{ // each table has 1 row and 3 columns { // each table has 1 row and 3 columns
[TestMethod] [TestMethod]
public void Test_EverybodyHasAHome_Loaded() public void Test_EverybodyHasAHome_Loaded()
{ {
// Load a document.
using (DocX document = DocX.Load(directory_documents + "EverybodyHasAHome.docx"))
// Load a document.
using (DocX document = DocX.Load(_directoryWithFiles + "EverybodyHasAHome.docx"))
{ {
// Main document tests. // Main document tests.
string document_xml_file = document.mainPart.Uri.OriginalString; string document_xml_file = document.mainPart.Uri.OriginalString;
public void Test_Insert_Picture_ParagraphBeforeSelf() public void Test_Insert_Picture_ParagraphBeforeSelf()
{ {
// Create test document. // Create test document.
using (DocX document = DocX.Create(directory_documents + "Test.docx"))
using (DocX document = DocX.Create(_directoryDocuments + "Test.docx"))
{ {
// Add an Image to this document.
Novacode.Image img = document.AddImage(directory_documents + "purple.png");
// Add an Image to this document.
Novacode.Image img = document.AddImage(_directoryWithFiles + "purple.png");


// Create a Picture from this Image. // Create a Picture from this Image.
Picture pic = img.CreatePicture(); Picture pic = img.CreatePicture();
public void Test_Insert_Picture_ParagraphAfterSelf() public void Test_Insert_Picture_ParagraphAfterSelf()
{ {
// Create test document. // Create test document.
using (DocX document = DocX.Create(directory_documents + "Test.docx"))
using (DocX document = DocX.Create(_directoryDocuments + "Test.docx"))
{ {
// Add an Image to this document.
Novacode.Image img = document.AddImage(directory_documents + "purple.png");
// Add an Image to this document.
Novacode.Image img = document.AddImage(_directoryWithFiles + "purple.png");


// Create a Picture from this Image. // Create a Picture from this Image.
Picture pic = img.CreatePicture(); Picture pic = img.CreatePicture();
[TestMethod] [TestMethod]
public void Test_Document_AddImage_FromDisk() public void Test_Document_AddImage_FromDisk()
{ {
using (DocX document = DocX.Create(directory_documents + "test_add_images.docx"))
using (DocX document = DocX.Create(_directoryDocuments + "test_add_images.docx"))
{ {
// Add a png to into this document
Novacode.Image png = document.AddImage(directory_documents + "purple.png");
// Add a png to into this document
Novacode.Image png = document.AddImage(_directoryWithFiles + "purple.png");
Assert.IsTrue(document.Images.Count == 1); Assert.IsTrue(document.Images.Count == 1);
Assert.IsTrue(Path.GetExtension(png.pr.TargetUri.OriginalString) == ".png"); Assert.IsTrue(Path.GetExtension(png.pr.TargetUri.OriginalString) == ".png");


// Add a tiff into to this document
Novacode.Image tif = document.AddImage(directory_documents + "yellow.tif");
// Add a tiff into to this document
Novacode.Image tif = document.AddImage(_directoryWithFiles + "yellow.tif");
Assert.IsTrue(document.Images.Count == 2); Assert.IsTrue(document.Images.Count == 2);
Assert.IsTrue(Path.GetExtension(tif.pr.TargetUri.OriginalString) == ".tif"); Assert.IsTrue(Path.GetExtension(tif.pr.TargetUri.OriginalString) == ".tif");


// Add a gif into to this document
Novacode.Image gif = document.AddImage(directory_documents + "orange.gif");
// Add a gif into to this document
Novacode.Image gif = document.AddImage(_directoryWithFiles + "orange.gif");
Assert.IsTrue(document.Images.Count == 3); Assert.IsTrue(document.Images.Count == 3);
Assert.IsTrue(Path.GetExtension(gif.pr.TargetUri.OriginalString) == ".gif"); Assert.IsTrue(Path.GetExtension(gif.pr.TargetUri.OriginalString) == ".gif");


// Add a jpg into to this document
Novacode.Image jpg = document.AddImage(directory_documents + "green.jpg");
// Add a jpg into to this document
Novacode.Image jpg = document.AddImage(_directoryWithFiles + "green.jpg");
Assert.IsTrue(document.Images.Count == 4); Assert.IsTrue(document.Images.Count == 4);
Assert.IsTrue(Path.GetExtension(jpg.pr.TargetUri.OriginalString) == ".jpg"); Assert.IsTrue(Path.GetExtension(jpg.pr.TargetUri.OriginalString) == ".jpg");


// Add a bitmap to this document
Novacode.Image bitmap = document.AddImage(directory_documents + "red.bmp");
// Add a bitmap to this document
Novacode.Image bitmap = document.AddImage(_directoryWithFiles + "red.bmp");
Assert.IsTrue(document.Images.Count == 5); Assert.IsTrue(document.Images.Count == 5);
// Word does not allow bmp make sure it was inserted as a png. // Word does not allow bmp make sure it was inserted as a png.
Assert.IsTrue(Path.GetExtension(bitmap.pr.TargetUri.OriginalString) == ".png"); Assert.IsTrue(Path.GetExtension(bitmap.pr.TargetUri.OriginalString) == ".png");
[TestMethod] [TestMethod]
public void Test_Document_AddImage_FromStream() public void Test_Document_AddImage_FromStream()
{ {
using (DocX document = DocX.Create(directory_documents + "test_add_images.docx"))
using (DocX document = DocX.Create(_directoryDocuments + "test_add_images.docx"))
{ {
// DocX will always insert Images that come from Streams as jpeg. // DocX will always insert Images that come from Streams as jpeg.


// Add a png to into this document
Novacode.Image png = document.AddImage(new FileStream(directory_documents + "purple.png", FileMode.Open));
// Add a png to into this document
Novacode.Image png = document.AddImage(new FileStream(_directoryWithFiles + "purple.png", FileMode.Open));
Assert.IsTrue(document.Images.Count == 1); Assert.IsTrue(document.Images.Count == 1);
Assert.IsTrue(Path.GetExtension(png.pr.TargetUri.OriginalString) == ".jpeg"); Assert.IsTrue(Path.GetExtension(png.pr.TargetUri.OriginalString) == ".jpeg");


// Add a tiff into to this document
Novacode.Image tif = document.AddImage(new FileStream(directory_documents + "yellow.tif", FileMode.Open));
// Add a tiff into to this document
Novacode.Image tif = document.AddImage(new FileStream(_directoryWithFiles + "yellow.tif", FileMode.Open));
Assert.IsTrue(document.Images.Count == 2); Assert.IsTrue(document.Images.Count == 2);
Assert.IsTrue(Path.GetExtension(tif.pr.TargetUri.OriginalString) == ".jpeg"); Assert.IsTrue(Path.GetExtension(tif.pr.TargetUri.OriginalString) == ".jpeg");


// Add a gif into to this document
Novacode.Image gif = document.AddImage(new FileStream(directory_documents + "orange.gif", FileMode.Open));
// Add a gif into to this document
Novacode.Image gif = document.AddImage(new FileStream(_directoryWithFiles + "orange.gif", FileMode.Open));
Assert.IsTrue(document.Images.Count == 3); Assert.IsTrue(document.Images.Count == 3);
Assert.IsTrue(Path.GetExtension(gif.pr.TargetUri.OriginalString) == ".jpeg"); Assert.IsTrue(Path.GetExtension(gif.pr.TargetUri.OriginalString) == ".jpeg");


// Add a jpg into to this document
Novacode.Image jpg = document.AddImage(new FileStream(directory_documents + "green.jpg", FileMode.Open));
// Add a jpg into to this document
Novacode.Image jpg = document.AddImage(new FileStream(_directoryWithFiles + "green.jpg", FileMode.Open));
Assert.IsTrue(document.Images.Count == 4); Assert.IsTrue(document.Images.Count == 4);
Assert.IsTrue(Path.GetExtension(jpg.pr.TargetUri.OriginalString) == ".jpeg"); Assert.IsTrue(Path.GetExtension(jpg.pr.TargetUri.OriginalString) == ".jpeg");


// Add a bitmap to this document
Novacode.Image bitmap = document.AddImage(new FileStream(directory_documents + "red.bmp", FileMode.Open));
// Add a bitmap to this document
Novacode.Image bitmap = document.AddImage(new FileStream(_directoryWithFiles + "red.bmp", FileMode.Open));
Assert.IsTrue(document.Images.Count == 5); Assert.IsTrue(document.Images.Count == 5);
// Word does not allow bmp make sure it was inserted as a png. // Word does not allow bmp make sure it was inserted as a png.
Assert.IsTrue(Path.GetExtension(bitmap.pr.TargetUri.OriginalString) == ".jpeg"); Assert.IsTrue(Path.GetExtension(bitmap.pr.TargetUri.OriginalString) == ".jpeg");


[TestMethod] [TestMethod]
public void Test_Tables() public void Test_Tables()
{
using (DocX document = DocX.Load(directory_documents + "Tables.docx"))
{
using (DocX document = DocX.Load(_directoryWithFiles + "Tables.docx"))
{ {
// There is only one Paragraph at the document level. // There is only one Paragraph at the document level.
Assert.IsTrue(document.Paragraphs.Count() == 13); Assert.IsTrue(document.Paragraphs.Count() == 13);


[TestMethod] [TestMethod]
public void Test_Images() public void Test_Images()
{
using (DocX document = DocX.Load(directory_documents + "Images.docx"))
{
using (DocX document = DocX.Load(_directoryWithFiles + "Images.docx"))
{ {
// Extract Images from Document. // Extract Images from Document.
List<Novacode.Image> document_images = document.Images; List<Novacode.Image> document_images = document.Images;
public void Test_Insert_Picture() public void Test_Insert_Picture()
{ {
// Load test document. // Load test document.
using (DocX document = DocX.Create(directory_documents + "Test.docx"))
using (DocX document = DocX.Create(_directoryDocuments + "Test.docx"))
{ {
// Add Headers and Footers into this document. // Add Headers and Footers into this document.
document.AddHeaders(); document.AddHeaders();
document.DifferentFirstPage = true; document.DifferentFirstPage = true;
document.DifferentOddAndEvenPages = true; document.DifferentOddAndEvenPages = true;


// Add an Image to this document.
Novacode.Image img = document.AddImage(directory_documents + "purple.png");
// Add an Image to this document.
Novacode.Image img = document.AddImage(_directoryWithFiles + "purple.png");


// Create a Picture from this Image. // Create a Picture from this Image.
Picture pic = img.CreatePicture(); Picture pic = img.CreatePicture();
public void Test_Insert_Hyperlink() public void Test_Insert_Hyperlink()
{ {
// Load test document. // Load test document.
using (DocX document = DocX.Create(directory_documents + "Test.docx"))
using (DocX document = DocX.Create(_directoryDocuments + "Test.docx"))
{ {
// Add Headers and Footers into this document. // Add Headers and Footers into this document.
document.AddHeaders(); document.AddHeaders();
[TestMethod] [TestMethod]
public void Test_Get_Set_Hyperlink() public void Test_Get_Set_Hyperlink()
{ {
// Load test document.
using (DocX document = DocX.Load(directory_documents + "Hyperlinks.docx"))
// Load test document.
using (DocX document = DocX.Load(_directoryWithFiles + "Hyperlinks.docx"))
{ {
// Hyperlinks in the document. // Hyperlinks in the document.
Assert.IsTrue(document.Hyperlinks.Count == 3); Assert.IsTrue(document.Hyperlinks.Count == 3);
public void Test_Append_Hyperlink() public void Test_Append_Hyperlink()
{ {
// Load test document. // Load test document.
using (DocX document = DocX.Create(directory_documents + "Test.docx"))
using (DocX document = DocX.Create(_directoryDocuments + "Test.docx"))
{ {
// Add Headers and Footers into this document. // Add Headers and Footers into this document.
document.AddHeaders(); document.AddHeaders();


[TestMethod] [TestMethod]
public void Test_Append_Picture() public void Test_Append_Picture()
{
{
// Create test document. // Create test document.
using (DocX document = DocX.Create(directory_documents + "Test.docx"))
using (DocX document = DocX.Create(_directoryDocuments + "Test.docx"))
{ {
// Add Headers and Footers into this document. // Add Headers and Footers into this document.
document.AddHeaders(); document.AddHeaders();
document.DifferentFirstPage = true; document.DifferentFirstPage = true;
document.DifferentOddAndEvenPages = true; document.DifferentOddAndEvenPages = true;


// Add an Image to this document.
Novacode.Image img = document.AddImage(directory_documents + "purple.png");
// Add an Image to this document.
Novacode.Image img = document.AddImage(_directoryWithFiles + "purple.png");


// Create a Picture from this Image. // Create a Picture from this Image.
Picture pic = img.CreatePicture(); Picture pic = img.CreatePicture();
[TestMethod] [TestMethod]
public void Test_Move_Picture_Load() public void Test_Move_Picture_Load()
{ {
// Load test document.
using (DocX document = DocX.Load(directory_documents + "MovePicture.docx"))
// Load test document.
using (DocX document = DocX.Load(_directoryWithFiles + "MovePicture.docx"))
{ {
// Extract the first Picture from the first Paragraph. // Extract the first Picture from the first Paragraph.
Picture picture = document.Paragraphs.First().Pictures.First(); Picture picture = document.Paragraphs.First().Pictures.First();
footer_odd.Paragraphs.First().AppendPicture(picture); footer_odd.Paragraphs.First().AppendPicture(picture);


// Save this as MovedPicture.docx // Save this as MovedPicture.docx
document.SaveAs(directory_documents + "MovedPicture.docx");
document.SaveAs(_directoryDocuments + "MovedPicture.docx");
} }
} }


[TestMethod] [TestMethod]
public void Test_Document_Paragraphs() public void Test_Document_Paragraphs()
{ {
// Load the document 'Paragraphs.docx'
using (DocX document = DocX.Load(directory_documents + "Paragraphs.docx"))
// Load the document 'Paragraphs.docx'
using (DocX document = DocX.Load(_directoryWithFiles + "Paragraphs.docx"))
{ {
// Extract the Paragraphs from this document. // Extract the Paragraphs from this document.
List<Paragraph> paragraphs = document.Paragraphs; List<Paragraph> paragraphs = document.Paragraphs;
document.Paragraphs.ForEach(p => Assert.IsTrue(p.PackagePart.Uri.ToString() == package_part_document)); document.Paragraphs.ForEach(p => Assert.IsTrue(p.PackagePart.Uri.ToString() == package_part_document));


// Test the saving of the document. // Test the saving of the document.
document.SaveAs(file_temp);
document.SaveAs(FileTemp);
} }


// Delete the tempory file. // Delete the tempory file.
File.Delete(file_temp);
File.Delete(FileTemp);
} }


[TestMethod] [TestMethod]
public void Test_Table_InsertRowAndColumn() public void Test_Table_InsertRowAndColumn()
{ {
// Create a table // Create a table
using (DocX document = DocX.Create(directory_documents + "Tables2.docx"))
using (DocX document = DocX.Create(_directoryDocuments + "Tables2.docx"))
{ {
// Add a Table to a document. // Add a Table to a document.
Table t = document.AddTable(2, 2); Table t = document.AddTable(2, 2);
} }


// Check table // Check table
using (DocX document = DocX.Load(directory_documents + "Tables2.docx"))
using (DocX document = DocX.Load(_directoryDocuments + "Tables2.docx"))
{ {
// Get a table from a document // Get a table from a document
Table t = document.Tables[0]; Table t = document.Tables[0];
using (MemoryStream documentStream = new MemoryStream()) using (MemoryStream documentStream = new MemoryStream())
{ {
using (DocX document = DocX.Create(documentStream)) using (DocX document = DocX.Create(documentStream))
{
document.ApplyTemplate(directory_documents + "Template.dotx");
{
document.ApplyTemplate(_directoryWithFiles + "Template.dotx");
document.Save(); document.Save();


Header firstHeader = document.Headers.first; Header firstHeader = document.Headers.first;


[TestMethod] [TestMethod]
public void Test_ParentContainer_When_Reading_Doc() public void Test_ParentContainer_When_Reading_Doc()
{
using (DocX document = DocX.Load(directory_documents + "Tables.docx"))
{
using (DocX document = DocX.Load(_directoryWithFiles + "Tables.docx"))
{ {
List<Paragraph> paragraphs = document.Paragraphs; List<Paragraph> paragraphs = document.Paragraphs;




[TestMethod] [TestMethod]
public void Test_Section_Count_When_Reading_Doc() public void Test_Section_Count_When_Reading_Doc()
{
using (DocX document = DocX.Load(directory_documents + "testdoc_SectionsWithSectionBreaks.docx"))
{
using (DocX document = DocX.Load(_directoryWithFiles + "testdoc_SectionsWithSectionBreaks.docx"))
{ {
var sections = document.GetSections(); var sections = document.GetSections();




[TestMethod] [TestMethod]
public void Test_Section_Paragraph_Count_Match_When_Reading_Doc() public void Test_Section_Paragraph_Count_Match_When_Reading_Doc()
{
using (DocX document = DocX.Load(directory_documents + "testdoc_SectionsWithSectionBreaksMultiParagraph.docx"))
{
using (DocX document = DocX.Load(_directoryWithFiles + "testdoc_SectionsWithSectionBreaksMultiParagraph.docx"))
{ {


var sections = document.GetSections(); var sections = document.GetSections();


[TestMethod] [TestMethod]
public void Test_Section_Paragraph_Content_Match_When_Reading_Doc() public void Test_Section_Paragraph_Content_Match_When_Reading_Doc()
{
using (DocX document = DocX.Load(directory_documents + "testdoc_SectionsWithSectionBreaks.docx"))
{
using (DocX document = DocX.Load(_directoryWithFiles + "testdoc_SectionsWithSectionBreaks.docx"))
{ {


var sections = document.GetSections(); var sections = document.GetSections();


[TestMethod] [TestMethod]
public void Test_Ordered_List_When_Reading_Doc() public void Test_Ordered_List_When_Reading_Doc()
{
using (DocX document = DocX.Load(directory_documents + "testdoc_OrderedList.docx"))
{
using (DocX document = DocX.Load(_directoryWithFiles + "testdoc_OrderedList.docx"))
{ {


var sections = document.GetSections(); var sections = document.GetSections();


[TestMethod] [TestMethod]
public void Test_Unordered_List_When_Reading_Doc() public void Test_Unordered_List_When_Reading_Doc()
{
using (DocX document = DocX.Load(directory_documents + "testdoc_UnorderedList.docx"))
{
using (DocX document = DocX.Load(_directoryWithFiles + "testdoc_UnorderedList.docx"))
{ {


var sections = document.GetSections(); var sections = document.GetSections();


[TestMethod] [TestMethod]
public void Test_Ordered_Unordered_Lists_When_Reading_Doc() public void Test_Ordered_Unordered_Lists_When_Reading_Doc()
{
using (DocX document = DocX.Load(directory_documents + "testdoc_OrderedUnorderedLists.docx"))
{
using (DocX document = DocX.Load(_directoryWithFiles + "testdoc_OrderedUnorderedLists.docx"))
{ {


var sections = document.GetSections(); var sections = document.GetSections();


[TestMethod] [TestMethod]
public void WhenADocumentHasListsTheListPropertyReturnsTheCorrectNumberOfLists() public void WhenADocumentHasListsTheListPropertyReturnsTheCorrectNumberOfLists()
{
using (DocX document = DocX.Load(directory_documents + "testdoc_OrderedUnorderedLists.docx"))
{
using (DocX document = DocX.Load(_directoryWithFiles + "testdoc_OrderedUnorderedLists.docx"))
{ {
var lists = document.Lists; var lists = document.Lists;




[TestMethod] [TestMethod]
public void WhileReadingWhenTextIsBoldItalicUnderlineItShouldReadTheProperFormatting() public void WhileReadingWhenTextIsBoldItalicUnderlineItShouldReadTheProperFormatting()
{
using (DocX document = DocX.Load(directory_documents + "FontFormat.docx"))
{
using (DocX document = DocX.Load(_directoryWithFiles + "FontFormat.docx"))
{ {
var underlinedTextFormatting = document.Paragraphs[0].MagicText[0].formatting; var underlinedTextFormatting = document.Paragraphs[0].MagicText[0].formatting;
var boldTextFormatting = document.Paragraphs[0].MagicText[2].formatting; var boldTextFormatting = document.Paragraphs[0].MagicText[2].formatting;

+ 72
- 0
UnitTests/RelativeDirectory.cs Zobrazit soubor

using System;
using System.IO;
namespace UnitTests
{
class RelativeDirectory
{
// Author D. Bolton see http://cplus.about.com (c) 2010
private DirectoryInfo _dirInfo;
public string Dir
{
get
{
return _dirInfo.Name;
}
}
public string Path
{
get { return _dirInfo.FullName; }
set
{
try
{
DirectoryInfo newDir = new DirectoryInfo(value);
_dirInfo = newDir;
}
catch
{
// silent
}
}
}
public RelativeDirectory()
{
_dirInfo = new DirectoryInfo(Environment.CurrentDirectory);
}
public RelativeDirectory(string absoluteDir)
{
_dirInfo = new DirectoryInfo(absoluteDir);
}
public Boolean Up(int numLevels)
{
for (int i = 0; i < numLevels; i++)
{
DirectoryInfo tempDir = _dirInfo.Parent;
if (tempDir != null)
_dirInfo = tempDir;
else
return false;
}
return true;
}
public Boolean Up()
{
return Up(1);
}
public Boolean Down(string match)
{
DirectoryInfo[] dirs = _dirInfo.GetDirectories(match + '*');
_dirInfo = dirs[0];
return true;
}
}
}

+ 1
- 0
UnitTests/UnitTests.csproj Zobrazit soubor

<ItemGroup> <ItemGroup>
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="DocXUnitTests.cs" /> <Compile Include="DocXUnitTests.cs" />
<Compile Include="RelativeDirectory.cs" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<None Include="documents\EverybodyHasAHome.docx" /> <None Include="documents\EverybodyHasAHome.docx" />

Načítá se…
Zrušit
Uložit