New greater_than and less_than commands

This commit is contained in:
sagie gur ari 2020-01-18 16:56:49 +00:00
parent 80d09ca94c
commit 2a3bfb2f99
11 changed files with 338 additions and 1 deletions

View file

@ -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.

View file

@ -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

View 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
```

View 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(),
})
}

View 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()),
);
}

View 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
```

View 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(),
})
}

View 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()),
);
}

View file

@ -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(())
}

View 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

View 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