Skip to content

Commit

Permalink
Merge pull request #421 from NetOfficeFw/dev/fix_excel_unit_tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jozefizso authored Jun 9, 2024
2 parents ad4b541 + 04b6a6c commit bff8b92
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,48 +18,49 @@ public class CustomXMLPartIntegrationTests
public void SetUp()
{
this.ExcelApplication = new Application();
this.Workbook = this.ExcelApplication.Workbooks.Add();
}

[TearDown]
public void TearDown()
{
this.Workbook.Close(false);
this.Workbook.Dispose();

this.ExcelApplication?.Quit();
this.ExcelApplication?.Dispose();
}

public Application ExcelApplication { get; set; }

public Workbook Workbook { get; set; }


[Test]
public void LoadXML_VadliXmlValue_LoadsIt()
public void LoadXML_VadidXmlValue_LoadsIt()
{
// Arrange
using (var workbook = this.ExcelApplication.Workbooks.Add())
{
CustomXMLPart cxp1 = workbook.CustomXMLParts.Add();
CustomXMLPart cxp1 = Workbook.CustomXMLParts.Add();

// Act
cxp1.LoadXML("<discounts><discount>0.10</discount></discounts>");
// Act
cxp1.LoadXML("<discounts><discount>0.10</discount></discounts>");

// Assert
Assert.AreEqual("<discounts><discount>0.10</discount></discounts>", cxp1.XML);
}
// Assert
Assert.AreEqual("<discounts><discount>0.10</discount></discounts>", cxp1.XML);
}

[Test]
public void Delete_ValidXmlPart_RemovesIt()
{
// Arrange
using (var workbook = this.ExcelApplication.Workbooks.Add())
{
CustomXMLPart cxp1 = workbook.CustomXMLParts.Add();
cxp1.LoadXML("<discounts><discount>0.10</discount></discounts>");
CustomXMLPart cxp1 = Workbook.CustomXMLParts.Add();
cxp1.LoadXML("<discounts><discount>0.10</discount></discounts>");

// Act
cxp1.Delete();
// Act
cxp1.Delete();

// Assert
Assert.Pass();
}
// Assert
Assert.Pass();
}

/// <summary>
Expand All @@ -69,17 +70,14 @@ public void Delete_ValidXmlPart_RemovesIt()
public void AddNode_NetOfficeCall_ShouldWork()
{
// Arrange
using (var workbook = this.ExcelApplication.Workbooks.Add())
{
CustomXMLPart cxp1 = workbook.CustomXMLParts.Add("<invoice />");
CustomXMLNode cxn = cxp1.SelectSingleNode("/invoice");
CustomXMLPart cxp1 = Workbook.CustomXMLParts.Add("<invoice />");
CustomXMLNode cxn = cxp1.SelectSingleNode("/invoice");

// Act
Assert.Throws<MethodCOMException>(() => cxp1.AddNode(cxn, "upcode", "urn:invoice:namespace"));
// Act
Assert.Throws<MethodCOMException>(() => cxp1.AddNode(cxn, "upcode", "urn:invoice:namespace"));

// Assert
// Assert.AreEqual(@"<invoice><upccode xmlns=""urn: invoice:namespace""/></invoice>", cxp1.XML);
}
// Assert
// Assert.AreEqual(@"<invoice><upccode xmlns=""urn: invoice:namespace""/></invoice>", cxp1.XML);
}

/// <summary>
Expand All @@ -89,17 +87,14 @@ public void AddNode_NetOfficeCall_ShouldWork()
public void AddNode_NullObjects_ShouldWork()
{
// Arrange
using (var workbook = this.ExcelApplication.Workbooks.Add())
{
CustomXMLPart cxp1 = workbook.CustomXMLParts.Add("<invoice />");
CustomXMLNode cxn = cxp1.SelectSingleNode("/invoice");
CustomXMLPart cxp1 = Workbook.CustomXMLParts.Add("<invoice />");
CustomXMLNode cxn = cxp1.SelectSingleNode("/invoice");

// Act
Assert.Throws<MethodCOMException>(() => cxp1.AddNode(cxn, null, null, null, null, null));
// Act
Assert.Throws<MethodCOMException>(() => cxp1.AddNode(cxn, null, null, null, null, null));

// Assert
// Assert.AreEqual(@"<invoice><upccode xmlns=""urn: invoice:namespace""/></invoice>", cxp1.XML);
}
// Assert
// Assert.AreEqual(@"<invoice><upccode xmlns=""urn: invoice:namespace""/></invoice>", cxp1.XML);
}

/// <summary>
Expand All @@ -109,20 +104,17 @@ public void AddNode_NullObjects_ShouldWork()
public void AddNode_DynamicCall_ShouldWork()
{
// Arrange
using (var workbook = this.ExcelApplication.Workbooks.Add())
{
var dyn = (dynamic)workbook.UnderlyingObject;
dynamic cxp1 = dyn.CustomXMLParts.Add("<invoice />");
dynamic cxn = cxp1.SelectSingleNode("/invoice");
cxn.GetType();

// Act
var ex = Assert.Throws<ArgumentException>(() => cxp1.AddNode(ref cxn, "upcode", "urn:invoice:namespace"));

// Assert
// Assert.AreEqual(@"<invoice><upccode xmlns=""urn: invoice:namespace""/></invoice>", cxp1.XML);
Assert.AreEqual("Could not convert argument 0 for call to AddNode.", ex.Message);
}
var dyn = (dynamic)Workbook.UnderlyingObject;
dynamic cxp1 = dyn.CustomXMLParts.Add("<invoice />");
dynamic cxn = cxp1.SelectSingleNode("/invoice");
cxn.GetType();

// Act
var ex = Assert.Throws<ArgumentException>(() => cxp1.AddNode(ref cxn, "upcode", "urn:invoice:namespace"));

// Assert
// Assert.AreEqual(@"<invoice><upccode xmlns=""urn: invoice:namespace""/></invoice>", cxp1.XML);
Assert.AreEqual("Could not convert argument 0 for call to AddNode.", ex.Message);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ public void AutoSaveOn_NewWorkbookDocument_ReturnsFalse()

// Assert
Assert.IsFalse(actual, "Workbook.AutoSaveOn property should be false.");

// Cleanup
workbook.Close(false);
}
}

Expand All @@ -49,6 +52,9 @@ public void AutoSaveOn_SetValueForLocalWorkbook_Fails([Values]bool autoSaveOnVal
var ex = Assert.Throws<PropertySetCOMException>(() => workbook.AutoSaveOn = autoSaveOnValue);

Assert.AreEqual("Failed to proceed PropertySet on Excel.Workbook=>AutoSaveOn.", ex.Message);

// Cleanup
workbook.Close(false);
}
}
}
Expand Down

0 comments on commit bff8b92

Please sign in to comment.