From 2baad5fd00bcd8bdac23eabb3644951c6aa3abd3 Mon Sep 17 00:00:00 2001 From: Alex Snaps Date: Wed, 19 Jun 2024 11:19:22 -0400 Subject: [PATCH] Added missing endsWith macro for String --- interpreter/src/context.rs | 1 + interpreter/src/functions.rs | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/interpreter/src/context.rs b/interpreter/src/context.rs index 5761a03..3cb2437 100644 --- a/interpreter/src/context.rs +++ b/interpreter/src/context.rs @@ -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); diff --git a/interpreter/src/functions.rs b/interpreter/src/functions.rs index 6ca5409..4b219b1 100644 --- a/interpreter/src/functions.rs +++ b/interpreter/src/functions.rs @@ -224,6 +224,16 @@ pub fn starts_with(This(this): This>, prefix: Arc) -> 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>, suffix: Arc) -> bool { + this.ends_with(suffix.as_str()) +} + /// Returns true if a string matches the regular expression. /// /// # Example @@ -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() { [(