Skip to content

Commit 66abe52

Browse files
committed
Began adding Unit Tests
1 parent 9bbd931 commit 66abe52

File tree

5 files changed

+84
-55
lines changed

5 files changed

+84
-55
lines changed

pom.xml

+6-6
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,6 @@
5757
</build>
5858

5959
<dependencies>
60-
<dependency>
61-
<groupId>junit</groupId>
62-
<artifactId>junit-dep</artifactId>
63-
<version>4.10</version>
64-
<scope>test</scope>
65-
</dependency>
6660
<dependency>
6761
<groupId>commons-io</groupId>
6862
<artifactId>commons-io</artifactId>
@@ -79,5 +73,11 @@
7973
<artifactId>forms_rt</artifactId>
8074
<version>7.0.3</version>
8175
</dependency>
76+
<dependency>
77+
<groupId>org.junit.jupiter</groupId>
78+
<artifactId>junit-jupiter-api</artifactId>
79+
<version>RELEASE</version>
80+
<scope>test</scope>
81+
</dependency>
8282
</dependencies>
8383
</project>

src/main/java/jterm/command/Pause.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public void execute(List<String> options) {
3030
JTerm.out.println("Command syntax:\n\tpause [-h] [input]");
3131
return;
3232
} else {
33-
JTerm.out.print(Util.getRest(options, 0));
33+
JTerm.out.print(Util.getAsString(options));
3434
}
3535
}
3636

src/main/java/jterm/io/InputHandler.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ static void tabEvent() {
124124

125125
static void newLineEvent() {
126126
lastArrowPress = Keys.NONE;
127-
boolean empty = Util.containsOnlySpaces(command);
127+
boolean empty = command.trim().isEmpty();
128128

129129
ArrayList<String> prevCommands = getPrevCommands();
130130

@@ -184,7 +184,7 @@ static void backspaceEvent() {
184184
private static void parse() {
185185
String[] commands = command.split("&&");
186186
for (String command : commands) {
187-
command = Util.removeSpaces(command);
187+
command = command.trim();
188188
JTerm.executeCommand(command);
189189
}
190190
}
@@ -287,7 +287,7 @@ private static String[] disassembleCommand(String command) {
287287
}
288288

289289
// Remove spaces so that autocomplete can work properly
290-
splitCommand[1] = Util.removeSpaces(splitCommand[1]);
290+
splitCommand[1] = splitCommand[1].trim();
291291

292292
return splitCommand;
293293
}

src/main/java/jterm/util/Util.java

+2-45
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import jterm.io.InputHandler;
55

66
import java.util.ArrayList;
7+
import java.util.Arrays;
78
import java.util.List;
89
import java.util.Scanner;
910

@@ -62,13 +63,7 @@ public static void clearLine(String line, int cursorPos, boolean clearPrompt) {
6263
}
6364

6465
public static ArrayList<String> getAsArray(String options) {
65-
try (Scanner tokenizer = new Scanner(options)) {
66-
ArrayList<String> optionsArray = new ArrayList<>();
67-
while (tokenizer.hasNext()) {
68-
optionsArray.add(tokenizer.next());
69-
}
70-
return optionsArray;
71-
}
66+
return new ArrayList<>(Arrays.asList(options.split(" ")));
7267
}
7368

7469
public static String getAsString(List<String> options) {
@@ -94,44 +89,6 @@ public static String getRest(List<String> options, int index) {
9489
return outputBuilder.toString();
9590
}
9691

97-
/**
98-
* Removes blank space before and after command if any exists.
99-
*
100-
* @param command Command to parse
101-
* @return Command without white space
102-
*/
103-
public static String removeSpaces(String command) {
104-
int fpos = 0;
105-
for (int i = 0; i < command.length(); i++) {
106-
if (command.charAt(i) == ' ')
107-
fpos++;
108-
else
109-
break;
110-
}
111-
112-
int bpos = command.length() > 0 ? command.length() : 0;
113-
for (int i = command.length() - 1; i > 0; i--) {
114-
if (command.charAt(i) == ' ')
115-
bpos--;
116-
else
117-
break;
118-
}
119-
120-
return command.substring(fpos, bpos);
121-
}
122-
123-
/**
124-
* Determines if a string is composed only of spaces.
125-
*
126-
* @param s string to check
127-
* @return true if s is composed of only spaces, false if there is a character in it
128-
*/
129-
public static boolean containsOnlySpaces(String s) {
130-
return s.trim().isEmpty();
131-
}
132-
public static int shrinkToBounds(int i, int min, int max){
133-
return Math.min(Math.max(i, min), max);
134-
}
13592
public static String getFullPath(String fileName) {
13693
if (!fileName.startsWith("/")) {
13794
fileName = JTerm.currentDirectory + "/" + fileName;
+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package jterm.util;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import java.util.Arrays;
6+
7+
import static org.junit.jupiter.api.Assertions.*;
8+
9+
class UtilTest {
10+
@Test
11+
void getRunTime() {
12+
assertEquals(Util.getRunTime(172799999), "1 days, 23 hours, 59 minutes, 59 seconds, 999 millis");
13+
}
14+
15+
@Test
16+
void clearLine() {
17+
}
18+
19+
@Test
20+
void getAsArray() {
21+
assertEquals(Util.getAsArray("This function is just splitting on spaces"),
22+
Arrays.asList("This", "function", "is", "just", "splitting", "on", "spaces"));
23+
}
24+
25+
@Test
26+
void getAsString() {
27+
assertEquals("This function is just concatenating an array",
28+
Util.getAsString(Arrays.asList("This", "function", "is", "just", "concatenating", "an", "array")));
29+
}
30+
31+
@Test
32+
void getRest() {
33+
assertEquals("is just concatenating an array",
34+
Util.getRest(Arrays.asList("This", "function", "is", "just", "concatenating", "an", "array"), 2));
35+
}
36+
37+
@Test
38+
void getFullPath() {
39+
}
40+
41+
private void compareTimes(Runnable r1, Runnable r2) {
42+
averageTimeOfExecution(r1);
43+
long r1time = averageTimeOfExecution(r1);
44+
randomBranches();
45+
averageTimeOfExecution(r2);
46+
long r2time = averageTimeOfExecution(r2);
47+
System.out.println(String.format("R1: %d, R2: %d", r1time, r2time));
48+
}
49+
50+
private void randomBranches(){
51+
int rand = 0;
52+
for(int i = 0; i < 10000; i++){
53+
if(i+(Math.random() * 100) < i+(Math.random() * 100)){
54+
rand++;
55+
}else{
56+
rand--;
57+
}
58+
}
59+
}
60+
61+
private long averageTimeOfExecution(Runnable r) {
62+
long before, after;
63+
long total = 0;
64+
for (int i = 0; i < 1000; i++) {
65+
before = System.nanoTime();
66+
r.run();
67+
after = System.nanoTime();
68+
total += after - before;
69+
}
70+
return total / 1000;
71+
}
72+
}

0 commit comments

Comments
 (0)