mirror of
https://github.com/sagiegurari/duckscript
synced 2024-10-14 11:53:05 +00:00
New greater_than and less_than commands
This commit is contained in:
parent
80d09ca94c
commit
2a3bfb2f99
|
@ -2,7 +2,8 @@
|
|||
|
||||
### v0.1.8
|
||||
|
||||
* wget (http_client) command #20
|
||||
* New greater_than and less_than commands.
|
||||
* New wget (http_client) command #20
|
||||
* Reduce binary executable size.
|
||||
* Fix CLI help documentation.
|
||||
|
||||
|
|
54
docs/sdk.md
54
docs/sdk.md
|
@ -46,6 +46,8 @@
|
|||
* [std::fs::Read (readfile)](#std__fs__Read)
|
||||
* [std::fs::Write (writefile)](#std__fs__Write)
|
||||
* [std::math::Calc (calc)](#std__math__Calc)
|
||||
* [std::math::GreaterThan (greater_than)](#std__math__GreaterThan)
|
||||
* [std::math::LessThan (less_than)](#std__math__LessThan)
|
||||
* [std::net::Hostname (hostname)](#std__net__Hostname)
|
||||
* [std::net::HttpClient (http_client, wget)](#std__net__HttpClient)
|
||||
* [std::process::Execute (exec)](#std__process__Execute)
|
||||
|
@ -1656,6 +1658,58 @@ result = calc 1 + 5 * 7
|
|||
#### Aliases:
|
||||
calc
|
||||
|
||||
<a name="std__math__GreaterThan"></a>
|
||||
## std::math::GreaterThan
|
||||
```sh
|
||||
var = greater_than left right
|
||||
```
|
||||
|
||||
This command returns true/false based on left > right calculation.
|
||||
|
||||
#### Parameters
|
||||
|
||||
Two numeric values to compare.
|
||||
|
||||
#### Return Value
|
||||
|
||||
True if first argument is bigger than second argument.
|
||||
|
||||
#### Examples
|
||||
|
||||
```sh
|
||||
result = greater_than 2 1.5
|
||||
```
|
||||
|
||||
|
||||
#### Aliases:
|
||||
greater_than
|
||||
|
||||
<a name="std__math__LessThan"></a>
|
||||
## std::math::LessThan
|
||||
```sh
|
||||
var = less_than left right
|
||||
```
|
||||
|
||||
This command returns true/false based on left < right calculation.
|
||||
|
||||
#### Parameters
|
||||
|
||||
Two numeric values to compare.
|
||||
|
||||
#### Return Value
|
||||
|
||||
True if first argument is smaller than second argument.
|
||||
|
||||
#### Examples
|
||||
|
||||
```sh
|
||||
result = less_than 1 1.5
|
||||
```
|
||||
|
||||
|
||||
#### Aliases:
|
||||
less_than
|
||||
|
||||
<a name="std__net__Hostname"></a>
|
||||
## std::net::Hostname
|
||||
```sh
|
||||
|
|
19
duckscript_sdk/src/sdk/std/math/greater_than/help.md
Normal file
19
duckscript_sdk/src/sdk/std/math/greater_than/help.md
Normal file
|
@ -0,0 +1,19 @@
|
|||
```sh
|
||||
var = greater_than left right
|
||||
```
|
||||
|
||||
This command returns true/false based on left > right calculation.
|
||||
|
||||
#### Parameters
|
||||
|
||||
Two numeric values to compare.
|
||||
|
||||
#### Return Value
|
||||
|
||||
True if first argument is bigger than second argument.
|
||||
|
||||
#### Examples
|
||||
|
||||
```sh
|
||||
result = greater_than 2 1.5
|
||||
```
|
57
duckscript_sdk/src/sdk/std/math/greater_than/mod.rs
Executable file
57
duckscript_sdk/src/sdk/std/math/greater_than/mod.rs
Executable file
|
@ -0,0 +1,57 @@
|
|||
use crate::utils::pckg;
|
||||
use duckscript::types::command::{Command, CommandResult};
|
||||
|
||||
#[cfg(test)]
|
||||
#[path = "./mod_test.rs"]
|
||||
mod mod_test;
|
||||
|
||||
struct CommandImpl {
|
||||
package: String,
|
||||
}
|
||||
|
||||
impl Command for CommandImpl {
|
||||
fn name(&self) -> String {
|
||||
pckg::concat(&self.package, "GreaterThan")
|
||||
}
|
||||
|
||||
fn aliases(&self) -> Vec<String> {
|
||||
vec!["greater_than".to_string()]
|
||||
}
|
||||
|
||||
fn help(&self) -> String {
|
||||
include_str!("help.md").to_string()
|
||||
}
|
||||
|
||||
fn run(&self, arguments: Vec<String>) -> CommandResult {
|
||||
if arguments.len() != 2 {
|
||||
CommandResult::Error("Invalid/Missing input.".to_string())
|
||||
} else {
|
||||
let left: f64 = match arguments[0].parse() {
|
||||
Ok(value) => value,
|
||||
Err(_) => {
|
||||
return CommandResult::Error(
|
||||
format!("Non numeric value: {} provided.", &arguments[0]).to_string(),
|
||||
);
|
||||
}
|
||||
};
|
||||
let right: f64 = match arguments[1].parse() {
|
||||
Ok(value) => value,
|
||||
Err(_) => {
|
||||
return CommandResult::Error(
|
||||
format!("Non numeric value: {} provided.", &arguments[1]).to_string(),
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
let result = if left > right { true } else { false };
|
||||
|
||||
CommandResult::Continue(Some(result.to_string()))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn create(package: &str) -> Box<dyn Command> {
|
||||
Box::new(CommandImpl {
|
||||
package: package.to_string(),
|
||||
})
|
||||
}
|
45
duckscript_sdk/src/sdk/std/math/greater_than/mod_test.rs
Normal file
45
duckscript_sdk/src/sdk/std/math/greater_than/mod_test.rs
Normal file
|
@ -0,0 +1,45 @@
|
|||
use super::*;
|
||||
use crate::test;
|
||||
use crate::test::CommandValidation;
|
||||
|
||||
#[test]
|
||||
fn common_functions() {
|
||||
test::test_common_command_functions(create(""));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn run_no_args() {
|
||||
test::run_script_and_error(vec![create("")], "out = greater_than", "out");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn run_single_arg() {
|
||||
test::run_script_and_error(vec![create("")], "out = greater_than 1", "out");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn run_equal() {
|
||||
test::run_script_and_validate(
|
||||
vec![create("")],
|
||||
"out = greater_than 1.5 1.5",
|
||||
CommandValidation::Match("out".to_string(), "false".to_string()),
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn run_less() {
|
||||
test::run_script_and_validate(
|
||||
vec![create("")],
|
||||
"out = greater_than 1.5 2",
|
||||
CommandValidation::Match("out".to_string(), "false".to_string()),
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn run_greater() {
|
||||
test::run_script_and_validate(
|
||||
vec![create("")],
|
||||
"out = greater_than 1.5 1",
|
||||
CommandValidation::Match("out".to_string(), "true".to_string()),
|
||||
);
|
||||
}
|
19
duckscript_sdk/src/sdk/std/math/less_than/help.md
Normal file
19
duckscript_sdk/src/sdk/std/math/less_than/help.md
Normal file
|
@ -0,0 +1,19 @@
|
|||
```sh
|
||||
var = less_than left right
|
||||
```
|
||||
|
||||
This command returns true/false based on left < right calculation.
|
||||
|
||||
#### Parameters
|
||||
|
||||
Two numeric values to compare.
|
||||
|
||||
#### Return Value
|
||||
|
||||
True if first argument is smaller than second argument.
|
||||
|
||||
#### Examples
|
||||
|
||||
```sh
|
||||
result = less_than 1 1.5
|
||||
```
|
57
duckscript_sdk/src/sdk/std/math/less_than/mod.rs
Executable file
57
duckscript_sdk/src/sdk/std/math/less_than/mod.rs
Executable file
|
@ -0,0 +1,57 @@
|
|||
use crate::utils::pckg;
|
||||
use duckscript::types::command::{Command, CommandResult};
|
||||
|
||||
#[cfg(test)]
|
||||
#[path = "./mod_test.rs"]
|
||||
mod mod_test;
|
||||
|
||||
struct CommandImpl {
|
||||
package: String,
|
||||
}
|
||||
|
||||
impl Command for CommandImpl {
|
||||
fn name(&self) -> String {
|
||||
pckg::concat(&self.package, "LessThan")
|
||||
}
|
||||
|
||||
fn aliases(&self) -> Vec<String> {
|
||||
vec!["less_than".to_string()]
|
||||
}
|
||||
|
||||
fn help(&self) -> String {
|
||||
include_str!("help.md").to_string()
|
||||
}
|
||||
|
||||
fn run(&self, arguments: Vec<String>) -> CommandResult {
|
||||
if arguments.len() != 2 {
|
||||
CommandResult::Error("Invalid/Missing input.".to_string())
|
||||
} else {
|
||||
let left: f64 = match arguments[0].parse() {
|
||||
Ok(value) => value,
|
||||
Err(_) => {
|
||||
return CommandResult::Error(
|
||||
format!("Non numeric value: {} provided.", &arguments[0]).to_string(),
|
||||
);
|
||||
}
|
||||
};
|
||||
let right: f64 = match arguments[1].parse() {
|
||||
Ok(value) => value,
|
||||
Err(_) => {
|
||||
return CommandResult::Error(
|
||||
format!("Non numeric value: {} provided.", &arguments[1]).to_string(),
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
let result = if left < right { true } else { false };
|
||||
|
||||
CommandResult::Continue(Some(result.to_string()))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn create(package: &str) -> Box<dyn Command> {
|
||||
Box::new(CommandImpl {
|
||||
package: package.to_string(),
|
||||
})
|
||||
}
|
45
duckscript_sdk/src/sdk/std/math/less_than/mod_test.rs
Normal file
45
duckscript_sdk/src/sdk/std/math/less_than/mod_test.rs
Normal file
|
@ -0,0 +1,45 @@
|
|||
use super::*;
|
||||
use crate::test;
|
||||
use crate::test::CommandValidation;
|
||||
|
||||
#[test]
|
||||
fn common_functions() {
|
||||
test::test_common_command_functions(create(""));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn run_no_args() {
|
||||
test::run_script_and_error(vec![create("")], "out = less_than", "out");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn run_single_arg() {
|
||||
test::run_script_and_error(vec![create("")], "out = less_than 1", "out");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn run_equal() {
|
||||
test::run_script_and_validate(
|
||||
vec![create("")],
|
||||
"out = less_than 1.5 1.5",
|
||||
CommandValidation::Match("out".to_string(), "false".to_string()),
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn run_less() {
|
||||
test::run_script_and_validate(
|
||||
vec![create("")],
|
||||
"out = less_than 1.5 2",
|
||||
CommandValidation::Match("out".to_string(), "true".to_string()),
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn run_greater() {
|
||||
test::run_script_and_validate(
|
||||
vec![create("")],
|
||||
"out = less_than 1.5 1",
|
||||
CommandValidation::Match("out".to_string(), "false".to_string()),
|
||||
);
|
||||
}
|
|
@ -1,4 +1,6 @@
|
|||
mod calc;
|
||||
mod greater_than;
|
||||
mod less_than;
|
||||
|
||||
use crate::utils::pckg;
|
||||
use duckscript::types::command::Commands;
|
||||
|
@ -10,6 +12,8 @@ pub(crate) fn load(commands: &mut Commands, parent: &str) -> Result<(), ScriptEr
|
|||
let package = pckg::concat(parent, PACKAGE);
|
||||
|
||||
commands.set(calc::create(&package))?;
|
||||
commands.set(greater_than::create(&package))?;
|
||||
commands.set(less_than::create(&package))?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
|
18
test/std/math/greater_than_test.ds
Normal file
18
test/std/math/greater_than_test.ds
Normal file
|
@ -0,0 +1,18 @@
|
|||
|
||||
function test_equals
|
||||
result = greater_than 1 1
|
||||
|
||||
assert_false ${result}
|
||||
end
|
||||
|
||||
function test_less_than
|
||||
result = greater_than 1 1.5
|
||||
|
||||
assert_false ${result}
|
||||
end
|
||||
|
||||
function test_greater_than
|
||||
result = greater_than 2 1.5
|
||||
|
||||
assert ${result}
|
||||
end
|
18
test/std/math/less_than_test.ds
Normal file
18
test/std/math/less_than_test.ds
Normal file
|
@ -0,0 +1,18 @@
|
|||
|
||||
function test_equals
|
||||
result = less_than 1 1
|
||||
|
||||
assert_false ${result}
|
||||
end
|
||||
|
||||
function test_less_than
|
||||
result = less_than 1 1.5
|
||||
|
||||
assert ${result}
|
||||
end
|
||||
|
||||
function test_greater_than
|
||||
result = less_than 2 1.5
|
||||
|
||||
assert_false ${result}
|
||||
end
|
Loading…
Reference in a new issue