New map_contains_key command

This commit is contained in:
sagie gur ari 2020-05-03 20:57:59 +00:00
parent 170e1e4a90
commit e64effc131
8 changed files with 187 additions and 0 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_key command.
* New get_all_var_names command #100
* New get_by_name command.
* New set_by_name command.

View file

@ -19,6 +19,7 @@
* [std::collections::IsMap (is_map)](#std__collections__IsMap)
* [std::collections::Map (map)](#std__collections__Map)
* [std::collections::MapClear (map_clear)](#std__collections__MapClear)
* [std::collections::MapContainsKey (map_contains_key)](#std__collections__MapContainsKey)
* [std::collections::MapGet (map_get)](#std__collections__MapGet)
* [std::collections::MapIsEmpty (map_is_empty)](#std__collections__MapIsEmpty)
* [std::collections::MapKeys (map_keys)](#std__collections__MapKeys)
@ -888,6 +889,62 @@ release ${handle}
#### Aliases:
map_clear
<a name="std__collections__MapContainsKey"></a>
## std::collections::MapContainsKey
```sh
var = map_contains_key handle key
```
Returns true if the provided key was found in the map.
#### Parameters
* The map handle.
* The key
#### Return Value
True if the key was found in the map.
#### Examples
```sh
handle = map
map_put ${handle} key value
found = map_contains_key ${handle} key
```
#### Source:
```sh
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}
```
#### Aliases:
map_contains_key
<a name="std__collections__MapGet"></a>
## std::collections::MapGet
```sh

View file

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

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, "MapContainsKey");
let command = create_alias_command(
name,
vec!["map_contains_key".to_string()],
include_str!("help.md").to_string(),
"map_contains_key".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,19 @@
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}

View file

@ -11,6 +11,7 @@ mod is_array;
mod is_map;
mod map;
mod map_clear;
mod map_contains_key;
mod map_get;
mod map_is_empty;
mod map_keys;
@ -45,6 +46,7 @@ pub(crate) fn load(commands: &mut Commands, parent: &str) -> Result<(), ScriptEr
commands.set(is_map::create(&package))?;
commands.set(map::create(&package))?;
commands.set(map_clear::create(&package))?;
commands.set(map_contains_key::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_key ${handle} key
release ${handle}
assert_false ${found}
end
fn test_not_found
handle = map
map_put ${handle} key1 value
found = map_contains_key ${handle} key2
release ${handle}
assert_false ${found}
end
fn test_not_found
handle = map
map_put ${handle} key1 value
found = map_contains_key ${handle} key2
release ${handle}
assert_false ${found}
end
fn test_found
handle = map
map_put ${handle} key value
found = map_contains_key ${handle} key
release ${handle}
assert ${found}
end
fn test_both
handle = map
map_put ${handle} key1 value
map_put ${handle} key2 value
found = map_contains_key ${handle} key1
assert ${found}
found = map_contains_key ${handle} key2
assert ${found}
found = map_contains_key ${handle} key3
assert_false ${found}
release ${handle}
end