<
%@ CodeTemplate Language
=
"
VB
"
TargetLanguage
=
"
Text
"
Description
=
"
Template description here.
"
Debug
=
"
False
"
%
>
<
%@
Assembly
Name
=
"
System.Design
"
%
>
<
%@ Import
Namespace
=
"
System.Reflection
"
%
>
<
%@ Import
Namespace
=
"
System.Collections
"
%
>
<
%@ Import
Namespace
=
"
System.Text
"
%
>
<
%@
Property
Name
=
"
FullClassName
"
Type
=
"
System.String
"
Category
=
"
Template Options
"
Description
=
"
This is the fully qualified name of the class to test.
"
%
>
<
%@
Property
Name
=
"
IncludeInheritedMembers
"
Type
=
"
System.Boolean
"
Category
=
"
Template Options
"
Required
=
"
true
"
default
=
"
false
"
%
>
<
%@
Property
Name
=
"
DeclaringTypeToExclude
"
Type
=
"
System.String
"
Category
=
"
Template Options
"
Required
=
"
false
"
%
>
<
%@
Property
Name
=
"
GenerateConstructorClasses
"
Type
=
"
System.Boolean
"
Category
=
"
Constructor Tests
"
Required
=
"
true
"
Default
=
"
true
"
Description
=
"
Should the appropriate test classes be generated.
"
%
>
<
%@
Property
Name
=
"
GeneratePropertyClasses
"
Type
=
"
System.Boolean
"
Category
=
"
Property Tests
"
Required
=
"
true
"
Default
=
"
true
"
Description
=
"
Should the appropriate test classes be generated.
"
%
>
<
%@
Property
Name
=
"
GenerateMethodClasses
"
Type
=
"
System.Boolean
"
Category
=
"
Method Tests
"
Required
=
"
true
"
Default
=
"
true
"
Description
=
"
Should the appropriate test classes be generated.
"
%
>
<
% InitTemplate() %
>
Imports
System
Imports
NUnit.Framework
Imports
<
%
=
GetNamespaceOnly(FullClassName)%
>
Namespace
<
%
=
GetTestNamespace(FullClassName) %
>
<
%
'
/*
'
----------------------------------------------
'
--------------- Constructors -----------------
'
----------------------------------------------
'
*/
If
(GenerateConstructorClasses)
Then
%
>
#Region
"Constructor Tests"
<
TestFixture
>
_
public
class
ConstructorTesting
#Region
"Test Preparation Methods"
#Region
"TestFixture SetUp and TearDown"
<
TestFixtureSetUp
>
_
Public
Sub
FixtureInit()
End Sub
<
TestFixtureTearDown
>
_
public
Sub
FixtureDispose()
End Sub
#End Region
#Region
"Test SetUp and TearDown"
<
SetUp
>
_
public
Sub
TestInit()
End Sub
<
TearDown
>
_
public
Sub
TestDispose()
End Sub
#End Region
#End Region
#Region
"Test Methods"
<
%
Dim
i
As
Integer
%
>
<
%
For
i
=
0
To
constructors.Length
-
1
%
>
<
%
If
i
=
0
Then
%
>
<
%
=
CreateConstructorTest(GetClassName(FullClassName),constructors(i))%
>
<
%
Else
%
>
<
%
=
CreateConstructorTest(GetClassName(FullClassName)
+
"
_Overload
"
+
i.ToString(),constructors(i))%
>
<
%
End
iF
%
>
<
%
Next
%
>
#End Region
End Sub
#End Region
<
%
End
If
%
>
<
%
If
(GenerateMethodClasses)
Then
%
>
<
%
'
/*
'
----------------------------------------------
'
-------------- Static Methods ----------------
'
----------------------------------------------
'
*/%>
#Region
Static Method Tests
<
TestFixture
>
_
public
class
StaticMethodTesting
#Region
"Test Preparation Methods"
#Region
"TestFixture Setup and TearDown"
<
TestFixtureSetUp
>
_
public
Sub
FixtureInit()
End Sub
<
TestFixtureTearDown
>
_
public
Sub
FixtureDispose()
End Sub
#End Region
#Region
"Test SetUp and TearDown"
<
SetUp
>
_
public
Sub
TestInit()
End Sub
<
TearDown
>
_
public
Sub
TestDispose()
End Sub
#End Region
#End Region
#Region
Test Methods
<
%
Dim
i
As
Integer
%
>
<
%
For
i
=
0
To
methods.Length
-
1
If
(methods(i).IsStatic _
AndAlso
(
Not
methods(i).Name.StartsWith(
"
get_
"
) _
AndAlso
(
Not
methods(i).Name.StartsWith(
"
set_
"
) _
AndAlso
((methods(i).DeclaringType.ToString
<>
DeclaringTypeToExclude) _
AndAlso
(methods(i).DeclaringType.ToString
<>
"
System.Object
"
)))))
Then
%
>
<
%
=
CreateMethodTest(methods(i), staticMethodCount) %
>
<
%
End
iF
%
>
<
%
Next
%
>
#End Region
End Sub
#End Region
<
%
'
/*
'
----------------------------------------------
'
------------------ Methods -------------------
'
----------------------------------------------
'
*/%>
#Region
Method Tests
<
TestFixture
>
_
public
class
MethodTesting
//
Variable
to
hold
object
to
be tested
for
reuse by init functions
<
%
=
FullClassName %
>
m_
<
%
=
GetCamelCaseName(GetClassName(FullClassName)) %
>
;
#Region
"Test Preparation Methods"
#Region
"TestFixture SetUp and TearDown"
<
TestFixtureSetUp
>
_
public
Sub
FixtureInit()
End Sub
<
TestFixtureTearDown
>
_
public
Sub
FixtureDispose()
End Sub
#End Region
#Region
"Test SetUp and TearDown"
<
%
=
CreateMethodTestInit(FullClassName) %
>
<
%
=
CreateMethodTestDispose(GetClassName(FullClassName)) %
>
#End Region
#End Region
#Region
Test Methods
<
%
For
i
=
0
To
properties.Length
-
1
if
(
Not
methods(i).IsStatic _
AndAlso
(
Not
methods(i).Name.StartsWith(
"
get_
"
) _
AndAlso
(
Not
methods(i).Name.StartsWith(
"
set_
"
) _
AndAlso
((methods(i).DeclaringType.ToString
<>
DeclaringTypeToExclude) _
AndAlso
(methods(i).DeclaringType.ToString
<>
"
System.Object
"
)))))
Then
%
>
<
%
=
CreateMethodTest(methods(i), instanceMethodCount) %
>
<
%
End
iF
%
>
<
%
Next
%
>
#End Region
End Class
#End Region
<
%
End
If
%
>
<
%
If
(GeneratePropertyClasses)
Then
%
>
<
%
'
/*
'
----------------------------------------------
'
----------------- Properties -----------------
'
----------------------------------------------
'
*/%>
#Region
"Property Tests"
<
TestFixture
>
_
public
class
PropertyTesting
//
Variable
to
hold
object
to
be tested
for
reuse by init functions
<
%
=
FullClassName %
>
m_
<
%
=
GetCamelCaseName(GetClassName(FullClassName)) %
>
;
#Region
Test Preparation Methods
#Region
TestFixture SetUp and TearDown
<
%
=
CreatePropertyTestFixtureInit(FullClassName) %
>
<
%
=
CreatePropertyTestFixtureDispose(GetClassName(FullClassName)) %
>
#End Region
#Region
Test SetUp and TearDown
<
SetUp
>
_
public
Sub
TestInit()
End Sub
<
TearDown
>
_
public
Sub
TestDispose()
End Sub
#End Region
#End Region
#Region
Test Methods
<
%
Dim
i
As
Integer
%
>
<
%
For
i
=
0
To
properties.Length
-
1
If
(properties(i).DeclaringType.ToString()
<>
DeclaringTypeToExclude)
Then
%
>
<
%
=
CreatePropertyTest(properties(i))%
>
<
%
End
iF
%
>
<
%
Next
%
>
#End Region
End Sub
#End Region
<
%
End
If
%
>
End Namespace
<
script runat
=
"
template
"
>
Dim
constructors()
As
ConstructorInfo
=
Nothing
Dim
methods()
As
MethodInfo
=
Nothing
Dim
properties()
As
PropertyInfo
=
Nothing
Dim
staticMethodCount
As
Hashtable
=
New
Hashtable
Dim
instanceMethodCount
As
Hashtable
=
New
Hashtable
Dim
instanceConstructorCount
As
Hashtable
=
New
Hashtable
Private
_assemblyPath
As
String
<
Editor(
GetType
(System.Windows.Forms.Design.FileNameEditor),
GetType
(System.Drawing.Design.UITypeEditor)), _
CodeTemplateProperty(CodeTemplatePropertyOption.Required), _
Category(
"
Template Options
"
), _
Description(
"
This is the full path, including the assembly name, of the assembly which contains the class to test.
"
&
_
""
)
>
_
Public
Property
AssemblyPath
As
String
Get
Return
_assemblyPath
End
Get
Set
_assemblyPath
=
value
End
Set
End Property
Public
Sub
InitTemplate()
'
Debugger.Break();
'
Reflect object properties
constructors
=
ReflectClassConstructors(AssemblyPath, FullClassName)
methods
=
ReflectClassMethods(AssemblyPath, FullClassName)
properties
=
ReflectClassProperties(AssemblyPath, FullClassName)
End Sub
Public
Function
GetCamelCaseName(
ByVal
value
As
String
)
As
String
Return
(value.Substring(
0
,
1
).ToLower
+
value.Substring(
1
))
End Function
Public
Function
GetCaseName(
ByVal
value
As
String
)
As
String
Return
(value.Substring(
0
,
1
).ToUpper
+
value.Substring(
1
))
End Function
'
Returns just the class name given a fully qualified class name; i.e. BHCS.Portal.McCoy.Engine.Entities.Module
Public
Function
GetClassName(
ByVal
fullClassName
As
String
)
As
String
Return
fullClassName.Substring((fullClassName.LastIndexOf(
"
.
"
)
+
1
))
End Function
Public
Function
GetNamespaceOnly(
ByVal
fullClassName
As
String
)
As
String
Return
fullClassName.Substring(
0
, fullClassName.LastIndexOf(
"
.
"
))
End Function
Public
Function
GetTestNamespace(
ByVal
fullClassName
As
String
)
As
String
Return
(GetNamespaceOnly(fullClassName)
+
(
"
.Test.
"
+
GetClassName(fullClassName)))
End Function
Public
Function
CreateConstructorTestBody(
ByVal
constructor
As
ConstructorInfo)
As
String
Dim
sb
As
StringBuilder
=
New
StringBuilder
sb.Append(
"
// Declare variables to pass to constructor call
"
&
vbLf)
sb.Append(
""
&
vbTab
&
vbTab
&
vbTab
&
"
//TODO: Set values for variables
"
&
vbLf)
Dim
parameters()
As
ParameterInfo
=
constructor.GetParameters
Dim
c
As
Integer
=
0
Do
While
(c
<
parameters.Length)
sb.Append(
""
&
vbTab
&
vbTab
&
vbTab)
sb.Append(parameters(c).ParameterType.ToString)
sb.Append(
"
_
"
)
sb.Append(parameters(c).Name)
sb.Append(
"
=
"
)
sb.Append(GetTypeDefaultValue(parameters(c).ParameterType.ToString))
sb.Append(
"
;
"
&
vbLf)
c
=
(c
+
1
)
Loop
sb.Append(
""
&
vbTab
&
vbTab
&
vbTab
&
vbLf)
sb.Append(
""
&
vbTab
&
vbTab
&
vbTab
&
"
// Declare return type to hold constructor result
"
&
vbLf)
sb.Append(
""
&
vbTab
&
vbTab
&
vbTab)
sb.Append(FullClassName)
sb.Append(
"
_returnValue;
"
&
vbLf)
sb.Append(
""
&
vbTab
&
vbTab
&
vbTab
&
vbLf)
sb.Append(
""
&
vbTab
&
vbTab
&
vbTab
&
"
// Instantiate object
"
&
vbLf)
sb.Append(
""
&
vbTab
&
vbTab
&
vbTab
&
"
_returnValue = new
"
)
sb.Append(FullClassName)
sb.Append(
"
(
"
)
Do
While
(c
<
parameters.Length)
sb.Append(
"
_
"
)
sb.Append(parameters(c).Name)
sb.Append(
"
,
"
)
c
=
(c
+
1
)
Loop
If
(parameters.Length
>
0
)
Then
sb.Remove((sb.Length
-
2
),
2
)
End
If
sb.Append(
"
);
"
&
vbLf)
sb.Append(
""
&
vbTab
&
vbTab
&
vbTab
&
vbLf)
sb.Append(
""
&
vbTab
&
vbTab
&
vbTab
&
"
// Perform Assert Tests
"
&
vbLf)
sb.Append(
""
&
vbTab
&
vbTab
&
vbTab
&
"
//TODO: Write Assert Tests for object instance
"
&
vbLf)
Return
sb.ToString
End Function
'
Creates the constructor test method
Public
Function
CreateConstructorTest(
ByVal
constructorName
As
String
,
ByVal
constructor
As
ConstructorInfo)
As
String
Dim
sb
As
StringBuilder
=
New
StringBuilder
sb.Append(
"
<Test, Ignore(\""Setup
"
)
sb.Append(FullClassName)
sb.Append(
"
parameter value and Assert tests for the constructor\"")> _
"
&
vbLf)
sb.Append(
""
&
vbTab
&
vbTab
&
"
public Sub
"
)
sb.Append(constructorName)
sb.Append(
"
()
"
&
vbLf)
sb.Append(
""
&
vbTab
&
vbTab
&
""
&
vbLf
&
vbTab
&
vbTab
&
vbTab)
sb.Append(CreateConstructorTestBody(constructor))
sb.Append(
""
&
vbLf
&
vbTab
&
vbTab
&
"
End Sub
"
)
Return
sb.ToString
End Function
Public
Function
ReflectClassConstructors(
ByVal
assemblyPath
As
String
,
ByVal
fullyQualifiedClassName
As
String
)
As
ConstructorInfo()
'
Retrieve the type of the class
Dim
type
As
System.Type
=
GetClassType(assemblyPath, fullyQualifiedClassName)
'
Return the list of properties
If
IncludeInheritedMembers
Then
Return
type.GetConstructors((BindingFlags.Public
Or
BindingFlags.Instance))
Else
Return
type.GetConstructors((BindingFlags.Public _
Or
(BindingFlags.Instance
Or
BindingFlags.DeclaredOnly)))
End
If
End Function
Public
Function
CreatePropertyTestFixtureInit(
ByVal
fullClassName
As
String
)
As
String
Dim
sb
As
StringBuilder
=
New
StringBuilder
sb.Append(
"
<TestFixtureSetUp> _
"
&
vbLf)
sb.Append(
""
&
vbTab
&
vbTab
&
"
public Sub FixtureInit()
"
&
vbLf)
sb.Append(
""
&
vbTab
&
vbTab
&
""
&
vbLf)
sb.Append(
""
&
vbTab
&
vbTab
&
vbTab
&
"
//TODO: Initialize the
"
)
sb.Append(fullClassName)
sb.Append(
"
for the test suite with the desired constructor and variables
"
&
vbLf)
sb.Append(
""
&
vbTab
&
vbTab
&
vbTab)
sb.Append(
"
m_
"
)
sb.Append(GetCamelCaseName(GetClassName(fullClassName)))
sb.Append(
"
= new
"
)
sb.Append(fullClassName)
sb.Append(
"
();
"
&
vbLf)
sb.Append(
""
&
vbTab
&
vbTab
&
"
End Sub
"
)
Return
sb.ToString
End Function
Public
Function
CreatePropertyTestFixtureDispose(
ByVal
className
As
String
)
As
String
Dim
sb
As
StringBuilder
=
New
StringBuilder
sb.Append(
"
<TestFixtureTearDown> _
"
&
vbLf)
sb.Append(
""
&
vbTab
&
vbTab
&
"
public Sub FixtureDispose()
"
&
vbLf)
sb.Append(
""
&
vbTab
&
vbTab
&
""
&
vbLf)
sb.Append(
""
&
vbTab
&
vbTab
&
vbTab
&
"
m_
"
)
sb.Append(GetCamelCaseName(className))
sb.Append(
"
= null;
"
&
vbLf)
sb.Append(
""
&
vbTab
&
vbTab
&
"
End Sub
"
)
Return
sb.ToString
End Function
'
Create the body of the _property test
Public
Function
Create_propertyTestBody(
ByVal
_property
As
propertyInfo)
As
String
Dim
sb
As
StringBuilder
=
New
StringBuilder
If
_property.CanRead
Then
sb.Append(
""
&
vbTab
&
vbTab
&
vbTab
&
"
// Declare return variable to hold _property get method
"
&
vbLf)
sb.Append(
""
&
vbTab
&
vbTab
&
vbTab)
sb.Append(_property.propertyType.ToString)
sb.Append(
"
_getValue =
"
)
sb.Append(GetTypeDefaultValue(_property.propertyType.ToString))
sb.Append(
"
;
"
&
vbLf)
sb.Append(
""
&
vbLf
&
vbTab
&
vbTab
&
vbTab
&
"
// Test get method
"
&
vbLf)
sb.Append(
""
&
vbTab
&
vbTab
&
vbTab
&
"
_getValue =
"
)
If
_property.GetAccessors()(
0
).IsStatic
Then
sb.Append(FullClassName)
Else
sb.Append(
"
m_
"
)
sb.Append(GetCamelCaseName(GetClassName(FullClassName)))
End
If
sb.Append(
"
.
"
)
sb.Append(_property.Name)
sb.Append(
"
;
"
&
vbLf)
End
If
If
_property.CanWrite
Then
sb.Append(
""
&
vbLf)
sb.Append(
""
&
vbTab
&
vbTab
&
vbTab
&
"
// Declare variable to hold _property set method
"
&
vbLf)
sb.Append(
""
&
vbTab
&
vbTab
&
vbTab)
sb.Append(_property.propertyType.ToString)
sb.Append(
"
_setValue =
"
)
sb.Append(GetTypeDefaultValue(_property.propertyType.ToString))
sb.Append(
"
;
"
&
vbLf)
sb.Append(
""
&
vbLf
&
vbTab
&
vbTab
&
vbTab
&
"
// Test set method
"
&
vbLf)
If
_property.GetAccessors()(
0
).IsStatic
Then
sb.Append(
""
&
vbTab
&
vbTab
&
vbTab)
sb.Append(FullClassName)
Else
sb.Append(
""
&
vbTab
&
vbTab
&
vbTab
&
"
m_
"
)
sb.Append(GetCamelCaseName(GetClassName(FullClassName)))
End
If
sb.Append(
"
.
"
)
sb.Append(_property.Name)
sb.Append(
"
= _setValue
"
)
sb.Append(
"
;
"
&
vbLf)
End
If
sb.Append(
""
&
vbTab
&
vbTab
&
vbTab
&
vbLf)
sb.Append(
""
&
vbTab
&
vbTab
&
vbTab
&
"
// Perform Assert Tests
"
&
vbLf)
sb.Append(
""
&
vbTab
&
vbTab
&
vbTab
&
"
//TODO: Write Assert Tests for
"
)
sb.Append(_property.Name)
sb.Append(
"
()
"
&
vbLf)
Return
sb.ToString
End Function
'
Create the body of the _property test
Public
Function
CreatePropertyTestBody(
ByVal
_property
As
PropertyInfo)
As
String
Dim
sb
As
StringBuilder
=
New
StringBuilder
If
_property.CanRead
Then
sb.Append(
""
&
vbTab
&
vbTab
&
vbTab
&
"
// Declare return variable to hold _property get method
"
&
vbLf)
sb.Append(
""
&
vbTab
&
vbTab
&
vbTab)
sb.Append(_property.PropertyType.ToString)
sb.Append(
"
_getValue =
"
)
sb.Append(GetTypeDefaultValue(_property.PropertyType.ToString))
sb.Append(
"
;
"
&
vbLf)
sb.Append(
""
&
vbLf
&
vbTab
&
vbTab
&
vbTab
&
"
// Test get method
"
&
vbLf)
sb.Append(
""
&
vbTab
&
vbTab
&
vbTab
&
"
_getValue =
"
)
If
_property.GetAccessors()(
0
).IsStatic
Then
sb.Append(FullClassName)
Else
sb.Append(
"
m_
"
)
sb.Append(GetCamelCaseName(GetClassName(FullClassName)))
End
If
sb.Append(
"
.
"
)
sb.Append(_property.Name)
sb.Append(
"
;
"
&
vbLf)
End
If
If
_property.CanWrite
Then
sb.Append(
""
&
vbLf)
sb.Append(
""
&
vbTab
&
vbTab
&
vbTab
&
"
// Declare variable to hold _property set method
"
&
vbLf)
sb.Append(
""
&
vbTab
&
vbTab
&
vbTab)
sb.Append(_property.PropertyType.ToString)
sb.Append(
"
_setValue =
"
)
sb.Append(GetTypeDefaultValue(_property.PropertyType.ToString))
sb.Append(
"
;
"
&
vbLf)
sb.Append(
""
&
vbLf
&
vbTab
&
vbTab
&
vbTab
&
"
// Test set method
"
&
vbLf)
If
_property.GetAccessors()(
0
).IsStatic
Then
sb.Append(
""
&
vbTab
&
vbTab
&
vbTab)
sb.Append(FullClassName)
Else
sb.Append(
""
&
vbTab
&
vbTab
&
vbTab
&
"
m_
"
)
sb.Append(GetCamelCaseName(GetClassName(FullClassName)))
End
If
sb.Append(
"
.
"
)
sb.Append(_property.Name)
sb.Append(
"
= _setValue
"
)
sb.Append(
"
;
"
&
vbLf)
End
If
sb.Append(
""
&
vbTab
&
vbTab
&
vbTab
&
vbLf)
sb.Append(
""
&
vbTab
&
vbTab
&
vbTab
&
"
// Perform Assert Tests
"
&
vbLf)
sb.Append(
""
&
vbTab
&
vbTab
&
vbTab
&
"
//TODO: Write Assert Tests for
"
)
sb.Append(_property.Name)
sb.Append(
"
()
"
&
vbLf)
Return
sb.ToString
End Function
'
Creates the property test method
Public
Function
CreatePropertyTest(
ByVal
_property
As
PropertyInfo)
As
String
Dim
sb
As
StringBuilder
=
New
StringBuilder
sb.Append(
"
<Test, Ignore(\""Setup
"
)
sb.Append(
"
Assert tests for the property
"
)
sb.Append(GetCaseName(_property.Name))
sb.Append(
"
\"")> _
"
&
vbLf
&
vbTab
&
vbTab
&
"
public Sub
"
)
sb.Append(GetCaseName(_property.Name))
sb.Append(
"
()
"
&
vbLf)
sb.Append(
""
&
vbTab
&
vbTab
&
""
&
vbLf)
sb.Append(CreatePropertyTestBody(_property))
sb.Append(
""
&
vbTab
&
vbTab
&
"
End Sub
"
)
Return
sb.ToString
End Function
Public
Function
ReflectClassProperties(
ByVal
assemblyPath
As
String
,
ByVal
fullyQualifiedClassName
As
String
)
As
PropertyInfo()
'
Retrieve the type of the class
Dim
type
As
System.Type
=
GetClassType(assemblyPath, fullyQualifiedClassName)
'
Return the list of properties
If
IncludeInheritedMembers
Then
Return
type.GetProperties((BindingFlags.Public _
Or
(BindingFlags.Static
Or
BindingFlags.Instance)))
Else
Return
type.GetProperties((BindingFlags.Public _
Or
(BindingFlags.Static _
Or
(BindingFlags.Instance
Or
BindingFlags.DeclaredOnly))))
End
If
End Function
Public
Function
CreateMethodTestInit(
ByVal
fullClassName
As
String
)
As
String
Dim
sb
As
StringBuilder
=
New
StringBuilder
sb.Append(
"
<SetUp> _
"
&
vbLf)
sb.Append(
""
&
vbTab
&
vbTab
&
"
public Sub TestInit()
"
&
vbLf)
sb.Append(
""
&
vbTab
&
vbTab
&
""
&
vbLf)
sb.Append(
""
&
vbTab
&
vbTab
&
vbTab
&
"
//TODO: Initialize the
"
)
sb.Append(fullClassName)
sb.Append(
"
for each test with the desired constructor and variables
"
&
vbLf)
sb.Append(
""
&
vbTab
&
vbTab
&
vbTab)
sb.Append(
"
m_
"
)
sb.Append(GetCamelCaseName(GetClassName(fullClassName)))
sb.Append(
"
= new
"
)
sb.Append(fullClassName)
sb.Append(
"
();
"
&
vbLf)
sb.Append(
""
&
vbTab
&
vbTab
&
"
End Sub
"
)
Return
sb.ToString
End Function
Public
Function
CreateMethodTestDispose(
ByVal
className
As
String
)
As
String
Dim
sb
As
StringBuilder
=
New
StringBuilder
sb.Append(
"
<TearDown> _
"
&
vbLf)
sb.Append(
""
&
vbTab
&
vbTab
&
"
public Sub TestDispose()
"
&
vbLf)
sb.Append(
""
&
vbTab
&
vbTab
&
""
&
vbLf)
sb.Append(
""
&
vbTab
&
vbTab
&
vbTab
&
"
m_
"
)
sb.Append(GetCamelCaseName(className))
sb.Append(
"
= null;
"
&
vbLf)
sb.Append(
""
&
vbTab
&
vbTab
&
"
End Sub
"
)
Return
sb.ToString
End Function
Public
Function
CreateMethodTestBody(
ByVal
method
As
MethodInfo)
As
String
Dim
sb
As
StringBuilder
=
New
StringBuilder
sb.Append(
""
&
vbTab
&
vbTab
&
vbTab
&
"
// Declare variables to pass to method call
"
&
vbLf)
sb.Append(
""
&
vbTab
&
vbTab
&
vbTab
&
"
//TODO: Set values for variables
"
&
vbLf)
Dim
parameters()
As
ParameterInfo
=
method.GetParameters
Dim
c
As
Integer
=
0
Do
While
(c
<
parameters.Length)
sb.Append(
""
&
vbTab
&
vbTab
&
vbTab)
sb.Append(parameters(c).ParameterType.ToString)
sb.Append(
"
_
"
)
sb.Append(parameters(c).Name)
sb.Append(
"
=
"
)
sb.Append(GetTypeDefaultValue(parameters(c).ParameterType.ToString))
sb.Append(
"
;
"
&
vbLf)
c
=
(c
+
1
)
Loop
sb.Append(
""
&
vbTab
&
vbTab
&
vbTab
&
vbLf)
If
(method.ReturnType.ToString
<>
"
System.Sub
"
)
Then
sb.Append(
""
&
vbTab
&
vbTab
&
vbTab
&
"
// Declare return type to hold method result
"
&
vbLf)
sb.Append(
""
&
vbTab
&
vbTab
&
vbTab)
sb.Append(method.ReturnType.ToString)
sb.Append(
"
_returnValue;
"
&
vbLf)
sb.Append(
""
&
vbTab
&
vbTab
&
vbTab
&
vbLf)
sb.Append(
""
&
vbTab
&
vbTab
&
vbTab
&
"
// Make method call
"
&
vbLf)
sb.Append(
""
&
vbTab
&
vbTab
&
vbTab
&
"
_returnValue =
"
)
Else
sb.Append(
""
&
vbTab
&
vbTab
&
vbTab
&
"
// Make method call
"
&
vbLf)
sb.Append(
""
&
vbTab
&
vbTab
&
vbTab)
End
If
If
method.IsStatic
Then
sb.Append(FullClassName)
Else
sb.Append(
"
m_
"
)
sb.Append(GetCamelCaseName(GetClassName(FullClassName)))
End
If
sb.Append(
"
.
"
)
sb.Append(method.Name)
sb.Append(
"
(
"
)
Do
While
(c
<
parameters.Length)
sb.Append(
"
_
"
)
sb.Append(parameters(c).Name)
sb.Append(
"
,
"
)
c
=
(c
+
1
)
Loop
If
(parameters.Length
>
0
)
Then
sb.Remove((sb.Length
-
2
),
2
)
End
If
sb.Append(
"
);
"
&
vbLf)
sb.Append(
""
&
vbTab
&
vbTab
&
vbTab
&
vbLf)
sb.Append(
""
&
vbTab
&
vbTab
&
vbTab
&
"
// Perform Assert Tests
"
&
vbLf)
sb.Append(
""
&
vbTab
&
vbTab
&
vbTab
&
"
//TODO: Write Assert Tests for
"
)
sb.Append(method.Name)
sb.Append(
"
()
"
&
vbLf)
Return
sb.ToString
End Function
'
Creates the method test method
Public
Function
CreateMethodTest(
ByVal
method
As
MethodInfo,
ByVal
methodCount
As
Hashtable)
As
String
Dim
sb
As
StringBuilder
=
New
StringBuilder
Dim
currentOverloadCount
As
Integer
=
MethodOverloadCount(method.Name, methodCount)
sb.Append(
"
<Test, Ignore(\""Setup
"
)
sb.Append(
"
parameter value(s) and Assert tests for the method
"
)
sb.Append(GetCaseName(method.Name))
sb.Append(
"
\"")> _
"
&
vbLf
&
vbTab
&
vbTab
&
"
public Sub
"
)
sb.Append(GetCaseName(method.Name))
If
(currentOverloadCount
>
1
)
Then
sb.Append(
"
_Overload
"
)
sb.Append(currentOverloadCount.ToString)
End
If
sb.Append(
"
()
"
&
vbLf)
sb.Append(
""
&
vbTab
&
vbTab
&
""
&
vbLf)
sb.Append(CreateMethodTestBody(method))
sb.Append(
""
&
vbTab
&
vbTab
&
"
End Sub
"
)
Return
sb.ToString
End Function
'
Returns the current number of overloads for a given method name
Public
Function
MethodOverloadCount(
ByVal
methodName
As
String
,
ByVal
methodCount
As
Hashtable)
As
Integer
Dim
currentCount
As
Integer
=
0
If
methodCount.ContainsKey(methodName)
Then
currentCount
=
CType
(methodCount(methodName),
Integer
)
End
If
Dim
i
As
Integer
=
0
For
i
=
0
To
methods.Length
-
1
If
(methods(i).Name
=
methodName)
Then
currentCount
=
(currentCount
+
1
)
If
methodCount.ContainsKey(methodName)
Then
If
(currentCount
>
CType
(methodCount(methodName),
Integer
))
Then
methodCount(methodName)
=
(
CType
(methodCount(methodName),
Integer
)
+
1
)
End
If
Else
methodCount.Add(methodName,
1
)
End
If
If
(currentCount
=
CType
(methodCount(methodName),
Integer
))
Then
Exit
For
End
If
End
If
Next
Return
CType
(methodCount(methodName),
Integer
)
End Function
Public
Function
ReflectClassMethods(
ByVal
assemblyPath
As
String
,
ByVal
fullyQualifiedClassName
As
String
)
As
MethodInfo()
'
Retrieve the type of the class
Dim
type
As
System.Type
=
GetClassType(assemblyPath, fullyQualifiedClassName)
'
Return the list of properties
If
IncludeInheritedMembers
Then
Return
type.GetMethods((BindingFlags.Public _
Or
(BindingFlags.Static
Or
BindingFlags.Instance)))
Else
Return
type.GetMethods((BindingFlags.Public _
Or
(BindingFlags.Static _
Or
(BindingFlags.Instance
Or
BindingFlags.DeclaredOnly))))
End
If
End Function
Public
Function
GetClassType(
ByVal
assemblyPath
As
String
,
ByVal
fullyQualifiedClassName
As
String
)
As
System.Type
'
Load the assembly which contains the class to reflect
Dim
assembly
As
System.Reflection.Assembly
=
System.Reflection.Assembly.LoadFile(assemblyPath)
'
Retrieve the type of the class
Dim
type
As
System.Type
=
assembly
.GetType(fullyQualifiedClassName)
'
Release the assembly
assembly
=
Nothing
Return
type
End Function
Public
Function
GetTypeDefaultValue(
ByVal
typeName
As
String
)
As
String
'
init the variable value
Dim
variableValue
As
String
=
string
.Empty
'
determine the default variable value from the variable type
Select
Case
(
typeName
)
Case
"
System.Binary
"
,
"
System.Byte
"
,
"
System.Int16
"
,
"
System.Int32
"
,
"
System.Int64
"
variableValue
=
"
0
"
Exit
Select
Case
"
System.String
"
variableValue
=
"
String.Empty
"
Exit
Select
Case
"
System.Boolean
"
variableValue
=
"
false
"
Exit
Select
Case
"
System.Guid
"
variableValue
=
"
Guid.NewGuid()
"
Exit
Select
Case
"
System.DateTime
"
variableValue
=
"
DateTime.MinValue
"
Exit
Select
Case
Else
variableValue
=
"
null
"
Exit
Select
End
Select
Return
variableValue
End Function
</
script
>
C#版
<%
@ CodeTemplate Language
=
"
C#
"
TargetLanguage
=
"
Text
"
Description
=
"
Template description here.
"
Debug
=
"
False
"
%>
<%
@ Assembly Name
=
"
System.Design
"
%>
<%
@ Import Namespace
=
"
System.Reflection
"
%>
<%
@ Import Namespace
=
"
System.Collections
"
%>
<%
@ Import Namespace
=
"
System.Text
"
%>
<%
@ Property Name
=
"
FullClassName
"
Type
=
"
System.String
"
Category
=
"
Template Options
"
Description
=
"
This is the fully qualified name of the class to test.
"
%>
<%
@ Property Name
=
"
IncludeInheritedMembers
"
Type
=
"
System.Boolean
"
Category
=
"
Template Options
"
Required
=
"
true
"
default
=
"
false
"
%>
<%
@ Property Name
=
"
DeclaringTypeToExclude
"
Type
=
"
System.String
"
Category
=
"
Template Options
"
Required
=
"
false
"
%>
<%
@ Property Name
=
"
GenerateConstructorClasses
"
Type
=
"
System.Boolean
"
Category
=
"
Constructor Tests
"
Required
=
"
true
"
Default
=
"
true
"
Description
=
"
Should the appropriate test classes be generated.
"
%>
<%
@ Property Name
=
"
GeneratePropertyClasses
"
Type
=
"
System.Boolean
"
Category
=
"
Property Tests
"
Required
=
"
true
"
Default
=
"
true
"
Description
=
"
Should the appropriate test classes be generated.
"
%>
<%
@ Property Name
=
"
GenerateMethodClasses
"
Type
=
"
System.Boolean
"
Category
=
"
Method Tests
"
Required
=
"
true
"
Default
=
"
true
"
Description
=
"
Should the appropriate test classes be generated.
"
%>
<%
InitTemplate();
%>
using
System;
using
NUnit.Framework;
using
<%=
GetNamespaceOnly(FullClassName)
%>
;
namespace
<%=
GetTestNamespace(FullClassName)
%>
{
<%
/*
----------------------------------------------
--------------- Constructors -----------------
----------------------------------------------
*/
if
(GenerateConstructorClasses){
%>
#region
Constructor Tests
[TestFixture]
public
class
ConstructorTesting
{
#region
Test Preparation Methods
#region
TestFixture SetUp and TearDown
[TestFixtureSetUp]
public
void
FixtureInit()
{
}
[TestFixtureTearDown]
public
void
FixtureDispose()
{
}
#endregion
#region
Test SetUp and TearDown
[SetUp]
public
void
TestInit()
{
}
[TearDown]
public
void
TestDispose()
{
}
#endregion
#endregion
#region
Test Methods
<%
for
(
int
i
=
0
; i
<
constructors.Length; i
++
){
%>
<%
if
( i
==
0
) {
%>
<%=
CreateConstructorTest(GetClassName(FullClassName),constructors[i])
%>
<%
}
else
{
%>
<%=
CreateConstructorTest(GetClassName(FullClassName)
+
"
_Overload
"
+
i.ToString(),constructors[i])
%>
<%
}}
%>
#endregion
}
#endregion
<%
}
%>
<%
if
(GenerateMethodClasses) {
%>
<%
/*
----------------------------------------------
-------------- Static Methods ----------------
----------------------------------------------
*/
%>
#region
Static Method Tests
[TestFixture]
public
class
StaticMethodTesting
{
#region
Test Preparation Methods
#region
TestFixture Setup and TearDown
[TestFixtureSetUp]
public
void
FixtureInit()
{
}
[TestFixtureTearDown]
public
void
FixtureDispose()
{
}
#endregion
#region
Test SetUp and TearDown
[SetUp]
public
void
TestInit()
{
}
[TearDown]
public
void
TestDispose()
{
}
#endregion
#endregion
#region
Test Methods
<%
for
(
int
i
=
0
; i
<
methods.Length; i
++
){
if
(methods[i].IsStatic
&&
!
methods[i].Name.StartsWith(
"
get_
"
)
&&
!
methods[i].Name.StartsWith(
"
set_
"
)
&&
methods[i].DeclaringType.ToString()
!=
DeclaringTypeToExclude
&&
methods[i].DeclaringType.ToString()
!=
"
System.Object
"
){
%>
<%=
CreateMethodTest(methods[i], staticMethodCount)
%>
<%
}}
%>
#endregion
}
#endregion
<%
/*
----------------------------------------------
------------------ Methods -------------------
----------------------------------------------
*/
%>
#region
Method Tests
[TestFixture]
public
class
MethodTesting
{
//
Variable to hold object to be tested for reuse by init functions
<%=
FullClassName
%>
m_
<%=
GetCamelCaseName(GetClassName(FullClassName))
%>
;
#region
Test Preparation Methods
#region
TestFixture SetUp and TearDown
[TestFixtureSetUp]
public
void
FixtureInit()
{
}
[TestFixtureTearDown]
public
void
FixtureDispose()
{
}
#endregion
#region
Test SetUp and TearDown
<%=
CreateMethodTestInit(FullClassName)
%>
<%=
CreateMethodTestDispose(GetClassName(FullClassName))
%>
#endregion
#endregion
#region
Test Methods
<%
for
(
int
i
=
0
; i
<
methods.Length; i
++
){
if
(
!
methods[i].IsStatic
&&
!
methods[i].Name.StartsWith(
"
get_
"
)
&&
!
methods[i].Name.StartsWith(
"
set_
"
)
&&
methods[i].DeclaringType.ToString()
!=
DeclaringTypeToExclude
&&
methods[i].DeclaringType.ToString()
!=
"
System.Object
"
){
%>
<%=
CreateMethodTest(methods[i], instanceMethodCount)
%>
<%
}}
%>
#endregion
}
#endregion
<%
}
%>
<%
if
(GeneratePropertyClasses) {
%>
<%
/*
----------------------------------------------
----------------- Properties -----------------
----------------------------------------------
*/
%>
#region
Property Tests
[TestFixture]
public
class
PropertyTesting
{
//
Variable to hold object to be tested for reuse by init functions
<%=
FullClassName
%>
m_
<%=
GetCamelCaseName(GetClassName(FullClassName))
%>
;
#region
Test Preparation Methods
#region
TestFixture SetUp and TearDown
<%=
CreatePropertyTestFixtureInit(FullClassName)
%>
<%=
CreatePropertyTestFixtureDispose(GetClassName(FullClassName))
%>
#endregion
#region
Test SetUp and TearDown
[SetUp]
public
void
TestInit()
{
}
[TearDown]
public
void
TestDispose()
{
}
#endregion
#endregion
#region
Test Methods
<%
for
(
int
i
=
0
; i
<
properties.Length; i
++
){
if
(properties[i].DeclaringType.ToString()
!=
DeclaringTypeToExclude){
%>
<%=
CreatePropertyTest(properties[i])
%>
<%
}}
%>
#endregion
}
#endregion
<%
}
%>
}
<
script runat
=
"
template
"
>
ConstructorInfo[] constructors
=
null
;
MethodInfo[] methods
=
null
;
PropertyInfo[] properties
=
null
;
//
Hashtables used to identify overloads for methods and constructors
Hashtable staticMethodCount
=
new
Hashtable();
Hashtable instanceMethodCount
=
new
Hashtable();
Hashtable instanceConstructorCount
=
new
Hashtable();
[Editor(
typeof
(System.Windows.Forms.Design.FileNameEditor),
typeof
(System.Drawing.Design.UITypeEditor))]
[CodeTemplateProperty(CodeTemplatePropertyOption.Required)]
[Category(
"
Template Options
"
)]
[Description(
"
This is the full path, including the assembly name, of the assembly which contains the class to test.
"
)]
public
string
AssemblyPath
{
get
{
return
assemblyPath; }
set
{ assemblyPath
=
value; }
}
private
string
assemblyPath;
public
void
InitTemplate()
{
//
Debugger.Break();
//
Reflect object properties
constructors
=
ReflectClassConstructors(AssemblyPath, FullClassName);
methods
=
ReflectClassMethods(AssemblyPath, FullClassName);
properties
=
ReflectClassProperties(AssemblyPath, FullClassName);
}
public
string
GetCamelCaseName(
string
value)
{
return
value.Substring(
0
,
1
).ToLower()
+
value.Substring(
1
);
}
public
string
GetCaseName(
string
value)
{
return
value.Substring(
0
,
1
).ToUpper()
+
value.Substring(
1
);
}
//
Returns just the class name given a fully qualified class name; i.e. BHCS.Portal.McCoy.Engine.Entities.Module
public
string
GetClassName(
string
fullClassName)
{
return
fullClassName.Substring(fullClassName.LastIndexOf(
"
.
"
)
+
1
);
}
public
string
GetNamespaceOnly(
string
fullClassName)
{
return
fullClassName.Substring(
0
,fullClassName.LastIndexOf(
"
.
"
));
}
public
string
GetTestNamespace(
string
fullClassName)
{
return
GetNamespaceOnly(fullClassName)
+
"
.Test.
"
+
GetClassName(fullClassName);
}
public
string
CreateConstructorTestBody(ConstructorInfo constructor)
{
StringBuilder sb
=
new
StringBuilder();
sb.Append(
"
// Declare variables to pass to constructor call\n
"
);
sb.Append(
"
\t\t\t//TODO: Set values for variables\n
"
);
ParameterInfo[] parameters
=
constructor.GetParameters();
for
(
int
c
=
0
; c
<
parameters.Length; c
++
)
{
sb.Append(
"
\t\t\t
"
);
sb.Append(parameters[c].ParameterType.ToString());
sb.Append(
"
_
"
);
sb.Append(parameters[c].Name);
sb.Append(
"
=
"
);
sb.Append(GetTypeDefaultValue(parameters[c].ParameterType.ToString()));
sb.Append(
"
;\n
"
);
}
sb.Append(
"
\t\t\t\n
"
);
sb.Append(
"
\t\t\t// Declare return type to hold constructor result\n
"
);
sb.Append(
"
\t\t\t
"
);
sb.Append(FullClassName);
sb.Append(
"
_returnValue;\n
"
);
sb.Append(
"
\t\t\t\n
"
);
sb.Append(
"
\t\t\t// Instantiate object\n
"
);
sb.Append(
"
\t\t\t_returnValue = new
"
);
sb.Append(FullClassName);
sb.Append(
"
(
"
);
for
(
int
c
=
0
; c
<
parameters.Length; c
++
)
{
sb.Append(
"
_
"
);
sb.Append(parameters[c].Name);
sb.Append(
"
,
"
);
}
if
(parameters.Length
>
0
)
sb.Remove(sb.Length
-
2
,
2
);
sb.Append(
"
);\n
"
);
sb.Append(
"
\t\t\t\n
"
);
sb.Append(
"
\t\t\t// Perform Assert Tests\n
"
);
sb.Append(
"
\t\t\t//TODO: Write Assert Tests for object instance\n
"
);
return
sb.ToString();
}
//
Creates the constructor test method
public
string
CreateConstructorTest(
string
constructorName, ConstructorInfo constructor)
{
StringBuilder sb
=
new
StringBuilder();
sb.Append(
"
[Test, Ignore(\
"
Setup
"
);
sb.Append(FullClassName);
sb.Append(
"
parameter value and Assert tests for the constructor\
"
)]\n
"
);
sb.Append(
"
\t\tpublic void
"
);
sb.Append(constructorName);
sb.Append(
"
()\n
"
);
sb.Append(
"
\t\t{\n\t\t\t
"
);
sb.Append(CreateConstructorTestBody(constructor));
sb.Append(
"
\n\t\t}
"
);
return
sb.ToString();
}
public
ConstructorInfo[] ReflectClassConstructors(
string
assemblyPath,
string
fullyQualifiedClassName)
{
//
Retrieve the type of the class
System.Type type
=
GetClassType(assemblyPath, fullyQualifiedClassName);
//
Return the list of properties
if
(IncludeInheritedMembers)
return
type.GetConstructors(BindingFlags.Public
|
BindingFlags.Instance);
else
return
type.GetConstructors(BindingFlags.Public
|
BindingFlags.Instance
|
BindingFlags.DeclaredOnly);
}
public
string
CreatePropertyTestFixtureInit(
string
fullClassName)
{
StringBuilder sb
=
new
StringBuilder();
sb.Append(
"
[TestFixtureSetUp]\n
"
);
sb.Append(
"
\t\tpublic void FixtureInit()\n
"
);
sb.Append(
"
\t\t{\n
"
);
sb.Append(
"
\t\t\t//TODO: Initialize the
"
);
sb.Append(fullClassName);
sb.Append(
"
for the test suite with the desired constructor and variables\n
"
);
sb.Append(
"
\t\t\t
"
);
sb.Append(
"
m_
"
);
sb.Append(GetCamelCaseName(GetClassName(fullClassName)));
sb.Append(
"
= new
"
);
sb.Append(fullClassName);
sb.Append(
"
();\n
"
);
sb.Append(
"
\t\t}
"
);
return
sb.ToString();
}
public
string
CreatePropertyTestFixtureDispose(
string
className)
{
StringBuilder sb
=
new
StringBuilder();
sb.Append(
"
[TestFixtureTearDown]\n
"
);
sb.Append(
"
\t\tpublic void FixtureDispose()\n
"
);
sb.Append(
"
\t\t{\n
"
);
sb.Append(
"
\t\t\tm_
"
);
sb.Append(GetCamelCaseName(className));
sb.Append(
"
= null;\n
"
);
sb.Append(
"
\t\t}
"
);
return
sb.ToString();
}
//
Create the body of the property test
public
string
CreatePropertyTestBody(PropertyInfo property)
{
StringBuilder sb
=
new
StringBuilder();
if
(property.CanRead)
{
sb.Append(
"
\t\t\t// Declare return variable to hold property get method\n
"
);
sb.Append(
"
\t\t\t
"
);
sb.Append(property.PropertyType.ToString());
sb.Append(
"
_getValue =
"
);
sb.Append(GetTypeDefaultValue(property.PropertyType.ToString()));
sb.Append(
"
;\n
"
);
sb.Append(
"
\n\t\t\t// Test get method\n
"
);
sb.Append(
"
\t\t\t_getValue =
"
);
if
(property.GetAccessors()[
0
].IsStatic)
{
sb.Append(FullClassName);
}
else
{
sb.Append(
"
m_
"
);
sb.Append(GetCamelCaseName(GetClassName(FullClassName)));
}
sb.Append(
"
.
"
);
sb.Append(property.Name);
sb.Append(
"
;\n
"
);
}
if
(property.CanWrite)
{
sb.Append(
"
\n
"
);
sb.Append(
"
\t\t\t// Declare variable to hold property set method\n
"
);
sb.Append(
"
\t\t\t
"
);
sb.Append(property.PropertyType.ToString());
sb.Append(
"
_setValue =
"
);
sb.Append(GetTypeDefaultValue(property.PropertyType.ToString()));
sb.Append(
"
;\n
"
);
sb.Append(
"
\n\t\t\t// Test set method\n
"
);
if
(property.GetAccessors()[
0
].IsStatic)
{
sb.Append(
"
\t\t\t
"
);
sb.Append(FullClassName);
}
else
{
sb.Append(
"
\t\t\tm_
"
);
sb.Append(GetCamelCaseName(GetClassName(FullClassName)));
}
sb.Append(
"
.
"
);
sb.Append(property.Name);
sb.Append(
"
= _setValue
"
);
sb.Append(
"
;\n
"
);
}
sb.Append(
"
\t\t\t\n
"
);
sb.Append(
"
\t\t\t// Perform Assert Tests\n
"
);
sb.Append(
"
\t\t\t//TODO: Write Assert Tests for
"
);
sb.Append(property.Name);
sb.Append(
"
()\n
"
);
return
sb.ToString();
}
//
Creates the property test method
public
string
CreatePropertyTest(PropertyInfo property)
{
StringBuilder sb
=
new
StringBuilder();
sb.Append(
"
[Test, Ignore(\
"
Setup
"
);
sb.Append(
"
Assert tests for the property
"
);
sb.Append(GetCaseName(property.Name));
sb.Append(
"
\
"
)]\n\t\tpublic
void
"
);
sb.Append(GetCaseName(property.Name));
sb.Append(
"
()\n
"
);
sb.Append(
"
\t\t{\n
"
);
sb.Append(CreatePropertyTestBody(property));
sb.Append(
"
\t\t}
"
);
return
sb.ToString();
}
public
PropertyInfo[] ReflectClassProperties(
string
assemblyPath,
string
fullyQualifiedClassName)
{
//
Retrieve the type of the class
System.Type type
=
GetClassType(assemblyPath, fullyQualifiedClassName);
//
Return the list of properties
if
(IncludeInheritedMembers)
return
type.GetProperties(BindingFlags.Public
|
BindingFlags.Static
|
BindingFlags.Instance);
else
return
type.GetProperties(BindingFlags.Public
|
BindingFlags.Static
|
BindingFlags.Instance
|
BindingFlags.DeclaredOnly);
}
public
string
CreateMethodTestInit(
string
fullClassName)
{
StringBuilder sb
=
new
StringBuilder();
sb.Append(
"
[SetUp]\n
"
);
sb.Append(
"
\t\tpublic void TestInit()\n
"
);
sb.Append(
"
\t\t{\n
"
);
sb.Append(
"
\t\t\t//TODO: Initialize the
"
);
sb.Append(fullClassName);
sb.Append(
"
for each test with the desired constructor and variables\n
"
);
sb.Append(
"
\t\t\t
"
);
sb.Append(
"
m_
"
);
sb.Append(GetCamelCaseName(GetClassName(fullClassName)));
sb.Append(
"
= new
"
);
sb.Append(fullClassName);
sb.Append(
"
();\n
"
);
sb.Append(
"
\t\t}
"
);
return
sb.ToString();
}
public
string
CreateMethodTestDispose(
string
className)
{
StringBuilder sb
=
new
StringBuilder();
sb.Append(
"
[TearDown]\n
"
);
sb.Append(
"
\t\tpublic void TestDispose()\n
"
);
sb.Append(
"
\t\t{\n
"
);
sb.Append(
"
\t\t\tm_
"
);
sb.Append(GetCamelCaseName(className));
sb.Append(
"
= null;\n
"
);
sb.Append(
"
\t\t}
"
);
return
sb.ToString();
}
public
string
CreateMethodTestBody(MethodInfo method)
{
StringBuilder sb
=
new
StringBuilder();
sb.Append(
"
\t\t\t// Declare variables to pass to method call\n
"
);
sb.Append(
"
\t\t\t//TODO: Set values for variables\n
"
);
ParameterInfo[] parameters
=
method.GetParameters();
for
(
int
c
=
0
; c
<
parameters.Length; c
++
)
{
sb.Append(
"
\t\t\t
"
);
sb.Append(parameters[c].ParameterType.ToString());
sb.Append(
"
_
"
);
sb.Append(parameters[c].Name);
sb.Append(
"
=
"
);
sb.Append(GetTypeDefaultValue(parameters[c].ParameterType.ToString()));
sb.Append(
"
;\n
"
);
}
sb.Append(
"
\t\t\t\n
"
);
if
(method.ReturnType.ToString()
!=
"
System.Void
"
)
{
sb.Append(
"
\t\t\t// Declare return type to hold method result\n
"
);
sb.Append(
"
\t\t\t
"
);
sb.Append(method.ReturnType.ToString());
sb.Append(
"
_returnValue;\n
"
);
sb.Append(
"
\t\t\t\n
"
);
sb.Append(
"
\t\t\t// Make method call\n
"
);
sb.Append(
"
\t\t\t_returnValue =
"
);
}
else
{
sb.Append(
"
\t\t\t// Make method call\n
"
);
sb.Append(
"
\t\t\t
"
);
}
if
(method.IsStatic)
{
sb.Append(FullClassName);
}
else
{
sb.Append(
"
m_
"
);
sb.Append(GetCamelCaseName(GetClassName(FullClassName)));
}
sb.Append(
"
.
"
);
sb.Append(method.Name);
sb.Append(
"
(
"
);
for
(
int
c
=
0
; c
<
parameters.Length; c
++
)
{
sb.Append(
"
_
"
);
sb.Append(parameters[c].Name);
sb.Append(
"
,
"
);
}
if
(parameters.Length
>
0
)
sb.Remove(sb.Length
-
2
,
2
);
sb.Append(
"
);\n
"
);
sb.Append(
"
\t\t\t\n
"
);
sb.Append(
"
\t\t\t// Perform Assert Tests\n
"
);
sb.Append(
"
\t\t\t//TODO: Write Assert Tests for
"
);
sb.Append(method.Name);
sb.Append(
"
()\n
"
);
return
sb.ToString();
}
//
Creates the method test method
public
string
CreateMethodTest(MethodInfo method, Hashtable methodCount)
{
StringBuilder sb
=
new
StringBuilder();
int
currentOverloadCount
=
MethodOverloadCount(method.Name, methodCount);
sb.Append(
"
[Test, Ignore(\
"
Setup
"
);
sb.Append(
"
parameter value(s) and Assert tests for the method
"
);
sb.Append(GetCaseName(method.Name));
sb.Append(
"
\
"
)]\n\t\tpublic
void
"
);
sb.Append(GetCaseName(method.Name));
if
(currentOverloadCount
>
1
)
{
sb.Append(
"
_Overload
"
);
sb.Append(currentOverloadCount.ToString());
}
sb.Append(
"
()\n
"
);
sb.Append(
"
\t\t{\n
"
);
sb.Append(CreateMethodTestBody(method));
sb.Append(
"
\t\t}
"
);
return
sb.ToString();
}
//
Returns the current number of overloads for a given method name
public
int
MethodOverloadCount(
string
methodName, Hashtable methodCount)
{
int
currentCount
=
0
;
if
(methodCount.ContainsKey(methodName))
{
currentCount
=
(
int
)methodCount[methodName];
}
for
(
int
i
=
0
; i
<
methods.Length; i
++
)
{
if
(methods[i].Name
==
methodName)
{
currentCount
++
;
if
(methodCount.ContainsKey(methodName))
{
if
(currentCount
>
(
int
)methodCount[methodName])
methodCount[methodName]
=
(
int
)methodCount[methodName]
+
1
;
}
else
methodCount.Add(methodName,
1
);
if
(currentCount
==
(
int
)methodCount[methodName])
break
;
}
}
return
((
int
)methodCount[methodName]);
}
public
MethodInfo[] ReflectClassMethods(
string
assemblyPath,
string
fullyQualifiedClassName)
{
//
Retrieve the type of the class
System.Type type
=
GetClassType(assemblyPath, fullyQualifiedClassName);
//
Return the list of properties
if
(IncludeInheritedMembers)
return
type.GetMethods(BindingFlags.Public
|
BindingFlags.Static
|
BindingFlags.Instance);
else
return
type.GetMethods(BindingFlags.Public
|
BindingFlags.Static
|
BindingFlags.Instance
|
BindingFlags.DeclaredOnly);
}
public
System.Type GetClassType(
string
assemblyPath,
string
fullyQualifiedClassName)
{
//
Load the assembly which contains the class to reflect
System.Reflection.Assembly assembly
=
System.Reflection.Assembly.LoadFile(assemblyPath);
//
Retrieve the type of the class
System.Type type
=
assembly.GetType(fullyQualifiedClassName);
//
Release the assembly
assembly
=
null
;
return
type;
}
public
string
GetTypeDefaultValue(
string
typeName)
{
//
init the variable value
string
variableValue
=
string
.Empty;
//
determine the default variable value from the variable type
switch
(typeName)
{
case
"
System.Binary
"
:
case
"
System.Byte
"
:
case
"
System.Int16
"
:
case
"
System.Int32
"
:
case
"
System.Int64
"
:
{
variableValue
=
"
0
"
;
break
;
}
case
"
System.String
"
:
{
variableValue
=
"
String.Empty
"
;
break
;
}
case
"
System.Boolean
"
:
{
variableValue
=
"
false
"
;
break
;
}
case
"
System.Guid
"
:
{
variableValue
=
"
Guid.NewGuid()
"
;
break
;
}
case
"
System.DateTime
"
:
{
variableValue
=
"
DateTime.MinValue
"
;
break
;
}
default
:
{
variableValue
=
"
null
"
;
break
;
}
}
return
variableValue;
}
</
script
>