LoginSignup
3

More than 5 years have passed since last update.

ズンドコにテストつけてみたい

Last updated at Posted at 2016-03-15

前回、ズンドコなアレはとりあえずできたと思われるが、ほんとにちゃんと終了判定できているのか不安になってきた。
ユニットテストを書きたいのでいろいろと好き勝手に切り離してみた。

ズンドコする部分

zundoko.cs
using System.Collections.Generic;

namespace zundoko
{
    public class zundoko
    {
        public IConsole view;
        public IEnumerable<int> nums;

        public zundoko(IConsole view, IEnumerable<int> nums)
        {
            this.view = view;
            this.nums = nums;
        }

        public void mainproc()
        {
            var q = new Queue<string>();
            const string z = "ズン";
            const string d = "ドコ";
            const string g = z + z + z + z + d;

            foreach (var n in nums)
            {
                q.Enqueue(n == 0 ? z : d);
                if (q.Count == 5)
                {
                    if (string.Join("", q) == g)
                    {
                        view.WriteLine(g + "キ・ヨ・シ!");
                        break;
                    }
                    view.Write(q.Dequeue());
                }
            }
        }
    }
}

画面部分

Consoles.cs
using System;

namespace zundoko
{
    public interface IConsole
    {
        void WriteLine(string p);
        void Write(string p);
    }

    public class RealConsole : IConsole
    {
        public void Write(string p)
        {
            Console.Write(p);
        }

        public void WriteLine(string p)
        {
            Console.WriteLine(p);
        }
    }
}

実行部分と乱数部分

Program.cs
using System;
using System.Collections.Generic;

namespace zundoko
{
    class Program
    {
        static void Main(string[] args)
        {
            var z = new zundoko(new RealConsole(), randomnumbers());
            z.mainproc();
        }

        static IEnumerable<int> randomnumbers()
        {
            var r = new Random();
            while (true)
            {
                yield return r.Next(2);
            }
        }
    }
}

これでやっとテストが書ける

UnitTest1.cs
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace zundoko
{
    [TestClass]
    public class UnitTest1
    {
        [TestMethod]
        public void TestMethod1()
        {
            var c = new TestConsole();
            var target = new zundoko(c, new[] { 0, 0, 0, 0, 1 });
            target.mainproc();

            Assert.AreEqual("ズンズンズンズンドコキ・ヨ・シ!" + Environment.NewLine, c.CurrentState);
        }

        [TestMethod]
        public void TestMethod2()
        {
            var c = new TestConsole();
            var target = new zundoko(c, new[] { 0, 0, 0, 0, 0, 1 });
            target.mainproc();

            Assert.AreEqual("ズンズンズンズンズンドコキ・ヨ・シ!" + Environment.NewLine, c.CurrentState);
        }
    }

    public class TestConsole : IConsole
    {
        private string m = "";

        public void Write(string p)
        {
            m = m + p;
        }

        public void WriteLine(string p)
        {
            m = m + p + Environment.NewLine;
        }

        public string CurrentState
        {
            get
            {
                return m;
            }
        }
    }
}

この場合、テスト対象となるのはIConsoleだかzundokoだか?
実際のロジック持ってるのはzundokoだけど結果を持ってるのはIConsoleだなぁ。
切り離し方も含めてもっと勉強がいる。

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3