File size: 3,705 Bytes
f0f4f2b |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
use assert_cmd::Command;
use predicates::prelude::*;
use std::ffi::OsString;
use std::fs;
use std::path::PathBuf;
fn get_sample_path(sample_name: &str) -> OsString {
let mut path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
path.push(format!("src/tests/data/samples/{}", sample_name));
path.as_os_str().to_os_string()
}
#[test]
fn test_cli_single_file() {
let mut cmd = Command::cargo_bin("normalizer").unwrap();
cmd.args(&[get_sample_path("sample-arabic-1.txt")])
.assert()
.success()
.code(predicate::eq(0))
.stdout(predicate::str::contains("language\": \"Arabic\""));
}
#[test]
fn test_cli_version_output_success() {
let mut cmd = Command::cargo_bin("normalizer").unwrap();
cmd.args(["--version"])
.assert()
.success()
.code(predicate::eq(0))
.stdout(predicate::str::contains(
"The Real First Universal Charset Detector",
));
}
#[test]
fn test_cli_single_file_normalize() {
let mut cmd = Command::cargo_bin("normalizer").unwrap();
cmd.args(&[
get_sample_path("sample-arabic-1.txt"),
OsString::from("--normalize"),
])
.assert()
.success()
.code(predicate::eq(0))
.stdout(predicate::str::contains("language\": \"Arabic\""));
let normalized_path = &get_sample_path("sample-arabic-1.windows-1256.txt");
assert!(fs::metadata(normalized_path).is_ok());
fs::remove_file(normalized_path).expect("Normalized file is not exists");
}
#[test]
fn test_cli_single_verbose_file() {
let mut cmd = Command::cargo_bin("normalizer").unwrap();
cmd.args(&[
get_sample_path("sample-arabic-1.txt"),
OsString::from("--verbose"),
])
.assert()
.success()
.code(predicate::eq(0))
.stdout(predicate::str::contains("language\": \"Arabic\""));
}
#[test]
fn test_cli_multiple_files() {
let mut cmd = Command::cargo_bin("normalizer").unwrap();
cmd.args(&[
get_sample_path("sample-arabic-1.txt"),
get_sample_path("sample-french.txt"),
get_sample_path("sample-chinese.txt"),
])
.assert()
.success()
.code(predicate::eq(0));
}
#[test]
fn test_cli_multiple_files_with_alternative() {
let mut cmd = Command::cargo_bin("normalizer").unwrap();
cmd.args(&[
OsString::from("-a"),
get_sample_path("sample-arabic-1.txt"),
get_sample_path("sample-french.txt"),
get_sample_path("sample-chinese.txt"),
])
.assert()
.success()
.code(predicate::eq(0));
}
#[test]
fn test_cli_multiple_files_with_minimal_output() {
let mut cmd = Command::cargo_bin("normalizer").unwrap();
cmd.args(&[
OsString::from("-m"),
get_sample_path("sample-arabic-1.txt"),
get_sample_path("sample-french.txt"),
get_sample_path("sample-chinese.txt"),
])
.assert()
.success()
.code(predicate::eq(0));
}
#[test]
fn test_cli_non_existent_file() {
let mut cmd = Command::cargo_bin("normalizer").unwrap();
cmd.args(&[get_sample_path("non-exists-file.txt")])
.assert()
.failure()
.code(predicate::gt(0));
}
#[test]
fn test_cli_replace_without_normalize() {
let mut cmd = Command::cargo_bin("normalizer").unwrap();
cmd.args(&[
OsString::from("--replace"),
get_sample_path("sample-arabic-1.txt"),
])
.assert()
.failure()
.code(predicate::gt(0));
}
#[test]
fn test_cli_force_replace_without_replace() {
let mut cmd = Command::cargo_bin("normalizer").unwrap();
cmd.args(&[
OsString::from("--replace"),
get_sample_path("sample-arabic-1.txt"),
])
.assert()
.failure()
.code(predicate::gt(0));
}
|