Skip to content

Instantly share code, notes, and snippets.

@ritalin
Created June 11, 2012 07:34
Show Gist options
  • Save ritalin/2908909 to your computer and use it in GitHub Desktop.
Save ritalin/2908909 to your computer and use it in GitHub Desktop.
A helper class testing the pre-requred fixture.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NUnit.Framework;
namespace Samples {
public class AssertRequired<TResult> where TResult : class, new() {
private class BoundFunc {
public Func<TResult, Task<bool>> F1 = null;
public Func<TResult, Task> F2 = null;
public string FailedMessage = "";
}
private List<BoundFunc> mBoundFuncs = new List<BoundFunc>();
public AssertRequired() : this(new TResult()) { }
public AssertRequired(TResult inContext) {
this.Context = inContext;
}
public AssertRequired<TResult> Bind(Func<TResult, bool> inBindFunc, string FailedMessage) {
if (inBindFunc != null) {
if (!inBindFunc(this.Context)) {
Assert.Fail(FailedMessage);
}
}
return this;
}
public AssertRequired<TResult> Bind(Action<TResult> inBindFunc, string FailedMessage) {
if (inBindFunc != null) {
try {
inBindFunc(this.Context);
}
catch {
Assert.Fail(FailedMessage);
}
}
return this;
}
public AssertRequired<TResult> BindAsync(Func<TResult, Task<bool>> inBindFunc, string inFailedMessage) {
if (inBindFunc != null) {
mBoundFuncs.Add(new BoundFunc {
F1 = inBindFunc,
FailedMessage = inFailedMessage
});
}
return this;
}
public AssertRequired<TResult> BindAsync(Func<TResult, Task> inBindFunc, string inFailedMessage) {
if (inBindFunc != null) {
mBoundFuncs.Add(new BoundFunc {
F2 = inBindFunc,
FailedMessage = inFailedMessage
});
}
return this;
}
public async Task<TResult> Eval() {
foreach (var f in mBoundFuncs) {
if (f.F1 != null) {
if (!await f.F1(this.Context)) {
Assert.Fail(f.FailedMessage);
}
}
else if (f.F2 != null) {
try {
await f.F2(this.Context);
}
catch {
Assert.Fail(f.FailedMessage);
}
}
}
return this.Context;
}
public TResult Context { get; private set; }
}
}
private class FileMoveCopyHolder {
public StorageFolder BaseFolder = null;
public StorageFolder OriginalSrcFolder = null;
public StorageFile SrcFile = null;
public StorageFolder DestFolder = null;
}
[Test]
public void _事前条件チェック() {
var holder = await
new AssertRequired<FileMoveCopyHolder>()
.BindAsync(async(h) => {
return ( h.BaseFolder = await FileUtils.GetStorageFolderAsync(KnownFolders.DocumentsLibrary, "tmp", false)) != null;
}, "tmpフォルダが用意されていない")
.BindAsync(async (h) => {
return (h.SrcFile = await FileUtils.GetStorageFileAsync(h.BaseFolder, @"copy_move_src\copy_target.txt", false)) != null;
}, "コピー元ファイルが用意されていない")
.BindAsync(async (h) => {
return (h.DestFolder = await FileUtils.TryCreateStorageFolderAsync(h.BaseFolder, "copy_move_dest", false)) != null;
}, "コピー先フォルダが用意できない")
.Eval()
;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment