Skip to content

Commit

Permalink
Fix the includes function
Browse files Browse the repository at this point in the history
  • Loading branch information
akihiro17 committed Jun 3, 2024
1 parent 0a78048 commit 7c3ff6a
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 14 deletions.
8 changes: 6 additions & 2 deletions src/std/main.ab
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,10 @@ pub fun exit(code: Num): Null {
}

pub fun includes(arr, value) {
unsafe $[[ "\$\{{nameof arr}[@]}" =~ "{value}" ]]$
return status == 0
loop v in arr {
if v == value {
return true
}
}
return false
}
40 changes: 28 additions & 12 deletions src/tests/stdlib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -330,21 +330,37 @@ fn exit() {
assert_eq!(cmd.wait().expect("Couldn't wait for bash to execute").code(), Some(37));
}

#[test]
fn includes() {
let code = "
import * from \"std\"
main {
if includes([\"apple\", \"banana\", \"cherry\"], \"banana\") {
echo \"Found\"
} else {
echo \"Not Found\"
}
macro_rules! test_includes {
($name:ident, $array_declaration:expr, $element:expr, $expected:expr) => {
#[test]
fn $name() {
let array_declaration = $array_declaration.to_string();
let element = $element.to_string();
let code = format!(r#"
import * from "std"
main {{
let array = {array_declaration}
if includes(array, {element}) {{
echo "Found"
}} else {{
echo "Not Found"
}}
}}
"#);

test_amber!(code, $expected.to_string())
}
";
test_amber!(code, "Found")
}
}

test_includes!(includes_empty_text_array, r#"[Text]"#, "\"\"", "Not Found");
test_includes!(includes_text_array, r#"["apple", "banana", "cherry"]"#, "\"banana\"", "Found");
test_includes!(includes_exact_match, r#"["apple", "banana cherry"]"#, "\"banana cherry\"", "Found");
test_includes!(includes_prefix_match, r#"["apple", "banana cherry"]"#, "\"banana\"", "Not Found");
test_includes!(includes_partial_match_with_expanded_element, r#"["foo", "bar", "baz"]"#, "\"oo ba\"", "Not Found");
test_includes!(includes_empty_num_array, r#"[Num]"#, 0, "Not Found");

#[test]
fn dir_exist() {
let temp_dir = tempdir().expect("Failed to create temporary directory");
Expand Down

0 comments on commit 7c3ff6a

Please sign in to comment.