Aug 18 2016

A Flexible Data-Driven Select Case Function

It is possible to define conditions which, if true, trigger a call to another function or subroutine, or execute a block of code. Usually this is done by writing a decision-making structure in our code, such as If-Else If-Else for the simpler cases or a Select Case (switch in JavaScript) for the more complex ones.

However, it is also possible to achieve this in a Data-Driven fashion. For example, suppose that an Excel file has two columns named CONDITION and ACTION. Each row would contain a string that represents an expression that evaluates as true or false on the CONDITION column, and on the ACTION column the name of a function (or even a valid code string) to be executed if the former is found true.

Such a list is loaded into a Dictionary (Hash Table) at runtime and passed when needed to a function that evaluates each condition, until one is found true or the end of the list is found. So here below you can find the function [Select Case] which implements precisely this feature.

The function signature is:

Function [Select Case](ByVal dicCases)

Where dicCases is a Dictionary object with key-value pairs defining each condition and the action to execute if evaluated as true.

Note: The square brackets is a not too well known (or documented) feature of VBScript which enables using special characters and reserved keywords in the names of identifiers.

Function [Select Case](ByVal dicCases)
     Dim [Case], [Cases]
     [Select Case] = False    
     If (Not TypeName(dicCases) = "Dictionary") Then    
         Print("Nothing to do")
         Exit Function
     End If
     If (dicCases.Count = 0) Then
         Print("Nothing to do")
         Exit Function
     End If
     [Cases] = dicCases.keys
     For each [Case] in [Cases]
         [Select Case] = Eval([Case])        
         If([Select Case])Then
             Exit For
         End If
 End Function


Dim arrTemp, i
Dim dic
Set dic = CreateObject("Scripting.Dictionary")
dic.Add "arrTemp(i)>70", "Print(arrTemp(i)&"" Wear a T-Shirt!"")"
dic.Add "arrTemp(i)>60", "Print(arrTemp(i)&"" Wear a hat!"")"
dic.Add "arrTemp(i)>50", "Print(arrTemp(i)&"" Wear a long-sleeved shirt!"")"
dic.Add "arrTemp(i)< =50", "Print(arrTemp(i)&"" Wear a coat!"")"
arrTemp = Array(50, 55, 60, 65, 70, 80)
For i = LBound(arrTemp) To UBound(arrTemp)
	[Select Case](dic)

50 Wear a coat!
55 Wear a long-sleeved shirt!
60 Wear a long-sleeved shirt!
65 Wear a hat!
70 Wear a hat!
80 Wear a T-Shirt!

About Meir Bar-Tal

Meir Bar-Tal is an Automation Architect with extensive technical, educational and leadership experience in Software Design, Development and Testing. For many years Meir has helped companies establish solid foundations for their testing activities with a wide variety of software applications and systems and across different technologies and platforms. Meir is a well-known expert in Data-Driven, Keyword Driven and Hybrid methodologies and has published a number of influential articles on the applications of Design Patterns to Test Automation. For the last five years, he has been pushing forward an ambitious program towards the development of an Object Oriented automation framework named Sunscrit, a huge project which is still underway. Meir is the owner of www.AdvancedQTP.com of which he was a co-founder in 2007 and served as its Editor in Chief, Author and Forums Administrator ever since. Meir lives in Israel and cooperates closely with HP R&D and other colleagues to improve the user experience and productivity with HP Software tools.