Added a test / changes to ensure null globals and/or null variable names do not affect outputs.

This commit is contained in:
Michael L Smith 2016-02-24 21:36:44 -08:00
parent 052760ecf1
commit 8678c3293d
4 changed files with 42 additions and 3 deletions

View File

@ -158,8 +158,8 @@ namespace OSCADSharp.UnitTests
public void OSCADObject_ToFileIncludesGlobalVariablesDefinedInSettings()
{
var cube = new Cube();
string[] output = null;
Settings.Globals["$fn"] = 100;
string[] output = null;
var mock = new Mock<IFileWriter>();
mock.Setup(_wrtr => _wrtr.WriteAllLines(It.IsAny<string>(), It.IsAny<string[]>()))
@ -167,8 +167,8 @@ namespace OSCADSharp.UnitTests
Settings.FileWriter = mock.Object;
cube.ToFile("myFile");
Assert.AreEqual("$fn = 100;\r\n", output[1]);
bool outputMatches = "$fn = 100;\r\n" == output[1];
Assert.IsTrue(outputMatches);
}
}
}

View File

@ -56,6 +56,7 @@
</Otherwise>
</Choose>
<ItemGroup>
<Compile Include="SettingsTests.cs" />
<Compile Include="Solids\CubeTests.cs" />
<Compile Include="Solids\CylinderTests.cs" />
<Compile Include="Transforms\HullTests.cs" />

View File

@ -0,0 +1,33 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using OSCADSharp.Scripting;
using OSCADSharp.Solids;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace OSCADSharp.UnitTests
{
[TestClass]
public class SettingsTests
{
[TestMethod]
public void Settings_NullVariablesDoNothing()
{
var cube = new Cube();
Settings.Globals["thing"] = null;
string[] output = null;
var mock = new Mock<IFileWriter>();
mock.Setup(_wrtr => _wrtr.WriteAllLines(It.IsAny<string>(), It.IsAny<string[]>()))
.Callback<string, string[]>((path, contents) => { output = contents; });
Settings.FileWriter = mock.Object;
cube.ToFile("myFile");
Assert.AreEqual("", output[1]);
}
}
}

View File

@ -54,6 +54,11 @@ namespace OSCADSharp.Scripting
StringBuilder sb = new StringBuilder();
foreach (var kvp in this.variables)
{
if(kvp.Value == null || String.IsNullOrEmpty(kvp.Value.ToString()) || String.IsNullOrEmpty(kvp.Key))
{
continue;
}
sb.Append(kvp.Key);
sb.Append(" = ");
sb.Append(kvp.Value);