mirror of
https://github.com/microsoft/regorus.git
synced 2026-08-05 02:16:11 +00:00
strings: concat and contains
Signed-off-by: Anand Krishnamoorthi <anakrish@microsoft.com>
This commit is contained in:
committed by
Anand Krishnamoorthi
parent
89e26e37a2
commit
27c5afd8ee
@@ -13,6 +13,7 @@ use anyhow::Result;
|
||||
|
||||
pub fn register(m: &mut HashMap<&'static str, builtins::BuiltinFcn>) {
|
||||
m.insert("concat", concat);
|
||||
m.insert("contains", contains);
|
||||
}
|
||||
|
||||
fn concat(span: &Span, params: &[Expr], args: &[Value]) -> Result<Value> {
|
||||
@@ -22,3 +23,11 @@ fn concat(span: &Span, params: &[Expr], args: &[Value]) -> Result<Value> {
|
||||
let collection = ensure_string_collection(name, ¶ms[1], &args[1])?;
|
||||
Ok(Value::String(collection.join(&delimiter)))
|
||||
}
|
||||
|
||||
fn contains(span: &Span, params: &[Expr], args: &[Value]) -> Result<Value> {
|
||||
let name = "contains";
|
||||
ensure_args_count(span, name, params, args, 2)?;
|
||||
let s1 = ensure_string(name, ¶ms[0], &args[0])?;
|
||||
let s2 = ensure_string(name, ¶ms[1], &args[1])?;
|
||||
Ok(Value::Bool(s1.contains(&s2)))
|
||||
}
|
||||
|
||||
@@ -31,6 +31,26 @@ cases:
|
||||
s3: ""
|
||||
s4: ""
|
||||
|
||||
- note: undefined-delimiter
|
||||
data: {}
|
||||
modules:
|
||||
- |
|
||||
package test
|
||||
x { false }
|
||||
y = concat(x, [])
|
||||
query: data.test
|
||||
want_result: {}
|
||||
|
||||
- note: undefined-collection
|
||||
data: {}
|
||||
modules:
|
||||
- |
|
||||
package test
|
||||
x { false }
|
||||
y = concat(",", x)
|
||||
query: data.test
|
||||
want_result: {}
|
||||
|
||||
- note: invalid-null-delimiter
|
||||
data: {}
|
||||
modules: ["package test\nx=concat(null, [])"]
|
||||
116
tests/interpreter/cases/builtins/strings/contains.yaml
Normal file
116
tests/interpreter/cases/builtins/strings/contains.yaml
Normal file
@@ -0,0 +1,116 @@
|
||||
# Copyright (c) Microsoft Corporation.
|
||||
# Licensed under the MIT License.
|
||||
|
||||
cases:
|
||||
- note: all
|
||||
data: {}
|
||||
modules:
|
||||
- |
|
||||
package test
|
||||
|
||||
v1 = contains("Hello, world", ", ")
|
||||
v2 = contains("Hello world", "") # empty substring
|
||||
v3 = contains("", ",") # empty string
|
||||
v4 = contains("", "") # empty substring and string
|
||||
|
||||
query: data.test
|
||||
want_result:
|
||||
v1: true
|
||||
v2: true
|
||||
v3: false
|
||||
v4: true
|
||||
|
||||
- note: undefined-string
|
||||
data: {}
|
||||
modules:
|
||||
- |
|
||||
package test
|
||||
x { false }
|
||||
y = contains(x, "")
|
||||
query: data.test
|
||||
want_result: {}
|
||||
|
||||
- note: undefined-substring
|
||||
data: {}
|
||||
modules:
|
||||
- |
|
||||
package test
|
||||
x { false }
|
||||
y = contains(",", x)
|
||||
query: data.test
|
||||
want_result: {}
|
||||
|
||||
- note: invalid-null-string
|
||||
data: {}
|
||||
modules: ["package test\nx=contains(null, ``)"]
|
||||
query: data.test
|
||||
error: "`contains` expects string argument."
|
||||
|
||||
- note: invalid-bool-string
|
||||
data: {}
|
||||
modules: ["package test\nx=contains(true, ``)"]
|
||||
query: data.test
|
||||
error: "`contains` expects string argument."
|
||||
|
||||
- note: invalid-number-string
|
||||
data: {}
|
||||
modules: ["package test\nx=contains(1, ``)"]
|
||||
query: data.test
|
||||
error: "`contains` expects string argument."
|
||||
|
||||
- note: invalid-array-string
|
||||
data: {}
|
||||
modules: ["package test\nx=contains([], ``)"]
|
||||
query: data.test
|
||||
error: "`contains` expects string argument."
|
||||
|
||||
- note: invalid-set-string
|
||||
data: {}
|
||||
modules: ["package test\nx=contains(set(), ``)"]
|
||||
query: data.test
|
||||
error: "`contains` expects string argument."
|
||||
|
||||
- note: invalid-object-string
|
||||
data: {}
|
||||
modules: ["package test\nx=contains({}, ``)"]
|
||||
query: data.test
|
||||
error: "`contains` expects string argument."
|
||||
|
||||
|
||||
|
||||
|
||||
- note: invalid-null-substring
|
||||
data: {}
|
||||
modules: ["package test\nx=contains(``, null)"]
|
||||
query: data.test
|
||||
error: "`contains` expects string argument."
|
||||
|
||||
- note: invalid-bool-substring
|
||||
data: {}
|
||||
modules: ["package test\nx=contains(``, true)"]
|
||||
query: data.test
|
||||
error: "`contains` expects string argument."
|
||||
|
||||
- note: invalid-number-substring
|
||||
data: {}
|
||||
modules: ["package test\nx=contains(``, 1)"]
|
||||
query: data.test
|
||||
error: "`contains` expects string argument."
|
||||
|
||||
- note: invalid-array-substring
|
||||
data: {}
|
||||
modules: ["package test\nx=contains(``, [])"]
|
||||
query: data.test
|
||||
error: "`contains` expects string argument."
|
||||
|
||||
- note: invalid-set-substring
|
||||
data: {}
|
||||
modules: ["package test\nx=contains(``, set())"]
|
||||
query: data.test
|
||||
error: "`contains` expects string argument."
|
||||
|
||||
- note: invalid-object-substring
|
||||
data: {}
|
||||
modules: ["package test\nx=contains(``, {})"]
|
||||
query: data.test
|
||||
error: "`contains` expects string argument."
|
||||
Reference in New Issue
Block a user