New map_contains_value command

This commit is contained in:
sagie gur ari 2020-05-04 05:42:53 +00:00
parent e64effc131
commit 1efee55f79
8 changed files with 133 additions and 18 deletions

View file

@ -3,6 +3,7 @@
### v0.3.4
* \[Breaking Change\] Runtime - REPL mode doesn't stop due to crashes from user commands #103
* New map_contains_value command.
* New map_contains_key command.
* New get_all_var_names command #100
* New get_by_name command.

View file

@ -1,19 +1,3 @@
scope::map_contains_key::found = set false
scope::map_contains_key::not_empty = not map_is_empty ${scope::map_contains_key::argument::1}
if ${scope::map_contains_key::not_empty}
scope::map_contains_key::key = set ${scope::map_contains_key::argument::2}
scope::map_contains_key::key_array_handle = map_keys ${scope::map_contains_key::argument::1}
for scope::map_contains_key::item in ${scope::map_contains_key::key_array_handle}
scope::map_contains_key::found = equals ${scope::map_contains_key::item} ${scope::map_contains_key::key}
if ${scope::map_contains_key::found}
release ${scope::map_contains_key::key_array_handle}
end
end
end
release ${scope::map_contains_key::key_array_handle}
set ${scope::map_contains_key::found}
scope::map_contains_key::value = map_get ${scope::map_contains_key::argument::1} ${scope::map_contains_key::argument::2}
is_defined scope::map_contains_key::value

View file

@ -0,0 +1,22 @@
```sh
var = map_contains_value handle value
```
Returns true if the provided value was found in the map.
#### Parameters
* The map handle.
* The value
#### Return Value
True if the value was found in the map.
#### Examples
```sh
handle = map
map_put ${handle} key value
found = map_contains_value ${handle} value
```

View file

@ -0,0 +1,22 @@
use crate::types::command::create_alias_command;
use crate::utils::pckg;
use duckscript::types::command::Command;
use duckscript::types::error::ScriptError;
#[cfg(test)]
#[path = "./mod_test.rs"]
mod mod_test;
pub(crate) fn create(package: &str) -> Result<Box<dyn Command>, ScriptError> {
let name = pckg::concat(package, "MapContainsValue");
let command = create_alias_command(
name,
vec!["map_contains_value".to_string()],
include_str!("help.md").to_string(),
"map_contains_value".to_string(),
include_str!("script.ds").to_string(),
2,
)?;
Ok(Box::new(command))
}

View file

@ -0,0 +1,7 @@
use super::*;
use crate::test;
#[test]
fn common_functions() {
test::test_common_command_functions(create("").unwrap());
}

View file

@ -0,0 +1,20 @@
scope::map_contains_value::found = set false
scope::map_contains_value::not_empty = not map_is_empty ${scope::map_contains_value::argument::1}
if ${scope::map_contains_value::not_empty}
scope::map_contains_value::value = set ${scope::map_contains_value::argument::2}
scope::map_contains_value::key_array_handle = map_keys ${scope::map_contains_value::argument::1}
for scope::map_contains_value::item in ${scope::map_contains_value::key_array_handle}
scope::map_contains_value::next_value = map_get ${scope::map_contains_value::argument::1} ${scope::map_contains_value::item}
scope::map_contains_value::found = equals ${scope::map_contains_value::next_value} ${scope::map_contains_value::value}
if ${scope::map_contains_value::found}
release ${scope::map_contains_value::key_array_handle}
end
end
end
release ${scope::map_contains_value::key_array_handle}
set ${scope::map_contains_value::found}

View file

@ -12,6 +12,7 @@ mod is_map;
mod map;
mod map_clear;
mod map_contains_key;
mod map_contains_value;
mod map_get;
mod map_is_empty;
mod map_keys;
@ -47,6 +48,7 @@ pub(crate) fn load(commands: &mut Commands, parent: &str) -> Result<(), ScriptEr
commands.set(map::create(&package))?;
commands.set(map_clear::create(&package))?;
commands.set(map_contains_key::create(&package)?)?;
commands.set(map_contains_value::create(&package)?)?;
commands.set(map_get::create(&package))?;
commands.set(map_is_empty::create(&package)?)?;
commands.set(map_keys::create(&package))?;

View file

@ -0,0 +1,57 @@
fn test_empty
handle = map
found = map_contains_value ${handle} key
release ${handle}
assert_false ${found}
end
fn test_not_found
handle = map
map_put ${handle} key1 value
found = map_contains_value ${handle} key2
release ${handle}
assert_false ${found}
end
fn test_not_found
handle = map
map_put ${handle} key1 value1
found = map_contains_value ${handle} value2
release ${handle}
assert_false ${found}
end
fn test_found
handle = map
map_put ${handle} key value
found = map_contains_value ${handle} value
release ${handle}
assert ${found}
end
fn test_both
handle = map
map_put ${handle} key1 value1
map_put ${handle} key2 value2
found = map_contains_value ${handle} value1
assert ${found}
found = map_contains_value ${handle} value2
assert ${found}
found = map_contains_value ${handle} value3
assert_false ${found}
release ${handle}
end