1
How to use the Words / GetWords method on a string
Question asked by Patrick Sager - 4/16/2021 at 2:05 AM
Answered
Hello

In GhostDoc pro I'm editing the rule for comments.of unit test methods.

My unit test names have the following naming format:

MethodToBeTested_Condition_ExpectedResult

I'd like to generate a summary of the following format:

"Checks that if MethodToBeTested is called with Condition, then ExpectedResult is returned."

I've already adapted the corresponding part in the rule according to the following:

// Check the setting for the test methods documentation option. Default value is "true".
else if((bool)Context.GetSettingValue("UseTestMethodTemplate", true) && IsTestMethod())        
{
  string[] parts = Context.CurrentCodeElement.Name.Split('_');
  System.Text.StringBuilder summary = new System.Text.StringBuilder("Checks that if ");
  if (parts.Length >= 1)            
  {
     summary.AppendFormat("{0} is called", parts[0]);
  }
  if (parts.Length >= 2)            
  {
     summary.AppendFormat(" with {0}", parts[1]);
  }
  if (parts.Length >= 3)            
  {
    summary.AppendFormat(", then {0} is returned.", parts[2]);
  }
  this.WriteLine(summary.ToString());
}

This works. 
However, the condition consists quite often of multiple words (e.g., "XLarger2AndYNegative").
Therefore, I'd like to split the condition part into words (e.g., x larger 2 and y negative").
I've tried to use the Words macro and the Context.GetWords method. But I was not able to figure out a way to use them on a string. How could I do that?

Any help appreciated
-- rotello


7 Replies

Reply to Thread
0
Misha Zhutov Replied
Employee Post
Hello, 

The Context.GetWords method returns the array of "tokenized" words from the method name.
For example, for the "MethodToBeTested_Condition_ExpectedResult" it returns ["method", "to", "be", "tested", "condition", "expected", "result"].
For the "MethodToBeTested_XLarger2AndYNegative_ExpectedResult" it returns ["method", "to", "be", "tested", "x", "larger2", "and", "y", "negative" "expected", "result"].
You can combine the result array to create your own summary.
0
Patrick Sager Replied
But this way, I need to figure out, which words belong to the method name and which to the condition.
Is there no easy way to get the words from a string (e.g., GetWords("XLarger2AndYNegative")?
0
Misha Zhutov Replied
Employee Post
It seems your question is not clear to me. You can run the the Context.GetWords("XLarger2AndYNegative"), for example. from T4 template and you will get ["x", "larger2", "and", "y", "negative"] array.
0
Patrick Sager Replied
Ok, now I see.
GetWords returns an object of type SubMain.GhostDoc.Words.
However, I cannot iterate over it with foreach.
I see that in the template code that it has the properties Count and First.
How can I iterate over a Words object, or even better where can I find the API documentation for the Words class?


0
Misha Zhutov Replied
Employee Post
Hi Partick, 

We did not add any documentation for the Words object because it was for internal use only. You can open the SubMain.GhostDoc.Configuration.dll in Reflector and find the SubMain.GhostDoc.Words object there.
0
Patrick Sager Replied
Marked As Answer
Hi Misha
Thanks for your quick replies.
".All" did the trick.
Regs
-- Pat
0
Misha Zhutov Replied
Employee Post
Thank you for the update.

Reply to Thread