Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package bg.sofia.uni.fmi.mjt.spellchecker;
- import org.junit.AfterClass;
- import org.junit.BeforeClass;
- import org.junit.Test;
- import java.io.IOException;
- import java.io.Reader;
- import java.io.StringReader;
- import java.io.StringWriter;
- import java.io.Writer;
- import java.util.List;
- import static org.junit.Assert.assertTrue;
- public class NaiveSpellCheckerTest {
- private final static String METADATA_HEADER = "= = = Metadata = = =";
- private final static String FINDINGS_HEADER = "= = = Findings = = =";
- private static Reader dictionaryReader;
- private static Reader stopwordsReader;
- private static NaiveSpellChecker testSpellChecker;
- @BeforeClass
- public static void setUp() {
- dictionaryReader = new StringReader(String.join(System.lineSeparator(),
- List.of("cat", "dog", "bird", "hello")));
- stopwordsReader = new StringReader(String.join(System.lineSeparator(), List.of("a", "am", "me", "is")));
- testSpellChecker = new NaiveSpellChecker(dictionaryReader, stopwordsReader);
- }
- @AfterClass
- public static void tearDown() {
- try {
- dictionaryReader.close();
- stopwordsReader.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- @Test
- public void testAnalyze() {
- //36 characters, 4 words, 2 spelling-issues
- String inputString = "-=-=-CaT is beautiful" //19 characters, 2 words, 1 issue (beautiful not in dictionary)
- + System.lineSeparator()
- + "//hallo, ==bird=-."; //17 characters, 2 words, 1 spelling-issue (hallo)
- String outputString;
- Reader input = new StringReader(inputString);
- Writer output = new StringWriter();
- testSpellChecker.analyze(input, output, 1);
- outputString = output.toString();
- try {
- input.close();
- output.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- assertTrue(outputString.contains(inputString));
- assertTrue(outputString.contains(METADATA_HEADER) && outputString.contains(FINDINGS_HEADER));
- assertTrue(outputString.contains("36 characters, 4 words, 2 spelling issue(s) found"));
- assertTrue(outputString.contains("Line #1, {beautiful} - Possible suggestions are {dog}"));
- assertTrue(outputString.contains("Line #2, {//hallo} - Possible suggestions are {hello}"));
- }
- @Test
- public void testAnalyzeRemovesPunctuationMarksInFindingsSection() {
- //36 characters, 4 words, 2 spelling-issues
- String inputString = "helllo,.?!:;";
- String outputString;
- Reader input = new StringReader(inputString);
- Writer output = new StringWriter();
- testSpellChecker.analyze(input, output, 1);
- outputString = output.toString();
- try {
- input.close();
- output.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- assertTrue(outputString.contains("{helllo}"));
- }
- @Test(expected = IllegalArgumentException.class)
- public void testAnalyzeWithNegativeNumberOfSuggestions() {
- testSpellChecker.analyze(null, null, -1);
- }
- @Test(expected = IllegalArgumentException.class)
- public void testAnalyzeWithGreaterThanDictionarySizeNumberOfSuggestions() {
- testSpellChecker.analyze(null, null, 100);
- }
- @Test(expected = IllegalArgumentException.class)
- public void testFindClosestWordsWithNullArgumentForWord() {
- testSpellChecker.findClosestWords(null, 1);
- }
- @Test(expected = IllegalArgumentException.class)
- public void testFindClosestWordsWithZeroNumberOfSuggestions() {
- testSpellChecker.findClosestWords("word", 0);
- }
- @Test(expected = IllegalArgumentException.class)
- public void testFindClosestWordsWithGreaterThanDictionarySizeNumberOfSuggestions() {
- testSpellChecker.findClosestWords("word", 100);
- }
- @Test
- public void testMetadataWithSingleLetterWords() {
- String inputString = "A B C " //A is contained in stopwords, so it is ignored in words count
- + "X Y Z";
- Reader input = new StringReader(inputString);
- Metadata actual = testSpellChecker.metadata(input);
- try {
- input.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- assertTrue(actual.words() == 5 && actual.characters() == 6 && actual.mistakes() == 0);
- }
- @Test
- public void testMetadataWithRandomWords() {
- //42 characters, 5 words, 2 spelling-issue (hallo)
- String inputString = ";;12fasd hello ===bird--, " //23 characters, 3 words, 1 spelling-issue (hallo)
- + " fds //cat==- ===-A-=="; //19 characters, 2 words, 1 spelling-issue (hallo)
- Reader input = new StringReader(inputString);
- Metadata actual = testSpellChecker.metadata(input);
- try {
- input.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- assertTrue(actual.words() == 5 && actual.characters() == 42 && actual.mistakes() == 2);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement