-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathIntakeTest.java
More file actions
160 lines (130 loc) · 5.17 KB
/
IntakeTest.java
File metadata and controls
160 lines (130 loc) · 5.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package com.team2813.subsystems;
import static com.google.common.truth.Truth.assertThat;
import static com.team2813.subsystems.Intake.BUMP_VOLTAGE;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import com.team2813.IsolatedNetworkTablesExtension;
import com.team2813.lib2813.testing.FakeMotor;
import com.team2813.lib2813.testing.junit.jupiter.CommandTester;
import com.team2813.lib2813.testing.junit.jupiter.WPILibExtension;
import edu.wpi.first.networktables.NetworkTableInstance;
import edu.wpi.first.units.Units;
import edu.wpi.first.wpilibj.DigitalInput;
import edu.wpi.first.wpilibj2.command.Command;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
@ExtendWith({IsolatedNetworkTablesExtension.class, WPILibExtension.class})
public final class IntakeTest {
final FakeMotor fakeMotor = new FakeMotor();
final DigitalInput mockBeamBreak = mock(DigitalInput.class);
@Test
public void constructRealInstance(NetworkTableInstance ntInstance) {
try (Intake ignored = new Intake(ntInstance)) {
fakeMotor.assertIsStopped();
}
}
@Test
public void initialState(NetworkTableInstance ntInstance) {
try (Intake ignored = new Intake(fakeMotor, mockBeamBreak, ntInstance)) {
fakeMotor.assertIsStopped();
}
}
@Test
public void intakeCoral(NetworkTableInstance ntInstance, CommandTester commandTester) {
try (Intake intake = new Intake(fakeMotor, mockBeamBreak, ntInstance)) {
Command command = intake.intakeItemCommand();
fakeMotor.assertIsStopped();
commandTester.runUntilComplete(command);
assertThat(fakeMotor.getMotorVoltage().in(Units.Volts))
.isWithin(0.01)
.of(Intake.PARAMS.intakeDemand());
}
}
@Test
public void stopAfterIntakingCoral(NetworkTableInstance ntInstance, CommandTester commandTester) {
try (Intake intake = new Intake(fakeMotor, mockBeamBreak, ntInstance)) {
Command command = intake.intakeItemCommand();
commandTester.runUntilComplete(command);
command = intake.stopMotorCommand();
assertMotorIsRunning();
commandTester.runUntilComplete(command);
assertMotorIsStopped();
}
}
@Test
public void outtakeCoral(NetworkTableInstance ntInstance, CommandTester commandTester) {
try (Intake intake = new Intake(fakeMotor, mockBeamBreak, ntInstance)) {
Command command = intake.intakeItemCommand();
commandTester.runUntilComplete(command);
command = intake.stopMotorCommand();
assertMotorIsRunning();
commandTester.runUntilComplete(command);
assertMotorIsStopped();
command = intake.outtakeItemCommand();
commandTester.runUntilComplete(command);
assertMotorIsRunning();
assertThat(fakeMotor.getMotorVoltage().in(Units.Volts))
.isWithin(0.01)
.of(Intake.PARAMS.outtakeDemand());
}
}
@Test
public void stopAfterOutakingCoral(NetworkTableInstance ntInstance, CommandTester commandTester) {
try (Intake intake = new Intake(fakeMotor, mockBeamBreak, ntInstance)) {
Command command = intake.intakeItemCommand();
commandTester.runUntilComplete(command);
command = intake.stopMotorCommand();
assertMotorIsRunning();
commandTester.runUntilComplete(command);
assertMotorIsStopped();
command = intake.outtakeItemCommand();
commandTester.runUntilComplete(command);
command = intake.stopMotorCommand();
assertMotorIsRunning();
commandTester.runUntilComplete(command);
assertMotorIsStopped();
}
}
@Test
public void bumpAlgae(NetworkTableInstance ntInstance, CommandTester commandTester) {
try (Intake intake = new Intake(fakeMotor, mockBeamBreak, ntInstance)) {
Command command = intake.intakeItemCommand();
commandTester.runUntilComplete(command);
command = intake.bumpAlgaeCommand();
assertMotorIsRunning();
commandTester.runUntilComplete(command);
assertThat(fakeMotor.getMotorVoltage().in(Units.Volts)).isWithin(0.01).of(BUMP_VOLTAGE);
}
}
@Test
public void stopAfterBumpingAlgae(NetworkTableInstance ntInstance, CommandTester commandTester) {
try (Intake intake = new Intake(fakeMotor, mockBeamBreak, ntInstance)) {
Command command = intake.bumpAlgaeCommand();
commandTester.runUntilComplete(command);
command = intake.stopMotorCommand();
assertMotorIsRunning();
commandTester.runUntilComplete(command);
assertMotorIsStopped();
}
}
@Test
public void hasCoral_withCoral(NetworkTableInstance ntInstance) {
try (Intake intake = new Intake(fakeMotor, mockBeamBreak, ntInstance)) {
when(mockBeamBreak.get()).thenReturn(false);
assertThat(intake.hasCoral()).isTrue();
}
}
@Test
public void hasCoral_withoutCoral(NetworkTableInstance ntInstance) {
try (Intake intake = new Intake(fakeMotor, mockBeamBreak, ntInstance)) {
when(mockBeamBreak.get()).thenReturn(true);
assertThat(intake.hasCoral()).isFalse();
}
}
private void assertMotorIsStopped() {
fakeMotor.assertIsStopped();
}
private void assertMotorIsRunning() {
assertThat(fakeMotor.getMotorVoltage().in(Units.Volts)).isNotWithin(0.01).of(0.0);
}
}