new assert_eq command #22

This commit is contained in:
sagie gur ari 2020-01-02 16:32:24 +00:00
parent 4f24f9a601
commit ee78556b71
5 changed files with 178 additions and 0 deletions

View File

@ -26,6 +26,7 @@
* [sdk::process::Execute](#sdk__process__Execute)
* [sdk::process::Exit](#sdk__process__Exit)
* [sdk::test::Assert](#sdk__test__Assert)
* [sdk::test::AssertEquals](#sdk__test__AssertEquals)
* [sdk::test::AssertFail](#sdk__test__AssertFail)
* [sdk::thread::Sleep](#sdk__thread__Sleep)
@ -1099,6 +1100,47 @@ assert false "This is my error message"
#### Aliases:
assert
<a name="sdk__test__AssertEquals"></a>
## sdk::test::AssertEquals
```sh
assert_eq value1 value2 [error message]
```
Used to validate the input is the same.<br>
If they are not, the command will exist with an error.
#### Parameters
* Two values to evaluate if they are equal
* Optional error message
#### Return Value
**true** if truthy.
#### Examples
Valid condition:
```sh
assert_eq yes yes
assert_eq false false
value = set "some text"
assert_eq ${value} "some text"
```
Error example:
```sh
assert_eq 1 2
assert_eq 1 2 "This is my error message"
```
#### Aliases:
assert_eq
<a name="sdk__test__AssertFail"></a>
## sdk::test::AssertFail
```sh

View File

@ -0,0 +1,34 @@
```sh
assert_eq value1 value2 [error message]
```
Used to validate the input is the same.<br>
If they are not, the command will exist with an error.
#### Parameters
* Two values to evaluate if they are equal
* Optional error message
#### Return Value
**true** if truthy.
#### Examples
Valid condition:
```sh
assert_eq yes yes
assert_eq false false
value = set "some text"
assert_eq ${value} "some text"
```
Error example:
```sh
assert_eq 1 2
assert_eq 1 2 "This is my error message"
```

View File

@ -0,0 +1,54 @@
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, "AssertEquals")
}
fn aliases(&self) -> Vec<String> {
vec!["assert_eq".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("Assert failed, two arguments are required.".to_string())
} else {
let passed = arguments[0] == arguments[1];
if passed {
CommandResult::Continue(Some("true".to_string()))
} else {
let error_message = if arguments.len() == 2 {
format!(
"Assert failed, value: {} does not match: {}",
&arguments[0], &arguments[1]
)
.to_string()
} else {
arguments[2].clone()
};
CommandResult::Error(error_message)
}
}
}
}
pub(crate) fn create(package: &str) -> Box<dyn Command> {
Box::new(CommandImpl {
package: package.to_string(),
})
}

View File

@ -0,0 +1,46 @@
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_fail(vec![create("")], "out = assert_eq");
}
#[test]
fn run_single_argument() {
test::run_script_and_fail(vec![create("")], "out = assert_eq true");
}
#[test]
fn run_two_arguments_equal() {
test::run_script_and_validate(
vec![create("")],
"out = assert_eq false false",
CommandValidation::Match("out".to_string(), "true".to_string()),
);
}
#[test]
fn run_two_arguments_equal_with_error_message() {
test::run_script_and_validate(
vec![create("")],
"out = assert_eq false false message",
CommandValidation::Match("out".to_string(), "true".to_string()),
);
}
#[test]
fn run_two_arguments_not_equal() {
test::run_script_and_fail(vec![create("")], "out = assert_eq 1 false");
}
#[test]
fn run_two_arguments_not_equal_error_message() {
test::run_script_and_fail(vec![create("")], r#"out = assert_eq 1 false "test error""#);
}

View File

@ -1,4 +1,5 @@
mod assert;
mod assert_eq;
mod assert_fail;
use crate::utils::pckg;
@ -11,6 +12,7 @@ pub(crate) fn load(commands: &mut Commands, parent: &str) -> Result<(), ScriptEr
let package = pckg::concat(parent, PACKAGE);
commands.set(assert::create(&package))?;
commands.set(assert_eq::create(&package))?;
commands.set(assert_fail::create(&package))?;
Ok(())