Friday, December 01, 2006

Regular Expressions to the Rescue of Testing

Recently I wrote about the problem of maintaining lots of xml test files. Here at IniTech we have 15 major test groups in my current project, each with a config file containing about ten descriptions of test components. This morning I needed to add three new attributes to each of the roughly 150 test components. Like most IDE since the 80s, Visual Studio 2005 has regular expression replacement. I needed to add jobNumber, localJobNumber, and nickName attributes with the value being a varient of the 'name' attribute.
<Survey name='USM1' groupName='US Malls' priority='4' status='1' cellLimiter='100'
surveyLimiter='100' dateStart='2004-01-07T09:30:00' dateEnd='2006-01-28T16:30:00'
exclusions='0' cluster='Cluster1' allSurveysExclusion='-1' surveyType='AdHoc'
excludeSurveyType='' excludeSurveyTypeDays='-1' >
becomes
<Survey name='USM1' groupName='US Malls' priority='4' status='1' cellLimiter='100'
surveyLimiter='100' dateStart='2004-01-07T09:30:00' dateEnd='2006-01-28T16:30:00'
exclusions='0' cluster='Cluster1' allSurveysExclusion='-1' surveyType='AdHoc'
excludeSurveyType='' excludeSurveyTypeDays='-1' jobNumber='JN-USM1'
localJobNumber='LJN-USM1' nickName='Nick-USM1' >
This handy little regex replaced them all with the highest level of quality assurance:
Find What:
 \<Survey name='{[a-zA-Z0-9]+}'{[^>]+}{\>}
Replace With:
 \<Survey name='\1'\2 jobNumber='JN-\1' localJobNumber='LJN-\1' nickName='Nick-\1' >

Note that the value of the "name" attribute is gathered into the '\1' variable because its description is surrounded with curly braces.

No comments: