Skip to content

Commit

Permalink
#138, #373 Added unit tests for analog input and Variable Input Action
Browse files Browse the repository at this point in the history
  • Loading branch information
DocMoebiuz committed Jul 18, 2021
1 parent fca9192 commit f7e5a41
Show file tree
Hide file tree
Showing 8 changed files with 189 additions and 1 deletion.
1 change: 0 additions & 1 deletion MobiFlight/InputConfig/VariableInputAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ public class VariableInputAction : InputAction, ICloneable
new public const String Label = "MobiFlight Variable";
public const String TYPE = "VariableInputAction";
public MobiFlightVariable Variable = new MobiFlightVariable();
public String Value = "0";

override public object Clone()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using MobiFlight.InputConfig;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace MobiFlight.InputConfig.Tests
{
[TestClass()]
public class AnalogInputConfigTests
{
[TestMethod()]
public void CloneTest()
{
AnalogInputConfig o = generateTestObject();
AnalogInputConfig c = (AnalogInputConfig) o.Clone();

Assert.AreNotSame(o, c, "Cloned object is the same");
Assert.AreEqual((o.onChange as VariableInputAction).Variable.Name, (c.onChange as VariableInputAction).Variable.Name, "onChange is not correct");
}

private AnalogInputConfig generateTestObject()
{
AnalogInputConfig o = new AnalogInputConfig();
o.onChange = new VariableInputAction();
(o.onChange as VariableInputAction).Variable.Name = "AnalogInputConfigTest";
return o;
}

[TestMethod()]
public void GetSchemaTest()
{
ButtonInputConfig o = new ButtonInputConfig();
Assert.IsNull(o.GetSchema());
}

[TestMethod()]
public void ReadXmlTest()
{
AnalogInputConfig o = new AnalogInputConfig();
String s = System.IO.File.ReadAllText(@"assets\MobiFlight\InputConfig\AnalogInputConfig\ReadXmlTest.1.xml");
StringReader sr = new StringReader(s);
XmlReaderSettings settings = new XmlReaderSettings();
settings.IgnoreWhitespace = true;

System.Xml.XmlReader xmlReader = System.Xml.XmlReader.Create(sr, settings);
xmlReader.ReadToDescendant("analog");
o.ReadXml(xmlReader);

Assert.AreEqual("AnalogInputConfigReadXML", (o.onChange as VariableInputAction).Variable.Name, "Variable.Name are not Equal");

}

[TestMethod()]
public void WriteXmlTest()
{
StringWriter sw = new StringWriter();
XmlWriterSettings settings = new XmlWriterSettings();
settings.Encoding = System.Text.Encoding.UTF8;
settings.Indent = true;
//settings.NewLineHandling = NewLineHandling.Entitize;
System.Xml.XmlWriter xmlWriter = System.Xml.XmlWriter.Create(sw, settings);

AnalogInputConfig o = generateTestObject();
xmlWriter.WriteStartElement("analog");
o.WriteXml(xmlWriter);
xmlWriter.WriteEndElement();
xmlWriter.Flush();
string s = sw.ToString();

String result = System.IO.File.ReadAllText(@"assets\MobiFlight\InputConfig\AnalogInputConfig\WriteXmlTest.1.xml");

Assert.AreEqual(result, s, "The both strings are not equal");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using MobiFlight.InputConfig;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace MobiFlight.InputConfig.Tests
{
[TestClass()]
public class VariableInputActionTests
{
[TestMethod()]
public void CloneTest()
{
VariableInputAction o = generateTestObject();
VariableInputAction c = (VariableInputAction) o.Clone();
Assert.AreNotSame(o, c, "Clone is the same object");
Assert.AreEqual(o.Variable.TYPE, c.Variable.TYPE, "EventId not the same");
Assert.AreEqual(o.Variable.Name, c.Variable.Name, "EventId not the same");
Assert.AreEqual(o.Variable.Expression, c.Variable.Expression, "EventId not the same");
}

private VariableInputAction generateTestObject()
{
VariableInputAction o = new VariableInputAction();
o.Variable.Name = "VariableInputActionTests";
o.Variable.TYPE = "string";
o.Variable.Text = "VariableInputActionTests";
o.Variable.Expression = "$+1";
return o;
}

[TestMethod()]
public void ReadXmlTest()
{
VariableInputAction o = new VariableInputAction();
String s = System.IO.File.ReadAllText(@"assets\MobiFlight\InputConfig\VariableInputAction\ReadXmlTest.1.xml");
StringReader sr = new StringReader(s);
XmlReaderSettings settings = new XmlReaderSettings();
settings.IgnoreWhitespace = true;

System.Xml.XmlReader xmlReader = System.Xml.XmlReader.Create(sr, settings);
xmlReader.ReadToDescendant("onPress");
o.ReadXml(xmlReader);

Assert.AreEqual(o.Variable.TYPE, "string", "Variable.TYPE are not the same");
Assert.AreEqual(o.Variable.Name, "VariableInputActionReadXMLTests", "Param not the same");
}

[TestMethod()]
public void WriteXmlTest()
{
StringWriter sw = new StringWriter();
XmlWriterSettings settings = new XmlWriterSettings();
settings.Encoding = System.Text.Encoding.UTF8;
settings.Indent = true;
//settings.NewLineHandling = NewLineHandling.Entitize;
System.Xml.XmlWriter xmlWriter = System.Xml.XmlWriter.Create(sw, settings);

VariableInputAction o = generateTestObject();
o.Variable.Name = "VariableInputActionWriteXMLTests";
xmlWriter.WriteStartElement("onPress");
o.WriteXml(xmlWriter);
xmlWriter.WriteEndElement();
xmlWriter.Flush();
string s = sw.ToString();

String result = System.IO.File.ReadAllText(@"assets\MobiFlight\InputConfig\VariableInputAction\WriteXmlTest.1.xml");

Assert.AreEqual(s, result, "The both strings are not equal");
}

[TestMethod()]
public void executeTest()
{
VariableInputAction o = generateTestObject();
MobiFlightUnitTests.mock.FSUIPC.FSUIPCCacheMock mock = new MobiFlightUnitTests.mock.FSUIPC.FSUIPCCacheMock();
MobiFlightUnitTests.mock.SimConnectMSFS.SimConnectCacheMock simConnectMock = new MobiFlightUnitTests.mock.SimConnectMSFS.SimConnectCacheMock();
MobiFlightUnitTests.mock.MobiFlight.MobiFlightCacheMock mobiflightCacheMock = new MobiFlightUnitTests.mock.MobiFlight.MobiFlightCacheMock();
o.Variable.TYPE = "number";
o.Variable.Expression = "$+1";

o.execute(mock, simConnectMock, mobiflightCacheMock, null, new List<ConfigRefValue>());
Assert.AreEqual(mobiflightCacheMock.GetMobiFlightVariable("VariableInputActionTests").Number, 1, "The number is not correct.");
}
}
}
18 changes: 18 additions & 0 deletions MobiFlightUnitTests/MobiFlightUnitTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,10 @@
<Compile Include="InterpolationTests.cs" />
<Compile Include="MobiFlight\Config\ConfigTests.cs" />
<Compile Include="MobiFlight\InputConfigItemTests.cs" />
<Compile Include="MobiFlight\InputConfig\AnalogInputConfigTest.cs" />
<Compile Include="MobiFlight\InputConfig\ButtonInputConfigTests.cs" />
<Compile Include="MobiFlight\InputConfig\EncoderInputConfigTests.cs" />
<Compile Include="MobiFlight\InputConfig\VariableInputActionTests.cs" />
<Compile Include="MobiFlight\InputConfig\PmdgEventIdInputActionTests.cs" />
<Compile Include="MobiFlight\InputConfig\EventIdInputActionTests.cs" />
<Compile Include="MobiFlight\InputConfig\FsuipcOffsetInputActionTests.cs" />
Expand Down Expand Up @@ -120,6 +122,14 @@
<SubType>Designer</SubType>
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="assets\MobiFlight\InputConfig\AnalogInputConfig\ReadXmlTest.1.xml">
<SubType>Designer</SubType>
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="assets\MobiFlight\InputConfig\AnalogInputConfig\WriteXmlTest.1.xml">
<SubType>Designer</SubType>
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="assets\MobiFlight\InputConfig\EncoderInputConfig\ReadXmlTest.1.xml">
<SubType>Designer</SubType>
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
Expand All @@ -136,6 +146,14 @@
<SubType>Designer</SubType>
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="assets\MobiFlight\InputConfig\VariableInputAction\ReadXmlTest.1.xml">
<SubType>Designer</SubType>
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="assets\MobiFlight\InputConfig\VariableInputAction\WriteXmlTest.1.xml">
<SubType>Designer</SubType>
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="assets\MobiFlight\InputConfig\InputConfigItem\ReadXmlTest.2.xml">
<SubType>Designer</SubType>
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

0 comments on commit f7e5a41

Please sign in to comment.