Improve test coverage of command examples (#5650)

* Ignore `cargo tarpaulin` output files

* Add expected result for `columns` example

Examples without provided expected output will never be tested.
The subset of commands available in `test_examples()` is limited thus
excluding the tests depending on other commands

* Add example test harness to `reject`

* Test and fix `wrap` example

* Test and fix `drop column` example

* Update `from ods` examples

* Update `from xlsx` examples

* Run `to nuon` examples

* Run `hash base64` examples

* Add example output to `path parse`

* Test and fix the `grid` examples
This commit is contained in:
Stefan Holderbach 2022-05-26 20:51:31 +02:00 committed by GitHub
parent 230c36f2fb
commit 507f24d029
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 156 additions and 26 deletions

4
.gitignore vendored
View File

@ -25,3 +25,7 @@ debian/nu/
# Helix configuration folder
.helix/*
.helix
# Coverage tools
lcov.info
tarpaulin-report.html

View File

@ -27,7 +27,14 @@ impl Command for Columns {
Example {
example: "[[name,age,grade]; [bill,20,a]] | columns",
description: "Get the columns from the table",
result: None,
result: Some(Value::List {
vals: vec![
Value::test_string("name"),
Value::test_string("age"),
Value::test_string("grade"),
],
span: Span::test_data(),
}),
},
Example {
example: "[[name,age,grade]; [bill,20,a]] | columns | first",

View File

@ -57,11 +57,18 @@ impl Command for DropColumn {
description: "Remove the last column of a table",
example: "echo [[lib, extension]; [nu-lib, rs] [nu-core, rb]] | drop column",
result: Some(Value::List {
vals: vec![Value::Record {
cols: vec!["lib".into()],
vals: vec![Value::test_string("nu-lib"), Value::test_string("nu-core")],
span: Span::test_data(),
}],
vals: vec![
Value::Record {
cols: vec!["lib".into()],
vals: vec![Value::test_string("nu-lib")],
span: Span::test_data(),
},
Value::Record {
cols: vec!["lib".into()],
vals: vec![Value::test_string("nu-core")],
span: Span::test_data(),
},
],
span: Span::test_data(),
}),
}]
@ -180,3 +187,13 @@ fn get_keep_columns(input: Vec<String>, mut num_of_columns_to_drop: i64) -> Vec<
let num_of_columns_to_keep = (vlen - num_of_columns_to_drop) as usize;
input[0..num_of_columns_to_keep].to_vec()
}
#[cfg(test)]
mod test {
#[test]
fn test_examples() {
use super::DropColumn;
use crate::test_examples;
test_examples(DropColumn {})
}
}

View File

@ -205,3 +205,13 @@ fn reject_record_columns(cols: &mut Vec<String>, vals: &mut Vec<Value>, rejects:
}
}
}
#[cfg(test)]
mod test {
#[test]
fn test_examples() {
use super::Reject;
use crate::test_examples;
test_examples(Reject {})
}
}

View File

@ -70,13 +70,35 @@ impl Command for Wrap {
description: "Wrap a list into a table with a given column name",
example: "echo [1 2 3] | wrap num",
result: Some(Value::List {
vals: vec![Value::Record {
cols: vec!["num".into()],
vals: vec![Value::test_int(1), Value::test_int(2), Value::test_int(3)],
span: Span::test_data(),
}],
vals: vec![
Value::Record {
cols: vec!["num".into()],
vals: vec![Value::test_int(1)],
span: Span::test_data(),
},
Value::Record {
cols: vec!["num".into()],
vals: vec![Value::test_int(2)],
span: Span::test_data(),
},
Value::Record {
cols: vec!["num".into()],
vals: vec![Value::test_int(3)],
span: Span::test_data(),
},
],
span: Span::test_data(),
}),
}]
}
}
#[cfg(test)]
mod test {
#[test]
fn test_examples() {
use super::Wrap;
use crate::test_examples;
test_examples(Wrap {})
}
}

View File

@ -55,12 +55,12 @@ impl Command for FromOds {
vec![
Example {
description: "Convert binary .ods data to a table",
example: "open test.txt | from ods",
example: "open --raw test.ods | from ods",
result: None,
},
Example {
description: "Convert binary .ods data to a table, specifying the tables",
example: "open test.txt | from ods -s [Spreadsheet1]",
example: "open --raw test.ods | from ods -s [Spreadsheet1]",
result: None,
},
]

View File

@ -55,12 +55,12 @@ impl Command for FromXlsx {
vec![
Example {
description: "Convert binary .xlsx data to a table",
example: "open test.txt | from xlsx",
example: "open --raw test.xlsx | from xlsx",
result: None,
},
Example {
description: "Convert binary .xlsx data to a table, specifying the tables",
example: "open test.txt | from xlsx -s [Spreadsheet1]",
example: "open --raw test.xlsx | from xlsx -s [Spreadsheet1]",
result: None,
},
]

View File

@ -39,7 +39,7 @@ impl Command for ToNuon {
fn examples(&self) -> Vec<Example> {
vec![Example {
description: "Outputs a nuon string representing the contents of this table",
description: "Outputs a nuon string representing the contents of this list",
example: "[1 2 3] | to nuon",
result: Some(Value::test_string("[1, 2, 3]")),
}]
@ -147,3 +147,13 @@ fn to_nuon(call: &Call, input: PipelineData) -> Result<String, ShellError> {
value_to_string(&v, call.head)
}
#[cfg(test)]
mod test {
#[test]
fn test_examples() {
use super::ToNuon;
use crate::test_examples;
test_examples(ToNuon {})
}
}

View File

@ -232,9 +232,15 @@ fn action(
#[cfg(test)]
mod tests {
use super::{action, ActionType, Base64Config};
use super::{action, ActionType, Base64, Base64Config};
use nu_protocol::{Span, Value};
#[test]
fn test_examples() {
use crate::test_examples;
test_examples(Base64 {})
}
#[test]
fn base64_encode_standard() {
let word = Value::string("username:password", Span::test_data());

View File

@ -77,7 +77,21 @@ On Windows, an extra 'prefix' column is added."#
Example {
description: "Parse a single path",
example: r"'C:\Users\viking\spam.txt' | path parse",
result: None,
result: Some(Value::Record {
cols: vec![
"prefix".into(),
"parent".into(),
"stem".into(),
"extension".into(),
],
vals: vec![
Value::test_string("C:"),
Value::test_string(r"C:\Users\viking"),
Value::test_string("spam"),
Value::test_string("txt"),
],
span: Span::test_data(),
}),
},
Example {
description: "Replace a complex extension",
@ -87,7 +101,21 @@ On Windows, an extra 'prefix' column is added."#
Example {
description: "Ignore the extension",
example: r"'C:\Users\viking.d' | path parse -e ''",
result: None,
result: Some(Value::Record {
cols: vec![
"prefix".into(),
"parent".into(),
"stem".into(),
"extension".into(),
],
vals: vec![
Value::test_string("C:"),
Value::test_string(r"C:\Users"),
Value::test_string("viking.d"),
Value::test_string(""),
],
span: Span::test_data(),
}),
},
Example {
description: "Parse all paths under the 'name' column",
@ -103,7 +131,15 @@ On Windows, an extra 'prefix' column is added."#
Example {
description: "Parse a path",
example: r"'/home/viking/spam.txt' | path parse",
result: None,
result: Some(Value::Record {
cols: vec!["parent".into(), "stem".into(), "extension".into()],
vals: vec![
Value::test_string("/home/viking"),
Value::test_string("spam"),
Value::test_string("txt"),
],
span: Span::test_data(),
}),
},
Example {
description: "Replace a complex extension",
@ -113,7 +149,15 @@ On Windows, an extra 'prefix' column is added."#
Example {
description: "Ignore the extension",
example: r"'/etc/conf.d' | path parse -e ''",
result: None,
result: Some(Value::Record {
cols: vec!["parent".into(), "stem".into(), "extension".into()],
vals: vec![
Value::test_string("/etc"),
Value::test_string("conf.d"),
Value::test_string(""),
],
span: Span::test_data(),
}),
},
Example {
description: "Parse all paths under the 'name' column",

View File

@ -139,7 +139,7 @@ prints out the list properly."#
description: "Render a simple list to a grid",
example: "[1 2 3 a b c] | grid",
result: Some(Value::String {
val: "1 │ 2 │ 3 │ a │ b │ c".to_string(),
val: "1 │ 2 │ 3 │ a │ b │ c\n".to_string(),
span: Span::test_data(),
}),
},
@ -147,7 +147,7 @@ prints out the list properly."#
description: "The above example is the same as:",
example: "[1 2 3 a b c] | wrap name | grid",
result: Some(Value::String {
val: "1 │ 2 │ 3 │ a │ b │ c".to_string(),
val: "1 │ 2 │ 3 │ a │ b │ c\n".to_string(),
span: Span::test_data(),
}),
},
@ -155,7 +155,7 @@ prints out the list properly."#
description: "Render a record to a grid",
example: "{name: 'foo', b: 1, c: 2} | grid",
result: Some(Value::String {
val: "foo".to_string(),
val: "foo\n".to_string(),
span: Span::test_data(),
}),
},
@ -163,7 +163,7 @@ prints out the list properly."#
description: "Render a list of records to a grid",
example: "[{name: 'A', v: 1} {name: 'B', v: 2} {name: 'C', v: 3}] | grid",
result: Some(Value::String {
val: "A │ B │ C".to_string(),
val: "A │ B │ C\n".to_string(),
span: Span::test_data(),
}),
},
@ -171,7 +171,7 @@ prints out the list properly."#
description: "Render a table with 'name' column in it to a grid",
example: "[[name patch]; [0.1.0 false] [0.1.1 true] [0.2.0 false]] | grid",
result: Some(Value::String {
val: "0.1.0 │ 0.1.1 │ 0.2.0".to_string(),
val: "0.1.0 │ 0.1.1 │ 0.2.0\n".to_string(),
span: Span::test_data(),
}),
},
@ -358,3 +358,13 @@ fn convert_to_list(
None
}
}
#[cfg(test)]
mod test {
#[test]
fn test_examples() {
use super::Griddle;
use crate::test_examples;
test_examples(Griddle {})
}
}