Skip to content
This repository was archived by the owner on Sep 14, 2019. It is now read-only.

Commit c0456c7

Browse files
committed
add test for AutoUtils
a total of 16 tests for parseScriptFile(), plus some other small changes to remove bugs after i found them with tests
1 parent 84c01b3 commit c0456c7

6 files changed

Lines changed: 219 additions & 18 deletions

File tree

Robot2018/.classpath

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<classpath>
33
<classpathentry kind="src" path="src"/>
4-
<classpathentry kind="src" path="tests"/>
4+
<classpathentry kind="src" path="test"/>
55
<classpathentry kind="var" path="wpilib" sourcepath="wpilib.sources"/>
66
<classpathentry kind="var" path="networktables" sourcepath="networktables.sources"/>
77
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>

Robot2018/src/org/usfirst/frc/team199/Robot2018/Robot.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ public void autonomousInit() {
112112
strategies.put(key, chooser.getSelected());
113113
}
114114

115-
Autonomous auto = new Autonomous(startPos, strategies, autoDelay, fmsInput);
115+
Autonomous auto = new Autonomous(startPos, strategies, autoDelay, fmsInput, false);
116116
auto.start();
117117
}
118118

Robot2018/src/org/usfirst/frc/team199/Robot2018/autonomous/AutoUtils.java

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,15 @@ public static Map<String, ArrayList<String[]>> parseScriptFile(String scriptFile
2121
ArrayList<String[]> currScript = new ArrayList<String[]>();
2222
String currScriptName = "";
2323

24-
int count = 0;
24+
int count = 1;
2525
for (String line : lines) {
2626
// remove comments
27-
line = line.substring(0, line.indexOf("#"));
27+
int commentIndex = line.indexOf("#");
28+
if (commentIndex != -1)
29+
line = line.substring(0, line.indexOf("#"));
2830

29-
// trim just to make it neater
30-
line = line.trim();
31+
// trim and remove extra whitespace just to make it neater
32+
line = line.trim().replaceAll("\\s+", " ");
3133

3234
// if there's no instruction on this line, skip
3335
if (line.equals("")) {
@@ -38,7 +40,7 @@ public static Map<String, ArrayList<String[]>> parseScriptFile(String scriptFile
3840
if (line.endsWith(":")) {
3941
autoScripts.put(currScriptName, currScript);
4042
currScript = new ArrayList<String[]>();
41-
currScriptName = line.substring(0, line.length() - 1);
43+
currScriptName = line.substring(0, line.indexOf(":"));
4244
} else {
4345

4446
// first separate the command into instruction and args
@@ -66,6 +68,9 @@ public static Map<String, ArrayList<String[]>> parseScriptFile(String scriptFile
6668
// puts the last script in
6769
autoScripts.put(currScriptName, currScript);
6870

71+
// remove the stray one in the beginning
72+
autoScripts.remove("");
73+
6974
return autoScripts;
7075
}
7176

@@ -191,8 +196,8 @@ private static boolean isPoint (String s) {
191196

192197

193198
// really ugly, but just checks if the stuff between the parentheses are numbers
194-
if (!isDouble(s.substring(s.indexOf('('), s.indexOf(',')))
195-
|| !isDouble(s.substring(s.indexOf(',') + 1, s.indexOf(')'))))
199+
if (!isDouble(s.substring(1, s.indexOf(',')))
200+
|| !isDouble(s.substring(s.indexOf(',') + 1, s.length() - 1)))
196201
return false;
197202

198203
return true;

Robot2018/src/org/usfirst/frc/team199/Robot2018/commands/Autonomous.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public String getSDName () {
8181
* @param fmsInput A three character String representing the FMS input of the positions of our side switch, scale,
8282
* and other side switch, respectively
8383
*/
84-
public Autonomous(Position startPos, Map<String, Strategy> strategies, double delay, String fmsInput) {
84+
public Autonomous(Position startPos, Map<String, Strategy> strategies, double delay, String fmsInput, boolean isTest) {
8585
Strategy chosenStrat = strategies.get(fmsInput.substring(0, 2));
8686

8787
// skip the whole thing if robot is chosen to do nothing
@@ -107,8 +107,10 @@ public Autonomous(Position startPos, Map<String, Strategy> strategies, double de
107107
else
108108
scriptName += "x";
109109

110-
addSequential(new WaitCommand(delay));
111-
addSequential(new RunScript(scriptName));
110+
if (!isTest) {
111+
addSequential(new WaitCommand(delay));
112+
addSequential(new RunScript(scriptName));
113+
}
112114
}
113115

114116
/**

Robot2018/tests/org/usfirst/frc/team199/Robot2018/TestAutonomous.java renamed to Robot2018/test/org/usfirst/frc/team199/Robot2018/AutonomousTest.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import java.util.HashMap;
1111
import java.util.Map;
1212

13-
class TestAutonomous {
13+
class AutonomousTest {
1414

1515
@Test
1616
void mansNotHot() {
@@ -31,7 +31,7 @@ void test0() {
3131
String fmsInput = "LLL";
3232
double delay = 0;
3333

34-
Autonomous testAuto = new Autonomous(pos, strats, delay, fmsInput);
34+
Autonomous testAuto = new Autonomous(pos, strats, delay, fmsInput, true);
3535

3636
assertEquals("", testAuto.getScriptName());
3737
assertEquals(delay, testAuto.getDelay());
@@ -50,7 +50,7 @@ void test1() {
5050
String fmsInput = "LRL";
5151
double delay = 0;
5252

53-
Autonomous testAuto = new Autonomous(pos, strats, delay, fmsInput);
53+
Autonomous testAuto = new Autonomous(pos, strats, delay, fmsInput, true);
5454

5555
assertEquals("Rxxx", testAuto.getScriptName());
5656
assertEquals(delay, testAuto.getDelay());
@@ -69,7 +69,7 @@ void test2() {
6969
String fmsInput = "RRL";
7070
double delay = 0;
7171

72-
Autonomous testAuto = new Autonomous(pos, strats, delay, fmsInput);
72+
Autonomous testAuto = new Autonomous(pos, strats, delay, fmsInput, true);
7373

7474
assertEquals("CRRx", testAuto.getScriptName());
7575
assertEquals(delay, testAuto.getDelay());
@@ -88,7 +88,7 @@ void test3() {
8888
String fmsInput = "LLR";
8989
double delay = 0;
9090

91-
Autonomous testAuto = new Autonomous(pos, strats, delay, fmsInput);
91+
Autonomous testAuto = new Autonomous(pos, strats, delay, fmsInput, true);
9292

9393
assertEquals("LLxx", testAuto.getScriptName());
9494
assertEquals(delay, testAuto.getDelay());
@@ -107,7 +107,7 @@ void test4() {
107107
String fmsInput = "LRR";
108108
double delay = 0;
109109

110-
Autonomous testAuto = new Autonomous(pos, strats, delay, fmsInput);
110+
Autonomous testAuto = new Autonomous(pos, strats, delay, fmsInput, true);
111111

112112
assertEquals("RLxE", testAuto.getScriptName());
113113
assertEquals(delay, testAuto.getDelay());
Lines changed: 194 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,194 @@
1+
package org.usfirst.frc.team199.Robot2018;
2+
3+
import static org.junit.jupiter.api.Assertions.*;
4+
5+
import java.util.ArrayList;
6+
import java.util.Map;
7+
8+
import org.junit.jupiter.api.Test;
9+
10+
import org.usfirst.frc.team199.Robot2018.autonomous.AutoUtils;;
11+
12+
class ParseScriptFileTest {
13+
14+
@Test
15+
void test0() {
16+
Map<String, ArrayList<String[]>> output = AutoUtils.parseScriptFile("asdf\nqwerty");
17+
assertEquals(true, output.isEmpty());
18+
}
19+
20+
@Test
21+
void test1() {
22+
String input = "asdf:\n"
23+
+ "move 43";
24+
Map<String, ArrayList<String[]>> output = AutoUtils.parseScriptFile(input);
25+
26+
assertEquals(1, output.size());
27+
assertEquals("move", output.get("asdf").get(0)[0]);
28+
assertEquals("43", output.get("asdf").get(0)[1]);
29+
}
30+
31+
@Test
32+
void test2() {
33+
String input = "LxRE:\n"
34+
+ "turn 30";
35+
Map<String, ArrayList<String[]>> output = AutoUtils.parseScriptFile(input);
36+
37+
assertEquals(1, output.size());
38+
assertEquals("turn", output.get("LxRE").get(0)[0]);
39+
assertEquals("30", output.get("LxRE").get(0)[1]);
40+
}
41+
42+
@Test
43+
void test3() {
44+
String input = "CLRx:\n"
45+
+ "turn (2,5)";
46+
Map<String, ArrayList<String[]>> output = AutoUtils.parseScriptFile(input);
47+
48+
assertEquals(1, output.size());
49+
assertEquals("turn", output.get("CLRx").get(0)[0]);
50+
assertEquals("(2,5)", output.get("CLRx").get(0)[1]);
51+
}
52+
53+
@Test
54+
void test4() {
55+
String input = "LRxE:\n"
56+
+ "moveto 96";
57+
Map<String, ArrayList<String[]>> output = AutoUtils.parseScriptFile(input);
58+
59+
assertEquals(1, output.size());
60+
assertEquals("moveto", output.get("LRxE").get(0)[0]);
61+
assertEquals("96", output.get("LRxE").get(0)[1]);
62+
}
63+
64+
@Test
65+
void test5() {
66+
String input = "CLxx:\n"
67+
+ "moveto (14,52)";
68+
Map<String, ArrayList<String[]>> output = AutoUtils.parseScriptFile(input);
69+
70+
assertEquals(1, output.size());
71+
assertEquals("moveto", output.get("CLxx").get(0)[0]);
72+
assertEquals("(14,52)", output.get("CLxx").get(0)[1]);
73+
}
74+
75+
@Test
76+
void test6() {
77+
String input = "RxRx:\n"
78+
+ "moveto (4,20) (18,47) 39";
79+
Map<String, ArrayList<String[]>> output = AutoUtils.parseScriptFile(input);
80+
81+
assertEquals(1, output.size());
82+
assertEquals("moveto", output.get("RxRx").get(0)[0]);
83+
assertEquals("(4,20) (18,47) 39", output.get("RxRx").get(0)[1]);
84+
}
85+
86+
@Test
87+
void test7() {
88+
String input = "Rxxx:\n"
89+
+ "wait 38";
90+
Map<String, ArrayList<String[]>> output = AutoUtils.parseScriptFile(input);
91+
92+
assertEquals(1, output.size());
93+
assertEquals("wait", output.get("Rxxx").get(0)[0]);
94+
assertEquals("38", output.get("Rxxx").get(0)[1]);
95+
}
96+
97+
@Test
98+
void test8() {
99+
String input = "LxxE:\n"
100+
+ "exchange";
101+
Map<String, ArrayList<String[]>> output = AutoUtils.parseScriptFile(input);
102+
103+
assertEquals(1, output.size());
104+
assertEquals("exchange", output.get("LxxE").get(0)[0]);
105+
assertEquals("", output.get("LxxE").get(0)[1]);
106+
}
107+
108+
@Test
109+
void test9() {
110+
String input = "RRxx:\n"
111+
+ "moveto (42,56) 45\n"
112+
+ "scale\n"
113+
+ "move 46\n"
114+
+ "LRxx:\n"
115+
+ "intake\n"
116+
+ "turn (32,5)\n";
117+
Map<String, ArrayList<String[]>> output = AutoUtils.parseScriptFile(input);
118+
119+
assertEquals(2, output.size());
120+
assertEquals("moveto", output.get("RRxx").get(0)[0]);
121+
assertEquals("(42,56) 45", output.get("RRxx").get(0)[1]);
122+
assertEquals("scale", output.get("RRxx").get(1)[0]);
123+
assertEquals("", output.get("RRxx").get(1)[1]);
124+
assertEquals("move", output.get("RRxx").get(2)[0]);
125+
assertEquals("46", output.get("RRxx").get(2)[1]);
126+
127+
assertEquals("intake", output.get("LRxx").get(0)[0]);
128+
assertEquals("", output.get("LRxx").get(0)[1]);
129+
assertEquals("turn", output.get("LRxx").get(1)[0]);
130+
assertEquals("(32,5)", output.get("LRxx").get(1)[1]);
131+
}
132+
133+
@Test
134+
void test10() {
135+
String input = "CLRx:\n"
136+
+ "turn 0x4a"; // unfortunately, the program does not except hexadecimal xD
137+
Map<String, ArrayList<String[]>> output = AutoUtils.parseScriptFile(input);
138+
139+
assertEquals(1, output.size());
140+
assertEquals(true, output.get("CLRx").isEmpty());
141+
}
142+
143+
@Test
144+
void test11() {
145+
String input = "CLRx:\n"
146+
+ "moveto (4,20 ) (18,47) 39";
147+
Map<String, ArrayList<String[]>> output = AutoUtils.parseScriptFile(input);
148+
149+
assertEquals(1, output.size());
150+
assertEquals(true, output.get("CLRx").isEmpty());
151+
}
152+
153+
@Test
154+
void test12() {
155+
String input = "CLRx:\n"
156+
+ "moveto (4,20)(18,47) 39";
157+
Map<String, ArrayList<String[]>> output = AutoUtils.parseScriptFile(input);
158+
159+
assertEquals(1, output.size());
160+
assertEquals(true, output.get("CLRx").isEmpty());
161+
}
162+
163+
@Test
164+
void test13() {
165+
String input = "CLRx:\n"
166+
+ "moveto (4 ,20) (18,47) 39";
167+
Map<String, ArrayList<String[]>> output = AutoUtils.parseScriptFile(input);
168+
169+
assertEquals(1, output.size());
170+
assertEquals(true, output.get("CLRx").isEmpty());
171+
}
172+
173+
@Test
174+
void test14() {
175+
String input = "LxxE:\n"
176+
+ " exchange ";
177+
Map<String, ArrayList<String[]>> output = AutoUtils.parseScriptFile(input);
178+
179+
assertEquals(1, output.size());
180+
assertEquals("exchange", output.get("LxxE").get(0)[0]);
181+
assertEquals("", output.get("LxxE").get(0)[1]);
182+
}
183+
184+
@Test
185+
void test15() {
186+
String input = "Rxxx:\n"
187+
+ "wait 38";
188+
Map<String, ArrayList<String[]>> output = AutoUtils.parseScriptFile(input);
189+
190+
assertEquals(1, output.size());
191+
assertEquals("wait", output.get("Rxxx").get(0)[0]);
192+
assertEquals("38", output.get("Rxxx").get(0)[1]);
193+
}
194+
}

0 commit comments

Comments
 (0)