SEBWIN-296: Extended unit tests for XML parser.
This commit is contained in:
parent
ef298836f1
commit
f2b3e8e32b
3 changed files with 70 additions and 2 deletions
|
@ -41,6 +41,7 @@ namespace SafeExamBrowser.Configuration.UnitTests.DataFormats
|
||||||
Assert.IsFalse(sut.CanParse(null));
|
Assert.IsFalse(sut.CanParse(null));
|
||||||
Assert.IsFalse(sut.CanParse(new MemoryStream(Encoding.UTF8.GetBytes("<key>someKey</key><value>1</value>"))));
|
Assert.IsFalse(sut.CanParse(new MemoryStream(Encoding.UTF8.GetBytes("<key>someKey</key><value>1</value>"))));
|
||||||
Assert.IsFalse(sut.CanParse(new MemoryStream(Encoding.UTF8.GetBytes("<html></html>"))));
|
Assert.IsFalse(sut.CanParse(new MemoryStream(Encoding.UTF8.GetBytes("<html></html>"))));
|
||||||
|
Assert.IsFalse(sut.CanParse(new MemoryStream(Encoding.UTF8.GetBytes("<?x"))));
|
||||||
Assert.IsTrue(sut.CanParse(new MemoryStream(Encoding.UTF8.GetBytes("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"))));
|
Assert.IsTrue(sut.CanParse(new MemoryStream(Encoding.UTF8.GetBytes("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"))));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -59,6 +60,7 @@ namespace SafeExamBrowser.Configuration.UnitTests.DataFormats
|
||||||
Assert.AreEqual(9876, result.RawData["someInteger"]);
|
Assert.AreEqual(9876, result.RawData["someInteger"]);
|
||||||
Assert.IsTrue(Convert.FromBase64String("/5vlF76sb+5vgkhjiNTOn7l1SN3Ho2UAMJD3TtLo49M=").SequenceEqual(result.RawData["someData"] as IEnumerable<byte>));
|
Assert.IsTrue(Convert.FromBase64String("/5vlF76sb+5vgkhjiNTOn7l1SN3Ho2UAMJD3TtLo49M=").SequenceEqual(result.RawData["someData"] as IEnumerable<byte>));
|
||||||
Assert.AreEqual(true, result.RawData["someBoolean"]);
|
Assert.AreEqual(true, result.RawData["someBoolean"]);
|
||||||
|
Assert.AreEqual(false, result.RawData["anotherBoolean"]);
|
||||||
Assert.AreEqual(12.34, result.RawData["someReal"]);
|
Assert.AreEqual(12.34, result.RawData["someReal"]);
|
||||||
Assert.AreEqual(new DateTime(2019, 02, 20, 12, 30, 00, 123), result.RawData["someDate"]);
|
Assert.AreEqual(new DateTime(2019, 02, 20, 12, 30, 00, 123), result.RawData["someDate"]);
|
||||||
|
|
||||||
|
@ -78,6 +80,71 @@ namespace SafeExamBrowser.Configuration.UnitTests.DataFormats
|
||||||
Assert.AreEqual(4, (dictThree["dictThreeKeyTwo"] as List<object>)[3]);
|
Assert.AreEqual(4, (dictThree["dictThreeKeyTwo"] as List<object>)[3]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public void MustCheckForRootDictionary()
|
||||||
|
{
|
||||||
|
var xml = "<?xml version=\"1.0\"?><plist></plist>";
|
||||||
|
var xml2 = "<?xml version=\"1.0\"?><plist><key>someKey</key><integer>5</integer></plist>";
|
||||||
|
var xml3 = "<?xml version=\"1.0\"?><plist><dictionary></dictionary></plist>";
|
||||||
|
var result = sut.TryParse(new MemoryStream(Encoding.UTF8.GetBytes(xml)));
|
||||||
|
|
||||||
|
Assert.AreEqual(LoadStatus.InvalidData, result.Status);
|
||||||
|
|
||||||
|
result = sut.TryParse(new MemoryStream(Encoding.UTF8.GetBytes(xml2)));
|
||||||
|
Assert.AreEqual(LoadStatus.InvalidData, result.Status);
|
||||||
|
|
||||||
|
result = sut.TryParse(new MemoryStream(Encoding.UTF8.GetBytes(xml3)));
|
||||||
|
Assert.AreEqual(LoadStatus.InvalidData, result.Status);
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public void MustParseEmptyXml()
|
||||||
|
{
|
||||||
|
var xml = "<?xml version=\"1.0\"?><plist><dict></dict></plist>";
|
||||||
|
var result = sut.TryParse(new MemoryStream(Encoding.UTF8.GetBytes(xml)));
|
||||||
|
|
||||||
|
Assert.AreEqual(LoadStatus.Success, result.Status);
|
||||||
|
Assert.AreEqual(0, result.RawData.Count);
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public void MustDetectInvalidKey()
|
||||||
|
{
|
||||||
|
var xml = "<?xml version=\"1.0\"?><plist><dict><nokey>blubb</nokey><true /></dict></plist>";
|
||||||
|
var result = sut.TryParse(new MemoryStream(Encoding.UTF8.GetBytes(xml)));
|
||||||
|
|
||||||
|
Assert.AreEqual(LoadStatus.InvalidData, result.Status);
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public void MustDetectInvalidValueType()
|
||||||
|
{
|
||||||
|
var xml = "<?xml version=\"1.0\"?><plist><dict><key>blubb</key><globb>1234</globb></dict></plist>";
|
||||||
|
var result = sut.TryParse(new MemoryStream(Encoding.UTF8.GetBytes(xml)));
|
||||||
|
|
||||||
|
Assert.AreEqual(LoadStatus.InvalidData, result.Status);
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public void MustAllowEmptyArray()
|
||||||
|
{
|
||||||
|
var xml = "<?xml version=\"1.0\"?><plist><dict><key>value</key><array /></dict></plist>";
|
||||||
|
var result = sut.TryParse(new MemoryStream(Encoding.UTF8.GetBytes(xml)));
|
||||||
|
|
||||||
|
Assert.AreEqual(LoadStatus.Success, result.Status);
|
||||||
|
Assert.IsInstanceOfType(result.RawData["value"], typeof(IList<object>));
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public void MustAllowEmptyDictionary()
|
||||||
|
{
|
||||||
|
var xml = "<?xml version=\"1.0\"?><plist><dict><key>value</key><dict /></dict></plist>";
|
||||||
|
var result = sut.TryParse(new MemoryStream(Encoding.UTF8.GetBytes(xml)));
|
||||||
|
|
||||||
|
Assert.AreEqual(LoadStatus.Success, result.Status);
|
||||||
|
Assert.IsInstanceOfType(result.RawData["value"], typeof(IDictionary<string, object>));
|
||||||
|
}
|
||||||
|
|
||||||
private Stream LoadTestData()
|
private Stream LoadTestData()
|
||||||
{
|
{
|
||||||
var path = $"{nameof(SafeExamBrowser)}.{nameof(Configuration)}.{nameof(UnitTests)}.{nameof(DataFormats)}.XmlTestData.xml";
|
var path = $"{nameof(SafeExamBrowser)}.{nameof(Configuration)}.{nameof(UnitTests)}.{nameof(DataFormats)}.XmlTestData.xml";
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
<?xml version="1.0" encoding="utf-8" ?>
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
<plist>
|
<plist>
|
||||||
<dict>
|
<dict>
|
||||||
<key>someString</key>
|
<key>someString</key>
|
||||||
|
@ -35,6 +35,8 @@
|
||||||
</array>
|
</array>
|
||||||
<key>someBoolean</key>
|
<key>someBoolean</key>
|
||||||
<true />
|
<true />
|
||||||
|
<key>anotherBoolean</key>
|
||||||
|
<false />
|
||||||
<key>someReal</key>
|
<key>someReal</key>
|
||||||
<real>12.34</real>
|
<real>12.34</real>
|
||||||
<key>someDate</key>
|
<key>someDate</key>
|
||||||
|
|
|
@ -176,7 +176,6 @@ namespace SafeExamBrowser.Configuration.DataFormats
|
||||||
return LoadStatus.InvalidData;
|
return LoadStatus.InvalidData;
|
||||||
}
|
}
|
||||||
|
|
||||||
reader.Read();
|
|
||||||
reader.MoveToContent();
|
reader.MoveToContent();
|
||||||
|
|
||||||
var status = ParseElement(reader, out object value);
|
var status = ParseElement(reader, out object value);
|
||||||
|
|
Loading…
Add table
Reference in a new issue