Skip to content

Commit

Permalink
Add tests for builtin strings::indexof method
Browse files Browse the repository at this point in the history
Signed-off-by: Sumedh Alok Sharma <[email protected]>
  • Loading branch information
Sumynwa committed Sep 10, 2024
1 parent 502b830 commit e795e86
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 6 deletions.
13 changes: 7 additions & 6 deletions src/builtins/strings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,13 @@ fn indexof(span: &Span, params: &[Ref<Expr>], args: &[Value], _strict: bool) ->
ensure_args_count(span, name, params, args, 2)?;
let s1 = ensure_string(name, &params[0], &args[0])?;
let s2 = ensure_string(name, &params[1], &args[1])?;
for (pos, (idx, _)) in s1.char_indices().enumerate() {
if s1[idx..].starts_with(s2.as_ref()) {
return Ok(Value::from(Number::from(pos)));
}
}
Ok(Value::from(Number::from(-1i64)))

let pos = match s1.find(s2.as_ref()) {
Some(n) => n,
_ => return Ok(Value::from(Number::from(-1i64))),
};

Ok(Value::from(Number::from(pos)))
}

fn indexof_n(span: &Span, params: &[Ref<Expr>], args: &[Value], _strict: bool) -> Result<Value> {
Expand Down
115 changes: 115 additions & 0 deletions tests/interpreter/cases/builtins/strings/indexof.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.

cases:
- note: base
data: {}
modules:
- |
package test
v1 = indexof("Hello world", "llo") # valid substring
v2 = indexof("Hello world", "hel") # case sensitive
v3 = indexof("Hello world", "l") # single character
v4 = indexof("", ",") # empty string
v5 = indexof("", "") # empty substring and string
query: data.test
want_result:
v1: 2
v2: -1
v3: 2
v4: -1
v5: 0

- note: undefined-string
data: {}
modules:
- |
package test
x { false }
y = indexof(x, "")
query: data.test
want_result: {}

- note: undefined-substring
data: {}
modules:
- |
package test
x { false }
y = indexof(",", x)
query: data.test
want_result: {}

- note: invalid-null-string
data: {}
modules: ["package test\nx=indexof(null, ``)"]
query: data.test
error: "`indexof` expects string argument."

- note: invalid-bool-string
data: {}
modules: ["package test\nx=indexof(true, ``)"]
query: data.test
error: "`indexof` expects string argument."

- note: invalid-number-string
data: {}
modules: ["package test\nx=indexof(1, ``)"]
query: data.test
error: "`indexof` expects string argument."

- note: invalid-array-string
data: {}
modules: ["package test\nx=indexof([], ``)"]
query: data.test
error: "`indexof` expects string argument."

- note: invalid-set-string
data: {}
modules: ["package test\nx=indexof(set(), ``)"]
query: data.test
error: "`indexof` expects string argument."

- note: invalid-object-string
data: {}
modules: ["package test\nx=indexof({}, ``)"]
query: data.test
error: "`indexof` expects string argument."

- note: invalid-null-substring
data: {}
modules: ["package test\nx=indexof(``, null)"]
query: data.test
error: "`indexof` expects string argument."

- note: invalid-bool-substring
data: {}
modules: ["package test\nx=indexof(``, true)"]
query: data.test
error: "`indexof` expects string argument."

- note: invalid-number-substring
data: {}
modules: ["package test\nx=indexof(``, 1)"]
query: data.test
error: "`indexof` expects string argument."

- note: invalid-array-substring
data: {}
modules: ["package test\nx=indexof(``, [])"]
query: data.test
error: "`indexof` expects string argument."

- note: invalid-set-substring
data: {}
modules: ["package test\nx=indexof(``, set())"]
query: data.test
error: "`indexof` expects string argument."

- note: invalid-object-substring
data: {}
modules: ["package test\nx=indexof(``, {})"]
query: data.test
error: "`indexof` expects string argument."

0 comments on commit e795e86

Please sign in to comment.