seb-win-refactoring/SafeExamBrowser.I18n.UnitTests/XmlTextResourceTests.cs

100 lines
2.7 KiB
C#

/*
* Copyright (c) 2020 ETH Zürich, Educational Development and Technology (LET)
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
using System;
using System.IO;
using System.Reflection;
using System.Xml;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using SafeExamBrowser.I18n.Contracts;
namespace SafeExamBrowser.I18n.UnitTests
{
[TestClass]
public class XmlTextResourceTests
{
[TestMethod]
public void MustCorrectlyLoadData()
{
var location = Assembly.GetAssembly(typeof(XmlTextResourceTests)).Location;
var path = $@"{Path.GetDirectoryName(location)}\Text_Valid.xml";
var sut = new XmlTextResource(path);
var text = sut.LoadText();
Assert.IsNotNull(text);
Assert.IsTrue(text.Count == 4);
Assert.AreEqual("Application Log", text[TextKey.LogWindow_Title]);
Assert.AreEqual("Version", text[TextKey.Version]);
}
[TestMethod]
[ExpectedException(typeof(XmlException))]
public void MustFailWithInvalidData()
{
var location = Assembly.GetAssembly(typeof(XmlTextResourceTests)).Location;
var path = $@"{Path.GetDirectoryName(location)}\Text_Invalid.txt";
var sut = new XmlTextResource(path);
sut.LoadText();
}
[TestMethod]
public void MustNeverReturnNull()
{
var location = Assembly.GetAssembly(typeof(XmlTextResourceTests)).Location;
var path = $@"{Path.GetDirectoryName(location)}\Text_Incompatible.xml";
var sut = new XmlTextResource(path);
var text = sut.LoadText();
Assert.IsNotNull(text);
Assert.IsTrue(text.Count == 0);
}
[TestMethod]
public void MustNeverSetNullValue()
{
var location = Assembly.GetAssembly(typeof(XmlTextResourceTests)).Location;
var path = $@"{Path.GetDirectoryName(location)}\Text_Valid.xml";
var sut = new XmlTextResource(path);
var text = sut.LoadText();
Assert.IsNotNull(text);
Assert.AreEqual(string.Empty, text[TextKey.Notification_LogTooltip]);
}
[TestMethod]
[ExpectedException(typeof(ArgumentException))]
public void MustNotAcceptInvalidPath()
{
new XmlTextResource("This is not a valid path");
}
[TestMethod]
[ExpectedException(typeof(ArgumentNullException))]
public void MustNotAcceptNullAsPath()
{
new XmlTextResource(null);
}
[TestMethod]
public void MustTrimValues()
{
var location = Assembly.GetAssembly(typeof(XmlTextResourceTests)).Location;
var path = $@"{Path.GetDirectoryName(location)}\Text_Valid.xml";
var sut = new XmlTextResource(path);
var text = sut.LoadText();
Assert.IsNotNull(text);
Assert.AreEqual("Hello world", text[TextKey.Notification_AboutTooltip]);
}
}
}