main
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Reflection;
5
6namespace tests
7{
8 public delegate void after_the_sut_has_been_created();
9
10 public abstract class runner<SUT> : test
11 {
12 static IDictionary<Type, object> dependencies = new Dictionary<Type, object>();
13 ConstructorInfo greediest_constructor;
14
15 static protected SUT sut { get; private set; }
16
17 public override void initialize()
18 {
19 auto_register_dependencies();
20
21 context();
22 sut = create_sut();
23 after_create_sut();
24 because();
25 }
26
27 protected virtual void after_create_sut()
28 {
29 GetType().run_stacked<after_the_sut_has_been_created>(this);
30 }
31
32 public virtual SUT create_sut()
33 {
34 return (SUT) greediest_constructor.Invoke(dependencies.Values.ToArray());
35 }
36
37 protected void it(string should, Action<SUT> test)
38 {
39 it(should, () =>
40 {
41 test(sut);
42 });
43 }
44
45 static protected T dependency<T>() where T : class
46 {
47 if (dependencies.ContainsKey(typeof (T)))
48 {
49 return (T) dependencies[typeof (T)];
50 }
51 return mock_factory.create<T>();
52 }
53
54 void auto_register_dependencies()
55 {
56 dependencies.Clear();
57 greediest_constructor = typeof (SUT).find_the_greediest_constructor();
58 if (null == greediest_constructor) return;
59 greediest_constructor.GetParameters().each(x => register_dependency(x.ParameterType));
60 }
61
62 void register_dependency(Type dependency)
63 {
64 if (dependency.IsValueType)
65 dependencies[dependency] = Activator.CreateInstance(dependency);
66 else
67 dependencies[dependency] = mock_factory.create(dependency);
68 }
69 }
70}