Integration tests refactoring and visibility in them.

This commit is contained in:
Andrés N. Robalino 2019-07-16 05:28:55 -05:00
parent 3d28b50a53
commit 5ca9d307c6
35 changed files with 234 additions and 177 deletions

85
tests/commands_test.rs Normal file
View file

@ -0,0 +1,85 @@
mod helpers;
use helpers::in_directory as cwd;
#[test]
fn enter() {
nu!(output,
cwd("tests/fixtures/formats"),
r#"
enter sgml_description.json
cd glossary
cd GlossDiv
cd GlossList
cd GlossEntry
cd GlossSee
ls | echo $it
exit
"#);
assert_eq!(output, "markup");
}
#[test]
fn lines() {
nu!(output,
cwd("tests/fixtures/formats"),
"open cargo_sample.toml --raw | lines | skip-while $it != \"[dependencies]\" | skip 1 | first 1 | split-column \"=\" | get Column1 | trim | echo $it");
assert_eq!(output, "rustyline");
}
#[test]
fn open_toml() {
nu!(output,
cwd("tests/fixtures/formats"),
"open cargo_sample.toml | get package.edition | echo $it");
assert_eq!(output, "2018");
}
#[test]
fn open_json() {
nu!(output,
cwd("tests/fixtures/formats"),
"open sgml_description.json | get glossary.GlossDiv.GlossList.GlossEntry.GlossSee | echo $it");
assert_eq!(output, "markup")
}
#[test]
fn open_xml() {
nu!(output,
cwd("tests/fixtures/formats"),
"open jonathan.xml | get rss.channel.item.link | echo $it");
assert_eq!(output, "http://www.jonathanturner.org/2015/10/off-to-new-adventures.html")
}
#[test]
fn open_ini() {
nu!(output,
cwd("tests/fixtures/formats"),
"open sample.ini | get SectionOne.integer | echo $it");
assert_eq!(output, "1234")
}
#[test]
fn open_unknown_format_as_raw_single_value() {
nu!(output,
cwd("tests/fixtures/formats"),
"open skinfolds.unsupported | echo $it");
assert_eq!(output, "ABS:3.0|PEC:3.0")
}
#[test]
fn open_error_if_file_not_found() {
nu_error!(output,
cwd("tests/fixtures/formats"),
"open i_dont_exist.txt | echo $it");
assert!(output.contains("File cound not be opened"));
}

View file

@ -1 +0,0 @@
markup

View file

@ -1,10 +0,0 @@
cd tests
enter test.json
cd glossary
cd GlossDiv
cd GlossList
cd GlossEntry
cd GlossSee
ls | echo $it
exit
exit

View file

@ -1 +0,0 @@
10

View file

@ -1,3 +0,0 @@
cd tests
open test.json | get glossary.GlossDiv.GlossList.GlossEntry.Height | echo $it
exit

48
tests/filters_test.rs Normal file
View file

@ -0,0 +1,48 @@
mod helpers;
use helpers::in_directory as cwd;
#[test]
fn can_convert_table_to_json_text_and_from_json_text_back_into_table() {
nu!(output,
cwd("tests/fixtures/formats"),
"open sgml_description.json | to-json | from-json | get glossary.GlossDiv.GlossList.GlossEntry.GlossSee | echo $it");
assert_eq!(output, "markup");
}
#[test]
fn can_convert_table_to_toml_text_and_from_toml_text_back_into_table() {
nu!(output,
cwd("tests/fixtures/formats"),
"open cargo_sample.toml | to-toml | from-toml | get package.name | echo $it");
assert_eq!(output, "nu");
}
#[test]
fn can_sort_by_column() {
nu!(output,
cwd("tests/fixtures/formats"),
"open cargo_sample.toml --raw | lines | skip 1 | first 4 | split-column \"=\" | sort-by Column1 | skip 1 | first 1 | get Column1 | trim | echo $it");
assert_eq!(output, "description");
}
#[test]
fn can_split_by_column() {
nu!(output,
cwd("tests/fixtures/formats"),
"open cargo_sample.toml --raw | lines | skip 1 | first 1 | split-column \"=\" | get Column1 | trim | echo $it");
assert_eq!(output, "name");
}
#[test]
fn can_filter_by_unit_size_comparison() {
nu!(output,
cwd("tests/fixtures/formats"),
"ls | where size > 1kb | get name | trim | echo $it");
assert_eq!(output, "cargo_sample.toml");
}

View file

@ -0,0 +1 @@
"ABS:3.0|PEC:3.0"

82
tests/helpers/mod.rs Normal file
View file

@ -0,0 +1,82 @@
use std::path::PathBuf;
#[macro_export]
macro_rules! nu {
($out:ident, $cwd:expr, $commands:expr) => {
use std::error::Error;
use std::io::prelude::*;
use std::process::{Command, Stdio};
let commands = &*format!("
cd {}
{}
exit",
$cwd,
$commands);
let process = match Command::new(helpers::executable_path())
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.spawn() {
Ok(child) => child,
Err(why) => panic!("Can't run test {}", why.description()),
};
match process.stdin.unwrap().write_all(commands.as_bytes()) {
Err(why) => panic!("couldn't write to wc stdin: {}", why.description()),
Ok(_) => {}
}
let mut _s = String::new();
match process.stdout.unwrap().read_to_string(&mut _s) {
Err(why) => panic!("couldn't read stdout: {}", why.description()),
Ok(_) => {
let _s = _s.replace("\r\n", "\n");
}
}
let $out = _s.replace("\n", "");
};
}
#[macro_export]
macro_rules! nu_error {
($out:ident, $cwd:expr, $commands:expr) => {
use std::error::Error;
use std::io::prelude::*;
use std::process::{Command, Stdio};
let commands = &*format!("
cd {}
{}
exit",
$cwd,
$commands);
let mut process = Command::new(helpers::executable_path())
.stdin(Stdio::piped())
.stderr(Stdio::piped())
.spawn()
.expect("couldn't run test");
let stdin = process.stdin.as_mut().expect("couldn't open stdin");
stdin.write_all(commands.as_bytes()).expect("couldn't write to stdin");
let output = process.wait_with_output().expect("couldn't read from stderr");
let $out = String::from_utf8_lossy(&output.stderr);
};
}
pub fn executable_path() -> PathBuf {
let mut buf = PathBuf::new();
buf.push("target");
buf.push("debug");
buf.push("nu");
buf
}
pub fn in_directory(str: &str) -> &str {
str
}

View file

@ -1 +0,0 @@
11

View file

@ -1,3 +0,0 @@
cd tests
open test.json | get glossary.GlossDiv.GlossList.GlossEntry.Height | inc | echo $it
exit

View file

@ -1 +0,0 @@
markup

View file

@ -1,3 +0,0 @@
cd tests
open test.json | get glossary.GlossDiv.GlossList.GlossEntry.GlossSee | echo $it
exit

View file

@ -1 +0,0 @@
rustyline

View file

@ -1,3 +0,0 @@
cd tests
open test.toml --raw | lines | skip-while $it != "[dependencies]" | skip 1 | first 1 | split-column "=" | get Column1 | trim | echo $it
exit

View file

@ -1 +0,0 @@
1234

View file

@ -1,3 +0,0 @@
cd tests
open test.ini | get SectionOne.integer | echo $it
exit

View file

@ -1 +0,0 @@
markup

View file

@ -1,3 +0,0 @@
cd tests
open test.json | get glossary.GlossDiv.GlossList.GlossEntry.GlossSee | echo $it
exit

View file

@ -1 +0,0 @@
2018

View file

@ -1,3 +0,0 @@
cd tests
open test.toml | get package.edition | echo $it
exit

View file

@ -1 +0,0 @@
http://www.jonathanturner.org/2015/10/off-to-new-adventures.html

View file

@ -1,3 +0,0 @@
cd tests
open test.xml | get rss.channel.item.link | echo $it
exit

View file

@ -1 +0,0 @@
description

View file

@ -1,3 +0,0 @@
cd tests
open test.toml --raw | lines | skip 1 | first 4 | split-column "=" | sort-by Column1 | skip 1 | first 1 | get Column1 | trim | echo $it
exit

View file

@ -1 +0,0 @@
name

View file

@ -1,3 +0,0 @@
cd tests
open test.toml --raw | lines | skip 1 | first 1 | split-column "=" | get Column1 | trim | echo $it
exit

View file

@ -1,120 +1,21 @@
#[cfg(test)]
mod tests {
use std::error::Error;
use std::io::prelude::*;
use std::path::PathBuf;
use std::process::{Command, Stdio};
mod helpers;
fn test_helper(test_name: &str) {
let mut baseline_path = PathBuf::new();
baseline_path.push("tests");
baseline_path.push(test_name);
baseline_path.set_extension("out");
use helpers::in_directory as cwd;
let mut txt_path = PathBuf::new();
txt_path.push("tests");
txt_path.push(test_name);
txt_path.set_extension("txt");
let executable = {
let mut buf = PathBuf::new();
buf.push("target");
buf.push("debug");
buf.push("nu");
buf
};
let process = match Command::new(executable)
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.spawn()
{
Ok(process) => process,
Err(why) => panic!("Can't run test {}", why.description()),
};
let baseline_out = std::fs::read_to_string(baseline_path).unwrap();
let baseline_out = baseline_out.replace("\r\n", "\n");
let input_commands = std::fs::read_to_string(txt_path).unwrap();
match process.stdin.unwrap().write_all(input_commands.as_bytes()) {
Err(why) => panic!("couldn't write to wc stdin: {}", why.description()),
Ok(_) => {}
}
let mut s = String::new();
match process.stdout.unwrap().read_to_string(&mut s) {
Err(why) => panic!("couldn't read stdout: {}", why.description()),
Ok(_) => {
let s = s.replace("\r\n", "\n");
assert_eq!(s, baseline_out);
}
}
}
#[test]
fn open_toml() {
test_helper("open_toml");
}
#[test]
fn open_json() {
test_helper("open_json");
}
#[test]
fn open_xml() {
test_helper("open_xml");
}
#[test]
fn open_ini() {
test_helper("open_ini");
}
#[test]
fn json_roundtrip() {
test_helper("json_roundtrip");
}
#[test]
fn toml_roundtrip() {
test_helper("toml_roundtrip");
}
#[test]
fn sort_by() {
test_helper("sort_by");
}
#[test]
fn split() {
test_helper("split");
}
#[test]
fn enter() {
test_helper("enter");
}
#[test]
fn lines() {
test_helper("lines");
}
#[test]
fn external_num() {
test_helper("external_num");
}
#[test]
fn unit() {
test_helper("unit");
}
#[test]
fn inc_plugin() {
test_helper("inc_plugin");
}
#[test]
fn external_num() {
nu!(output,
cwd("tests/fixtures/formats"),
"open sgml_description.json | get glossary.GlossDiv.GlossList.GlossEntry.Height | echo $it");
assert_eq!(output, "10");
}
#[test]
fn inc_plugin() {
nu!(output,
cwd("tests/fixtures/formats"),
"open sgml_description.json | get glossary.GlossDiv.GlossList.GlossEntry.Height | inc | echo $it");
assert_eq!(output, "11");
}

View file

@ -1 +0,0 @@
nu

View file

@ -1,3 +0,0 @@
cd tests
open test.toml | to-toml | from-toml | get package.name | echo $it
exit

View file

@ -1 +0,0 @@
test.toml

View file

@ -1,4 +0,0 @@
cd tests
cd dirtest
ls | where size > 1kb | get name | trim | echo $it
exit