Skip to content

Instantly share code, notes, and snippets.

@ryz310
Last active December 16, 2015 14:39
Show Gist options
  • Save ryz310/5449877 to your computer and use it in GitHub Desktop.
Save ryz310/5449877 to your computer and use it in GitHub Desktop.
MyAssert.cs のテストプログラム。とどのつまりはテストのテスト。
using System;
using System.Text;
using CommonGscuht;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using TestHelper;
using System.Collections;
namespace TestHelperTest
{
/// <summary>
///MyAssertTest のテスト クラスです。すべての
///MyAssertTest 単体テストをここに含めます
///</summary>
[TestClass()]
public class MyAssertTest
{
const string TEST_FILE_DIR = "(Your Project Directory Name)";
const string theFile = TEST_FILE_DIR + "testfile1.txt";
const string sameFile = TEST_FILE_DIR + "testfile2.txt";
const string diffFile = TEST_FILE_DIR + "testfile3.txt";
readonly Encoding encoding = Encoding.GetEncoding("Shift_JIS");
/// <summary>
///AreEqual のテスト
///</summary>
[TestMethod()]
public void AreEqualTest()
{
int[] array1 = { 1, 2, 3, 4 };
int[] array2 = { 1, 2, 3 };
int[] array3 = { };
GoodCase(() => MyAssert.AreEqual(array1, array1));
BadCase (() => MyAssert.AreEqual(array1, array2));
GoodCase(() => MyAssert.AreEqual(array3, array3));
BadCase (() => MyAssert.AreEqual(array3, array1));
}
/// <summary>
///IsNullOrEmpty のテスト
///</summary>
[TestMethod()]
public void IsNullOrEmptyTest()
{
GoodCase(() => MyAssert.IsNullOrEmpty(null));
GoodCase(() => MyAssert.IsNullOrEmpty("" ));
BadCase (() => MyAssert.IsNullOrEmpty(" " ));
}
/// <summary>
///IsNotNullAndEmpty のテスト
///</summary>
[TestMethod()]
public void IsNotNullAndEmptyTest()
{
BadCase (() => MyAssert.IsNotNullAndEmpty(null));
BadCase (() => MyAssert.IsNotNullAndEmpty("" ));
GoodCase(() => MyAssert.IsNotNullAndEmpty(" " ));
}
/// <summary>
///AreEqualFiles のテスト
///</summary>
[TestMethod()]
public void AreEqualFilesTest()
{
GoodCase(() => MyAssert.AreEqualFiles(encoding, theFile, sameFile));
GoodCase(() => MyAssert.AreEqualFiles(encoding, theFile, sameFile, "message"));
BadCase (() => MyAssert.AreEqualFiles(encoding, theFile, diffFile));
BadCase (() => MyAssert.AreEqualFiles(encoding, theFile, diffFile, "message"));
}
/// <summary>
///AreNotEqualFiles のテスト
///</summary>
[TestMethod()]
public void AreNotEqualFilesTest()
{
GoodCase(() => MyAssert.AreNotEqualFiles(encoding, theFile, diffFile));
GoodCase(() => MyAssert.AreNotEqualFiles(encoding, theFile, diffFile, "message"));
BadCase (() => MyAssert.AreNotEqualFiles(encoding, theFile, sameFile));
BadCase (() => MyAssert.AreNotEqualFiles(encoding, theFile, sameFile, "message"));
}
/// <summary>
///IsMatch のテスト
///</summary>
[TestMethod()]
public void IsMatchTest()
{
string pattern = "\\d{6}";
string goodString = "123456";
string badString = "1Z3456";
GoodCase(() => MyAssert.IsMatch(pattern, goodString));
GoodCase(() => MyAssert.IsMatch(pattern, goodString, "message"));
BadCase (() => MyAssert.IsMatch(pattern, badString ));
BadCase (() => MyAssert.IsMatch(pattern, badString, "message"));
}
/// <summary>
///IsTrueSqlQuery のテスト
///</summary>
[TestMethod()]
public void IsTrueSqlQueryTest()
{
string goodQuery = "select * from UQT3130";
string badQuery = "zelect * from UQT3130";
GoodCase(() => MyAssert.IsTrueSqlQuery(goodQuery));
BadCase (() => MyAssert.IsTrueSqlQuery(badQuery ));
}
/// <summary>
///Throws のテスト
///</summary>
[TestMethod()]
public void ThrowsTest()
{
GoodCase(() => MyAssert.Throws<Exception >(() => { throw new Exception(); }));
GoodCase(() => MyAssert.Throws<MyException>(() => { throw new MyException(); }));
BadCase (() => MyAssert.Throws<MyException>(() => { return; }));
// BadCase (() => MyAssert.Throws<MyException>(() => { throw new NullReferenceException(); }));
string message = "A error message.";
GoodCase(() => MyAssert.Throws<Exception >(() => { throw new Exception(); }, message));
GoodCase(() => MyAssert.Throws<MyException>(() => { throw new MyException(); }, message));
BadCase (() => MyAssert.Throws<MyException>(() => { return; }, message));
// BadCase (() => MyAssert.Throws<MyException>(() => { throw new NullReferenceException(); }, message));
}
/// <summary>
///NotThrows のテスト
///</summary>
[TestMethod()]
public void NotThrowsTest()
{
GoodCase(() => MyAssert.NotThrows(() => { return; }));
BadCase (() => MyAssert.NotThrows(() => { throw new Exception(); }));
string message = "A error message.";
GoodCase(() => MyAssert.NotThrows(() => { return; }, message));
BadCase (() => MyAssert.NotThrows(() => { throw new Exception(); }, message));
}
/// <summary>
/// MyAssert.Throwsのテスト用例外クラス
/// </summary>
class MyException : Exception { }
/// <summary>
/// 成功すべきテストで成功するかどうかをテストする
/// </summary>
/// <param name="TestMethod">
/// テストメソッド。
/// 以下のようなラムダ式で指定する。
/// () => function(args1, args2, ...)</param>
private static void GoodCase(Action TestMethod)
{
try
{
TestMethod();
Assert.IsTrue(true);
}
catch (AssertFailedException)
{
Assert.Fail("成功すべきテストに失敗。");
}
catch (Exception)
{
Assert.Fail("予期せぬ例外が発生。");
}
}
/// <summary>
/// 失敗すべきテストで失敗するかどうかをテストする
/// </summary>
/// <param name="TestMethod">
/// テストメソッド。
/// 以下のようなラムダ式で指定する。
/// () => function(args1, args2, ...)</param>
private static void BadCase(Action TestMethod)
{
try
{
TestMethod();
Assert.Fail("失敗すべきテストに成功。");
}
catch (AssertFailedException)
{
Assert.IsTrue(true);
}
catch (Exception)
{
Assert.Fail("予期せぬ例外が発生。");
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment