mirror of
https://github.com/sagiegurari/duckscript
synced 2024-10-04 15:09:08 +00:00
The set command now supports 'or' condition
This commit is contained in:
parent
674093f8b8
commit
3ee2413ab8
|
@ -2,6 +2,7 @@
|
|||
|
||||
### v0.1.9
|
||||
|
||||
* The set command now supports 'or' condition
|
||||
* New base64 command #79
|
||||
* New write_binary_file command #78
|
||||
* New read_binary_file command #78
|
||||
|
|
22
docs/sdk.md
22
docs/sdk.md
|
@ -574,19 +574,27 @@ release
|
|||
<a name="std__Set"></a>
|
||||
## std::Set
|
||||
```sh
|
||||
var = set arg
|
||||
var = set arg [or arg]*
|
||||
```
|
||||
|
||||
The set command will simply return the provided argument and set it to the output variable.
|
||||
The set command will simply return the provided argument and set it to the output variable.<br>
|
||||
In case the argument is falsy it will attempt to provide another value if an 'or' keyword is set.
|
||||
|
||||
A value is considered falsy if it is one of the following:
|
||||
|
||||
* false (case insensitive)
|
||||
* 0
|
||||
* no (case insensitive)
|
||||
* Empty value
|
||||
|
||||
#### Parameters
|
||||
|
||||
Only the first argument will be returned.
|
||||
The argument to set or an 'or' conditional arguments.
|
||||
|
||||
|
||||
#### Return Value
|
||||
|
||||
The first command argument.
|
||||
The first truthy value
|
||||
|
||||
#### Examples
|
||||
|
||||
|
@ -596,6 +604,12 @@ var = set hello
|
|||
|
||||
# Return expanded value: 'home: ....'
|
||||
var = set "home: ${HOME}"
|
||||
|
||||
value = set test or false
|
||||
assert_eq ${value} test
|
||||
|
||||
value = set 0 or no or false or NO or FALSE
|
||||
assert_eq ${value} FALSE
|
||||
```
|
||||
|
||||
|
||||
|
|
|
@ -1,17 +1,25 @@
|
|||
```sh
|
||||
var = set arg
|
||||
var = set arg [or arg]*
|
||||
```
|
||||
|
||||
The set command will simply return the provided argument and set it to the output variable.
|
||||
The set command will simply return the provided argument and set it to the output variable.<br>
|
||||
In case the argument is falsy it will attempt to provide another value if an 'or' keyword is set.
|
||||
|
||||
A value is considered falsy if it is one of the following:
|
||||
|
||||
* false (case insensitive)
|
||||
* 0
|
||||
* no (case insensitive)
|
||||
* Empty value
|
||||
|
||||
#### Parameters
|
||||
|
||||
Only the first argument will be returned.
|
||||
The argument to set or an 'or' conditional arguments.
|
||||
|
||||
|
||||
#### Return Value
|
||||
|
||||
The first command argument.
|
||||
The first truthy value
|
||||
|
||||
#### Examples
|
||||
|
||||
|
@ -21,4 +29,10 @@ var = set hello
|
|||
|
||||
# Return expanded value: 'home: ....'
|
||||
var = set "home: ${HOME}"
|
||||
|
||||
value = set test or false
|
||||
assert_eq ${value} test
|
||||
|
||||
value = set 0 or no or false or NO or FALSE
|
||||
assert_eq ${value} FALSE
|
||||
```
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
use crate::utils::pckg;
|
||||
use crate::utils::{condition, pckg};
|
||||
use duckscript::types::command::{Command, CommandResult};
|
||||
|
||||
#[cfg(test)]
|
||||
|
@ -10,6 +10,32 @@ pub(crate) struct CommandImpl {
|
|||
package: String,
|
||||
}
|
||||
|
||||
fn get_output(arguments: &Vec<String>) -> Result<Option<String>, String> {
|
||||
let mut looking_for_value = true;
|
||||
let mut last_value = None;
|
||||
for argument in arguments {
|
||||
if looking_for_value {
|
||||
last_value = Some(argument.clone());
|
||||
|
||||
if condition::is_true(Some(argument.clone())) {
|
||||
return Ok(last_value);
|
||||
}
|
||||
|
||||
looking_for_value = false;
|
||||
} else if argument == "or" {
|
||||
looking_for_value = true;
|
||||
} else {
|
||||
return Err(format!("Keyword 'or' expected, found: {}", &argument).to_string());
|
||||
}
|
||||
}
|
||||
|
||||
if looking_for_value {
|
||||
Err("Keyword 'or' found, expected a value afterwards.".to_string())
|
||||
} else {
|
||||
Ok(last_value)
|
||||
}
|
||||
}
|
||||
|
||||
impl Command for CommandImpl {
|
||||
fn name(&self) -> String {
|
||||
pckg::concat(&self.package, "Set")
|
||||
|
@ -30,8 +56,13 @@ impl Command for CommandImpl {
|
|||
fn run(&self, arguments: Vec<String>) -> CommandResult {
|
||||
let output = if arguments.is_empty() {
|
||||
None
|
||||
} else {
|
||||
} else if arguments.len() == 1 {
|
||||
Some(arguments[0].clone())
|
||||
} else {
|
||||
match get_output(&arguments) {
|
||||
Ok(output) => output,
|
||||
Err(error) => return CommandResult::Error(error),
|
||||
}
|
||||
};
|
||||
|
||||
CommandResult::Continue(output)
|
||||
|
|
|
@ -29,3 +29,31 @@ fn run_multiple_arguments() {
|
|||
CommandValidation::Match("out".to_string(), "test1".to_string()),
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn run_multiple_arguments_first_falsy() {
|
||||
test::run_script_and_validate(
|
||||
vec![create("")],
|
||||
"out = set false or 0 or no or test",
|
||||
CommandValidation::Match("out".to_string(), "test".to_string()),
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn run_multiple_arguments_all_falsy() {
|
||||
test::run_script_and_validate(
|
||||
vec![create("")],
|
||||
"out = set false or 0 or no",
|
||||
CommandValidation::Match("out".to_string(), "no".to_string()),
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn run_end_with_or() {
|
||||
test::run_script_and_error(vec![create("")], "out = set false or 0 or no or", "out");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn run_end_missing_or() {
|
||||
test::run_script_and_error(vec![create("")], "out = set false 0", "out");
|
||||
}
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
|
||||
function test_concat_no_input
|
||||
fn test_concat_no_input
|
||||
arr = array_concat
|
||||
|
||||
is_arr = is_array ${arr}
|
||||
|
@ -8,7 +8,7 @@ function test_concat_no_input
|
|||
assert ${is_arr}
|
||||
end
|
||||
|
||||
function test_concat_single_array
|
||||
fn test_concat_single_array
|
||||
input1 = range 1 4
|
||||
|
||||
arr = array_concat ${input1}
|
||||
|
@ -34,7 +34,7 @@ function test_concat_single_array
|
|||
assert ${is_arr}
|
||||
end
|
||||
|
||||
function test_concat_multiple_arrays
|
||||
fn test_concat_multiple_arrays
|
||||
input1 = range 1 4
|
||||
input2 = range 4 6
|
||||
input3 = range 6 8
|
||||
|
@ -76,7 +76,7 @@ function test_concat_multiple_arrays
|
|||
assert ${is_arr}
|
||||
end
|
||||
|
||||
function test_concat_not_an_array
|
||||
fn test_concat_not_an_array
|
||||
error = get_last_error
|
||||
empty = is_empty ${error}
|
||||
assert ${empty}
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
|
||||
function test_to_string
|
||||
fn test_to_string
|
||||
arr = array hello world
|
||||
string = array_join ${arr} ", "
|
||||
|
||||
|
@ -8,7 +8,7 @@ function test_to_string
|
|||
assert_eq ${string} "hello, world"
|
||||
end
|
||||
|
||||
function test_numbers
|
||||
fn test_numbers
|
||||
arr = range 1 5
|
||||
string = array_join ${arr} ", "
|
||||
|
||||
|
@ -17,7 +17,7 @@ function test_numbers
|
|||
assert_eq ${string} "1, 2, 3, 4"
|
||||
end
|
||||
|
||||
function test_empty_array
|
||||
fn test_empty_array
|
||||
arr = array
|
||||
string = array_join ${arr} ", "
|
||||
|
||||
|
@ -26,7 +26,7 @@ function test_empty_array
|
|||
assert_eq ${string} ""
|
||||
end
|
||||
|
||||
function test_empty_separator
|
||||
fn test_empty_separator
|
||||
arr = range 1 5
|
||||
string = array_join ${arr} ""
|
||||
|
||||
|
@ -35,7 +35,7 @@ function test_empty_separator
|
|||
assert_eq ${string} "1234"
|
||||
end
|
||||
|
||||
function test_missing_separator
|
||||
fn test_missing_separator
|
||||
arr = range 1 5
|
||||
string = array_join ${arr}
|
||||
|
||||
|
@ -48,7 +48,7 @@ function test_missing_separator
|
|||
assert_eq ${string} false
|
||||
end
|
||||
|
||||
function test_not_an_array
|
||||
fn test_not_an_array
|
||||
arr = set 1
|
||||
string = array_join ${arr} ", "
|
||||
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
|
||||
function test_array_with_data
|
||||
fn test_array_with_data
|
||||
arr = array 1 2 3
|
||||
|
||||
last_element = array_pop ${arr}
|
||||
|
@ -19,7 +19,7 @@ function test_array_with_data
|
|||
assert ${released}
|
||||
end
|
||||
|
||||
function test_array_no_data
|
||||
fn test_array_no_data
|
||||
arr = array
|
||||
|
||||
last_element = array_pop ${arr}
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
|
||||
function test_array_with_data
|
||||
fn test_array_with_data
|
||||
arr = array a
|
||||
counter = range 1 4
|
||||
for index in ${counter}
|
||||
|
@ -26,7 +26,7 @@ function test_array_with_data
|
|||
assert ${released}
|
||||
end
|
||||
|
||||
function test_array_empty
|
||||
fn test_array_empty
|
||||
arr = array
|
||||
counter = range 1 4
|
||||
for index in ${counter}
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
|
||||
function test_array_with_data
|
||||
fn test_array_with_data
|
||||
arr = array 1 2 3
|
||||
|
||||
len = array_length ${arr}
|
||||
|
@ -12,7 +12,7 @@ function test_array_with_data
|
|||
assert ${released}
|
||||
end
|
||||
|
||||
function test_array_no_data
|
||||
fn test_array_no_data
|
||||
arr = array
|
||||
|
||||
len = array_length ${arr}
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
|
||||
function test_array_found
|
||||
fn test_array_found
|
||||
arr = array 1 2 3
|
||||
|
||||
value = is_array ${arr}
|
||||
|
@ -9,14 +9,14 @@ function test_array_found
|
|||
assert ${released}
|
||||
end
|
||||
|
||||
function test_not_array
|
||||
fn test_not_array
|
||||
arr = set true
|
||||
|
||||
value = is_array ${arr}
|
||||
assert_false ${value}
|
||||
end
|
||||
|
||||
function test_not_found
|
||||
fn test_not_found
|
||||
value = is_array ${arr}
|
||||
assert_false ${value}
|
||||
end
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
|
||||
function test_range_with_data
|
||||
fn test_range_with_data
|
||||
arr = range 0 10
|
||||
|
||||
len = array_length ${arr}
|
||||
|
@ -12,7 +12,7 @@ function test_range_with_data
|
|||
assert ${released}
|
||||
end
|
||||
|
||||
function test_range_no_data
|
||||
fn test_range_no_data
|
||||
arr = range 0 0
|
||||
|
||||
len = array_length ${arr}
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
|
||||
function test_read_properties
|
||||
fn test_read_properties
|
||||
count = read_properties a=1\nb=2\na.b.c=3
|
||||
assert_eq ${count} 3
|
||||
|
||||
|
@ -8,7 +8,7 @@ function test_read_properties
|
|||
assert_eq ${a.b.c} 3
|
||||
end
|
||||
|
||||
function test_read_properties_with_prefix
|
||||
fn test_read_properties_with_prefix
|
||||
count = read_properties --prefix config a=1\nb=2\na.b.c=3
|
||||
assert_eq ${count} 3
|
||||
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
|
||||
function test_write_properties
|
||||
fn test_write_properties
|
||||
count = read_properties a=1\nb=2\na.b.c=3
|
||||
assert_eq ${count} 3
|
||||
|
||||
|
@ -21,7 +21,7 @@ function test_write_properties
|
|||
assert_eq ${a.b.c} 3
|
||||
end
|
||||
|
||||
function test_write_properties_with_prefix
|
||||
fn test_write_properties_with_prefix
|
||||
count = read_properties a=1\nb=2\na.b.c=3
|
||||
assert_eq ${count} 3
|
||||
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
|
||||
function test_duckscript_sdk_version
|
||||
fn test_duckscript_sdk_version
|
||||
version = duckscript_sdk_version
|
||||
empty = is_empty version
|
||||
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
|
||||
function test_duckscript_version
|
||||
fn test_duckscript_version
|
||||
version = duckscript_version
|
||||
empty = is_empty version
|
||||
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
|
||||
function test_dump
|
||||
fn test_dump
|
||||
one = set 1
|
||||
two = set 2
|
||||
values = array 1 2 yes true
|
||||
|
|
2
test/std/env/get_home_dir_test.ds
vendored
2
test/std/env/get_home_dir_test.ds
vendored
|
@ -1,5 +1,5 @@
|
|||
|
||||
function test_get
|
||||
fn test_get
|
||||
os = os_family
|
||||
|
||||
if not equals ${os} windows
|
||||
|
|
2
test/std/env/vars_test.ds
vendored
2
test/std/env/vars_test.ds
vendored
|
@ -1,5 +1,5 @@
|
|||
|
||||
function test_set_get_unset
|
||||
fn test_set_get_unset
|
||||
unset_env TEST_SET_GET_UNSET
|
||||
value = get_env TEST_SET_GET_UNSET
|
||||
assert_false ${value}
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
|
||||
function test_flow_control_empty_loop
|
||||
fn test_flow_control_empty_loop
|
||||
arguments = array
|
||||
|
||||
index = set 0
|
||||
|
@ -24,7 +24,7 @@ function test_flow_control_empty_loop
|
|||
release ${arguments}
|
||||
end
|
||||
|
||||
function test_flow_control_loop
|
||||
fn test_flow_control_loop
|
||||
arguments = array a b c
|
||||
|
||||
index = set 0
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
|
||||
function test_array
|
||||
fn test_array
|
||||
counter = set 0
|
||||
args = array a b c
|
||||
|
||||
|
@ -11,7 +11,7 @@ function test_array
|
|||
released = release ${args}
|
||||
end
|
||||
|
||||
function test_nested
|
||||
fn test_nested
|
||||
counter = set 0
|
||||
values = array 1 2 3
|
||||
|
||||
|
@ -25,7 +25,7 @@ function test_nested
|
|||
released = release ${args}
|
||||
end
|
||||
|
||||
function test_range
|
||||
fn test_range
|
||||
counter = set 0
|
||||
args = range 0 10
|
||||
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
os = os_family
|
||||
windows = equals os "windows"
|
||||
|
||||
function test_relative_path
|
||||
fn test_relative_path
|
||||
if ${windows}
|
||||
value = basename .\\test.txt
|
||||
else
|
||||
|
@ -12,7 +12,7 @@ function test_relative_path
|
|||
assert_eq ${value} test.txt
|
||||
end
|
||||
|
||||
function test_full_path
|
||||
fn test_full_path
|
||||
if ${windows}
|
||||
value = basename c:\\dir\\dir\\test.txt
|
||||
else
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
os = os_family
|
||||
windows = equals os "windows"
|
||||
|
||||
function test_relative_path
|
||||
fn test_relative_path
|
||||
if ${windows}
|
||||
value = dirname .\\test.txt
|
||||
else
|
||||
|
@ -12,7 +12,7 @@ function test_relative_path
|
|||
assert_eq ${value} .
|
||||
end
|
||||
|
||||
function test_full_path
|
||||
fn test_full_path
|
||||
if not ${windows}
|
||||
value = dirname /dir/test.txt
|
||||
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
|
||||
function test_goto_with_valid_label
|
||||
fn test_goto_with_valid_label
|
||||
goto :test_goto_with_valid_label
|
||||
|
||||
assert_fail
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
|
||||
function test_if_hardcoded_true
|
||||
fn test_if_hardcoded_true
|
||||
valid = set false
|
||||
|
||||
if true
|
||||
|
@ -9,13 +9,13 @@ function test_if_hardcoded_true
|
|||
assert ${valid}
|
||||
end
|
||||
|
||||
function test_if_hardcoded_false
|
||||
fn test_if_hardcoded_false
|
||||
if false
|
||||
assert_fail
|
||||
end
|
||||
end
|
||||
|
||||
function test_if_hardcoded_not_false
|
||||
fn test_if_hardcoded_not_false
|
||||
valid = set false
|
||||
|
||||
if not false
|
||||
|
@ -25,7 +25,7 @@ function test_if_hardcoded_not_false
|
|||
assert ${valid}
|
||||
end
|
||||
|
||||
function test_if_command_returns_true
|
||||
fn test_if_command_returns_true
|
||||
valid = set false
|
||||
|
||||
if set true
|
||||
|
@ -35,7 +35,7 @@ function test_if_command_returns_true
|
|||
assert ${valid}
|
||||
end
|
||||
|
||||
function test_simple_else
|
||||
fn test_simple_else
|
||||
valid = set false
|
||||
|
||||
if set false
|
||||
|
@ -47,7 +47,7 @@ function test_simple_else
|
|||
assert ${valid}
|
||||
end
|
||||
|
||||
function test_simple_elseif
|
||||
fn test_simple_elseif
|
||||
valid = set false
|
||||
|
||||
if set false
|
||||
|
@ -61,7 +61,7 @@ function test_simple_elseif
|
|||
assert ${valid}
|
||||
end
|
||||
|
||||
function test_nested_if
|
||||
fn test_nested_if
|
||||
valid = set false
|
||||
|
||||
if set false
|
||||
|
@ -77,7 +77,7 @@ function test_nested_if
|
|||
assert ${valid}
|
||||
end
|
||||
|
||||
function test_nested_if2
|
||||
fn test_nested_if2
|
||||
valid = set false
|
||||
|
||||
value = set false
|
||||
|
|
|
@ -1,17 +1,17 @@
|
|||
|
||||
function test_equals
|
||||
fn test_equals
|
||||
result = greater_than 1 1
|
||||
|
||||
assert_false ${result}
|
||||
end
|
||||
|
||||
function test_less_than
|
||||
fn test_less_than
|
||||
result = greater_than 1 1.5
|
||||
|
||||
assert_false ${result}
|
||||
end
|
||||
|
||||
function test_greater_than
|
||||
fn test_greater_than
|
||||
result = greater_than 2 1.5
|
||||
|
||||
assert ${result}
|
||||
|
|
|
@ -1,17 +1,17 @@
|
|||
|
||||
function test_equals
|
||||
fn test_equals
|
||||
result = less_than 1 1
|
||||
|
||||
assert_false ${result}
|
||||
end
|
||||
|
||||
function test_less_than
|
||||
fn test_less_than
|
||||
result = less_than 1 1.5
|
||||
|
||||
assert ${result}
|
||||
end
|
||||
|
||||
function test_greater_than
|
||||
fn test_greater_than
|
||||
result = less_than 2 1.5
|
||||
|
||||
assert_false ${result}
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
|
||||
function test_get
|
||||
fn test_get
|
||||
response = http_client https://www.rust-lang.org/
|
||||
|
||||
found = contains ${response} Rust
|
||||
|
@ -7,7 +7,7 @@ function test_get
|
|||
assert ${found}
|
||||
end
|
||||
|
||||
function test_get_to_file
|
||||
fn test_get_to_file
|
||||
file = set ./target/_duckscript_test/http_client/page.html
|
||||
rm ${file}
|
||||
|
||||
|
@ -20,7 +20,7 @@ function test_get_to_file
|
|||
assert ${response_size}
|
||||
end
|
||||
|
||||
function test_post
|
||||
fn test_post
|
||||
payload = set {\"login\":\"login\",\"password\":\"password\"}
|
||||
response = http_client --method POST --payload ${payload} https://reqbin.com/echo/post/json
|
||||
|
||||
|
@ -29,7 +29,7 @@ function test_post
|
|||
assert ${found}
|
||||
end
|
||||
|
||||
function test_invalid_url
|
||||
fn test_invalid_url
|
||||
error = get_last_error
|
||||
empty = is_empty ${error}
|
||||
assert ${empty}
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
|
||||
function test_get
|
||||
fn test_get
|
||||
response = wget https://www.rust-lang.org/
|
||||
|
||||
found = contains ${response} Rust
|
||||
|
@ -7,7 +7,7 @@ function test_get
|
|||
assert ${found}
|
||||
end
|
||||
|
||||
function test_get_to_file
|
||||
fn test_get_to_file
|
||||
file = set ./target/_duckscript_test/wget/page.html
|
||||
rm ${file}
|
||||
|
||||
|
@ -20,7 +20,7 @@ function test_get_to_file
|
|||
assert ${response_size}
|
||||
end
|
||||
|
||||
function test_post
|
||||
fn test_post
|
||||
payload = set {\"login\":\"login\",\"password\":\"password\"}
|
||||
response = wget --method=HTTP-POST --post-data=${payload} https://reqbin.com/echo/post/json
|
||||
|
||||
|
|
|
@ -1,11 +1,11 @@
|
|||
|
||||
function test_not_true
|
||||
fn test_not_true
|
||||
value = not true
|
||||
|
||||
assert_false ${value}
|
||||
end
|
||||
|
||||
function test_not_false
|
||||
fn test_not_false
|
||||
value = not false
|
||||
|
||||
assert ${value}
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
|
||||
function test_set_error
|
||||
fn test_set_error
|
||||
set_error "my error message"
|
||||
|
||||
error = get_last_error
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
|
||||
function test_trigger_error_with_message
|
||||
fn test_trigger_error_with_message
|
||||
trigger_error "my error message"
|
||||
|
||||
error = get_last_error
|
||||
|
@ -7,7 +7,7 @@ function test_trigger_error_with_message
|
|||
assert_eq ${error} "my error message"
|
||||
end
|
||||
|
||||
function test_trigger_error_no_message
|
||||
fn test_trigger_error_no_message
|
||||
trigger_error
|
||||
|
||||
error = get_last_error
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
|
||||
function test_echo
|
||||
fn test_echo
|
||||
output = exec echo hello world
|
||||
|
||||
stdout = trim ${output.stdout}
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
|
||||
function test_process_id
|
||||
fn test_process_id
|
||||
id = pid
|
||||
|
||||
empty = is_empty {id}
|
||||
|
|
|
@ -1,41 +1,41 @@
|
|||
|
||||
function test_no_retries
|
||||
fn test_no_retries
|
||||
count = watchdog --max-retries 0 -- echo test
|
||||
|
||||
assert_eq ${count} 1
|
||||
end
|
||||
|
||||
function test_with_retries
|
||||
fn test_with_retries
|
||||
count = watchdog --max-retries 3 -- echo test
|
||||
|
||||
assert_eq ${count} 4
|
||||
end
|
||||
|
||||
function test_with_retries_and_interval
|
||||
fn test_with_retries_and_interval
|
||||
count = watchdog --max-retries 3 --interval 10 -- echo test
|
||||
|
||||
assert_eq ${count} 4
|
||||
end
|
||||
|
||||
function test_bad_command
|
||||
fn test_bad_command
|
||||
count = watchdog --max-retries 3 --interval 10 -- badcommand
|
||||
|
||||
assert_eq ${count} false
|
||||
end
|
||||
|
||||
function test_negatived_max_retries
|
||||
fn test_negatived_max_retries
|
||||
count = watchdog --max-retries -3 --interval 10 -- echo test
|
||||
|
||||
assert_eq ${count} 1
|
||||
end
|
||||
|
||||
function test_invalid_interval
|
||||
fn test_invalid_interval
|
||||
count = watchdog --max-retries 3 --interval -10 -- echo test
|
||||
|
||||
assert_eq ${count} false
|
||||
end
|
||||
|
||||
function test_missing_command_separator
|
||||
fn test_missing_command_separator
|
||||
count = watchdog --max-retries 0 echo test
|
||||
|
||||
assert_eq ${count} false
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
|
||||
function test_clear
|
||||
fn test_clear
|
||||
testscope = set true
|
||||
testscope::1 = set 1
|
||||
testscope::2 = set 2
|
||||
|
|
65
test/std/set_test.ds
Normal file
65
test/std/set_test.ds
Normal file
|
@ -0,0 +1,65 @@
|
|||
|
||||
fn test_no_value
|
||||
value = set "hello world"
|
||||
assert_eq ${value} "hello world"
|
||||
|
||||
value = set
|
||||
defined = is_defined value
|
||||
assert_false ${defined}
|
||||
end
|
||||
|
||||
fn test_single_value
|
||||
value = set "hello world"
|
||||
|
||||
assert_eq ${value} "hello world"
|
||||
end
|
||||
|
||||
fn test_single_value_falsy
|
||||
value = set false
|
||||
|
||||
assert_eq ${value} false
|
||||
end
|
||||
|
||||
fn test_multiple_values_true
|
||||
value = set 1 2 3
|
||||
|
||||
assert_eq ${value} 1
|
||||
end
|
||||
|
||||
fn test_multiple_first_falsy
|
||||
value = set "" or 0 or no or false or NO or FALSE or test
|
||||
|
||||
assert_eq ${value} test
|
||||
end
|
||||
|
||||
fn test_multiple_all_falsy
|
||||
value = set "" or 0 or no or false or NO or FALSE
|
||||
|
||||
assert_eq ${value} FALSE
|
||||
end
|
||||
|
||||
fn test_multiple_last_falsy
|
||||
value = set test or false
|
||||
|
||||
assert_eq ${value} test
|
||||
end
|
||||
|
||||
fn test_multiple_missing_value_after_or
|
||||
value = set false or false or
|
||||
|
||||
error = get_last_error
|
||||
empty = is_empty ${error}
|
||||
assert_false ${empty}
|
||||
|
||||
assert_false ${value}
|
||||
end
|
||||
|
||||
fn test_multiple_missing_or
|
||||
value = set false false
|
||||
|
||||
error = get_last_error
|
||||
empty = is_empty ${error}
|
||||
assert_false ${empty}
|
||||
|
||||
assert_false ${value}
|
||||
end
|
|
@ -1,5 +1,5 @@
|
|||
|
||||
function test_encode_decode
|
||||
fn test_encode_decode
|
||||
handle = string_to_bytes "hello world"
|
||||
text = base64_encode ${handle}
|
||||
release ${handle}
|
||||
|
@ -13,7 +13,7 @@ function test_encode_decode
|
|||
assert_eq ${text} "hello world"
|
||||
end
|
||||
|
||||
function test_missing_input
|
||||
fn test_missing_input
|
||||
error = get_last_error
|
||||
empty = is_empty ${error}
|
||||
assert ${empty}
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
|
||||
function test_encode
|
||||
fn test_encode
|
||||
handle = string_to_bytes "hello world"
|
||||
text = base64_encode ${handle}
|
||||
|
||||
|
@ -8,7 +8,7 @@ function test_encode
|
|||
assert_eq ${text} aGVsbG8gd29ybGQ=
|
||||
end
|
||||
|
||||
function test_missing_input
|
||||
fn test_missing_input
|
||||
error = get_last_error
|
||||
empty = is_empty ${error}
|
||||
assert ${empty}
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
|
||||
function test_encode_no_flags
|
||||
fn test_encode_no_flags
|
||||
handle = string_to_bytes "hello world"
|
||||
text = base64 ${handle}
|
||||
|
||||
|
@ -8,7 +8,7 @@ function test_encode_no_flags
|
|||
assert_eq ${text} aGVsbG8gd29ybGQ=
|
||||
end
|
||||
|
||||
function test_encode_e_flag
|
||||
fn test_encode_e_flag
|
||||
handle = string_to_bytes "hello world"
|
||||
text = base64 -d -e ${handle}
|
||||
|
||||
|
@ -17,7 +17,7 @@ function test_encode_e_flag
|
|||
assert_eq ${text} aGVsbG8gd29ybGQ=
|
||||
end
|
||||
|
||||
function test_encode_encode_flag
|
||||
fn test_encode_encode_flag
|
||||
handle = string_to_bytes "hello world"
|
||||
text = base64 -d -encode ${handle}
|
||||
|
||||
|
@ -26,7 +26,7 @@ function test_encode_encode_flag
|
|||
assert_eq ${text} aGVsbG8gd29ybGQ=
|
||||
end
|
||||
|
||||
function test_decode_d_flag
|
||||
fn test_decode_d_flag
|
||||
handle = base64 -d aGVsbG8gd29ybGQ=
|
||||
text = bytes_to_string ${handle}
|
||||
|
||||
|
@ -35,7 +35,7 @@ function test_decode_d_flag
|
|||
assert_eq ${text} "hello world"
|
||||
end
|
||||
|
||||
function test_decode_decode_flag
|
||||
fn test_decode_decode_flag
|
||||
handle = base64 -decode aGVsbG8gd29ybGQ=
|
||||
text = bytes_to_string ${handle}
|
||||
|
||||
|
@ -44,7 +44,7 @@ function test_decode_decode_flag
|
|||
assert_eq ${text} "hello world"
|
||||
end
|
||||
|
||||
function test_encode_decode
|
||||
fn test_encode_decode
|
||||
handle = string_to_bytes "hello world"
|
||||
text = base64 ${handle}
|
||||
release ${handle}
|
||||
|
@ -56,7 +56,7 @@ function test_encode_decode
|
|||
assert_eq ${text} "hello world"
|
||||
end
|
||||
|
||||
function test_missing_input
|
||||
fn test_missing_input
|
||||
error = get_last_error
|
||||
empty = is_empty ${error}
|
||||
assert ${empty}
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
|
||||
function test_binary_string
|
||||
fn test_binary_string
|
||||
handle = string_to_bytes "hello world"
|
||||
text = bytes_to_string ${handle}
|
||||
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
|
||||
function test_no_args
|
||||
fn test_no_args
|
||||
output = concat
|
||||
empty = is_empty ${output}
|
||||
|
||||
|
@ -7,7 +7,7 @@ function test_no_args
|
|||
assert ${empty}
|
||||
end
|
||||
|
||||
function test_single_arg
|
||||
fn test_single_arg
|
||||
output = concat 1
|
||||
empty = is_empty ${output}
|
||||
|
||||
|
@ -15,7 +15,7 @@ function test_single_arg
|
|||
assert_false ${empty}
|
||||
end
|
||||
|
||||
function test_empty
|
||||
fn test_empty
|
||||
output = concat "" ""
|
||||
empty = is_empty ${output}
|
||||
|
||||
|
@ -23,7 +23,7 @@ function test_empty
|
|||
assert ${empty}
|
||||
end
|
||||
|
||||
function test_first_empty
|
||||
fn test_first_empty
|
||||
output = concat "" test
|
||||
empty = is_empty ${output}
|
||||
|
||||
|
@ -31,7 +31,7 @@ function test_first_empty
|
|||
assert_false ${empty}
|
||||
end
|
||||
|
||||
function test_second_empty
|
||||
fn test_second_empty
|
||||
output = concat test ""
|
||||
empty = is_empty ${output}
|
||||
|
||||
|
@ -39,7 +39,7 @@ function test_second_empty
|
|||
assert_false ${empty}
|
||||
end
|
||||
|
||||
function test_both_not_empty
|
||||
fn test_both_not_empty
|
||||
output = concat 1 2
|
||||
empty = is_empty ${output}
|
||||
|
||||
|
@ -47,7 +47,7 @@ function test_both_not_empty
|
|||
assert_false ${empty}
|
||||
end
|
||||
|
||||
function test_multiple
|
||||
fn test_multiple
|
||||
output = concat 1 2 3 4
|
||||
empty = is_empty ${output}
|
||||
|
||||
|
@ -55,7 +55,7 @@ function test_multiple
|
|||
assert_false ${empty}
|
||||
end
|
||||
|
||||
function test_with_spaces
|
||||
fn test_with_spaces
|
||||
output = concat 1 "2 3" 4
|
||||
empty = is_empty ${output}
|
||||
|
||||
|
|
|
@ -1,35 +1,35 @@
|
|||
|
||||
function test_prefix
|
||||
fn test_prefix
|
||||
output = contains "1 2 3 4 5" "1 2"
|
||||
|
||||
assert ${output}
|
||||
end
|
||||
|
||||
function test_suffix
|
||||
fn test_suffix
|
||||
output = contains "1 2 3 4 5" "4 5"
|
||||
|
||||
assert ${output}
|
||||
end
|
||||
|
||||
function test_middle
|
||||
fn test_middle
|
||||
output = contains "1 2 3 4 5" "2 3"
|
||||
|
||||
assert ${output}
|
||||
end
|
||||
|
||||
function test_all
|
||||
fn test_all
|
||||
output = contains "1 2 3 4 5" "1 2 3 4 5"
|
||||
|
||||
assert ${output}
|
||||
end
|
||||
|
||||
function test_empty
|
||||
fn test_empty
|
||||
output = contains "1 2 3 4 5" ""
|
||||
|
||||
assert ${output}
|
||||
end
|
||||
|
||||
function test_not_contained
|
||||
fn test_not_contained
|
||||
output = contains "1 2 3 4 5" "4 5 6"
|
||||
|
||||
assert_false ${output}
|
||||
|
|
|
@ -1,35 +1,35 @@
|
|||
|
||||
function test_prefix
|
||||
fn test_prefix
|
||||
output = ends_with "1 2 3 4 5" "1 2"
|
||||
|
||||
assert_false ${output}
|
||||
end
|
||||
|
||||
function test_suffix
|
||||
fn test_suffix
|
||||
output = ends_with "1 2 3 4 5" "4 5"
|
||||
|
||||
assert ${output}
|
||||
end
|
||||
|
||||
function test_middle
|
||||
fn test_middle
|
||||
output = ends_with "1 2 3 4 5" "2 3"
|
||||
|
||||
assert_false ${output}
|
||||
end
|
||||
|
||||
function test_all
|
||||
fn test_all
|
||||
output = ends_with "1 2 3 4 5" "1 2 3 4 5"
|
||||
|
||||
assert ${output}
|
||||
end
|
||||
|
||||
function test_empty
|
||||
fn test_empty
|
||||
output = ends_with "1 2 3 4 5" ""
|
||||
|
||||
assert ${output}
|
||||
end
|
||||
|
||||
function test_not_contained
|
||||
fn test_not_contained
|
||||
output = ends_with "1 2 3 4 5" "4 5 6"
|
||||
|
||||
assert_false ${output}
|
||||
|
|
|
@ -1,35 +1,35 @@
|
|||
|
||||
function test_prefix
|
||||
fn test_prefix
|
||||
output = equals "1 2 3 4 5" "1 2"
|
||||
|
||||
assert_false ${output}
|
||||
end
|
||||
|
||||
function test_suffix
|
||||
fn test_suffix
|
||||
output = equals "1 2 3 4 5" "4 5"
|
||||
|
||||
assert_false ${output}
|
||||
end
|
||||
|
||||
function test_middle
|
||||
fn test_middle
|
||||
output = equals "1 2 3 4 5" "2 3"
|
||||
|
||||
assert_false ${output}
|
||||
end
|
||||
|
||||
function test_all
|
||||
fn test_all
|
||||
output = equals "1 2 3 4 5" "1 2 3 4 5"
|
||||
|
||||
assert ${output}
|
||||
end
|
||||
|
||||
function test_empty
|
||||
fn test_empty
|
||||
output = equals "1 2 3 4 5" ""
|
||||
|
||||
assert_false ${output}
|
||||
end
|
||||
|
||||
function test_not_contained
|
||||
fn test_not_contained
|
||||
output = equals "1 2 3 4 5" "4 5 6"
|
||||
|
||||
assert_false ${output}
|
||||
|
|
|
@ -1,17 +1,17 @@
|
|||
|
||||
function test_not_found
|
||||
fn test_not_found
|
||||
output = indexof "1 2 3 4 5" "4 5 6"
|
||||
|
||||
assert_eq ${output} ""
|
||||
end
|
||||
|
||||
function test_prefix
|
||||
fn test_prefix
|
||||
output = indexof "1 2 3 4 5 1 2 3 4 5" "1 2"
|
||||
|
||||
assert_eq ${output} 0
|
||||
end
|
||||
|
||||
function test_suffix
|
||||
fn test_suffix
|
||||
output = indexof "1 2 3 4 5 1 2 3 4 5" "4 5"
|
||||
|
||||
assert_eq ${output} 6
|
||||
|
|
|
@ -1,17 +1,17 @@
|
|||
|
||||
function test_empty
|
||||
fn test_empty
|
||||
output = is_empty ""
|
||||
|
||||
assert ${output}
|
||||
end
|
||||
|
||||
function test_not_empty
|
||||
fn test_not_empty
|
||||
output = is_empty "test"
|
||||
|
||||
assert_false ${output}
|
||||
end
|
||||
|
||||
function test_undefined
|
||||
fn test_undefined
|
||||
output = is_empty ${test_undefined}
|
||||
|
||||
assert ${output}
|
||||
|
|
|
@ -1,17 +1,17 @@
|
|||
|
||||
function test_not_found
|
||||
fn test_not_found
|
||||
output = last_indexof "1 2 3 4 5" "4 5 6"
|
||||
|
||||
assert_eq ${output} ""
|
||||
end
|
||||
|
||||
function test_prefix
|
||||
fn test_prefix
|
||||
output = last_indexof "1 2 3 4 5 1 2 3 4 5" "1 2"
|
||||
|
||||
assert_eq ${output} 10
|
||||
end
|
||||
|
||||
function test_suffix
|
||||
fn test_suffix
|
||||
output = last_indexof "1 2 3 4 5 1 2 3 4 5" "4 5"
|
||||
|
||||
assert_eq ${output} 16
|
||||
|
|
|
@ -1,17 +1,17 @@
|
|||
|
||||
function test_empty
|
||||
fn test_empty
|
||||
output = length ""
|
||||
|
||||
assert_eq ${output} 0
|
||||
end
|
||||
|
||||
function test_not_empty
|
||||
fn test_not_empty
|
||||
output = length "test"
|
||||
|
||||
assert_eq ${output} 4
|
||||
end
|
||||
|
||||
function test_undefined
|
||||
fn test_undefined
|
||||
output = length ${test_undefined}
|
||||
|
||||
assert_eq ${output} 0
|
||||
|
|
|
@ -1,12 +1,12 @@
|
|||
|
||||
function test_found
|
||||
fn test_found
|
||||
text = set "my large text value with lots of text"
|
||||
updated = replace ${text} text stuff
|
||||
|
||||
assert_eq ${updated} "my large stuff value with lots of stuff"
|
||||
end
|
||||
|
||||
function test_not_found
|
||||
fn test_not_found
|
||||
text = set "my large text value with lots of text"
|
||||
updated = replace ${text} stuff other
|
||||
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
|
||||
function test_empty_string
|
||||
fn test_empty_string
|
||||
handle = split "" \n
|
||||
|
||||
len = array_length ${handle}
|
||||
|
@ -12,7 +12,7 @@ function test_empty_string
|
|||
assert_eq ${len} 1
|
||||
end
|
||||
|
||||
function test_not_found
|
||||
fn test_not_found
|
||||
handle = split 12345 6
|
||||
|
||||
len = array_length ${handle}
|
||||
|
@ -25,7 +25,7 @@ function test_not_found
|
|||
assert_eq ${len} 1
|
||||
end
|
||||
|
||||
function test_found
|
||||
fn test_found
|
||||
handle = split a23b23c23d23e 23
|
||||
|
||||
len = array_length ${handle}
|
||||
|
@ -46,7 +46,7 @@ function test_found
|
|||
assert_eq ${len} 5
|
||||
end
|
||||
|
||||
function test_found_at_start_and_end
|
||||
fn test_found_at_start_and_end
|
||||
handle = split 23a23b23c23d23e23 23
|
||||
|
||||
len = array_length ${handle}
|
||||
|
|
|
@ -1,35 +1,35 @@
|
|||
|
||||
function test_prefix
|
||||
fn test_prefix
|
||||
output = starts_with "1 2 3 4 5" "1 2"
|
||||
|
||||
assert ${output}
|
||||
end
|
||||
|
||||
function test_suffix
|
||||
fn test_suffix
|
||||
output = starts_with "1 2 3 4 5" "4 5"
|
||||
|
||||
assert_false ${output}
|
||||
end
|
||||
|
||||
function test_middle
|
||||
fn test_middle
|
||||
output = starts_with "1 2 3 4 5" "2 3"
|
||||
|
||||
assert_false ${output}
|
||||
end
|
||||
|
||||
function test_all
|
||||
fn test_all
|
||||
output = starts_with "1 2 3 4 5" "1 2 3 4 5"
|
||||
|
||||
assert ${output}
|
||||
end
|
||||
|
||||
function test_empty
|
||||
fn test_empty
|
||||
output = starts_with "1 2 3 4 5" ""
|
||||
|
||||
assert ${output}
|
||||
end
|
||||
|
||||
function test_not_contained
|
||||
fn test_not_contained
|
||||
output = starts_with "1 2 3 4 5" "4 5 6"
|
||||
|
||||
assert_false ${output}
|
||||
|
|
|
@ -1,47 +1,47 @@
|
|||
|
||||
function test_empty_with_range
|
||||
fn test_empty_with_range
|
||||
output = substring "" 0 0
|
||||
|
||||
assert_eq ${output} "false"
|
||||
end
|
||||
|
||||
function test_text_with_empty_range
|
||||
fn test_text_with_empty_range
|
||||
output = substring "test" 0 0
|
||||
|
||||
assert_eq ${output} ""
|
||||
end
|
||||
|
||||
function test_text_with_range
|
||||
fn test_text_with_range
|
||||
output = substring "test" 1 3
|
||||
|
||||
assert_eq ${output} "es"
|
||||
end
|
||||
|
||||
function test_text_with_start
|
||||
fn test_text_with_start
|
||||
output = substring "test" 1
|
||||
|
||||
assert_eq ${output} "est"
|
||||
end
|
||||
|
||||
function test_text_with_end
|
||||
fn test_text_with_end
|
||||
output = substring "test" -2
|
||||
|
||||
assert_eq ${output} "st"
|
||||
end
|
||||
|
||||
function test_text_with_range_and_start_too_big
|
||||
fn test_text_with_range_and_start_too_big
|
||||
output = substring "test" 6 8
|
||||
|
||||
assert_eq ${output} "false"
|
||||
end
|
||||
|
||||
function test_text_with_start_too_big
|
||||
fn test_text_with_start_too_big
|
||||
output = substring "test" 6
|
||||
|
||||
assert_eq ${output} "false"
|
||||
end
|
||||
|
||||
function test_text_with_range_and_end_too_big
|
||||
fn test_text_with_range_and_end_too_big
|
||||
output = substring "test" 0 8
|
||||
|
||||
assert_eq ${output} "false"
|
||||
|
|
|
@ -1,29 +1,29 @@
|
|||
|
||||
function test_empty
|
||||
fn test_empty
|
||||
output = trim_end ""
|
||||
|
||||
assert_eq ${output} ""
|
||||
end
|
||||
|
||||
function test_trimmed
|
||||
fn test_trimmed
|
||||
output = trim_end "test test"
|
||||
|
||||
assert_eq ${output} "test test"
|
||||
end
|
||||
|
||||
function test_start_untrimmed
|
||||
fn test_start_untrimmed
|
||||
output = trim_end " test test"
|
||||
|
||||
assert_eq ${output} " test test"
|
||||
end
|
||||
|
||||
function test_end_untrimmed
|
||||
fn test_end_untrimmed
|
||||
output = trim_end "test test "
|
||||
|
||||
assert_eq ${output} "test test"
|
||||
end
|
||||
|
||||
function test_both_untrimmed
|
||||
fn test_both_untrimmed
|
||||
output = trim_end " test test "
|
||||
|
||||
assert_eq ${output} " test test"
|
||||
|
|
|
@ -1,29 +1,29 @@
|
|||
|
||||
function test_empty
|
||||
fn test_empty
|
||||
output = trim_start ""
|
||||
|
||||
assert_eq ${output} ""
|
||||
end
|
||||
|
||||
function test_trimmed
|
||||
fn test_trimmed
|
||||
output = trim_start "test test"
|
||||
|
||||
assert_eq ${output} "test test"
|
||||
end
|
||||
|
||||
function test_start_untrimmed
|
||||
fn test_start_untrimmed
|
||||
output = trim_start " test test"
|
||||
|
||||
assert_eq ${output} "test test"
|
||||
end
|
||||
|
||||
function test_end_untrimmed
|
||||
fn test_end_untrimmed
|
||||
output = trim_start "test test "
|
||||
|
||||
assert_eq ${output} "test test "
|
||||
end
|
||||
|
||||
function test_both_untrimmed
|
||||
fn test_both_untrimmed
|
||||
output = trim_start " test test "
|
||||
|
||||
assert_eq ${output} "test test "
|
||||
|
|
|
@ -1,29 +1,29 @@
|
|||
|
||||
function test_empty
|
||||
fn test_empty
|
||||
output = trim ""
|
||||
|
||||
assert_eq ${output} ""
|
||||
end
|
||||
|
||||
function test_trimmed
|
||||
fn test_trimmed
|
||||
output = trim "test test"
|
||||
|
||||
assert_eq ${output} "test test"
|
||||
end
|
||||
|
||||
function test_start_untrimmed
|
||||
fn test_start_untrimmed
|
||||
output = trim " test test"
|
||||
|
||||
assert_eq ${output} "test test"
|
||||
end
|
||||
|
||||
function test_end_untrimmed
|
||||
fn test_end_untrimmed
|
||||
output = trim "test test "
|
||||
|
||||
assert_eq ${output} "test test"
|
||||
end
|
||||
|
||||
function test_both_untrimmed
|
||||
fn test_both_untrimmed
|
||||
output = trim " test test "
|
||||
|
||||
assert_eq ${output} "test test"
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
|
||||
function test_current_time
|
||||
fn test_current_time
|
||||
value = current_time
|
||||
|
||||
result = greater_than ${value} 0
|
||||
|
|
Loading…
Reference in a new issue