每周源代码6

[原文发表地址]The Weekly Source Code 6

[原文发表时间] 2007-09-16 01:04 AM

沿袭我一贯的信念阅读源代码以开发出更好的程序,我现在为大家呈上周系列“每周源代码”的第六篇,这是我这周在读的一些我很欣赏的源代码。

xUnit是出自“Original NUnit Guy”小组的Jim“The .NET Guy”小组的Wilson之手的一个新的单元测试框架。关于讨论“为什么”创建xUnit的博文已经漫天都是,但我保持中立,就像瑞士的中立立场一样。我觉得没有必要,不过只要你们开心就好。我们来看看源代码中有什么好的地方。你提供给他们样式点的可扩展性。看看他们是怎么创建一个新的属性“RepeatTest”,给它添加一个行为,然后在测试时使用这个属性,于是测试人员执行测试时将如同yield返回新的TestCommands.Clean。

public class Example
{
static int val;

    [RepeatTest(5, Timeout = 500)]
public void RepeatingTestMethod()
{
Thread.Sleep(100);
Assert.Equal(2, 2);
if (val == 0)
{
val++;
Thread.Sleep(1000);
}
}
}

public class RepeatTestAttribute : TestAttribute
{
readonly int repeatCount;

    public RepeatTestAttribute(int repeatCount)
{
this.repeatCount = repeatCount;
}

    public override IEnumerable<ITestCommand> CreateTestCommands(MethodInfo testMethod)
{
for (int index = 0; index < repeatCount; index++)
yield return new TestCommand(testMethod);
}
}

 

Keith Brown的密码管理工具(PWM)- 今天我把我最喜欢的密码管理工具弄崩溃了,所以我在本地重建了下,将平台设置为32位,这样就好了。我在弄的时候…看看这个无形的“Record”构造函数。

public Record(string site, string salt, string encryptedUserId, string encryptedPassword, string encryptedNotes, string useSetWindowText, string duration, string nagSpan, string nextReminder, string lastReset, string usageCount)

: this(site, salt, encryptedUserId, encryptedPassword, encryptedNotes, "true" == useSetWindowText,

"" == duration ? 0 : Convert.ToInt32(duration),

"" == nagSpan ? 0 : Convert.ToInt32(nagSpan),

"" == nextReminder ? DateTime.MaxValue : Convert.ToDateTime(nextReminder),

"" == lastReset ? DateTime.Now : Convert.ToDateTime(lastReset),

"" == usageCount ? 0 : Convert.ToInt32(usageCount)) {

}

 

RhinoMocksSVN源)- Matt Gilbert和Mike Minutillo都跟我推荐RhinoMocks。Mike说他喜欢这个Ayende深爱的DisposableAction模式。

namespace Rhino.Commons

{

public class DisposableAction<T> : IDisposable

{

Proc<T> _action;

T _val;

        public DisposableAction(Proc<T> action, T val)

{

if (action == null)

throw new ArgumentNullException("action");

_action = action;

_val = val;

}

        public T Value { get { return _val; } }

public void Dispose() { _action(_val); }

}

    public class DisposableAction : IDisposable

{

Proc _action;

        public DisposableAction(Proc action)

{

if (action == null)

throw new ArgumentNullException("action");

_action = action;

}

public void Dispose(){ _action(); }

}

}

 

Monorail HotSwap– 如果你还在,那就看一下这70行代码。我在想这会不会泄漏程序集,但因为它关注的是开发速度,所以应该不要紧。非常聪敏。你知道在.NET平台上编译新代码有多简单吗?

void CodeChanged(object sender, FileSystemEventArgs e)

{

string fileName = Path.GetFileNameWithoutExtension(e.FullPath);

string typeName = controllersNamespace+"."+fileName;

CompilerParameters options = CreateCompilerOptions();

    CSharpCodeProvider provider = new CSharpCodeProvider();

CompilerResults compilerResults = provider

.CompileAssemblyFromFile(options, e.FullPath);

    container.Kernel.RemoveComponent(typeName);

if(compilerResults.Errors.HasErrors)

return;

    Type type = compilerResults.CompiledAssembly.GetType(typeName);

container.AddComponent(type.FullName, type);

}

 

如果找到那些还没有被人好好读过的源代码,请随时告诉我。