Skip to content
Empty file.
323 changes: 323 additions & 0 deletions src/test/java/org/apache/commons/text/myWordsUtilsTests.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,323 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.text;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;


import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvFileSource;
import org.junit.jupiter.params.provider.CsvSource;
import org.junit.jupiter.api.Test;

public class myWordsUtilsTests {
//variables for whitespaced capitalization
private static String typicalSentence;
private static String[] whitespaceSelective;
private static String[] expectedWhitespaceSelective;

//variables for delimited capitalization
private static String delimiters;
private static String vowels;
private static String delimitedSentence;
private static String[] expectedDelimitedSentences;
private static String[] expectedVoweledSentences;

private static String sentenceForAbreviation;

private static String[] setencesForContainsAllWords;
private static String[] sentencesForSwapCase;
private static String accentedVowels;

private static String[] sentencesForInitals;

@BeforeAll
public static void setUp(){
typicalSentence = "The quick brown fox jumps over the lazy dog";
delimiters = "!@#$%^&*(";
vowels= "aeiou";
delimitedSentence = "!the @quick #brown $fox %jumps ^over &the *lazy (dog";
whitespaceSelective = new String[]{" thequickbrownfoxjumpsoverthelazydog", "the quickbrownfoxjumpsoverthelazydog", "thequick brownfoxjumpsoverthelazydog", "thequickbrown foxjumpsoverthelazydog", "thequickbrownfox jumpsoverthelazydog", "thequickbrownfoxjumps overthelazydog", "thequickbrownfoxjumpsover thelazydog", "thequickbrownfoxjumpsoverthe lazydog", "thequickbrownfoxjumpsoverthelazy dog", "thequickbrownfoxjumpsoverthelazydog "};
expectedWhitespaceSelective = new String[]{" Thequickbrownfoxjumpsoverthelazydog", "The Quickbrownfoxjumpsoverthelazydog", "Thequick Brownfoxjumpsoverthelazydog", "Thequickbrown Foxjumpsoverthelazydog", "Thequickbrownfox Jumpsoverthelazydog", "Thequickbrownfoxjumps Overthelazydog", "Thequickbrownfoxjumpsover Thelazydog", "Thequickbrownfoxjumpsoverthe Lazydog", "Thequickbrownfoxjumpsoverthelazy Dog", "Thequickbrownfoxjumpsoverthelazydog "};
expectedDelimitedSentences = new String[]{"!The @quick #brown $fox %jumps ^over &the *lazy (dog", "!the @Quick #brown $fox %jumps ^over &the *lazy (dog", "!the @quick #Brown $fox %jumps ^over &the *lazy (dog", "!the @quick #brown $Fox %jumps ^over &the *lazy (dog", "!the @quick #brown $fox %Jumps ^over &the *lazy (dog", "!the @quick #brown $fox %jumps ^Over &the *lazy (dog", "!the @quick #brown $fox %jumps ^over &The *lazy (dog", "!the @quick #brown $fox %jumps ^over &the *Lazy (dog", "!the @quick #brown $fox %jumps ^over &the *lazy (Dog"};
expectedVoweledSentences = new String[]{"!the @quick #brown $fox %jumps ^over &the *laZy (dog", "!the @quick #brown $fox %jumps ^oveR &the *lazy (dog", "!the @quiCk #brown $fox %jumps ^over &the *lazy (dog", "!the @quick #broWn $foX %jumps ^oVer &the *lazy (doG", "!the @quIck #brown $fox %juMps ^over &the *lazy (dog"};
sentenceForAbreviation = "One day work will end, play is forever! One day play will end, work is forever!";
setencesForContainsAllWords = new String[] {"\"\"", "\"Writing\" tests to ensure code is working properly", "Writing tests to ensure code is \\working\\ properly", "\'", "Writing tests to ensure code is working properly", "03 1985", "!@# $%^ ^&*"};
sentencesForSwapCase = new String[] {"\bWriting\btests\bto\bensure\bcode\bis\bworking\bproperly", "Writing tests to ensure code is working properly", "20 09 1555"};
accentedVowels = "�����";
sentencesForInitals = new String[]{"\b\n\t","\b \n \t", "&^%", "& ^ %", "[|]", "[ | ]", "\b[\n|\t]", "I'm gonna go grab some coffee.", "03 03 2023"};

}

//partiton testing
@Test
public void whitespaceCaptializeTestNoLetters() { assertEquals("!@#$%^&*()_+", WordUtils.capitalize("!@#$%^&*()_+")); }

@Test
public void whitespaceCapitalizeTestLettersPrefixedWithSymbols(){ assertEquals("!the @quick #brown $fox %jumps ^over &the *lazy (dog", WordUtils.capitalize(delimitedSentence)); }

@Test
public void whitespaceCapitalizeTestTypical(){ assertEquals("The Quick Brown Fox Jumps Over The Lazy Dog", WordUtils.capitalize(typicalSentence)); }

@Test
public void delimiterCapitalizeTestWithNoDelimiters(){ assertEquals("!the @quick #brown $fox %jumps ^over &the *lazy (dog", WordUtils.capitalize(delimitedSentence, null)); }


@Test
public void delimiterCapitalizeTestWithSymbolDelimiters(){
for (int x = 0; x < delimiters.length()-1; x++) {
assertEquals(expectedDelimitedSentences[x], WordUtils.capitalize(delimitedSentence, delimiters.charAt(x)));
}
}

@Test
public void delimiterCapitalizeTestWithVowelDelimiters(){
for (int x = 0; x < vowels.length()-1; x++) {
assertEquals(expectedVoweledSentences[x], WordUtils.capitalize(delimitedSentence, vowels.charAt(x)));
}
}

//robust worst case boundary testing

// lower: min to min+1, upper: min-1
@Test
public void abbreviateTestLowestExtremeBounds(){

assertEquals("One... I'll find some way to end this.", WordUtils.abbreviate(sentenceForAbreviation, -1, -1, "... I'll find some way to end this." ));
assertEquals("One...You will relive every key mistake you've ever made in your life.", WordUtils.abbreviate(sentenceForAbreviation, 0, -1, "...You will relive every key mistake you've ever made in your life."));
assertEquals("One...We are indeed close.", WordUtils.abbreviate(sentenceForAbreviation, 1, -1, "...We are indeed close."));
}

// lower: min-1 to min+1, upper: min
@Test
public void abbrevateTestLowestBounds(){
assertEquals("...Where did the sentence go?", WordUtils.abbreviate(sentenceForAbreviation, -1, 0, "...Where did the sentence go?" ));
assertEquals("...There's nothing there.", WordUtils.abbreviate(sentenceForAbreviation, 0, 0, "...There's nothing there."));
assertThrows(IllegalArgumentException.class, () -> WordUtils.abbreviate(sentenceForAbreviation, 1, 0, "...Oops, that's an exception."));
}

// lower: min-1 to min+1, upper: min+1
@Test
public void abbrevateTest2ndLowestBounds(){
assertEquals("O...Oh!", WordUtils.abbreviate(sentenceForAbreviation, -1, 1, "...Oh!" ));
assertEquals("O, Oh...", WordUtils.abbreviate(sentenceForAbreviation, 0, 1, ", Oh..."));
assertEquals("O...what about U?", WordUtils.abbreviate(sentenceForAbreviation, 1, 1, "...what about U?"));
}

// lower: min-1 to min+1, upper: typical
@Test
public void abbrevateTestTypicalLowestBounds(){
assertEquals("One...huh?", WordUtils.abbreviate(sentenceForAbreviation, -1, 20, "...huh?" ));
assertEquals("One...what?", WordUtils.abbreviate(sentenceForAbreviation, 0, 'Z', "...what?"));
assertEquals("One...why?", WordUtils.abbreviate(sentenceForAbreviation, 1, 'z', "...why?"));
}

// lower: min-1 to min+1, upper: max-1
@Test
public void abbrevateTestLowestWith2ndHighest(){
assertEquals("One", WordUtils.abbreviate(sentenceForAbreviation, -1, Integer.MAX_VALUE-1, "" ));
assertEquals("One", WordUtils.abbreviate(sentenceForAbreviation, 0, Integer.MAX_VALUE-1, ""));
assertEquals("One", WordUtils.abbreviate(sentenceForAbreviation, 1, Integer.MAX_VALUE-1, ""));
}

// lower: min-1 to min+1, upper: max
@Test
public void abbrevateTestLowestWithHighest(){
assertEquals("One", WordUtils.abbreviate(sentenceForAbreviation, -1, Integer.MAX_VALUE, "" ));
assertEquals("One", WordUtils.abbreviate(sentenceForAbreviation, 0, Integer.MAX_VALUE, ""));
assertEquals("One", WordUtils.abbreviate(sentenceForAbreviation, 1, Integer.MAX_VALUE, ""));
}

// lower: typical, upper: min-1 to min+1
@Test
public void abbrevateTestTypicalWithLowest(){
assertEquals("One day work will end, play is forever!... I'll find some way to end this.", WordUtils.abbreviate(sentenceForAbreviation, '!', -1, "... I'll find some way to end this." ));
assertThrows(IllegalArgumentException.class, () -> WordUtils.abbreviate(sentenceForAbreviation, 'A', 0, "#-%" ));
assertThrows(IllegalArgumentException.class, () -> WordUtils.abbreviate(sentenceForAbreviation, 'a', 1, "Nothing seemed to be abbreviated here." ));
}

// lower: typical, upper: typical
@Test
public void abbrevateTestTypical(){ assertEquals("One day work will end, play is forever! One day play will end, work#-%", WordUtils.abbreviate(sentenceForAbreviation, 'A', 'Z', "#-%" )); }

// lower: typical, upper: max-1 to max+1
@Test
public void abbrevateTestTypicalwithHighest(){
assertEquals("One day work will end, play is forever! One day play will end, work is forever!", WordUtils.abbreviate(sentenceForAbreviation, 'a', Integer.MAX_VALUE-1, " Nothing seemed to be abbreviated here." ));
assertEquals("One day work will end, play is forever! One day play will end, work is forever!", WordUtils.abbreviate(sentenceForAbreviation, 'a', Integer.MAX_VALUE, " Nothing seemed to be abbreviated here." ));
assertThrows(IllegalArgumentException.class, () -> WordUtils.abbreviate(sentenceForAbreviation, 'a', Integer.MAX_VALUE+1, "Nothing seemed to be abbreviated here." ));
}

// lower: max-1 to max+1, upper: min-1
@Test
public void abbrevateTestMaximumWithLowestExtreme(){

assertEquals("One day work will end, play is forever! One day play will end, work is forever!", WordUtils.abbreviate(sentenceForAbreviation, Integer.MAX_VALUE-1, -1, "" ));
assertEquals("One day work will end, play is forever! One day play will end, work is forever!", WordUtils.abbreviate(sentenceForAbreviation, Integer.MAX_VALUE, -1, ""));
assertEquals("One...We are indeed close.", WordUtils.abbreviate(sentenceForAbreviation, Integer.MAX_VALUE+1, -1, "...We are indeed close."));
}

// lower: max-1 to max+1, upper: min
@Test
public void abbrevateTestMaximumWithLowest(){

assertThrows(IllegalArgumentException.class, () -> WordUtils.abbreviate(sentenceForAbreviation, Integer.MAX_VALUE-1, 0, "" ));
assertThrows(IllegalArgumentException.class, () -> WordUtils.abbreviate(sentenceForAbreviation, Integer.MAX_VALUE, 0, ""));
assertEquals("...We are indeed close.", WordUtils.abbreviate(sentenceForAbreviation, Integer.MAX_VALUE+1, 0, "...We are indeed close."));
}

// lower: max-1 to max+1, upper: typical
@Test
public void abbrevateTestHighestWithTypical(){
assertThrows(IllegalArgumentException.class, () -> WordUtils.abbreviate(sentenceForAbreviation, Integer.MAX_VALUE-1, 20, "" ));
assertThrows(IllegalArgumentException.class, () -> WordUtils.abbreviate(sentenceForAbreviation, Integer.MAX_VALUE, 20, ""));
assertEquals("One...We are indeed close.", WordUtils.abbreviate(sentenceForAbreviation, Integer.MAX_VALUE+1, 20, "...We are indeed close."));
}

// lower: max-1 to max+1, upper: max-1
@Test
public void abbrevateTest2ndHighestBounds(){
assertEquals("One day work will end, play is forever! One day play will end, work is forever!", WordUtils.abbreviate(sentenceForAbreviation, Integer.MAX_VALUE-1, Integer.MAX_VALUE-1, "" ));
assertThrows(IllegalArgumentException.class, () -> WordUtils.abbreviate(sentenceForAbreviation, Integer.MAX_VALUE, Integer.MAX_VALUE-1, ""));
assertEquals("One...We are indeed close.", WordUtils.abbreviate(sentenceForAbreviation, Integer.MAX_VALUE+1, Integer.MAX_VALUE-1, "...We are indeed close."));
}

// lower: max-1 to max+1, upper: max
@Test
public void abbrevateTestHighestBounds(){
assertEquals("One day work will end, play is forever! One day play will end, work is forever!", WordUtils.abbreviate(sentenceForAbreviation, Integer.MAX_VALUE-1, Integer.MAX_VALUE, "" ));
assertEquals("One day work will end, play is forever! One day play will end, work is forever!",WordUtils.abbreviate(sentenceForAbreviation, Integer.MAX_VALUE, Integer.MAX_VALUE, ""));
assertEquals("One...We are indeed close.", WordUtils.abbreviate(sentenceForAbreviation, Integer.MAX_VALUE+1, Integer.MAX_VALUE, "...We are indeed close."));
}

// lower: max-1 to max+1, upper: max+1
@Test
public void abbreviateTestHighestExtremeBounds(){
assertThrows(IllegalArgumentException.class, () -> WordUtils.abbreviate(sentenceForAbreviation, Integer.MAX_VALUE-1, Integer.MAX_VALUE+1, "" ));
assertThrows(IllegalArgumentException.class, () -> WordUtils.abbreviate(sentenceForAbreviation, Integer.MAX_VALUE, Integer.MAX_VALUE+1, ""));
assertThrows(IllegalArgumentException.class, () -> WordUtils.abbreviate(sentenceForAbreviation, Integer.MAX_VALUE+1, Integer.MAX_VALUE+1, "...We are indeed close."));
}


//partiton testing
@Test
public void containsAllWordsTestQuoteEscape(){
assertEquals(false, WordUtils.containsAllWords(setencesForContainsAllWords[0], "\"","\""));
}

@Test
public void containsAllWordsTestSentenceAndQuotes(){
assertEquals(false, WordUtils.containsAllWords(setencesForContainsAllWords[1], "\"Writing\"", "tests", "to", "ensure", "code", "is", "working", "lazy", "properly"));
}

@Test
public void containsAllWordsTestNotScaningAll(){
assertEquals(false, WordUtils.containsAllWords(setencesForContainsAllWords[2], "tests", "code", "\\working\\"));

}

@Test
public void containsAllWordsTestEscapeApostrophe(){
assertEquals(false, WordUtils.containsAllWords(setencesForContainsAllWords[3], "\'"));
}

@Test
public void containsAllWordsTypical(){
assertEquals(true, WordUtils.containsAllWords(setencesForContainsAllWords[4], "Writing", "tests", "to", "ensure", "code", "is", "working", "properly"));
}

@Test
public void containsAllWordsNumbers(){
assertEquals(true, WordUtils.containsAllWords(setencesForContainsAllWords[5], "03", "1985"));

}

@Test
public void containsAllWordsSymbols(){
assertEquals(false, WordUtils.containsAllWords(setencesForContainsAllWords[6], "!@#", "$%^", "^&*"));

}

@Test
public void swapCaseTestSymbols(){
assertEquals(delimiters, WordUtils.swapCase(delimiters));
}

@Test
public void swapCaseTestEscapeSequencesInSentence(){
assertEquals("\bwRITING\bTESTS\bTO\bENSURE\bCODE\bIS\bWORKING\bPROPERLY", WordUtils.swapCase(sentencesForSwapCase[0]));
}

@Test
public void swapCaseTestTypical(){
assertEquals("wRITING TESTS TO ENSURE CODE IS WORKING PROPERLY", WordUtils.swapCase(sentencesForSwapCase[1]));
}

@Test
public void swapCaseTestNumbers(){
assertEquals(sentencesForSwapCase[2], WordUtils.swapCase(sentencesForSwapCase[2]));
}

@Test
public void swapCaseTestAccentedVowels(){
assertEquals("�����", WordUtils.swapCase(accentedVowels));
}

@Test
public void initalsTestEscapeCharacters(){ assertEquals("\b", WordUtils.initials(sentencesForInitals[0])); }

@Test
public void initalsTestEscapeCharactersSpaces(){ assertEquals("\b", WordUtils.initials(sentencesForInitals[1])); }

@Test
public void initalsTestSymbols(){ assertEquals("&", WordUtils.initials(sentencesForInitals[2])); }

@Test
public void initalsTestSymbolsSpaces(){ assertEquals("&^%", WordUtils.initials(sentencesForInitals[3])); }

@Test
public void initalsTestBracketLine(){ assertEquals("[", WordUtils.initials(sentencesForInitals[4])); }

@Test
public void initalsTestBracketLineSpaces(){ assertEquals("[|]", WordUtils.initials(sentencesForInitals[5])); }

@Test
public void initalsTestCombinationOfLastThree(){ assertEquals("\b|]", WordUtils.initials(sentencesForInitals[6])); }

@Test
public void initalsTestTypical(){ assertEquals("Igggsc", WordUtils.initials(sentencesForInitals[7])); }

@Test
public void initalsTestNumbers(){ assertEquals("002", WordUtils.initials(sentencesForInitals[8])); }













}