mirror of
https://github.com/nushell/nushell.git
synced 2024-11-29 11:54:02 +01:00
Extract tests.
This commit is contained in:
parent
cb4b2e6116
commit
5372463b37
@ -1,6 +1,6 @@
|
|||||||
use indexmap::IndexMap;
|
use indexmap::IndexMap;
|
||||||
use nu::{
|
use nu::{
|
||||||
serve_plugin, CallInfo, NamedType, Plugin, PositionalType, Primitive, ReturnSuccess,
|
serve_plugin, CallInfo, NamedType, Plugin, Primitive, ReturnSuccess,
|
||||||
ReturnValue, ShellError, Signature, Tagged, Value,
|
ReturnValue, ShellError, Signature, Tagged, Value,
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -82,16 +82,15 @@ impl Str {
|
|||||||
fn strutils(
|
fn strutils(
|
||||||
&self,
|
&self,
|
||||||
value: Tagged<Value>,
|
value: Tagged<Value>,
|
||||||
field: &Option<String>,
|
|
||||||
) -> Result<Tagged<Value>, ShellError> {
|
) -> Result<Tagged<Value>, ShellError> {
|
||||||
match value.item {
|
match value.item {
|
||||||
Value::Primitive(Primitive::String(ref s)) => {
|
Value::Primitive(Primitive::String(ref s)) => {
|
||||||
Ok(Tagged::from_item(self.apply(&s), value.tag()))
|
Ok(Tagged::from_item(self.apply(&s), value.tag()))
|
||||||
}
|
}
|
||||||
Value::Object(_) => match field {
|
Value::Object(_) => match self.field {
|
||||||
Some(f) => {
|
Some(ref f) => {
|
||||||
let replacement = match value.item.get_data_by_path(value.tag(), 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 => {
|
None => {
|
||||||
return Err(ShellError::string("str could not find field to replace"))
|
return Err(ShellError::string("str could not find field to replace"))
|
||||||
}
|
}
|
||||||
@ -129,7 +128,7 @@ impl Plugin for Str {
|
|||||||
|
|
||||||
Ok(Signature {
|
Ok(Signature {
|
||||||
name: "str".to_string(),
|
name: "str".to_string(),
|
||||||
positional: vec![PositionalType::optional_any("Field")],
|
positional: vec![],
|
||||||
is_filter: true,
|
is_filter: true,
|
||||||
named,
|
named,
|
||||||
rest_positional: true,
|
rest_positional: true,
|
||||||
@ -178,7 +177,7 @@ impl Plugin for Str {
|
|||||||
|
|
||||||
fn filter(&mut self, input: Tagged<Value>) -> Result<Vec<ReturnValue>, ShellError> {
|
fn filter(&mut self, input: Tagged<Value>) -> Result<Vec<ReturnValue>, ShellError> {
|
||||||
Ok(vec![ReturnSuccess::value(
|
Ok(vec![ReturnSuccess::value(
|
||||||
self.strutils(input, &self.field)?,
|
self.strutils(input)?,
|
||||||
)])
|
)])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -224,11 +223,11 @@ mod tests {
|
|||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
fn create(&self, name_span: Span) -> CallInfo {
|
fn create(&self) -> CallInfo {
|
||||||
CallInfo {
|
CallInfo {
|
||||||
args: EvaluatedArgs::new(Some(self.positionals.clone()), Some(self.flags.clone())),
|
args: EvaluatedArgs::new(Some(self.positionals.clone()), Some(self.flags.clone())),
|
||||||
source_map: SourceMap::new(),
|
source_map: SourceMap::new(),
|
||||||
name_span,
|
name_span: Span::unknown(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -258,7 +257,7 @@ mod tests {
|
|||||||
.begin_filter(
|
.begin_filter(
|
||||||
CallStub::new()
|
CallStub::new()
|
||||||
.with_long_flag("downcase")
|
.with_long_flag("downcase")
|
||||||
.create(Span::unknown())
|
.create()
|
||||||
)
|
)
|
||||||
.is_ok());
|
.is_ok());
|
||||||
assert!(plugin.action.is_some());
|
assert!(plugin.action.is_some());
|
||||||
@ -272,7 +271,7 @@ mod tests {
|
|||||||
.begin_filter(
|
.begin_filter(
|
||||||
CallStub::new()
|
CallStub::new()
|
||||||
.with_long_flag("upcase")
|
.with_long_flag("upcase")
|
||||||
.create(Span::unknown())
|
.create()
|
||||||
)
|
)
|
||||||
.is_ok());
|
.is_ok());
|
||||||
assert!(plugin.action.is_some());
|
assert!(plugin.action.is_some());
|
||||||
@ -286,7 +285,7 @@ mod tests {
|
|||||||
.begin_filter(
|
.begin_filter(
|
||||||
CallStub::new()
|
CallStub::new()
|
||||||
.with_long_flag("to-int")
|
.with_long_flag("to-int")
|
||||||
.create(Span::unknown())
|
.create()
|
||||||
)
|
)
|
||||||
.is_ok());
|
.is_ok());
|
||||||
assert!(plugin.action.is_some());
|
assert!(plugin.action.is_some());
|
||||||
@ -300,7 +299,7 @@ mod tests {
|
|||||||
.begin_filter(
|
.begin_filter(
|
||||||
CallStub::new()
|
CallStub::new()
|
||||||
.with_parameter("package.description")
|
.with_parameter("package.description")
|
||||||
.create(Span::unknown())
|
.create()
|
||||||
)
|
)
|
||||||
.is_ok());
|
.is_ok());
|
||||||
|
|
||||||
@ -317,7 +316,7 @@ mod tests {
|
|||||||
.with_long_flag("upcase")
|
.with_long_flag("upcase")
|
||||||
.with_long_flag("downcase")
|
.with_long_flag("downcase")
|
||||||
.with_long_flag("to-int")
|
.with_long_flag("to-int")
|
||||||
.create(Span::unknown()),
|
.create(),
|
||||||
)
|
)
|
||||||
.is_err());
|
.is_err());
|
||||||
assert_eq!(plugin.error, Some("can only apply one".to_string()));
|
assert_eq!(plugin.error, Some("can only apply one".to_string()));
|
||||||
@ -353,7 +352,7 @@ mod tests {
|
|||||||
CallStub::new()
|
CallStub::new()
|
||||||
.with_long_flag("upcase")
|
.with_long_flag("upcase")
|
||||||
.with_parameter("name")
|
.with_parameter("name")
|
||||||
.create(Span::unknown())
|
.create()
|
||||||
)
|
)
|
||||||
.is_ok());
|
.is_ok());
|
||||||
|
|
||||||
@ -381,7 +380,7 @@ mod tests {
|
|||||||
CallStub::new()
|
CallStub::new()
|
||||||
.with_long_flag("downcase")
|
.with_long_flag("downcase")
|
||||||
.with_parameter("name")
|
.with_parameter("name")
|
||||||
.create(Span::unknown())
|
.create()
|
||||||
)
|
)
|
||||||
.is_ok());
|
.is_ok());
|
||||||
|
|
||||||
@ -409,7 +408,7 @@ mod tests {
|
|||||||
CallStub::new()
|
CallStub::new()
|
||||||
.with_long_flag("to-int")
|
.with_long_flag("to-int")
|
||||||
.with_parameter("Nu_birthday")
|
.with_parameter("Nu_birthday")
|
||||||
.create(Span::unknown())
|
.create()
|
||||||
)
|
)
|
||||||
.is_ok());
|
.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");
|
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]
|
#[test]
|
||||||
fn can_sum() {
|
fn can_sum() {
|
||||||
nu!(
|
nu!(
|
||||||
@ -122,6 +78,7 @@ fn can_sum() {
|
|||||||
|
|
||||||
assert_eq!(output, "203")
|
assert_eq!(output, "203")
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn can_filter_by_unit_size_comparison() {
|
fn can_filter_by_unit_size_comparison() {
|
||||||
nu!(
|
nu!(
|
||||||
|
Loading…
Reference in New Issue
Block a user