Parcourir la source

prettier unit tests

Carolynn Vansant il y a 6 ans
Parent
révision
1acd89902f

+ 5
- 8
src/main/java/com/zipcodewilmington/phonebook/PhoneBook.java Voir le fichier

@@ -21,14 +21,13 @@ public class PhoneBook {
21 21
     }
22 22
 
23 23
     //add a name & number entry, add number
24
-    public void inputEntry(String name, ArrayList<String> number) {
24
+    public void addEntry(String name, ArrayList<String> number) {
25 25
         if (!treeMap.containsKey(name)) {
26 26
             treeMap.put(name, number);
27 27
         }
28 28
     }
29 29
 
30 30
     //add numbers to existing entries
31
-    //no test cases yet
32 31
     public void addNumberToEntry(String name, String number) {
33 32
         for (Map.Entry<String, ArrayList<String>> entry : treeMap.entrySet()) {
34 33
             if (name.equals(entry.getKey())) {
@@ -39,7 +38,6 @@ public class PhoneBook {
39 38
     }
40 39
 
41 40
     //remove numbers from existing entries
42
-    //no test cases yet
43 41
     public void removeNumberFromEntry(String name, String number) {
44 42
         for (Map.Entry<String, ArrayList<String>> entry : treeMap.entrySet()) {
45 43
             if (entry.getKey().equals(name)) {
@@ -50,17 +48,17 @@ public class PhoneBook {
50 48
         }
51 49
     }
52 50
 
53
-    //remove a name & number entry
51
+    //remove entire entry
54 52
     public void removeEntry(String name) {
55 53
             treeMap.remove(name);
56 54
     }
57 55
 
58
-    //find phone number lookup by name
56
+    //lookup: find phone number from name
59 57
     public ArrayList<String> lookup(String name) {
60 58
         return treeMap.get(name);
61 59
     }
62 60
 
63
-    //reverse lookup
61
+    //reverse lookup: find name from number
64 62
     public String reverseLookup (String number) {
65 63
         String name = "";
66 64
         for(Map.Entry<String, ArrayList<String>> entry: treeMap.entrySet()) {
@@ -78,8 +76,7 @@ public class PhoneBook {
78 76
     public String display(){
79 77
         StringBuilder printOut = new StringBuilder();
80 78
         for(Map.Entry<String, ArrayList<String>> entry: treeMap.entrySet()) {
81
-            printOut
82
-                    .append(entry.getKey())
79
+            printOut.append(entry.getKey())
83 80
                     .append(" ")
84 81
                     .append(entry.getValue().toString())
85 82
                     .append("\n");

+ 29
- 55
src/test/java/com/zipcodewilmington/phonebook/PhoneBookTest.java Voir le fichier

@@ -5,6 +5,7 @@ import org.junit.Test;
5 5
 import sun.nio.cs.ext.SJIS;
6 6
 
7 7
 import java.util.ArrayList;
8
+import java.util.Arrays;
8 9
 import java.util.TreeMap;
9 10
 
10 11
 /**
@@ -20,26 +21,19 @@ public class PhoneBookTest {
20 21
     @Test
21 22
     public void testConstructorWithArgument() {
22 23
         PhoneBook book = new PhoneBook();
23
-        ArrayList<String> number = new ArrayList<String>();
24
-        number.add("302-555-2222");
25
-        number.add("302-555-3456");
26
-        book.inputEntry("Bob", number);
24
+        ArrayList<String> number = new ArrayList<String>(Arrays.asList("302-555-2222","302-555-3456"));
25
+        book.addEntry("Bob", number);
27 26
         Assert.assertNotNull(book);
28 27
     }
29 28
 
30 29
     @Test
31
-    public void testInputEntry() {
30
+    public void testAddEntry() {
32 31
         //Given
33 32
         PhoneBook book = new PhoneBook();
34
-
35
-        ArrayList<String> expectedAddition = new ArrayList<String>();
36
-        expectedAddition.add("302-555-2222");
37
-        expectedAddition.add("302-555-3456");
38
-
33
+        ArrayList<String> expectedAddition = new ArrayList<String>(Arrays.asList("302-555-2222","302-555-3456"));
39 34
         //When
40
-        book.inputEntry("Bob", expectedAddition);
35
+        book.addEntry("Bob", expectedAddition);
41 36
         ArrayList<String> actualAddition = new ArrayList<String>(book.lookup("Bob"));
42
-
43 37
         //Then
44 38
         Assert.assertEquals(expectedAddition, actualAddition);
45 39
     }
@@ -48,12 +42,10 @@ public class PhoneBookTest {
48 42
     public void addNumberToEntry() {
49 43
         //Given
50 44
         PhoneBook book = new PhoneBook();
51
-        ArrayList<String> bobNumber = new ArrayList<String>();
52
-        bobNumber.add("302-555-2234");
53
-        book.inputEntry("Bob", bobNumber);
54
-        ArrayList<String> expectedAddition = new ArrayList<String>();
55
-        expectedAddition.add("302-555-2234");
56
-        expectedAddition.add("302-555-1111");
45
+        ArrayList<String> bobNumber = new ArrayList<String>(Arrays.asList("302-555-2234"));
46
+        book.addEntry("Bob", bobNumber);
47
+        //Expected
48
+        ArrayList<String> expectedAddition = new ArrayList<String>(Arrays.asList("302-555-2234","302-555-1111"));
57 49
         //When
58 50
         book.addNumberToEntry("Bob", "302-555-1111");
59 51
         ArrayList<String> actualAddition = book.lookup("Bob");
@@ -65,14 +57,10 @@ public class PhoneBookTest {
65 57
     public void removeNumberFromEntry() {
66 58
         //Given
67 59
         PhoneBook book = new PhoneBook();
68
-        ArrayList<String> bobNumber = new ArrayList<String>();
69
-        bobNumber.add("302-555-2234");
70
-        bobNumber.add("302-555-1111");
71
-        book.inputEntry("Bob", bobNumber);
72
-
73
-        ArrayList<String> expectedAfterDeletion = new ArrayList<String>();
74
-        expectedAfterDeletion.add("302-555-2234");
75
-
60
+        ArrayList<String> bobNumber = new ArrayList<String>(Arrays.asList("302-555-2234","302-555-1111"));
61
+        book.addEntry("Bob", bobNumber);
62
+        //Expected
63
+        ArrayList<String> expectedAfterDeletion = new ArrayList<String>(Arrays.asList("302-555-2234"));
76 64
         //When
77 65
         book.removeNumberFromEntry("Bob", "302-555-1111");
78 66
         ArrayList<String> actualAfterDeletion = book.lookup("Bob");
@@ -85,16 +73,13 @@ public class PhoneBookTest {
85 73
 
86 74
         //Given
87 75
         PhoneBook book = new PhoneBook();
88
-        ArrayList<String> bobNumber = new ArrayList<String>();
89
-        bobNumber.add("302-555-2222");
90
-        bobNumber.add("302-555-3456");
91
-        book.inputEntry("Bob", bobNumber);
76
+        ArrayList<String> bobNumber = new ArrayList<String>(Arrays.asList("302-555-2222","302-555-3456"));
77
+        book.addEntry("Bob", bobNumber);
78
+        //Expected
92 79
         String expectedValue = "";
93
-
94 80
         //When
95 81
         book.removeEntry("Bob");
96 82
         String actualValue = book.display();
97
-
98 83
         //Then
99 84
         Assert.assertEquals(expectedValue, actualValue);
100 85
     }
@@ -104,10 +89,8 @@ public class PhoneBookTest {
104 89
         //To test if lookup returns all of the person's phone numbers
105 90
         //Given
106 91
         PhoneBook book = new PhoneBook();
107
-        ArrayList<String> expectedNumber = new ArrayList<String>();
108
-        expectedNumber.add("302-555-2223");
109
-        expectedNumber.add("302-555-3456");
110
-        book.inputEntry("Bob", expectedNumber);
92
+        ArrayList<String> expectedNumber = new ArrayList<String>(Arrays.asList("302-555-2223","302-555-3456"));
93
+        book.addEntry("Bob", expectedNumber);
111 94
         //When
112 95
         ArrayList<String> actualNumber = new ArrayList<String>(book.lookup("Bob"));
113 96
         //Then
@@ -119,13 +102,11 @@ public class PhoneBookTest {
119 102
         //To test if lookup picks out the right person when there are multiple entries
120 103
         //Given
121 104
         PhoneBook book = new PhoneBook();
122
-        ArrayList<String> bobNumber = new ArrayList<String>();
123
-        bobNumber.add("302-555-2223");
124
-        book.inputEntry("Bob", bobNumber);
105
+        ArrayList<String> bobNumber = new ArrayList<String>(Arrays.asList("302-555-2223"));
106
+        book.addEntry("Bob", bobNumber);
125 107
         //Expected
126
-        ArrayList<String> expectedNumber = new ArrayList<String>();
127
-        expectedNumber.add("302-555-9988");
128
-        book.inputEntry("Frank", expectedNumber);
108
+        ArrayList<String> expectedNumber = new ArrayList<String>(Arrays.asList("302-555-9988"));
109
+        book.addEntry("Frank", expectedNumber);
129 110
         //When
130 111
         ArrayList<String> actualNumber = new ArrayList<String> (book.lookup("Frank"));
131 112
         //Then
@@ -137,11 +118,9 @@ public class PhoneBookTest {
137 118
         // to test if right person is returned when searching by one of multiple phone numbers
138 119
         //Given
139 120
         PhoneBook book = new PhoneBook();
140
-        ArrayList<String> bobNumber = new ArrayList<String>();
141
-        bobNumber.add("302-555-2223");
142
-        bobNumber.add("302-555-5555");
121
+        ArrayList<String> bobNumber = new ArrayList<String>(Arrays.asList("302-555-2223","302-555-5555"));
143 122
         String expectedName = "Bob";
144
-        book.inputEntry(expectedName, bobNumber);
123
+        book.addEntry(expectedName, bobNumber);
145 124
         //When
146 125
         String actualName = book.reverseLookup("302-555-2223");
147 126
         //Then
@@ -153,15 +132,10 @@ public class PhoneBookTest {
153 132
     public void testDisplay() {
154 133
         //Given:
155 134
         PhoneBook book = new PhoneBook();
156
-
157
-        ArrayList<String> bobNumber = new ArrayList<String>();
158
-        bobNumber.add("302-555-1234");
159
-        bobNumber.add("302-556-1245");
160
-        book.inputEntry("Bob", bobNumber);
161
-
162
-        ArrayList<String> frankNumber = new ArrayList<String>();
163
-        frankNumber.add("777-555-1111");
164
-        book.inputEntry("Frank", frankNumber);
135
+        ArrayList<String> bobNumber = new ArrayList<String>(Arrays.asList("302-555-1234","302-556-1245"));
136
+        book.addEntry("Bob", bobNumber);
137
+        ArrayList<String> frankNumber = new ArrayList<String>(Arrays.asList("777-555-1111"));
138
+        book.addEntry("Frank", frankNumber);
165 139
         //Expected
166 140
         String expectedNameNumber = "Bob " + bobNumber.toString() + "\n" + "Frank " + frankNumber.toString() + "\n";
167 141
         //When