厌烦了assert.AreEqal();等等一堆的assert方法了吗? 偶然觅得下面这个奇思妙想
看看下面这个:
第二个参数是一个constraint对象.也就是你对something所要检查的约束.
这个功能实在很cool.
1. 没有了那么多的assert方法,看上去很舒服,也更容易理解.
2. 可以方便的组合约束
3. 自定义约束.
其实这里面也就是个责任分离的思想.为啥我们想不到? 当然放到Nunit中, 这个constraint可以通过Delegate来实现. 自己模拟了一下NUnit下的实现, 不知有没有更好的实现?
看看下面这个:
assertThat(something, eq("Hello"));
assertThat(something, eq(true));
assertThat(something, isA(Color.class));
assertThat(something, contains("World"));
assertThat(something, same(Food.CHEESE));
assertThat(something, NULL);
assertThat(something, NOT_NULL);
assertThat(something, eq(true));
assertThat(something, isA(Color.class));
assertThat(something, contains("World"));
assertThat(something, same(Food.CHEESE));
assertThat(something, NULL);
assertThat(something, NOT_NULL);
第二个参数是一个constraint对象.也就是你对something所要检查的约束.
这个功能实在很cool.
1. 没有了那么多的assert方法,看上去很舒服,也更容易理解.
2. 可以方便的组合约束
assertThat(something, not(eq("Hello")));
assertThat(something, not(contains("Cheese")));
assertThat(something, not(contains("Cheese")));
3. 自定义约束.
assertThat(something, between(10, 20));
public Constraint between(final int min, final int max) {
return new Constraint() {
public boolean eval(Object object) {
if (!object instanceof Integer) {
return false;
}
int value = ((Integer)object).intValue();
return value > min && value < max;
}
}
}
public Constraint between(final int min, final int max) {
return new Constraint() {
public boolean eval(Object object) {
if (!object instanceof Integer) {
return false;
}
int value = ((Integer)object).intValue();
return value > min && value < max;
}
}
}
其实这里面也就是个责任分离的思想.为啥我们想不到? 当然放到Nunit中, 这个constraint可以通过Delegate来实现. 自己模拟了一下NUnit下的实现, 不知有没有更好的实现?
using System;
namespace xnUnit
{
class Constraint
{
public delegate bool EvalDelegate(object obj);
public EvalDelegate EvalHandle;
public bool Eval(object obj)
{
return EvalHandle(obj);
}
}
class Assert
{
public static void AssertThat(object obj,Constraint con)
{
if (con.Eval(obj))
{
Console.WriteLine("Pass");
}
else
{
Console.WriteLine("Failed!");
}
}
}
}
namespace xnUnit
{
class Constraint
{
public delegate bool EvalDelegate(object obj);
public EvalDelegate EvalHandle;
public bool Eval(object obj)
{
return EvalHandle(obj);
}
}
class Assert
{
public static void AssertThat(object obj,Constraint con)
{
if (con.Eval(obj))
{
Console.WriteLine("Pass");
}
else
{
Console.WriteLine("Failed!");
}
}
}
}
using System;
namespace xnUnit
{
class Program
{
public static Constraint Between(int min, int max)
{
Constraint con = new Constraint();
con.EvalHandle = delegate(object obj)
{
int value = Convert.ToInt32(obj);
return value > min && value < max;
};
return con;
}
static void Main(string[] args)
{
Assert.AssertThat(3, Between(2, 4));
}
}
}
namespace xnUnit
{
class Program
{
public static Constraint Between(int min, int max)
{
Constraint con = new Constraint();
con.EvalHandle = delegate(object obj)
{
int value = Convert.ToInt32(obj);
return value > min && value < max;
};
return con;
}
static void Main(string[] args)
{
Assert.AssertThat(3, Between(2, 4));
}
}
}