SQLの窓

2013年09月15日


アプリケーションテスト用コンソールアプリ(TryApp)テンプレート

SkyDrive へ

VS2010用 TryApp.zip テンプレート



機能のテストをしたい場合、やはりコンソールアプリが手軽です。イベント単位で実装する事になるので、後から再利用する際にもわかり易くて良いと思います。
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace TryApp1
{
	public class Program
	{

		[STAThreadAttribute]
		static void Main(string[] args)
		{
			App app = new App();

			// イベントの追加
			app.ShowEnv += MyProc1;
			app.GetDir += (string path) =>
			{
				Console.WriteLine(path + " が選択されました" );
				string[] filenames = null;
				if (path != "") {
					filenames = Directory.GetFiles(path);
					foreach (string value in filenames) {
						Console.WriteLine(value);
					}
				}
			};

			app.GetFile += (string path) =>
			{
				Console.WriteLine(path + " が選択されました");
			};

			// メソッドによる実装
			app.Cmd += MyProc2;
			// ラムダ式による実装
			app.Cmd += (string cmd) =>
			{
				Console.WriteLine(cmd + " が実行されました");
			};

			// ******************************************
			// 終了せずに、コマンドプロンプトを保持する。
			// ( "q" で終了 )
			// ******************************************
			app.Loop();

		}

		// ******************************************
		// 環境周りの情報
		// ******************************************
		static private void MyProc1( string[] data ) {

			foreach (string value in data) {
				Console.WriteLine( value );
			}

		}

		// ******************************************
		// 処理のテンプレート
		// ******************************************
		static private void MyProc2( string cmd ) {

			Console.WriteLine( cmd );
		}
	
	}
}

STAThreadAttribute は、ダイアログを開く時に必要でした

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.IO;
using System.Windows.Forms;

namespace TryApp1
{

	class App
	{

		// 引数が一つのイベント
		public delegate void AppHandler(string cmd);
		// 引数が文字列配列のイベント
		public delegate void AppHandlerResults(string[] data);

		// 環境周りの情報を取得するイベント
		public event AppHandlerResults ShowEnv = null;
		// フォルダ選択ダイアログ
		public event AppHandler GetDir = null;
		// ファイル選択ダイアログ
		public event AppHandler GetFile = null;
		// イベントのテンプレート
		public event AppHandler Cmd = null;

		// ******************************************
		// コンストラクタ
		// ******************************************
		public App()
		{
		}

		// ******************************************
		// 終了せずに、コマンドプロンプトを保持する。
		// ******************************************
		public void Loop()
		{
			while (true) {
				// ******************************************
				// 専用プロンプト
				// ******************************************
				Console.Write("app>");
				string line = Console.ReadLine();

				// ******************************************
				// 終了コマンド
				// ******************************************
				if (line == "q") {
					break;
				}

				// ******************************************
				// 環境周りの情報を取得するイベント
				// ******************************************
				if (line == "env") {
					if (ShowEnv != null) {
						// 実行ファイル
						string thisPath = Assembly.GetExecutingAssembly().Location;
						// 実行ファイルのあるディレクトリ
						string thisDir = Path.GetDirectoryName(thisPath);
						string[] result = { line, thisPath, thisDir };
						ShowEnv(result);
					}
				}

				// ******************************************
				// フォルダ選択
				// ******************************************
				if (line == "dir") {
					if (GetDir != null) {
						// フォルダ選択ダイアログ
						var dir = new FolderBrowserDialog();
						dir.SelectedPath = @"c:\";
						var result = dir.ShowDialog();
						string path = "";
						if (result == DialogResult.OK) {
							path = dir.SelectedPath;
						}
						GetDir(path);
					}
				}

				// ******************************************
				// フアイル選択
				// ******************************************
				if (line == "file") {
					if (GetFile != null) {
						// ファイル選択ダイアログ
						var file = new OpenFileDialog();
						file.Filter = "全て|*.*|テキスト|*.txt;*.log";
						file.FilterIndex = 0;
						file.FileName = "";
						var dr = file.ShowDialog();
						string path = "";
						if (dr == DialogResult.OK) {
							path = file.FileName;
						}
						GetFile(path);
					}
				}

				// ******************************************
				// イベントのテンプレート
				// ******************************************
				if (line == "cmd") {
					if (Cmd != null) {
						Cmd(line);
					}
				}

			}
		}
	}
}

AppHandler または AppHandlerResults を使うか、デリゲートを増やして event を作成して機能追加します。

関連する記事コンソールアプリケーション(C#) で、クラス、デリゲート、イベント、ラムダ式の振舞いを理解するTryAppテンプレートを使って『The Zip, GZip, BZip2 and Tar Implementation For .NET』のテスト


【VS(C#)の最新記事】
posted by lightbox at 2013-09-15 19:45 | VS(C#) | このブログの読者になる | 更新情報をチェックする
container 終わり



フリーフォントで簡単ロゴ作成
フリーフォントでボタン素材作成
フリーフォントで吹き出し画像作成
フリーフォントではんこ画像作成
ほぼ自由に利用できるフリーフォント
フリーフォントの書体見本とサンプル
画像を大きく見る為のウインドウを開くボタンの作成

CSS ドロップシャドウの参考デモ
イラストAC
ぱくたそ
写真素材 足成
フリーフォント一覧
utf8 文字ツール
右サイド 終わり
base 終わり