Help us understand the problem. What is going on with this article?

"アンパン"→"あ行"みたいな変換をするクラスとそのテストコード

More than 5 years have passed since last update.

要件

  • 入力値は「カタカナ」で
  • 半濁音も対応してね
  • 英数字で始まるものは「英数字」と返してね

変換クラス

public class GroupConvert
{
    static public string doit(string target)
    {
        Dictionary<string, string> dict = new Dictionary<string, string>(){
            {@"^[ア-オ]", "あ行"},
            {@"^[カ-コガ-ゴ]", "か行"},
            {@"^[サ-ソザ-ゾ]", "さ行"},
            {@"^[タ-トダ-ド]", "た行"},
            {@"^[ナ-ノ]", "な行"},
            {@"^[ハ-ホバ-ボパ-ポ]", "は行"},
            {@"^[マ-モ]", "ま行"},
            {@"^[ヤ-ヨ]", "や行"},
            {@"^[ラ-ロ]", "ら行"},
            {@"^[ワ-ン]", "わ行"},
            {@"^\b[A-Z0-90-9A-Z]", "英数字"},
        };

        foreach (var d in dict)
        {
            if (new Regex(d.Key, RegexOptions.IgnoreCase).Match(target).Success)
            {
                return d.Value;
            }
        }
        return "";
    }
}

そのテスト

[TestMethod]
public void Test()
{
    Assert.AreEqual("あ行", GroupConvert.doit("アンパンマン"));

    Assert.AreEqual("か行", GroupConvert.doit("ガリアジ"));
    Assert.AreEqual("か行", GroupConvert.doit("ゴータマ・ブッダ"));
    Assert.AreEqual("か行", GroupConvert.doit("クラエ"));

    Assert.AreEqual("さ行", GroupConvert.doit("サンバ"));
    Assert.AreEqual("さ行", GroupConvert.doit("ゾウリムシ"));

    Assert.AreEqual("た行", GroupConvert.doit("ドングリ"));
    Assert.AreEqual("た行", GroupConvert.doit("チョイナ"));

    Assert.AreEqual("な行", GroupConvert.doit("ニーズヘッグ"));
    Assert.AreEqual("な行", GroupConvert.doit("ノマダン"));

    Assert.AreEqual("は行", GroupConvert.doit("パルルカン"));
    Assert.AreEqual("は行", GroupConvert.doit("ボーヨミ"));
    Assert.AreEqual("は行", GroupConvert.doit("ヒクソン"));

    Assert.AreEqual("ま行", GroupConvert.doit("ムリポ"));
    Assert.AreEqual("ま行", GroupConvert.doit("モリタケ"));

    Assert.AreEqual("や行", GroupConvert.doit("ユーグレナ"));
    Assert.AreEqual("や行", GroupConvert.doit("ヨンパチ"));

    Assert.AreEqual("ら行", GroupConvert.doit("ロマン"));
    Assert.AreEqual("ら行", GroupConvert.doit("ラック"));

    Assert.AreEqual("わ行", GroupConvert.doit("ワーカーホリック"));
    Assert.AreEqual("わ行", GroupConvert.doit("ンドゥール"));

    Assert.AreEqual("英数字", GroupConvert.doit("Aガタカンエン"));
    Assert.AreEqual("英数字", GroupConvert.doit("Bガタカタカタ"));
    Assert.AreEqual("英数字", GroupConvert.doit("1ガタニンゲン"));

    Assert.AreEqual("", GroupConvert.doit("@empty"));
    Assert.AreEqual("", GroupConvert.doit("-----"));
    Assert.AreEqual("", GroupConvert.doit("あぎょう"));
    Assert.AreEqual("あ行", GroupConvert.doit("ェ"));
    Assert.AreEqual("わ行", GroupConvert.doit("ヰ"));
}

もっとスマートにできるよ!ってあればどんどんつっこんでください!

2015/01/20 追記(Chaining Assertion ライブラリ導入した後のテストコード)

コメントで教えていただいたC#ユニットテスト用ライブラリ Chaining Assertion を適用したあとのテストコードを追加しておきます。便利でテストが気持ちよく書けます!

[TestMethod]
[TestCaseSource("toaruTestData")]
public void TestTestCaseSource()
{
    TestContext.Run((string result, string expected) =>
    {
        // Assert.AreEqual(...) が無くなり、スッキリ!
        GroupConvert.doit(expected).Is(result);
    });
}

// テストデータをまとめて TestCaseSource 属性に渡せる!
public static object[] toaruTestData = new []
{
    new object[] {"あ行", "アンパンマン"},
    new object[] {"か行", "ガリアジ"},
    new object[] {"か行", "ゴータマ・ブッダ"},
    new object[] {"か行", "クラエ"},
    new object[] {"さ行", "サンバ"},
    new object[] {"さ行", "ゾウリムシ"},
    new object[] {"た行", "ドングリ"},
    new object[] {"た行", "チョイナ"},
    new object[] {"な行", "ニーズヘッグ"},
    new object[] {"な行", "ノマダン"},
    new object[] {"は行", "パルルカン"},
    new object[] {"は行", "ボーヨミ"},
    new object[] {"は行", "ヒクソン"},
    new object[] {"ま行", "ムリポ"},
    new object[] {"ま行", "モリタケ"},
    new object[] {"や行", "ユーグレナ"},
    new object[] {"や行", "ヨンパチ"},
    new object[] {"ら行", "ロマン"},
    new object[] {"ら行", "ラック"},
    new object[] {"わ行", "ワーカーホリック"},
    new object[] {"わ行", "ンドゥール"},
    new object[] {"英数字", "Aガタカンエン"},
    new object[] {"英数字", "Bガタカタカタ"},
    new object[] {"英数字", "1ガタニンゲン"},
    new object[] {"", "@empty"},
    new object[] {"", "-----"},
    new object[] {"", "あぎょう"},
    new object[] {"あ行", "ェ"},
    new object[] {"わ行", "ヰ"}
};

こんな便利なライブラリがあったとは!激推し。

参考

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした