mirror of
https://github.com/nushell/nushell
synced 2025-01-28 21:05:48 +00:00
Merge branch 'refactor/add-tests'
This commit is contained in:
commit
d4df70c53f
2 changed files with 76 additions and 19 deletions
|
@ -27,6 +27,4 @@ let
|
||||||
in stdenv.mkDerivation {
|
in stdenv.mkDerivation {
|
||||||
name = "nushell-rust";
|
name = "nushell-rust";
|
||||||
buildInputs = nu-deps ++ rust;
|
buildInputs = nu-deps ++ rust;
|
||||||
RUST_SRC_PATH = "${nightly}/lib/rustlib/src/rust/src";
|
|
||||||
SSL_CERT_FILE = "/etc/ssl/certs/ca-certificates.crt";
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -33,38 +33,52 @@ impl WholeStreamCommand for FromSSV {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn from_ssv_string_to_value(
|
fn string_to_table(s: &str, headerless: bool) -> Vec<Vec<(String, String)>> {
|
||||||
s: &str,
|
let mut lines = s.lines().filter(|l| !l.trim().is_empty());
|
||||||
headerless: bool,
|
|
||||||
tag: impl Into<Tag>,
|
|
||||||
) -> Option<Tagged<Value>> {
|
|
||||||
let mut lines = s.lines().filter(|l| !l.is_empty());
|
|
||||||
|
|
||||||
let headers = lines
|
let headers = lines
|
||||||
.next()?
|
.next()
|
||||||
|
.unwrap()
|
||||||
.split_whitespace()
|
.split_whitespace()
|
||||||
.map(|s| s.to_owned())
|
.map(|s| s.to_owned())
|
||||||
.collect::<Vec<String>>();
|
.collect::<Vec<String>>();
|
||||||
|
|
||||||
let header_row = if headerless {
|
let header_row = if headerless {
|
||||||
(0..headers.len())
|
(1..=headers.len())
|
||||||
.map(|i| format!("Column{}", i + 1))
|
.map(|i| format!("Column{}", i))
|
||||||
.collect::<Vec<String>>()
|
.collect::<Vec<String>>()
|
||||||
} else {
|
} else {
|
||||||
headers
|
headers
|
||||||
};
|
};
|
||||||
|
|
||||||
let tag = tag.into();
|
lines
|
||||||
let rows = lines
|
|
||||||
.map(|l| {
|
.map(|l| {
|
||||||
let mut row = TaggedDictBuilder::new(tag);
|
header_row
|
||||||
for (column, value) in header_row.iter().zip(l.split_whitespace()) {
|
.iter()
|
||||||
row.insert_tagged(
|
.zip(l.split_whitespace())
|
||||||
column.to_owned(),
|
.map(|(a, b)| (String::from(a), String::from(b)))
|
||||||
Value::Primitive(Primitive::String(String::from(value))).tagged(tag),
|
.collect()
|
||||||
|
})
|
||||||
|
.collect()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn from_ssv_string_to_value(
|
||||||
|
s: &str,
|
||||||
|
headerless: bool,
|
||||||
|
tag: impl Into<Tag>,
|
||||||
|
) -> Option<Tagged<Value>> {
|
||||||
|
let tag = tag.into();
|
||||||
|
let rows = string_to_table(s, headerless)
|
||||||
|
.iter()
|
||||||
|
.map(|row| {
|
||||||
|
let mut tagged_dict = TaggedDictBuilder::new(tag);
|
||||||
|
for (col, entry) in row {
|
||||||
|
tagged_dict.insert_tagged(
|
||||||
|
col,
|
||||||
|
Value::Primitive(Primitive::String(String::from(entry))).tagged(tag),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
row.into_tagged_value()
|
tagged_dict.into_tagged_value()
|
||||||
})
|
})
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
|
@ -118,3 +132,48 @@ fn from_ssv(
|
||||||
|
|
||||||
Ok(stream.to_output_stream())
|
Ok(stream.to_output_stream())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
fn owned(x: &str, y: &str) -> (String, String) {
|
||||||
|
(String::from(x), String::from(y))
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn it_trims_empty_and_whitespace_only_lines() {
|
||||||
|
let input = r#"
|
||||||
|
|
||||||
|
a b
|
||||||
|
|
||||||
|
1 2
|
||||||
|
|
||||||
|
3 4
|
||||||
|
"#;
|
||||||
|
let result = string_to_table(input, false);
|
||||||
|
assert_eq!(
|
||||||
|
result,
|
||||||
|
vec![
|
||||||
|
vec![owned("a", "1"), owned("b", "2")],
|
||||||
|
vec![owned("a", "3"), owned("b", "4")]
|
||||||
|
]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn it_ignores_headers_when_headerless() {
|
||||||
|
let input = r#"
|
||||||
|
a b
|
||||||
|
1 2
|
||||||
|
3 4
|
||||||
|
"#;
|
||||||
|
let result = string_to_table(input, true);
|
||||||
|
assert_eq!(
|
||||||
|
result,
|
||||||
|
vec![
|
||||||
|
vec![owned("Column1", "1"), owned("Column2", "2")],
|
||||||
|
vec![owned("Column1", "3"), owned("Column2", "4")]
|
||||||
|
]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue