HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpMinor

Rubberduck Test Module Creation

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
testmodulerubberduckcreation

Problem

I recently started learning C# and am taking the opportunity to try and accelerate my introduction by helping the RubberDuck team (Website and GitHub Repo). The full class code that I'm working on can be found here. Its purpose is to create a Module in VBA that will allow unit testing.

-
Since FakesFieldDeclarationFormat and
AssertFieldDeclarationFormat are used only once would it be better
to move them to just above where they are fed into
DeclarationFormatFor?

-
Would it be better to migrate FolderAnnotation into
TestModuleEmptyTemplate since when formattedModuleTemplate is
created that is ultimately what is happening?

-
Does anyone have any suggestions for improving the names?

-
Anything else that help improve the code and that may assist me in learning is welcome.

The main code in question is below.

` private const string TestModuleEmptyTemplate = "'@TestModule\r\n{0}\r\n{1}\r\n{2}\r\n\r\n";
private const string FolderAnnotation = "'@Folder(\"Tests\")\r\n";

private const string FakesFieldDeclarationFormat = "Private Fakes As {0}";
private const string AssertFieldDeclarationFormat = "Private Assert As {0}";

private readonly string _moduleInit = string.Concat(
"'@ModuleInitialize\r\n",
"Public Sub ModuleInitialize()\r\n",
$" '{RubberduckUI.UnitTest_NewModule_RunOnce}.\r\n",
" {0}\r\n",
" {1}\r\n",
"End Sub\r\n\r\n",
"'@ModuleCleanup\r\n",
"Public Sub ModuleCleanup()\r\n",
$" '{RubberduckUI.UnitTest_NewModule_RunOnce}.\r\n",
" Set Assert = Nothing\r\n",
" Set Fakes = nothing\r\n",
"End Sub\r\n\r\n"
);

private readonly string _methodInit = string.Concat(
"'@TestInitialize\r\n"
, "Public Sub TestInitialize()\r\n"
, " '", RubberduckUI.UnitTest_NewModule_RunBeforeTest, ".\r\n"
, "End Sub\r\n\r\n"
, "'@TestCleanup\r\n"
, "Public Sub TestCleanup()\r\n"

Solution

Only targeting _moduleInit and _methodInit

IMO the use of string.Concat with many parameters together with \r\n is hurting the readability a lot.

How about using a StringBuilder instead like so

private readonly string _moduleInit = new StringBuilder(256)
    .AppendLine("'@ModuleInitialize")
    .AppendLine("Public Sub ModuleInitialize()")
    .AppendLine($"    '{RubberduckUI.UnitTest_NewModule_RunOnce}.")
    .AppendLine("    {0}")
    .AppendLine("    {1}")
    .AppendLine("End Sub")
    .AppendLine()
    .AppendLine("'@ModuleCleanup")
    .AppendLine("Public Sub ModuleCleanup()")
    .AppendLine($"    '{RubberduckUI.UnitTest_NewModule_RunOnce}.")
    .AppendLine("    Set Assert = Nothing")
    .AppendLine("    Set Fakes = Nothing")
    .AppendLine("End Sub")
    .AppendLine()
    .ToString();


which is easier to read. Now you will also notice that the Nothing in Set Fakes = Nothing is cased correctly.

For _methodInit this should be applied as well and you should decide if you want to use string interpolation $ or not. You used it for _moduleInit but you didn't for _methodInit. If you have choosen a style, you should stick to it.

Code Snippets

private readonly string _moduleInit = new StringBuilder(256)
    .AppendLine("'@ModuleInitialize")
    .AppendLine("Public Sub ModuleInitialize()")
    .AppendLine($"    '{RubberduckUI.UnitTest_NewModule_RunOnce}.")
    .AppendLine("    {0}")
    .AppendLine("    {1}")
    .AppendLine("End Sub")
    .AppendLine()
    .AppendLine("'@ModuleCleanup")
    .AppendLine("Public Sub ModuleCleanup()")
    .AppendLine($"    '{RubberduckUI.UnitTest_NewModule_RunOnce}.")
    .AppendLine("    Set Assert = Nothing")
    .AppendLine("    Set Fakes = Nothing")
    .AppendLine("End Sub")
    .AppendLine()
    .ToString();

Context

StackExchange Code Review Q#160620, answer score: 6

Revisions (0)

No revisions yet.