mirror of
https://github.com/nushell/nushell
synced 2024-12-26 13:03:07 +00:00
Extract tests.
This commit is contained in:
parent
cb4b2e6116
commit
5372463b37
3 changed files with 75 additions and 61 deletions
|
@ -1,6 +1,6 @@
|
|||
use indexmap::IndexMap;
|
||||
use nu::{
|
||||
serve_plugin, CallInfo, NamedType, Plugin, PositionalType, Primitive, ReturnSuccess,
|
||||
serve_plugin, CallInfo, NamedType, Plugin, Primitive, ReturnSuccess,
|
||||
ReturnValue, ShellError, Signature, Tagged, Value,
|
||||
};
|
||||
|
||||
|
@ -82,16 +82,15 @@ impl Str {
|
|||
fn strutils(
|
||||
&self,
|
||||
value: Tagged<Value>,
|
||||
field: &Option<String>,
|
||||
) -> Result<Tagged<Value>, ShellError> {
|
||||
match value.item {
|
||||
Value::Primitive(Primitive::String(ref s)) => {
|
||||
Ok(Tagged::from_item(self.apply(&s), value.tag()))
|
||||
}
|
||||
Value::Object(_) => match field {
|
||||
Some(f) => {
|
||||
Value::Object(_) => match self.field {
|
||||
Some(ref f) => {
|
||||
let replacement = match value.item.get_data_by_path(value.tag(), f) {
|
||||
Some(result) => self.strutils(result.map(|x| x.clone()), &None)?,
|
||||
Some(result) => self.strutils(result.map(|x| x.clone()))?,
|
||||
None => {
|
||||
return Err(ShellError::string("str could not find field to replace"))
|
||||
}
|
||||
|
@ -129,7 +128,7 @@ impl Plugin for Str {
|
|||
|
||||
Ok(Signature {
|
||||
name: "str".to_string(),
|
||||
positional: vec![PositionalType::optional_any("Field")],
|
||||
positional: vec![],
|
||||
is_filter: true,
|
||||
named,
|
||||
rest_positional: true,
|
||||
|
@ -178,7 +177,7 @@ impl Plugin for Str {
|
|||
|
||||
fn filter(&mut self, input: Tagged<Value>) -> Result<Vec<ReturnValue>, ShellError> {
|
||||
Ok(vec![ReturnSuccess::value(
|
||||
self.strutils(input, &self.field)?,
|
||||
self.strutils(input)?,
|
||||
)])
|
||||
}
|
||||
}
|
||||
|
@ -224,11 +223,11 @@ mod tests {
|
|||
self
|
||||
}
|
||||
|
||||
fn create(&self, name_span: Span) -> CallInfo {
|
||||
fn create(&self) -> CallInfo {
|
||||
CallInfo {
|
||||
args: EvaluatedArgs::new(Some(self.positionals.clone()), Some(self.flags.clone())),
|
||||
source_map: SourceMap::new(),
|
||||
name_span,
|
||||
name_span: Span::unknown(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -258,7 +257,7 @@ mod tests {
|
|||
.begin_filter(
|
||||
CallStub::new()
|
||||
.with_long_flag("downcase")
|
||||
.create(Span::unknown())
|
||||
.create()
|
||||
)
|
||||
.is_ok());
|
||||
assert!(plugin.action.is_some());
|
||||
|
@ -272,7 +271,7 @@ mod tests {
|
|||
.begin_filter(
|
||||
CallStub::new()
|
||||
.with_long_flag("upcase")
|
||||
.create(Span::unknown())
|
||||
.create()
|
||||
)
|
||||
.is_ok());
|
||||
assert!(plugin.action.is_some());
|
||||
|
@ -286,7 +285,7 @@ mod tests {
|
|||
.begin_filter(
|
||||
CallStub::new()
|
||||
.with_long_flag("to-int")
|
||||
.create(Span::unknown())
|
||||
.create()
|
||||
)
|
||||
.is_ok());
|
||||
assert!(plugin.action.is_some());
|
||||
|
@ -300,7 +299,7 @@ mod tests {
|
|||
.begin_filter(
|
||||
CallStub::new()
|
||||
.with_parameter("package.description")
|
||||
.create(Span::unknown())
|
||||
.create()
|
||||
)
|
||||
.is_ok());
|
||||
|
||||
|
@ -317,7 +316,7 @@ mod tests {
|
|||
.with_long_flag("upcase")
|
||||
.with_long_flag("downcase")
|
||||
.with_long_flag("to-int")
|
||||
.create(Span::unknown()),
|
||||
.create(),
|
||||
)
|
||||
.is_err());
|
||||
assert_eq!(plugin.error, Some("can only apply one".to_string()));
|
||||
|
@ -353,7 +352,7 @@ mod tests {
|
|||
CallStub::new()
|
||||
.with_long_flag("upcase")
|
||||
.with_parameter("name")
|
||||
.create(Span::unknown())
|
||||
.create()
|
||||
)
|
||||
.is_ok());
|
||||
|
||||
|
@ -381,7 +380,7 @@ mod tests {
|
|||
CallStub::new()
|
||||
.with_long_flag("downcase")
|
||||
.with_parameter("name")
|
||||
.create(Span::unknown())
|
||||
.create()
|
||||
)
|
||||
.is_ok());
|
||||
|
||||
|
@ -409,7 +408,7 @@ mod tests {
|
|||
CallStub::new()
|
||||
.with_long_flag("to-int")
|
||||
.with_parameter("Nu_birthday")
|
||||
.create(Span::unknown())
|
||||
.create()
|
||||
)
|
||||
.is_ok());
|
||||
|
||||
|
|
58
tests/filter_str_tests.rs
Normal file
58
tests/filter_str_tests.rs
Normal file
|
@ -0,0 +1,58 @@
|
|||
mod helpers;
|
||||
|
||||
use helpers::in_directory as cwd;
|
||||
|
||||
#[test]
|
||||
fn acts_without_passing_field() {
|
||||
nu!(
|
||||
output,
|
||||
cwd("tests/fixtures/formats"),
|
||||
"open caco3_plastics.csv | first 1 | get origin | str --downcase | echo $it"
|
||||
);
|
||||
|
||||
assert_eq!(output, "spain");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn str_can_only_apply_one() {
|
||||
nu_error!(
|
||||
output,
|
||||
cwd("tests/fixtures/formats"),
|
||||
"open caco3_plastics.csv | first 1 | str origin --downcase --upcase"
|
||||
);
|
||||
|
||||
assert!(output.contains("Usage: str field [--downcase|--upcase|--to-int]"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn downcases() {
|
||||
nu!(
|
||||
output,
|
||||
cwd("tests/fixtures/formats"),
|
||||
"open caco3_plastics.csv | first 1 | str origin --downcase | get origin | echo $it"
|
||||
);
|
||||
|
||||
assert_eq!(output, "spain");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn upcases() {
|
||||
nu!(
|
||||
output,
|
||||
cwd("tests/fixtures/formats"),
|
||||
"open appveyor.yml | str environment.global.PROJECT_NAME --upcase | get environment.global.PROJECT_NAME | echo $it"
|
||||
);
|
||||
|
||||
assert_eq!(output, "NUSHELL");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn converts_to_int() {
|
||||
nu!(
|
||||
output,
|
||||
cwd("tests/fixtures/formats"),
|
||||
"open caco3_plastics.csv | get 0 | str tariff_item --to-int | where tariff_item == 2509000000 | get tariff_item | echo $it"
|
||||
);
|
||||
|
||||
assert_eq!(output, "2509000000");
|
||||
}
|
|
@ -68,50 +68,6 @@ fn can_split_by_column() {
|
|||
assert_eq!(output, "name");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn str_can_only_apply_one() {
|
||||
nu_error!(
|
||||
output,
|
||||
cwd("tests/fixtures/formats"),
|
||||
"open caco3_plastics.csv | first 1 | str origin --downcase --upcase"
|
||||
);
|
||||
|
||||
assert!(output.contains("Usage: str field [--downcase|--upcase|--to-int]"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn str_downcases() {
|
||||
nu!(
|
||||
output,
|
||||
cwd("tests/fixtures/formats"),
|
||||
"open caco3_plastics.csv | first 1 | str origin --downcase | get origin | echo $it"
|
||||
);
|
||||
|
||||
assert_eq!(output, "spain");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn str_upcases() {
|
||||
nu!(
|
||||
output,
|
||||
cwd("tests/fixtures/formats"),
|
||||
"open appveyor.yml | str environment.global.PROJECT_NAME --upcase | get environment.global.PROJECT_NAME | echo $it"
|
||||
);
|
||||
|
||||
assert_eq!(output, "NUSHELL");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn str_converts_to_int() {
|
||||
nu!(
|
||||
output,
|
||||
cwd("tests/fixtures/formats"),
|
||||
"open caco3_plastics.csv | first 1 | str tariff_item --to-int | where tariff_item == 2509000000 | get tariff_item | echo $it"
|
||||
);
|
||||
|
||||
assert_eq!(output, "2509000000");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn can_sum() {
|
||||
nu!(
|
||||
|
@ -122,6 +78,7 @@ fn can_sum() {
|
|||
|
||||
assert_eq!(output, "203")
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn can_filter_by_unit_size_comparison() {
|
||||
nu!(
|
||||
|
|
Loading…
Reference in a new issue