Here's a brief explanation of my method:
- Provide a string and parse it to HTML code respecting a specific format.
The accepted format is:
For dropdown menu : Test DropDown~Select:Options1; Options2;Options3--- --- For Text area: Test Text area~TextArea:text to display**
I am inheriting this VB code from a legacy system we are using right now and trying to replace with a C# system. Though the naming convention is off, it is not how I declare my variables/methods (look at my C# unit testing code to have an idea).
How I can maintain the tests? We change the CSS classes a lot, which means I have to change the produced HTML, which means changing my tests. Is that ok?
Public Function BuildHtmlControlResponsive(sourceText As String, indexNum As Integer) As String
Dim out As StringBuilder = New StringBuilder
'To store the text after the "~"
Dim textSelection As String
Dim tildeLocation As Integer
'To store the text of the control to parse (EX:Text Area, Input , Options menu ...)
Dim controlOption As String = String.Empty
'To store the name the text of the label
Dim textLabel As String = String.Empty
'To store the list of options (If applicable)
Dim contentText As String = String.Empty
tildeLocation = sourceText.IndexOf("~"c)
If (tildeLocation > -1) Then
'This gets the text that comes after the ~. Example Company~TextArea would return TextArea.
textSelection = sourceText.Substring((tildeLocation + 1), (sourceText.Length - (tildeLocation + 1)))
textLabel = sourceText.Substring(0, sourceText.IndexOf("~"c))
'See if the string has the : sign
If (textSelection.IndexOf(":"c) > -1) Then
'This gets the text that comes before the : Example Company:TextArea would return TextArea.
controlOption = textSelection.Substring(0, textSelection.IndexOf(":"c))
'This gets the text that comes After the : Example Company:TextArea would return TextArea.
contentText = textSelection.Substring(textSelection.IndexOf(":"c) + 1)
End If
End If
out.Append("<div class=""form-group"">")
out.Append("<label for=""extField_" & indexNum & """ class=""col-md-4 control-label"" id=""extLabelField_" & indexNum & """ >" & textLabel & ":</label>" & vbCrLf)
out.Append("<div class=""col-md-8"">")
If (textSelection <> String.Empty) Then
Select Case controlOption.ToUpper()
Case "TEXTAREA", "TEXT AREA"
out.Append("<textarea id=""extField_" & indexNum & """ class=""form-control"" type=""text"" rows=""3"" placeholder=""" & textLabel & """> </textarea> ")
Case "SELECT"
If (sourceText.IndexOf(";"c) <> -1) Then
' list box. Syntax: name ; option 1 ; option 2 ; ...
Dim OptionList As String()
Dim i As Integer
OptionList = contentText.Split(";"c)
out.Append("<select class=""form-control"" id=""extField_" & indexNum & """>")
For i = 0 To UBound(OptionList)
out.Append("<option>" & OptionList(i) & "</option>")
Next
out.Append("</select>")
Else
' text box. Syntax: name
out.Append("<input type=""text"" class=""form-control"" id=""extField_" & indexNum & """ Columns=""18"" class=""form"" />" & vbCrLf)
End If
Case Else
out.Append("<input type=""text"" class=""form-control"" id=""extField_" & indexNum & """ placeholder=""" & textLabel & """>")
End Select
Else
'Defaults to outputting a text field if nothing is specified.
out.Append("<input type=""text"" class=""form-control"" id=""extField_" & indexNum & """ placeholder=""" & textLabel & """>")
End If
out.Append("</div>")
out.Append("</div>")
Return out.ToString()
End Function
Unit tests
[TestMethod]
public void ResponsiveEPriceCustomFieldsOptionsTestMethod()
{
//Initialize
string inputStringOptions = "Test~Select: option1;option2;Option3;option4";
//arrange
UCSendePrice UCsendPriceInstance = new userControl_sendeprice_r1();
string ExpectedHTMLResultOptions = @"<div class=""form-group""><label for=""extField_1"" class=""col-md-4 control-label"" id=""extLabelField_1"" >Test:</label>
<div class=""col-md-8""><select class=""form-control"" id=""extField_1""><option> option1</option><option>option2</option><option>Option3</option><option>option4</option></select></div></div>";
//Act
string actualHTMLCodeOptions = UCsendPriceInstance.BuildHtmlControlResponsive(inputStringOptions, 1);
//Assert
Assert.AreEqual(ExpectedHTMLResultOptions, actualHTMLCodeOptions);
}
[TestMethod]
public void ResponsiveEPriceCustomFieldsTextAreaTestMethod()
{
//Initialize
UCSendePrice uCsendPriceInstance = new userControl_sendeprice_r1();
string inputStringTextArea = "Test~TextArea: option1;option2;Option3;option4";
//arrange
string ExpectedHTMLResultTextArea = @"<div class=""form-group""><label for=""extField_1"" class=""col-md-4 control-label"" id=""extLabelField_1"" >Test:</label>
<div class=""col-md-8""><textarea id=""extField_1"" class=""form-control"" type=""text"" rows=""3"" placeholder=""Test""> </textarea> </div></div>";
//Act
string actualHTMLCodeTextArea = uCsendPriceInstance.BuildHtmlControlResponsive(inputStringTextArea, 1);
//Assert
Assert.AreEqual(ExpectedHTMLResultTextArea, actualHTMLCodeTextArea);
}
[TestMethod]
public void ResponsiveEPriceCustomFieldsInputTextTestMethod()
{
//Initialize
UCSendePrice uCsendPriceInstance = new userControl_sendeprice_r1();
string inputStringInputText = "Test Input~ option1;option2;Option3;option4";
//arrange
string ExpectedHTMLResultInputText = @"<div class=""form-group""><label for=""extField_1"" class=""col-md-4 control-label"" id=""extLabelField_1"" >Test Input:</label>
<div class=""col-md-8""><input type=""text"" class=""form-control"" id=""extField_1"" placeholder=""Test Input""></div></div>";
//Act
string actualHTMLCodeInputText = uCsendPriceInstance.BuildHtmlControlResponsive(inputStringInputText, 1);
//Assert
Assert.AreEqual(ExpectedHTMLResultInputText, actualHTMLCodeInputText);
}