C# Class NStub.CSharp.NamespaceDetector

Determines the namespace to use, based on a supplied list of type declarations.
Datei anzeigen Open project: Jedzia/NStub Class Usage Examples

Public Methods

Method Description
GetDifferingTypeFullname ( CodeTypeDeclaration inputType, string testNamespacePart ) : string

Get the differing namespace part between object to test and unit test module, seen relative from the ShortestNamespace, prefixed with the specified string.

If the full qualified name of the input type is "NStub.CSharp.BuildContext.ISetupAndTearDownContext" and the shortest (the root namespace of the library to test) namespace is "NStub.CSharp", then the output of this method is ".Tests.BuildContext". To build a namespace for the unit test, you simply sum the result in the following way:

InsertAfterShortestNamespace ( CodeTypeDeclaration inputType, string testNamespacePart ) : string

Insert the specified extra string into the namespace of a specified type, relative to the ShortestNamespace.

If the full qualified name of the input type is "NStub.CSharp.BuildContext.SetupAndTearDownContext" and the shortest (the root namespace of the library to test) namespace is "NStub.CSharp", then the output of this method is "NStub.CSharp.Tests.BuildContext.SetupAndTearDownContext", inserting the testNamespacePart between the ShortestNamespace and the rest of the full qualified name of inputType. Simply add "Test" at the end to it an you have the full qualified name to your test class.

NamespaceDetector ( CodeTypeDeclarationCollection typeDeclarations ) : System

Initializes a new instance of the NamespaceDetector class.

PrepareNamespaceImports ( IEnumerable imports ) : IEnumerable

Prepares the specified namespace imports to prevent collisions with the types under test.

This method detects problematic namespaces like in the example below and prefixes them with global:: to prevent namespace collisions in the test unit files.

The namespace of the object to test is so the unit test becomes:

Take a look at the 'using MbUnit.Framework' import. In that way it conflicts with the 'NStub.CSharp.MbUnit' import and circumvents the name resolution of the TestAttribute. This method detects such concerns and puts a global:: prefix in front of the 'MbUnit.Framework' namespace import.

Private Methods

Method Description
DetermineShortestNamespace ( IEnumerable typeDeclarations, IEnumerable &distinctNamespaces ) : string

Determines the shortest namespace from a list of type declarations.

PrepareNamespace ( string ns ) : string

Prepares and caches a namespace.

Method Details

GetDifferingTypeFullname() public method

Get the differing namespace part between object to test and unit test module, seen relative from the ShortestNamespace, prefixed with the specified string.
If the full qualified name of the input type is "NStub.CSharp.BuildContext.ISetupAndTearDownContext" and the shortest (the root namespace of the library to test) namespace is "NStub.CSharp", then the output of this method is ".Tests.BuildContext". To build a namespace for the unit test, you simply sum the result in the following way:
public GetDifferingTypeFullname ( CodeTypeDeclaration inputType, string testNamespacePart ) : string
inputType System.CodeDom.CodeTypeDeclaration The type with the input namespace.
testNamespacePart string The additional namespace part of the test project, e.g. ".Test".
return string

InsertAfterShortestNamespace() public method

Insert the specified extra string into the namespace of a specified type, relative to the ShortestNamespace.
If the full qualified name of the input type is "NStub.CSharp.BuildContext.SetupAndTearDownContext" and the shortest (the root namespace of the library to test) namespace is "NStub.CSharp", then the output of this method is "NStub.CSharp.Tests.BuildContext.SetupAndTearDownContext", inserting the testNamespacePart between the ShortestNamespace and the rest of the full qualified name of inputType. Simply add "Test" at the end to it an you have the full qualified name to your test class.
public InsertAfterShortestNamespace ( CodeTypeDeclaration inputType, string testNamespacePart ) : string
inputType System.CodeDom.CodeTypeDeclaration The type with the input namespace.
testNamespacePart string The additional namespace part of the test project, e.g. ".Test".
return string

NamespaceDetector() public method

Initializes a new instance of the NamespaceDetector class.
public NamespaceDetector ( CodeTypeDeclarationCollection typeDeclarations ) : System
typeDeclarations System.CodeDom.CodeTypeDeclarationCollection The type declarations to investigate.
return System

PrepareNamespaceImports() public method

Prepares the specified namespace imports to prevent collisions with the types under test.
This method detects problematic namespaces like in the example below and prefixes them with global:: to prevent namespace collisions in the test unit files.

The namespace of the object to test is so the unit test becomes:

Take a look at the 'using MbUnit.Framework' import. In that way it conflicts with the 'NStub.CSharp.MbUnit' import and circumvents the name resolution of the TestAttribute. This method detects such concerns and puts a global:: prefix in front of the 'MbUnit.Framework' namespace import.

public PrepareNamespaceImports ( IEnumerable imports ) : IEnumerable
imports IEnumerable The namespace imports.
return IEnumerable