Skip to content

Commit

Permalink
Merge pull request #47 from eiennohito/cleanup_tests
Browse files Browse the repository at this point in the history
Change panics of tests to error checks
  • Loading branch information
mh-northlander authored Sep 29, 2021
2 parents 81c3708 + 3882c61 commit 98e9846
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 131 deletions.
16 changes: 16 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions sudachi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ unicode-normalization = "0.1.19"

[dev-dependencies]
tempfile = "3"
claim = "0.5"

# Plugins for tests
default_input_text = { path = "../plugin/input_text/default_input_text" }
Expand Down
48 changes: 25 additions & 23 deletions sudachi/src/dic/character_category.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@
* limitations under the License.
*/

use multiset::HashMultiSet;
use std::cmp::{Ordering, Reverse};
use std::collections::BinaryHeap;
use std::fs;
use std::io::{BufRead, BufReader};
use std::iter::FromIterator;
use std::path::PathBuf;
use std::u32;

use multiset::HashMultiSet;
use thiserror::Error;

use crate::dic::category_type::{CategoryType, CategoryTypes};
Expand Down Expand Up @@ -272,11 +273,14 @@ impl CharacterCategory {

#[cfg(test)]
mod tests {
use super::*;
use std::char;
use std::io::{Seek, SeekFrom, Write};

use claim::assert_matches;
use tempfile::tempfile;

use super::*;

const TEST_RESOURCE_DIR: &str = "./tests/resources/";
const TEST_CHAR_DEF_FILE: &str = "char.def";

Expand Down Expand Up @@ -488,35 +492,33 @@ mod tests {
}

#[test]
#[should_panic]
fn read_character_definition_with_invalid_format() {
let mut file = tempfile().expect("Failed to get temporary file");
writeln!(file, "0x0030..0x0039").unwrap();
file.flush().expect("Failed to flush");
file.seek(SeekFrom::Start(0)).expect("Failed to seek");
let _ranges = CharacterCategory::read_character_definition(BufReader::new(file))
.expect("Failed to read tmp char def file");
let data = "0x0030..0x0039";
let result = CharacterCategory::read_character_definition(data.as_bytes());
assert_matches!(
result,
Err(SudachiError::InvalidCharacterCategory(
Error::InvalidFormat(0)
))
);
}

#[test]
#[should_panic]
fn read_character_definition_with_invalid_range() {
let mut file = tempfile().expect("Failed to get temporary file");
writeln!(file, "0x0030..0x0029 NUMERIC").unwrap();
file.flush().expect("Failed to flush");
file.seek(SeekFrom::Start(0)).expect("Failed to seek");
let _ranges = CharacterCategory::read_character_definition(BufReader::new(file))
.expect("Failed to read tmp char def file");
let data = "0x0030..0x0029 NUMERIC";
let result = CharacterCategory::read_character_definition(data.as_bytes());
assert_matches!(
result,
Err(SudachiError::InvalidCharacterCategory(
Error::InvalidFormat(0)
))
);
}

#[test]
#[should_panic]
fn read_character_definition_with_invalid_type() {
let mut file = tempfile().expect("Failed to get temporary file");
writeln!(file, "0x0030..0x0039 FOO").unwrap();
file.flush().expect("Failed to flush");
file.seek(SeekFrom::Start(0)).expect("Failed to seek");
let _ranges = CharacterCategory::read_character_definition(BufReader::new(file))
.expect("Failed to read tmp char def file");
let data = "0x0030..0x0039 FOO";
let result = CharacterCategory::read_character_definition(data.as_bytes());
assert_matches!(result, Err(SudachiError::InvalidCharacterCategory(Error::InvalidCategoryType(0, s))) if s == "FOO");
}
}
2 changes: 1 addition & 1 deletion sudachi/src/plugin/input_text/default_input_text/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ impl DefaultInputTextPlugin {
/// Plugin replaces the first by the second
/// Same target string cannot be defined multiple times
/// Empty or line starts with "#" will be ignored
fn read_rewrite_lists(&mut self, reader: BufReader<fs::File>) -> SudachiResult<()> {
fn read_rewrite_lists<T: BufRead>(&mut self, reader: T) -> SudachiResult<()> {
let mut ignore_normalize_set = HashSet::new();
let mut key_lengths = HashMap::new();
let mut replace_char_map = HashMap::new();
Expand Down
53 changes: 17 additions & 36 deletions sudachi/src/plugin/input_text/default_input_text/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@
* limitations under the License.
*/

use super::*;
use claim::assert_matches;
use serde_json::{Map, Value};
use std::io::{Seek, SeekFrom, Write};
use tempfile::tempfile;

use crate::config::Config;
use crate::dic::grammar::Grammar;
use crate::input_text::Utf8InputTextBuilder;

use super::*;

const TEST_RESOURCE_DIR: &str = "tests/resources/";
const ORIGINAL_TEXT: &str = "ÂBΓД㈱ガウ゛⼼Ⅲ";
const NORMALIZED_TEXT: &str = "âbγд(株)ガヴ⼼ⅲ";
Expand Down Expand Up @@ -79,49 +79,30 @@ fn after_rewrite() {
}

#[test]
#[should_panic]
fn invalid_format_ignorelist() {
let mut file = tempfile().expect("Failed to get temporary file");
writeln!(file, "# there are two characters in ignore list").unwrap();
writeln!(file, "12").unwrap();
file.flush().expect("Failed to flush");
file.seek(SeekFrom::Start(0)).expect("Failed to seek");

fn ignore_list_two_chars() {
let data = "12";
let mut plugin = DefaultInputTextPlugin::default();
plugin
.read_rewrite_lists(BufReader::new(file))
.expect("Failed to read rewrite lists");
let result = plugin.read_rewrite_lists(data.as_bytes());
assert_matches!(result, Err(SudachiError::InvalidDataFormat(0, _)))
}

#[test]
#[should_panic]
fn invalid_format_replacelist() {
let mut file = tempfile().expect("Failed to get temporary file");
writeln!(file, "# there are three columns in replace list").unwrap();
writeln!(file, "12 21 31").unwrap();
file.flush().expect("Failed to flush");
file.seek(SeekFrom::Start(0)).expect("Failed to seek");

fn replace_list_three_entries() {
let data = "12 21 31";
let mut plugin = DefaultInputTextPlugin::default();
plugin
.read_rewrite_lists(BufReader::new(file))
.expect("Failed to read rewrite lists");
let result = plugin.read_rewrite_lists(data.as_bytes());
assert_matches!(result, Err(SudachiError::InvalidDataFormat(0, _)))
}

#[test]
#[should_panic]
fn duplicated_lines_replacelist() {
let mut file = tempfile().expect("Failed to get temporary file");
writeln!(file, "# there are a duplicated replacement.").unwrap();
writeln!(file, "12 21").unwrap();
writeln!(file, "12 31").unwrap();
file.flush().expect("Failed to flush");
file.seek(SeekFrom::Start(0)).expect("Failed to seek");
fn replace_list_duplicates() {
let data = "
12 31
12 31";

let mut plugin = DefaultInputTextPlugin::default();
plugin
.read_rewrite_lists(BufReader::new(file))
.expect("Failed to read rewrite lists");
let result = plugin.read_rewrite_lists(data.as_bytes());
assert_matches!(result, Err(SudachiError::InvalidDataFormat(2, _)));
}

fn build_mock_bytes() -> Vec<u8> {
Expand Down
16 changes: 0 additions & 16 deletions sudachi/src/plugin/input_text/prolonged_sound_mark/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,6 @@ fn combined_continuous_prolonged_sound_marks_at_end() {
let expected = b"\xe3\x82\xb9\xe3\x83\xbc\xe3\x83\x91\xe3\x83\xbc";
assert_eq!(expected, text.modified.as_bytes());

for i in 0..text.modified.as_bytes().len() + 1 {
println!("{}, {}", i, text.get_original_index(i));
}

assert_eq!(0, text.get_original_index(0));
assert_eq!(3, text.get_original_index(3));
assert_eq!(6, text.get_original_index(6));
Expand All @@ -87,10 +83,6 @@ fn combine_continuous_prolonged_sound_marks_multi_times() {
let expected = b"\xe3\x82\xa8\xe3\x83\xbc\xe3\x83\x93\xe3\x83\xbc\xe3\x82\xb7\xe3\x83\xbc";
assert_eq!(expected, text.modified.as_bytes());

for i in 0..text.modified.as_bytes().len() + 1 {
println!("{}, {}", i, text.get_original_index(i));
}

assert_eq!(0, text.get_original_index(0));
assert_eq!(3, text.get_original_index(3));
assert_eq!(9, text.get_original_index(6));
Expand All @@ -116,10 +108,6 @@ fn combine_continuous_prolonged_sound_marks_multi_symbol_types() {
let expected = b"\xe3\x82\xa8\xe3\x83\xbc\xe3\x83\x93\xe3\x83\xbc\xe3\x82\xb7\xe3\x83\xbc";
assert_eq!(expected, text.modified.as_bytes());

for i in 0..text.modified.as_bytes().len() + 1 {
println!("{}, {}", i, text.get_original_index(i));
}

assert_eq!(0, text.get_original_index(0));
assert_eq!(3, text.get_original_index(3));
assert_eq!(9, text.get_original_index(6));
Expand All @@ -146,10 +134,6 @@ fn combine_continuous_prolonged_sound_marks_multi_mixed_symbol_types() {
let expected = b"\xe3\x82\xa8\xe3\x83\xbc\xe3\x83\x93\xe3\x83\xbc\xe3\x82\xb7\xe3\x83\xbc";
assert_eq!(expected, text.modified.as_bytes());

for i in 0..text.modified.as_bytes().len() + 1 {
println!("{}, {}", i, text.get_original_index(i));
}

assert_eq!(0, text.get_original_index(0));
assert_eq!(3, text.get_original_index(3));
assert_eq!(9, text.get_original_index(6));
Expand Down
8 changes: 4 additions & 4 deletions sudachi/src/plugin/oov/mecab_oov/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ impl MeCabOovPlugin {
/// Loads character category definition
///
/// See resources/char.def for the syntax
fn read_character_property(
reader: BufReader<fs::File>,
fn read_character_property<T: BufRead>(
reader: T,
) -> SudachiResult<HashMap<CategoryType, CategoryInfo>> {
let mut categories = HashMap::new();
for (i, line) in reader.lines().enumerate() {
Expand Down Expand Up @@ -107,8 +107,8 @@ impl MeCabOovPlugin {
/// Load OOV definition
///
/// Each line contains: CategoryType, left_id, right_id, cost, and pos
fn read_oov(
reader: BufReader<fs::File>,
fn read_oov<T: BufRead>(
reader: T,
categories: &HashMap<CategoryType, CategoryInfo>,
grammar: &Grammar,
) -> SudachiResult<HashMap<CategoryType, Vec<OOV>>> {
Expand Down
Loading

0 comments on commit 98e9846

Please sign in to comment.