Skip to content

Commit

Permalink
Implement matches for string.
Browse files Browse the repository at this point in the history
  • Loading branch information
cloutierMat committed Oct 24, 2024
1 parent 2e3e901 commit a856a14
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
fail-fast: false
matrix:
python_version:
- "3.10"
- "3.11"
steps:
- name: Checkout
uses: actions/checkout@v2
Expand Down
3 changes: 2 additions & 1 deletion airspeed/operators.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ def dict_to_string(obj: dict) -> str:
"contains": lambda self, value: value in self,
"indexOf": lambda self, ch: self.index(ch),
"substring": lambda self, start, end=None: self[start:end],
"matches": lambda self, pattern: bool(re.fullmatch(pattern, self)),
},
list: {
"size": lambda self: len(self),
Expand Down Expand Up @@ -455,7 +456,7 @@ def calculate(self, namespace, loader):


class StringLiteral(_Element):
STRING = re.compile(r"'((?:\\['nrbt\\\\\\$]|[^'\\])*)'(.*)", re.S)
STRING = re.compile(r"'([^']*)'(.*)", re.S)
ESCAPED_CHAR = re.compile(r"\\([nrbt'\\])")

def parse(self):
Expand Down
34 changes: 34 additions & 0 deletions tests/test_templating.py
Original file line number Diff line number Diff line change
Expand Up @@ -845,6 +845,40 @@ def test_dict_to_string(self, test_render):
template = "#set( $myObject = {'k1': 'v1', 'k2': 'v2'} )$myObject.toString()"
test_render(template)

def test_string_matches_true(self, test_render):
template = "#set( $myString = '123456789' )$myString.matches( '[0-9]*' )"
test_render(template)

def test_string_matches_false(self, test_render):
template = "#set( $myString = 'd123' )$myString.matches( '[0-9]*' )"
test_render(template)

def test_string_matches_full_date(self, test_render):
template = (
"#set( $myString = '2020-01-20T08:00:00.000Z' )"
"$myString.matches('^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:00\.000Z$')" # noqa
)
test_render(template)

template = (
'#set( $myString = "2020-01-20T08:00:00.000Z" )'
'$myString.matches("^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:00\.000Z$")' # noqa
)
test_render(template)

def test_string_with_escaped_char(self, test_render):
template = "#set( $myString = '\{\n\t\r\%\5' )$myString" # noqa
test_render(template)

template = '#set( $myString = "\{\n\t\r\%\5" )$myString' # noqa
test_render(template)

template = "'\{\n\t\r\%\5'" # noqa
test_render(template)

template = '"\{\n\t\r\%\5"' # noqa
test_render(template)


class TestInternals:
"""
Expand Down
36 changes: 36 additions & 0 deletions tests/test_templating.snapshot.json
Original file line number Diff line number Diff line change
Expand Up @@ -1068,5 +1068,41 @@
"render-result-1-cli": "\n \"has-value\"\n ",
"render-result-1": "\"does-not-have-value\""
}
},
"tests/test_templating.py::TestTemplating::test_string_matches_true": {
"recorded-date": "24-10-2024, 19:59:43",
"recorded-content": {
"render-result-1-cli": "true",
"render-result-1": "true"
}
},
"tests/test_templating.py::TestTemplating::test_string_matches_false": {
"recorded-date": "24-10-2024, 19:59:21",
"recorded-content": {
"render-result-1-cli": "false",
"render-result-1": "false"
}
},
"tests/test_templating.py::TestTemplating::test_string_with_escaped_char": {
"recorded-date": "24-10-2024, 22:14:20",
"recorded-content": {
"render-result-1-cli": "\\{\n\t\r\\%\u0005",
"render-result-1": "\\{\n\t\r\\%\u0005",
"render-result-2-cli": "\\{\n\t\r\\%\u0005",
"render-result-2": "\\{\n\t\r\\%\u0005",
"render-result-3-cli": "'\\{\n\t\r\\%\u0005'",
"render-result-3": "'\\{\n\t\r\\%\u0005'",
"render-result-4-cli": "\"\\{\n\t\r\\%\u0005\"",
"render-result-4": "\"\\{\n\t\r\\%\u0005\""
}
},
"tests/test_templating.py::TestTemplating::test_string_matches_full_date": {
"recorded-date": "24-10-2024, 21:58:12",
"recorded-content": {
"render-result-1-cli": "true",
"render-result-1": "true",
"render-result-2-cli": "true",
"render-result-2": "true"
}
}
}

0 comments on commit a856a14

Please sign in to comment.