Practice with string and loop

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. package JavaStringCheese;
  2. import org.junit.Test;
  3. import static org.junit.Assert.*;
  4. public class StringParserTest {
  5. @Test
  6. public void upperCaseString() {
  7. //Given
  8. String string1 = "test string";
  9. String expected = "TEST STRING";
  10. //When
  11. String actual = StringParser.upperCaseString(string1);
  12. //Expect
  13. assertEquals(expected, actual);
  14. }
  15. @Test
  16. public void lowerCaseString() {
  17. //Given
  18. String string1 = "TEST STRING";
  19. String expected = "test string";
  20. //When
  21. String actual = StringParser.lowerCaseString(string1);
  22. //Expect
  23. assertEquals(expected, actual);
  24. }
  25. @Test
  26. public void getFirstCharacter() {
  27. //Given
  28. String string1 = "test string";
  29. Character expected = 't';
  30. //When
  31. Character actual = StringParser.getFirstCharacter(string1);
  32. //Expect
  33. assertEquals(expected, actual);
  34. }
  35. @Test
  36. public void getNthCharacter() {
  37. //Given
  38. String string1 = "test string";
  39. Integer index = 2;
  40. Character expected = 's';
  41. //When
  42. Character actual = StringParser.getNthCharacter(string1, index);
  43. //Expect
  44. assertEquals(expected, actual);
  45. }
  46. @Test
  47. public void upperCaseFirstCharacter() {
  48. //Given
  49. String string1 = "test string";
  50. String expected = "Test string";
  51. //When
  52. String actual = StringParser.upperCaseFirstCharacter(string1);
  53. //Expect
  54. assertEquals(expected, actual);
  55. }
  56. @Test
  57. public void camelCaseString() {
  58. //Given
  59. String string1 = "tEsT sTrInG";
  60. String expected = "TestString";
  61. //When
  62. String actual = StringParser.camelCaseString(string1);
  63. //Expect
  64. assertEquals(expected, actual);
  65. }
  66. @Test
  67. public void snakeCaseString() {
  68. //Given
  69. String string1 = "tEST STring";
  70. String expected = "test_string";
  71. //When
  72. String actual = StringParser.snakeCaseString(string1);
  73. //Expect
  74. assertEquals(expected, actual);
  75. }
  76. @Test
  77. public void getLength() {
  78. //Given
  79. String string1 = "A different string";
  80. Integer expected1 = 18;
  81. String string2 = "test string";
  82. Integer expected2 = 11;
  83. //When
  84. Integer actual1 = StringParser.getLength(string1);
  85. Integer actual2 = StringParser.getLength(string2);
  86. //Expect
  87. assertEquals(expected1, actual1);
  88. assertEquals(expected2, actual2);
  89. }
  90. @Test
  91. public void isEqual() {
  92. //Given
  93. String string1 = "A different string";
  94. String compareString1 = "a Different string";
  95. Boolean expected1 = false;
  96. String string2 = "test string";
  97. String compareString2 = "test string";
  98. Boolean expected2 = true;
  99. //When
  100. Boolean actual1 = StringParser.isEqual(string1, compareString1);
  101. Boolean actual2 = StringParser.isEqual(string2, compareString2);
  102. //Expect
  103. assertEquals(expected1, actual1);
  104. assertEquals(expected2, actual2);
  105. }
  106. @Test
  107. public void isEqualIgnoreCase() {
  108. //Given
  109. String string1 = "A different string";
  110. String compareString1 = "a Different string";
  111. Boolean expected1 = true;
  112. String string2 = "test string";
  113. String compareString2 = "test string";
  114. Boolean expected2 = true;
  115. String string3 = "A different string";
  116. String compareString3 = "A different string!";
  117. Boolean expected3 = false;
  118. //When
  119. Boolean actual1 = StringParser.isEqualIgnoreCase(string1, compareString1);
  120. Boolean actual2 = StringParser.isEqualIgnoreCase(string2, compareString2);
  121. Boolean actual3 = StringParser.isEqualIgnoreCase(string3, compareString3);
  122. //Expect
  123. assertEquals(expected1, actual1);
  124. assertEquals(expected2, actual2);
  125. assertEquals(expected3, actual3);
  126. }
  127. }