Newer
Older
TIASShot / TIASshot.Tests / ConfigTests.cs
using System;
using System.IO;
using NUnit.Framework;

namespace TIASshot.Tests {
    /// <summary>
    /// Config のテスト(XML パース・型変換・存在しないキーの例外)
    /// </summary>
    [TestFixture]
    public class ConfigTests {

        private string _tempConfigPath;

        // テスト用 XML の期待値定数
        private const float  ExpectedFloat  = 1.5f;
        private const int    ExpectedInt    = 42;
        private const string ExpectedString = "hello";
        private const string ExpectedNested = "deep";

        private const string TestConfigXml =
            "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
            "<Config>" +
            "  <FloatVal>1.5</FloatVal>" +
            "  <IntVal>42</IntVal>" +
            "  <StrVal>hello</StrVal>" +
            "  <Nested><Child>deep</Child></Nested>" +
            "</Config>";

        [SetUp]
        public void SetUp() {
            _tempConfigPath = Path.GetTempFileName();
            // 拡張子を .xml に変更(XmlDocument.Load は拡張子を問わないが明示するために rename)
            var xmlPath = _tempConfigPath + ".xml";
            File.Move(_tempConfigPath, xmlPath);
            _tempConfigPath = xmlPath;

            File.WriteAllText(_tempConfigPath, TestConfigXml, System.Text.Encoding.UTF8);

            Config.Load(_tempConfigPath);
        }

        [TearDown]
        public void TearDown() {
            if (File.Exists(_tempConfigPath)) File.Delete(_tempConfigPath);
        }

        /// <summary>
        /// GetFloat でフロート値が正しく取得できる
        /// </summary>
        [Test]
        public void GetFloat_存在するキー_正しい値を返す() {
            var value = Config.GetFloat("FloatVal");
            Assert.AreEqual(ExpectedFloat, value, 1e-6f);
        }

        /// <summary>
        /// GetInt で整数値が正しく取得できる
        /// </summary>
        [Test]
        public void GetInt_存在するキー_正しい値を返す() {
            var value = Config.GetInt("IntVal");
            Assert.AreEqual(ExpectedInt, value);
        }

        /// <summary>
        /// GetString で文字列値が正しく取得できる
        /// </summary>
        [Test]
        public void GetString_存在するキー_正しい値を返す() {
            var value = Config.GetString("StrVal");
            Assert.AreEqual(ExpectedString, value);
        }

        /// <summary>
        /// 存在しないキーを指定したとき ArgumentException がスローされる
        /// </summary>
        [Test]
        public void GetFloat_存在しないキー_ArgumentExceptionをスロー() {
            Assert.Throws<ArgumentException>(() => Config.GetFloat("NoSuchKey"));
        }

        /// <summary>
        /// 存在しないキーを GetInt で指定したとき ArgumentException がスローされる
        /// </summary>
        [Test]
        public void GetInt_存在しないキー_ArgumentExceptionをスロー() {
            Assert.Throws<ArgumentException>(() => Config.GetInt("NoSuchKey"));
        }

        /// <summary>
        /// 存在しないキーを GetString で指定したとき ArgumentException がスローされる
        /// </summary>
        [Test]
        public void GetString_存在しないキー_ArgumentExceptionをスロー() {
            Assert.Throws<ArgumentException>(() => Config.GetString("NoSuchKey"));
        }

        /// <summary>
        /// ネストした要素(Nested/Child)を XPath スラッシュ区切りで取得できる
        /// </summary>
        [Test]
        public void GetString_ネスト要素_正しい値を返す() {
            var value = Config.GetString("Nested/Child");
            Assert.AreEqual(ExpectedNested, value);
        }
    }
}