views:

13

answers:

3

Hi !

Say I have a regex

REGEX = re.compile('.*foo{')

How would you write a unit test that matches a set of string with python 2.4 ?

I know in python 2.7 I can use assertRegexMatches, unfortunately this doesn't work in 2.4 :/

I use self.assertEqual for the rest of my tests.

Cheers, M

+1  A: 

If you want an exact match you can do this:

assertTrue(REGEX.match(data))

If you do not care where it matches then:

assertTrue(REGEX.search(data))

Keep in mind the difference between matching and searching. Also if you are so inclined you can subclass TestCase and add your own assertion to do the above.

Manoj Govindan
A: 
self.assertTrue(REGEX.match(text))
leoluk
+1  A: 

Since you asked about a set of string rather than a single string

def createMatcher( self, regex ):
    def matchCheck( argument ):
        self.assertTrue( regex.match( argument ) )
    return matchCheck

Then in your function:

map( self.createMatcher( REGEX ), mySetOfStrings )
wheaties
Awesome ! Thanks a lot :) M
Martin