nushell/src/plugins/str.rs

488 lines
14 KiB
Rust
Raw Normal View History

2019-07-28 09:01:32 +02:00
use nu::{
Add support for ~ expansion This ended up being a bit of a yak shave. The basic idea in this commit is to expand `~` in paths, but only in paths. The way this is accomplished is by doing the expansion inside of the code that parses literal syntax for `SyntaxType::Path`. As a quick refresher: every command is entitled to expand its arguments in a custom way. While this could in theory be used for general-purpose macros, today the expansion facility is limited to syntactic hints. For example, the syntax `where cpu > 0` expands under the hood to `where { $it.cpu > 0 }`. This happens because the first argument to `where` is defined as a `SyntaxType::Block`, and the parser coerces binary expressions whose left-hand-side looks like a member into a block when the command is expecting one. This is mildly more magical than what most programming languages would do, but we believe that it makes sense to allow commands to fine-tune the syntax because of the domain nushell is in (command-line shells). The syntactic expansions supported by this facility are relatively limited. For example, we don't allow `$it` to become a bare word, simply because the command asks for a string in the relevant position. That would quickly become more confusing than it's worth. This PR adds a new `SyntaxType` rule: `SyntaxType::Path`. When a command declares a parameter as a `SyntaxType::Path`, string literals and bare words passed as an argument to that parameter are processed using the path expansion rules. Right now, that only means that `~` is expanded into the home directory, but additional rules are possible in the future. By restricting this expansion to a syntactic expansion when passed as an argument to a command expecting a path, we avoid making `~` a generally reserved character. This will also allow us to give good tab completion for paths with `~` characters in them when a command is expecting a path. In order to accomplish the above, this commit changes the parsing functions to take a `Context` instead of just a `CommandRegistry`. From the perspective of macro expansion, you can think of the `CommandRegistry` as a dictionary of in-scope macros, and the `Context` as the compile-time state used in expansion. This could gain additional functionality over time as we find more uses for the expansion system.
2019-08-26 21:21:03 +02:00
serve_plugin, CallInfo, Plugin, Primitive, ReturnSuccess, ReturnValue, ShellError, Signature,
SyntaxType, Tagged, Value,
2019-07-28 09:01:32 +02:00
};
2019-08-11 19:46:14 +02:00
#[derive(Debug, Eq, PartialEq)]
2019-08-01 00:26:04 +02:00
enum Action {
Downcase,
Upcase,
ToInteger,
}
2019-07-28 09:01:32 +02:00
struct Str {
field: Option<String>,
2019-08-11 19:46:14 +02:00
params: Option<Vec<String>>,
2019-07-29 01:34:37 +02:00
error: Option<String>,
2019-08-01 00:26:04 +02:00
action: Option<Action>,
2019-07-28 09:01:32 +02:00
}
impl Str {
fn new() -> Str {
Str {
field: None,
2019-08-11 19:46:14 +02:00
params: Some(Vec::<String>::new()),
2019-07-29 01:34:37 +02:00
error: None,
2019-08-01 00:26:04 +02:00
action: None,
2019-07-28 09:01:32 +02:00
}
}
fn apply(&self, input: &str) -> Result<Value, ShellError> {
let applied = match self.action.as_ref() {
Some(Action::Downcase) => Value::string(input.to_ascii_lowercase()),
Some(Action::Upcase) => Value::string(input.to_ascii_uppercase()),
Add support for ~ expansion This ended up being a bit of a yak shave. The basic idea in this commit is to expand `~` in paths, but only in paths. The way this is accomplished is by doing the expansion inside of the code that parses literal syntax for `SyntaxType::Path`. As a quick refresher: every command is entitled to expand its arguments in a custom way. While this could in theory be used for general-purpose macros, today the expansion facility is limited to syntactic hints. For example, the syntax `where cpu > 0` expands under the hood to `where { $it.cpu > 0 }`. This happens because the first argument to `where` is defined as a `SyntaxType::Block`, and the parser coerces binary expressions whose left-hand-side looks like a member into a block when the command is expecting one. This is mildly more magical than what most programming languages would do, but we believe that it makes sense to allow commands to fine-tune the syntax because of the domain nushell is in (command-line shells). The syntactic expansions supported by this facility are relatively limited. For example, we don't allow `$it` to become a bare word, simply because the command asks for a string in the relevant position. That would quickly become more confusing than it's worth. This PR adds a new `SyntaxType` rule: `SyntaxType::Path`. When a command declares a parameter as a `SyntaxType::Path`, string literals and bare words passed as an argument to that parameter are processed using the path expansion rules. Right now, that only means that `~` is expanded into the home directory, but additional rules are possible in the future. By restricting this expansion to a syntactic expansion when passed as an argument to a command expecting a path, we avoid making `~` a generally reserved character. This will also allow us to give good tab completion for paths with `~` characters in them when a command is expecting a path. In order to accomplish the above, this commit changes the parsing functions to take a `Context` instead of just a `CommandRegistry`. From the perspective of macro expansion, you can think of the `CommandRegistry` as a dictionary of in-scope macros, and the `Context` as the compile-time state used in expansion. This could gain additional functionality over time as we find more uses for the expansion system.
2019-08-26 21:21:03 +02:00
Some(Action::ToInteger) => match input.trim() {
other => match other.parse::<i64>() {
Ok(v) => Value::int(v),
Err(_) => Value::string(input),
},
2019-08-01 00:29:40 +02:00
},
None => Value::string(input),
};
Ok(applied)
2019-07-30 13:41:24 +02:00
}
2019-08-11 19:46:14 +02:00
fn for_field(&mut self, field: &str) {
self.field = Some(String::from(field));
}
fn permit(&mut self) -> bool {
self.action.is_none()
2019-07-30 13:41:24 +02:00
}
fn log_error(&mut self, message: &str) {
self.error = Some(message.to_string());
}
fn for_to_int(&mut self) {
if self.permit() {
self.action = Some(Action::ToInteger);
} else {
self.log_error("can only apply one");
}
}
2019-07-29 04:30:47 +02:00
fn for_downcase(&mut self) {
if self.permit() {
self.action = Some(Action::Downcase);
} else {
self.log_error("can only apply one");
}
2019-07-29 01:34:37 +02:00
}
2019-07-29 04:30:47 +02:00
fn for_upcase(&mut self) {
if self.permit() {
self.action = Some(Action::Upcase);
} else {
self.log_error("can only apply one");
}
2019-07-29 04:30:47 +02:00
}
pub fn usage() -> &'static str {
2019-09-12 05:20:42 +02:00
"Usage: str field [--downcase|--upcase|--to-int]"
2019-07-29 01:34:37 +02:00
}
2019-07-29 04:30:47 +02:00
}
2019-07-29 01:34:37 +02:00
2019-07-29 04:30:47 +02:00
impl Str {
2019-08-10 11:12:48 +02:00
fn strutils(&self, value: Tagged<Value>) -> Result<Tagged<Value>, ShellError> {
2019-07-28 09:01:32 +02:00
match value.item {
2019-08-01 07:32:36 +02:00
Value::Primitive(Primitive::String(ref s)) => {
Ok(Tagged::from_item(self.apply(&s)?, value.tag()))
2019-08-01 07:32:36 +02:00
}
Value::Row(_) => match self.field {
2019-08-10 11:11:38 +02:00
Some(ref f) => {
let replacement = match value.item.get_data_by_path(value.tag(), f) {
2019-08-10 11:11:38 +02:00
Some(result) => self.strutils(result.map(|x| x.clone()))?,
Add support for ~ expansion This ended up being a bit of a yak shave. The basic idea in this commit is to expand `~` in paths, but only in paths. The way this is accomplished is by doing the expansion inside of the code that parses literal syntax for `SyntaxType::Path`. As a quick refresher: every command is entitled to expand its arguments in a custom way. While this could in theory be used for general-purpose macros, today the expansion facility is limited to syntactic hints. For example, the syntax `where cpu > 0` expands under the hood to `where { $it.cpu > 0 }`. This happens because the first argument to `where` is defined as a `SyntaxType::Block`, and the parser coerces binary expressions whose left-hand-side looks like a member into a block when the command is expecting one. This is mildly more magical than what most programming languages would do, but we believe that it makes sense to allow commands to fine-tune the syntax because of the domain nushell is in (command-line shells). The syntactic expansions supported by this facility are relatively limited. For example, we don't allow `$it` to become a bare word, simply because the command asks for a string in the relevant position. That would quickly become more confusing than it's worth. This PR adds a new `SyntaxType` rule: `SyntaxType::Path`. When a command declares a parameter as a `SyntaxType::Path`, string literals and bare words passed as an argument to that parameter are processed using the path expansion rules. Right now, that only means that `~` is expanded into the home directory, but additional rules are possible in the future. By restricting this expansion to a syntactic expansion when passed as an argument to a command expecting a path, we avoid making `~` a generally reserved character. This will also allow us to give good tab completion for paths with `~` characters in them when a command is expecting a path. In order to accomplish the above, this commit changes the parsing functions to take a `Context` instead of just a `CommandRegistry`. From the perspective of macro expansion, you can think of the `CommandRegistry` as a dictionary of in-scope macros, and the `Context` as the compile-time state used in expansion. This could gain additional functionality over time as we find more uses for the expansion system.
2019-08-26 21:21:03 +02:00
None => return Ok(Tagged::from_item(Value::nothing(), value.tag)),
2019-07-28 09:01:32 +02:00
};
match value
.item
.replace_data_at_path(value.tag(), f, replacement.item.clone())
2019-07-28 09:01:32 +02:00
{
Some(v) => return Ok(v),
None => {
return Err(ShellError::string("str could not find field to replace"))
}
}
}
2019-07-30 13:41:24 +02:00
None => Err(ShellError::string(format!(
"{}: {}",
"str needs a column when applied to a value in a row",
Str::usage()
2019-07-30 13:41:24 +02:00
))),
2019-07-28 09:01:32 +02:00
},
x => Err(ShellError::string(format!(
"Unrecognized type in stream: {:?}",
x
))),
}
}
}
impl Plugin for Str {
2019-08-09 06:51:21 +02:00
fn config(&mut self) -> Result<Signature, ShellError> {
Add support for ~ expansion This ended up being a bit of a yak shave. The basic idea in this commit is to expand `~` in paths, but only in paths. The way this is accomplished is by doing the expansion inside of the code that parses literal syntax for `SyntaxType::Path`. As a quick refresher: every command is entitled to expand its arguments in a custom way. While this could in theory be used for general-purpose macros, today the expansion facility is limited to syntactic hints. For example, the syntax `where cpu > 0` expands under the hood to `where { $it.cpu > 0 }`. This happens because the first argument to `where` is defined as a `SyntaxType::Block`, and the parser coerces binary expressions whose left-hand-side looks like a member into a block when the command is expecting one. This is mildly more magical than what most programming languages would do, but we believe that it makes sense to allow commands to fine-tune the syntax because of the domain nushell is in (command-line shells). The syntactic expansions supported by this facility are relatively limited. For example, we don't allow `$it` to become a bare word, simply because the command asks for a string in the relevant position. That would quickly become more confusing than it's worth. This PR adds a new `SyntaxType` rule: `SyntaxType::Path`. When a command declares a parameter as a `SyntaxType::Path`, string literals and bare words passed as an argument to that parameter are processed using the path expansion rules. Right now, that only means that `~` is expanded into the home directory, but additional rules are possible in the future. By restricting this expansion to a syntactic expansion when passed as an argument to a command expecting a path, we avoid making `~` a generally reserved character. This will also allow us to give good tab completion for paths with `~` characters in them when a command is expecting a path. In order to accomplish the above, this commit changes the parsing functions to take a `Context` instead of just a `CommandRegistry`. From the perspective of macro expansion, you can think of the `CommandRegistry` as a dictionary of in-scope macros, and the `Context` as the compile-time state used in expansion. This could gain additional functionality over time as we find more uses for the expansion system.
2019-08-26 21:21:03 +02:00
Ok(Signature::build("str")
.desc("Apply string function. Optional use the field of a table")
Add support for ~ expansion This ended up being a bit of a yak shave. The basic idea in this commit is to expand `~` in paths, but only in paths. The way this is accomplished is by doing the expansion inside of the code that parses literal syntax for `SyntaxType::Path`. As a quick refresher: every command is entitled to expand its arguments in a custom way. While this could in theory be used for general-purpose macros, today the expansion facility is limited to syntactic hints. For example, the syntax `where cpu > 0` expands under the hood to `where { $it.cpu > 0 }`. This happens because the first argument to `where` is defined as a `SyntaxType::Block`, and the parser coerces binary expressions whose left-hand-side looks like a member into a block when the command is expecting one. This is mildly more magical than what most programming languages would do, but we believe that it makes sense to allow commands to fine-tune the syntax because of the domain nushell is in (command-line shells). The syntactic expansions supported by this facility are relatively limited. For example, we don't allow `$it` to become a bare word, simply because the command asks for a string in the relevant position. That would quickly become more confusing than it's worth. This PR adds a new `SyntaxType` rule: `SyntaxType::Path`. When a command declares a parameter as a `SyntaxType::Path`, string literals and bare words passed as an argument to that parameter are processed using the path expansion rules. Right now, that only means that `~` is expanded into the home directory, but additional rules are possible in the future. By restricting this expansion to a syntactic expansion when passed as an argument to a command expecting a path, we avoid making `~` a generally reserved character. This will also allow us to give good tab completion for paths with `~` characters in them when a command is expecting a path. In order to accomplish the above, this commit changes the parsing functions to take a `Context` instead of just a `CommandRegistry`. From the perspective of macro expansion, you can think of the `CommandRegistry` as a dictionary of in-scope macros, and the `Context` as the compile-time state used in expansion. This could gain additional functionality over time as we find more uses for the expansion system.
2019-08-26 21:21:03 +02:00
.switch("downcase")
.switch("upcase")
.switch("to-int")
.rest(SyntaxType::Member)
Add support for ~ expansion This ended up being a bit of a yak shave. The basic idea in this commit is to expand `~` in paths, but only in paths. The way this is accomplished is by doing the expansion inside of the code that parses literal syntax for `SyntaxType::Path`. As a quick refresher: every command is entitled to expand its arguments in a custom way. While this could in theory be used for general-purpose macros, today the expansion facility is limited to syntactic hints. For example, the syntax `where cpu > 0` expands under the hood to `where { $it.cpu > 0 }`. This happens because the first argument to `where` is defined as a `SyntaxType::Block`, and the parser coerces binary expressions whose left-hand-side looks like a member into a block when the command is expecting one. This is mildly more magical than what most programming languages would do, but we believe that it makes sense to allow commands to fine-tune the syntax because of the domain nushell is in (command-line shells). The syntactic expansions supported by this facility are relatively limited. For example, we don't allow `$it` to become a bare word, simply because the command asks for a string in the relevant position. That would quickly become more confusing than it's worth. This PR adds a new `SyntaxType` rule: `SyntaxType::Path`. When a command declares a parameter as a `SyntaxType::Path`, string literals and bare words passed as an argument to that parameter are processed using the path expansion rules. Right now, that only means that `~` is expanded into the home directory, but additional rules are possible in the future. By restricting this expansion to a syntactic expansion when passed as an argument to a command expecting a path, we avoid making `~` a generally reserved character. This will also allow us to give good tab completion for paths with `~` characters in them when a command is expecting a path. In order to accomplish the above, this commit changes the parsing functions to take a `Context` instead of just a `CommandRegistry`. From the perspective of macro expansion, you can think of the `CommandRegistry` as a dictionary of in-scope macros, and the `Context` as the compile-time state used in expansion. This could gain additional functionality over time as we find more uses for the expansion system.
2019-08-26 21:21:03 +02:00
.filter())
2019-07-28 09:01:32 +02:00
}
2019-07-29 01:34:37 +02:00
2019-07-28 09:01:32 +02:00
fn begin_filter(&mut self, call_info: CallInfo) -> Result<Vec<ReturnValue>, ShellError> {
2019-08-11 19:46:14 +02:00
let args = call_info.args;
if args.has("downcase") {
2019-07-29 04:30:47 +02:00
self.for_downcase();
2019-07-29 01:34:37 +02:00
}
2019-08-11 19:46:14 +02:00
if args.has("upcase") {
2019-07-29 04:30:47 +02:00
self.for_upcase();
2019-07-28 09:01:32 +02:00
}
2019-08-11 19:46:14 +02:00
if args.has("to-int") {
self.for_to_int();
}
2019-08-11 19:46:14 +02:00
if let Some(possible_field) = args.nth(0) {
match possible_field {
Tagged {
item: Value::Primitive(Primitive::String(s)),
..
} => match self.action {
Some(Action::Downcase)
| Some(Action::Upcase)
| Some(Action::ToInteger)
| None => {
self.for_field(&s);
2019-07-28 09:01:32 +02:00
}
2019-08-11 19:46:14 +02:00
},
_ => {
return Err(ShellError::string(format!(
"Unrecognized type in params: {:?}",
possible_field
)))
2019-07-28 09:01:32 +02:00
}
}
}
2019-08-11 19:46:14 +02:00
for param in args.positional_iter() {
match param {
Tagged {
item: Value::Primitive(Primitive::String(s)),
..
} => self.params.as_mut().unwrap().push(String::from(s)),
_ => {}
}
}
2019-07-29 01:34:37 +02:00
match &self.error {
Some(reason) => {
return Err(ShellError::string(format!("{}: {}", reason, Str::usage())))
2019-07-29 01:34:37 +02:00
}
2019-07-30 13:41:24 +02:00
None => Ok(vec![]),
2019-07-29 01:34:37 +02:00
}
2019-07-28 09:01:32 +02:00
}
2019-08-01 03:58:42 +02:00
fn filter(&mut self, input: Tagged<Value>) -> Result<Vec<ReturnValue>, ShellError> {
2019-08-10 11:12:48 +02:00
Ok(vec![ReturnSuccess::value(self.strutils(input)?)])
2019-07-28 09:01:32 +02:00
}
}
fn main() {
serve_plugin(&mut Str::new());
}
2019-07-30 13:41:24 +02:00
#[cfg(test)]
mod tests {
2019-09-12 05:20:42 +02:00
use super::{Action, Str};
2019-07-30 13:41:24 +02:00
use indexmap::IndexMap;
use nu::{
CallInfo, EvaluatedArgs, Plugin, Primitive, ReturnSuccess, SourceMap, Span, Tag, Tagged,
2019-08-09 06:51:21 +02:00
TaggedDictBuilder, TaggedItem, Value,
2019-07-30 13:41:24 +02:00
};
use num_bigint::BigInt;
2019-07-30 13:41:24 +02:00
struct CallStub {
2019-08-01 07:32:36 +02:00
positionals: Vec<Tagged<Value>>,
flags: IndexMap<String, Tagged<Value>>,
2019-07-30 13:41:24 +02:00
}
impl CallStub {
fn new() -> CallStub {
CallStub {
positionals: vec![],
flags: indexmap::IndexMap::new(),
}
}
fn with_long_flag(&mut self, name: &str) -> &mut Self {
self.flags.insert(
name.to_string(),
Value::boolean(true).simple_spanned(Span::unknown()),
2019-07-30 13:41:24 +02:00
);
self
}
fn with_parameter(&mut self, name: &str) -> &mut Self {
self.positionals
.push(Value::string(name.to_string()).simple_spanned(Span::unknown()));
2019-07-30 13:41:24 +02:00
self
}
2019-08-10 11:11:38 +02:00
fn create(&self) -> CallInfo {
2019-07-30 13:41:24 +02:00
CallInfo {
2019-08-09 06:51:21 +02:00
args: EvaluatedArgs::new(Some(self.positionals.clone()), Some(self.flags.clone())),
2019-07-30 13:41:24 +02:00
source_map: SourceMap::new(),
name_span: Span::unknown(),
2019-07-30 13:41:24 +02:00
}
}
}
2019-08-11 19:46:14 +02:00
fn structured_sample_record(key: &str, value: &str) -> Tagged<Value> {
let mut record = TaggedDictBuilder::new(Tag::unknown());
2019-07-30 18:29:11 +02:00
record.insert(key.clone(), Value::string(value));
2019-08-01 07:32:36 +02:00
record.into_tagged_value()
2019-07-30 13:41:24 +02:00
}
2019-08-11 19:46:14 +02:00
fn unstructured_sample_record(value: &str) -> Tagged<Value> {
Tagged::from_item(Value::string(value), Tag::unknown())
}
#[test]
fn str_plugin_configuration_flags_wired() {
let mut plugin = Str::new();
let configured = plugin.config().unwrap();
2019-09-12 05:20:42 +02:00
for action_flag in &["downcase", "upcase", "to-int"] {
assert!(configured.named.get(*action_flag).is_some());
}
}
2019-07-30 13:41:24 +02:00
#[test]
fn str_plugin_accepts_downcase() {
let mut plugin = Str::new();
2019-07-30 13:41:24 +02:00
assert!(plugin
2019-08-10 11:12:48 +02:00
.begin_filter(CallStub::new().with_long_flag("downcase").create())
2019-07-30 13:41:24 +02:00
.is_ok());
2019-08-11 19:46:14 +02:00
assert_eq!(plugin.action.unwrap(), Action::Downcase);
2019-07-30 13:41:24 +02:00
}
#[test]
fn str_plugin_accepts_upcase() {
let mut plugin = Str::new();
2019-07-30 13:41:24 +02:00
assert!(plugin
2019-08-10 11:12:48 +02:00
.begin_filter(CallStub::new().with_long_flag("upcase").create())
2019-07-30 13:41:24 +02:00
.is_ok());
2019-08-11 19:46:14 +02:00
assert_eq!(plugin.action.unwrap(), Action::Upcase);
2019-07-30 13:41:24 +02:00
}
#[test]
fn str_plugin_accepts_to_int() {
let mut plugin = Str::new();
assert!(plugin
2019-08-10 11:12:48 +02:00
.begin_filter(CallStub::new().with_long_flag("to-int").create())
.is_ok());
2019-08-11 19:46:14 +02:00
assert_eq!(plugin.action.unwrap(), Action::ToInteger);
}
2019-07-30 13:41:24 +02:00
#[test]
fn str_plugin_accepts_field() {
let mut plugin = Str::new();
2019-07-30 13:41:24 +02:00
assert!(plugin
2019-07-30 13:41:24 +02:00
.begin_filter(
CallStub::new()
.with_parameter("package.description")
2019-08-10 11:11:38 +02:00
.create()
2019-07-30 13:41:24 +02:00
)
.is_ok());
assert_eq!(plugin.field, Some("package.description".to_string()));
2019-07-30 13:41:24 +02:00
}
2019-08-01 00:29:40 +02:00
#[test]
fn str_plugin_accepts_only_one_action() {
let mut plugin = Str::new();
assert!(plugin
.begin_filter(
CallStub::new()
.with_long_flag("upcase")
.with_long_flag("downcase")
.with_long_flag("to-int")
2019-08-10 11:11:38 +02:00
.create(),
2019-08-01 00:29:40 +02:00
)
.is_err());
assert_eq!(plugin.error, Some("can only apply one".to_string()));
}
#[test]
fn str_downcases() {
let mut strutils = Str::new();
strutils.for_downcase();
assert_eq!(strutils.apply("ANDRES").unwrap(), Value::string("andres"));
}
#[test]
fn str_upcases() {
let mut strutils = Str::new();
strutils.for_upcase();
assert_eq!(strutils.apply("andres").unwrap(), Value::string("ANDRES"));
}
#[test]
fn str_to_int() {
let mut strutils = Str::new();
strutils.for_to_int();
assert_eq!(strutils.apply("9999").unwrap(), Value::int(9999 as i64));
}
2019-08-11 19:46:14 +02:00
#[test]
fn str_plugin_applies_upcase_with_field() {
let mut plugin = Str::new();
2019-07-30 13:41:24 +02:00
assert!(plugin
2019-07-30 13:41:24 +02:00
.begin_filter(
CallStub::new()
.with_long_flag("upcase")
.with_parameter("name")
2019-08-10 11:11:38 +02:00
.create()
2019-07-30 13:41:24 +02:00
)
.is_ok());
2019-08-11 19:46:14 +02:00
let subject = structured_sample_record("name", "jotandrehuda");
let output = plugin.filter(subject).unwrap();
2019-07-30 13:41:24 +02:00
match output[0].as_ref().unwrap() {
2019-08-01 07:32:36 +02:00
ReturnSuccess::Value(Tagged {
item: Value::Row(o),
2019-07-30 13:41:24 +02:00
..
}) => assert_eq!(
*o.get_data(&String::from("name")).borrow(),
Value::string(String::from("JOTANDREHUDA"))
),
_ => {}
}
}
#[test]
2019-08-11 19:46:14 +02:00
fn str_plugin_applies_upcase_without_field() {
let mut plugin = Str::new();
assert!(plugin
.begin_filter(CallStub::new().with_long_flag("upcase").create())
.is_ok());
let subject = unstructured_sample_record("jotandrehuda");
let output = plugin.filter(subject).unwrap();
match output[0].as_ref().unwrap() {
ReturnSuccess::Value(Tagged {
item: Value::Primitive(Primitive::String(s)),
..
}) => assert_eq!(*s, String::from("JOTANDREHUDA")),
_ => {}
}
}
#[test]
fn str_plugin_applies_downcase_with_field() {
let mut plugin = Str::new();
2019-07-30 13:41:24 +02:00
assert!(plugin
2019-07-30 13:41:24 +02:00
.begin_filter(
CallStub::new()
.with_long_flag("downcase")
.with_parameter("name")
2019-08-10 11:11:38 +02:00
.create()
2019-07-30 13:41:24 +02:00
)
.is_ok());
2019-08-11 19:46:14 +02:00
let subject = structured_sample_record("name", "JOTANDREHUDA");
let output = plugin.filter(subject).unwrap();
2019-07-30 13:41:24 +02:00
match output[0].as_ref().unwrap() {
2019-08-01 07:32:36 +02:00
ReturnSuccess::Value(Tagged {
item: Value::Row(o),
2019-07-30 13:41:24 +02:00
..
}) => assert_eq!(
*o.get_data(&String::from("name")).borrow(),
Value::string(String::from("jotandrehuda"))
),
_ => {}
}
}
#[test]
2019-08-11 19:46:14 +02:00
fn str_plugin_applies_downcase_without_field() {
let mut plugin = Str::new();
assert!(plugin
.begin_filter(CallStub::new().with_long_flag("downcase").create())
.is_ok());
let subject = unstructured_sample_record("JOTANDREHUDA");
let output = plugin.filter(subject).unwrap();
match output[0].as_ref().unwrap() {
ReturnSuccess::Value(Tagged {
item: Value::Primitive(Primitive::String(s)),
..
}) => assert_eq!(*s, String::from("jotandrehuda")),
_ => {}
}
}
#[test]
fn str_plugin_applies_to_int_with_field() {
let mut plugin = Str::new();
assert!(plugin
.begin_filter(
CallStub::new()
.with_long_flag("to-int")
.with_parameter("Nu_birthday")
2019-08-10 11:11:38 +02:00
.create()
)
.is_ok());
2019-08-11 19:46:14 +02:00
let subject = structured_sample_record("Nu_birthday", "10");
let output = plugin.filter(subject).unwrap();
match output[0].as_ref().unwrap() {
2019-08-01 07:32:36 +02:00
ReturnSuccess::Value(Tagged {
item: Value::Row(o),
..
}) => assert_eq!(
*o.get_data(&String::from("Nu_birthday")).borrow(),
Value::int(10)
),
_ => {}
}
}
2019-08-11 19:46:14 +02:00
#[test]
fn str_plugin_applies_to_int_without_field() {
let mut plugin = Str::new();
assert!(plugin
.begin_filter(CallStub::new().with_long_flag("to-int").create())
.is_ok());
let subject = unstructured_sample_record("10");
let output = plugin.filter(subject).unwrap();
match output[0].as_ref().unwrap() {
ReturnSuccess::Value(Tagged {
item: Value::Primitive(Primitive::Int(i)),
..
}) => assert_eq!(*i, BigInt::from(10)),
2019-08-11 19:46:14 +02:00
_ => {}
}
}
2019-07-30 13:41:24 +02:00
}