Skip to content

Commit

Permalink
Merge pull request #50 from fdesoye/add_str_matches
Browse files Browse the repository at this point in the history
Add String.matches to additional string methods
  • Loading branch information
purcell committed Dec 8, 2023
2 parents 0e85a5a + 80472a0 commit a4b4fc6
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
6 changes: 4 additions & 2 deletions airspeed/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
str: {
'length': lambda self: len(self),
'replaceAll': lambda self, pattern, repl: re.sub(pattern, repl, self),
'startsWith': lambda self, prefix: self.startswith(prefix)
'startsWith': lambda self, prefix: self.startswith(prefix),
'matches': lambda self, pattern: re.match(pattern, self)
},
list: {
'size': lambda self: len(self),
Expand All @@ -36,8 +37,9 @@
'add': lambda self, value: self.append(value)
},
dict: {
'isEmpty': lambda self: not bool(self),
'keySet': lambda self: self.keys(),
'put': lambda self, key, value: self.update({key: value}),
'keySet': lambda self: self.keys()
}
}

Expand Down
25 changes: 24 additions & 1 deletion tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1250,7 +1250,20 @@ def test_string_starts_with_true(self):
self.assertEqual(output, " yes!")

def test_string_starts_with_false(self):
template = airspeed.Template("#set($foo = 'nofoobar123') #if($foo.startsWith('foo'))yes!#end")
template = airspeed.Template(
"#set($foo = 'nofoobar123') #if($foo.startsWith('foo'))yes!#end")
output = template.merge({})
self.assertEqual(output, " ")

def test_string_matches_true(self):
template = airspeed.Template(
"#set($foo = 'nofoobar123') #if($foo.matches('.*foo.*'))yes!#end")
output = template.merge({})
self.assertEqual(output, " yes!")

def test_string_matches_false(self):
template = airspeed.Template(
"#set($foo = 'bar') #if($foo.matches('foo'))yes!#end")
output = template.merge({})
self.assertEqual(output, " ")

Expand All @@ -1260,6 +1273,16 @@ def test_dict_put_item(self):
output = template.merge({'test_dict': {'k': 'initial value'}})
self.assertEqual(output, "new value")

def test_dict_isEmpty(self):
template = airspeed.Template("#set( $emptyDict = {} )"
"$emptyDict.isEmpty()")
output = template.merge({})
self.assertEqual(output, "True")

template = airspeed.Template("#set( $emptyDict = {'foo': 'bar'} )"
"$emptyDict.isEmpty()")
output = template.merge({})
self.assertEqual(output, "False")

def test_evaluate(self):
template = airspeed.Template('''#set($source1 = "abc")
Expand Down

0 comments on commit a4b4fc6

Please sign in to comment.