1: /*
2: Copyright (c) 2011 <a href="http://www.gutgames.com">James Craig</a>
3:
4: Permission is hereby granted, free of charge, to any person obtaining a copy
5: of this software and associated documentation files (the "Software"), to deal
6: in the Software without restriction, including without limitation the rights
7: to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8: copies of the Software, and to permit persons to whom the Software is
9: furnished to do so, subject to the following conditions:
10:
11: The above copyright notice and this permission notice shall be included in
12: all copies or substantial portions of the Software.
13:
14: THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15: IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16: FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17: AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18: LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19: OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20: THE SOFTWARE.*/
21:
22: #region Usings
23: using System;
24: using System.Collections.Generic;
25: using System.Linq;
26: using System.Text;
27: using Utilities.DataTypes.Patterns.BaseClasses;
28: using System.Reflection;
29: using MoonUnit.Attributes;
30: using MoonUnit.Exceptions;
31: using MoonUnit.InternalClasses;
32: using Utilities.Profiler;
33: #endregion
34:
35: namespace MoonUnit
36: {
37: /// <summary>
38: /// Manager class for unit testing framework
39: /// </summary>
40: public class Manager : Singleton<Manager>
41: {
42: #region Constructor
43:
44: /// <summary>
45: /// Constructor
46: /// </summary>
47: protected Manager()
48: : base()
49: {
50: }
51:
52: #endregion
53:
54: #region Functions
55:
56: /// <summary>
57: /// Tests an assembly
58: /// </summary>
59: /// <param name="AssemblyToTest">Assembly to test</param>
60: /// <returns>The XML string of the results</returns>
61: public string Test(Assembly AssemblyToTest)
62: {
63: Type[] Types = AssemblyToTest.GetTypes();
64: return Test(Types);
65: }
66:
67: /// <summary>
68: /// Test a list of types
69: /// </summary>
70: /// <param name="Types">Types to test</param>
71: /// <returns>The XML string of the results</returns>
72: public string Test(Type[] Types)
73: {
74: Output TempOutput = new Output();
75: foreach (Type Type in Types)
76: TestClass(Type, TempOutput);
77: return TempOutput.ToString();
78: }
79:
80: /// <summary>
81: /// Tests a type
82: /// </summary>
83: /// <param name="Type">Type to test</param>
84: /// <returns>The XML string of the results</returns>
85: public string Test(Type Type)
86: {
87: Output TempOutput = new Output();
88: TestClass(Type, TempOutput);
89: return TempOutput.ToString();
90: }
91:
92: private static void TestClass(Type Type, Output TempOutput)
93: {
94: MethodInfo[] Methods = Type.GetMethods();
95: foreach (MethodInfo Method in Methods)
96: {
97: object[] Attributes = Method.GetCustomAttributes(false);
98: foreach (Attribute TempAttribute in Attributes)
99: {
100: if (TempAttribute is TestAttribute)
101: {
102: TestAttribute Attribute = (TestAttribute)TempAttribute;
103: if (!Attribute.Skip)
104: {
105: StopWatch Watch = new StopWatch();
106: object TestClass = Type.Assembly.CreateInstance(Type.FullName);
107: try
108: {
109: Watch.Start();
110: Method.Invoke(TestClass, Type.EmptyTypes);
111: Watch.Stop();
112: if (Attribute.TimeOut > 0 && Watch.ElapsedTime > Attribute.TimeOut)
113: throw new TimeOut("Method took longer than expected");
114: TempOutput.MethodCalled(Method);
115: }
116: catch (Exception e)
117: {
118: if(e.InnerException!=null)
119: TempOutput.MethodCalled(Method, e.InnerException);
120: else
121: TempOutput.MethodCalled(Method, e);
122: }
123: }
124: else
125: {
126: TempOutput.MethodCalled(Method, new Skipped(Attribute.ReasonForSkipping));
127: }
128: }
129: }
130: }
131: }
132:
133: #endregion
134: }
135: }