Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 39
Add string extensions quote and reverse#998
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
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
75fafc962078110e1a30f57e795a943ae3303d0a3679ff2066ca2c4dFile filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -33,6 +33,8 @@ | ||
| import dev.cel.runtime.CelEvaluationException; | ||
| import dev.cel.runtime.CelRuntime; | ||
| import dev.cel.runtime.CelRuntimeFactory; | ||
| import java.nio.charset.StandardCharsets; | ||
| import java.util.List; | ||
| import org.junit.Test; | ||
| import org.junit.runner.RunWith; | ||
| @@ -70,7 +72,9 @@ public void library() { | ||
| "lastIndexOf", | ||
| "lowerAscii", | ||
| "replace", | ||
| "reverse", | ||
| "split", | ||
| "strings.quote", | ||
| "substring", | ||
| "trim", | ||
| "upperAscii"); | ||
| @@ -1467,6 +1471,88 @@ public void stringExtension_functionSubset_success() throws Exception { | ||
| assertThat(evaluatedResult).isEqualTo(true); | ||
| } | ||
| @Test | ||
| @TestParameters("{string: 'abcd', expectedResult: 'dcba'}") | ||
| @TestParameters("{string: '', expectedResult: ''}") | ||
| @TestParameters("{string: 'a', expectedResult: 'a'}") | ||
| @TestParameters("{string: 'hello world', expectedResult: 'dlrow olleh'}") | ||
| @TestParameters("{string: 'ab가cd', expectedResult: 'dc가ba'}") | ||
| public void reverse_success(String string, String expectedResult) throws Exception { | ||
| CelAbstractSyntaxTree ast = COMPILER.compile("s.reverse()").getAst(); | ||
| CelRuntime.Program program = RUNTIME.createProgram(ast); | ||
| Object evaluatedResult = program.eval(ImmutableMap.of("s", string)); | ||
| assertThat(evaluatedResult).isEqualTo(expectedResult); | ||
| } | ||
| @Test | ||
pkwarren marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| @TestParameters("{string: '😁😑😦', expectedResult: '😦😑😁'}") | ||
| @TestParameters("{string: '\u180e\u200b\u200c\u200d\u2060\ufeff', expectedResult: '\ufeff\u2060\u200d\u200c\u200b\u180e'}") | ||
| public void reverse_unicode(String string, String expectedResult) throws Exception { | ||
| CelAbstractSyntaxTree ast = COMPILER.compile("s.reverse()").getAst(); | ||
| CelRuntime.Program program = RUNTIME.createProgram(ast); | ||
| Object evaluatedResult = program.eval(ImmutableMap.of("s", string)); | ||
| assertThat(evaluatedResult).isEqualTo(expectedResult); | ||
| } | ||
| @Test | ||
| @TestParameters("{string: 'hello', expectedResult: '\"hello\"'}") | ||
| @TestParameters("{string: '', expectedResult: '\"\"'}") | ||
| @TestParameters("{string: 'contains \\\"quotes\\\"', expectedResult: '\"contains \\\\\\\"quotes\\\\\\\"\"'}") | ||
pkwarren marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| @TestParameters("{string: 'ends with \\\\', expectedResult: '\"ends with \\\\\\\\\"'}") | ||
| @TestParameters("{string: '\\\\ starts with', expectedResult: '\"\\\\\\\\ starts with\"'}") | ||
| public void quote_success(String string, String expectedResult) throws Exception { | ||
pkwarren marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| CelAbstractSyntaxTree ast = COMPILER.compile("strings.quote(s)").getAst(); | ||
| CelRuntime.Program program = RUNTIME.createProgram(ast); | ||
| Object evaluatedResult = program.eval(ImmutableMap.of("s", string)); | ||
| assertThat(evaluatedResult).isEqualTo(expectedResult); | ||
| } | ||
| @Test | ||
| public void quote_singleWithDoubleQuotes() throws Exception { | ||
| String expr = "strings.quote('single-quote with \"double quote\"')"; | ||
| String expected = "\"\\\"single-quote with \\\\\\\"double quote\\\\\\\"\\\"\""; | ||
| CelAbstractSyntaxTree ast = COMPILER.compile(expr + " == " + expected).getAst(); | ||
| CelRuntime.Program program = RUNTIME.createProgram(ast); | ||
| Object evaluatedResult = program.eval(); | ||
| assertThat(evaluatedResult).isEqualTo(true); | ||
| } | ||
| @Test | ||
| public void quote_escapesSpecialCharacters() throws Exception { | ||
| CelAbstractSyntaxTree ast = COMPILER.compile("strings.quote(s)").getAst(); | ||
| CelRuntime.Program program = RUNTIME.createProgram(ast); | ||
| Object evaluatedResult = | ||
| program.eval( | ||
| ImmutableMap.of( | ||
| "s", "\u0007bell\u000Bvtab\bback\ffeed\rret\nline\ttab\\slash 가 😁")); | ||
| assertThat(evaluatedResult) | ||
| .isEqualTo("\"\\abell\\vvtab\\bback\\ffeed\\rret\\nline\\ttab\\\\slash 가 😁\""); | ||
| } | ||
| @Test | ||
| public void quote_escapesMalformed_endWithHighSurrogate() throws Exception { | ||
| CelRuntime.Program program = RUNTIME.createProgram(COMPILER.compile("strings.quote(s)").getAst()); | ||
| assertThat(program.eval(ImmutableMap.of("s", "end with high surrogate \uD83D"))) | ||
| .isEqualTo("\"end with high surrogate \uFFFD\""); | ||
| } | ||
| @Test | ||
| public void quote_escapesMalformed_unpairedHighSurrogate() throws Exception { | ||
| CelRuntime.Program program = RUNTIME.createProgram(COMPILER.compile("strings.quote(s)").getAst()); | ||
| assertThat(program.eval(ImmutableMap.of("s", "bad pair \uD83DA"))) | ||
| .isEqualTo("\"bad pair \uFFFDA\""); | ||
| } | ||
| @Test | ||
| public void stringExtension_compileUnallowedFunction_throws() { | ||
| CelCompiler celCompiler = | ||
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.