Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added missing endsWith macro for String #51

Merged
merged 1 commit into from
Jun 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions interpreter/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ impl<'a> Default for Context<'a> {
ctx.add_function("all", functions::all);
ctx.add_function("max", functions::max);
ctx.add_function("startsWith", functions::starts_with);
ctx.add_function("endsWith", functions::ends_with);
ctx.add_function("matches", functions::matches);
ctx.add_function("duration", functions::duration);
ctx.add_function("timestamp", functions::timestamp);
Expand Down
20 changes: 20 additions & 0 deletions interpreter/src/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,16 @@ pub fn starts_with(This(this): This<Arc<String>>, prefix: Arc<String>) -> bool {
this.starts_with(prefix.as_str())
}

/// Returns true if a string ends with another string.
///
/// # Example
/// ```cel
/// "abc".endsWith("c") == true
/// ```
pub fn ends_with(This(this): This<Arc<String>>, suffix: Arc<String>) -> bool {
this.ends_with(suffix.as_str())
}

/// Returns true if a string matches the regular expression.
///
/// # Example
Expand Down Expand Up @@ -671,6 +681,16 @@ mod tests {
.for_each(assert_script);
}

#[test]
fn test_ends_with() {
[
("ends with true", "'foobar'.endsWith('bar') == true"),
("ends with false", "'foobar'.endsWith('foo') == false"),
]
.iter()
.for_each(assert_script);
}

#[test]
fn test_timestamp() {
[(
Expand Down
Loading