Skip to content

Commit 47de342

Browse files
committed
Clean up ImproperReportingTest
1 parent 663d178 commit 47de342

1 file changed

Lines changed: 76 additions & 88 deletions

File tree

‎scijava/scijava-progress/src/test/java/org/scijava/progress/ImproperReportingTest.java‎

Lines changed: 76 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -56,87 +56,24 @@ public class ImproperReportingTest {
5656
return arr;
5757
};
5858

59-
/**
60-
* A progressible Task that defines fewer stages than it completes
61-
*/
62-
public final Supplier<Integer> tooFewStageTask = () -> {
63-
Progress.defineTotalProgress(2);
64-
int totalStages = 3;
65-
for (int i = 0; i < totalStages; i++) {
66-
Progress.setStageMax(1);
67-
Progress.update();
68-
}
69-
return totalStages;
70-
};
71-
72-
/**
73-
* A progressible task that defines more stages than it completes
74-
*/
75-
public final Supplier<Integer> tooManyStageTask = () -> {
76-
Progress.defineTotalProgress(3);
77-
int totalStages = 2;
78-
for (int i = 0; i < totalStages; i++) {
79-
Progress.setStageMax(1);
80-
Progress.update();
81-
}
82-
return totalStages;
83-
};
84-
85-
/**
86-
* A progressible task that defines fewer subtasks than it runs
87-
*
88-
* @param subtask the subtask being run
89-
* @return the summation of each subtask
90-
*/
91-
public static Integer tooFewSubTaskTask(Function<Integer, Integer> subtask) {
92-
Progress.defineTotalProgress(0, 2);
93-
return IntStream.range(0, 3) //
94-
.map(i -> subtask.apply(4)) //
95-
.sum();
96-
}
97-
98-
/**
99-
* A progressible task that defines more subtasks than it runs
100-
*
101-
* @param subtask the subtask being run
102-
* @return the summation of each subtask
103-
*/
104-
public static Integer tooManySubTaskTask(Function<Integer, Integer> subtask) {
105-
Progress.defineTotalProgress(0, 3);
106-
return IntStream.range(0, 2) //
107-
.map(i -> subtask.apply(4)) //
108-
.sum();
109-
}
110-
111-
/**
112-
* A testing subtask
113-
*/
114-
private final Function<Integer, Integer> subtask = (in) -> in;
115-
116-
/**
117-
* Tests that tasks who update progress without defining total progress result
118-
* in a thrown error.
119-
*/
120-
@Test
121-
public void testUpdateWithoutSetMax() {
122-
Function<Integer, int[]> task = arrayCreator;
123-
Progress.register(task);
124-
Assertions.assertThrows(IllegalStateException.class, () -> task.apply(3));
125-
Progress.complete();
126-
}
127-
12859
/**
12960
* Tests that tasks who updated progress past the defined maximum result in a
13061
* thrown error.
13162
*/
13263
@Test
13364
public void testDefineTooFewStages() {
134-
Supplier<Integer> task = tooFewStageTask;
135-
Assertions.assertThrows(IllegalStateException.class, () -> {
136-
Progress.register(task);
137-
task.get();
138-
Progress.complete();
139-
});
65+
assertISEFromTask( //
66+
// task
67+
() -> {
68+
// defines 2 stages
69+
Progress.defineTotalProgress(2);
70+
// but completes 3 stages
71+
for (int i = 0; i < 3; i++) {
72+
Progress.setStageMax(1);
73+
Progress.update();
74+
}
75+
return "All done";
76+
});
14077
}
14178

14279
/**
@@ -145,26 +82,47 @@ public void testDefineTooFewStages() {
14582
*/
14683
@Test
14784
public void testDefineTooManyStages() {
148-
Supplier<Integer> task = tooManyStageTask;
149-
Assertions.assertThrows(IllegalStateException.class, () -> {
150-
Progress.register(task);
151-
task.get();
152-
Progress.complete();
153-
});
85+
assertISEFromTask( //
86+
// task
87+
() -> {
88+
// defines 3 stages
89+
Progress.defineTotalProgress(3);
90+
// but completes 2 stages
91+
for (int i = 0; i < 2; i++) {
92+
Progress.setStageMax(1);
93+
Progress.update();
94+
}
95+
return "All done!";
96+
});
15497
}
15598

99+
/**
100+
* A testing subtask
101+
*/
102+
private final Function<Integer, Integer> subtask = (in) -> {
103+
return in;
104+
};
105+
156106
/**
157107
* Tests that tasks who updated progress past the defined maximum result in a
158108
* thrown error.
159109
*/
160110
@Test
161111
public void testDefineTooFewSubTasks() {
162-
Supplier<Integer> task = () -> tooFewSubTaskTask(subtask);
163-
Assertions.assertThrows(IllegalStateException.class, () -> {
164-
Progress.register(task);
165-
task.get();
166-
Progress.complete();
167-
});
112+
assertISEFromTask( //
113+
// task
114+
() -> {
115+
// define 2 subtasks
116+
Progress.defineTotalProgress(0, 2);
117+
// but complete 3 subtasks
118+
for( int i = 0; i < 3; i++) {
119+
// Call subtask
120+
Progress.register(subtask);
121+
subtask.apply(4);
122+
Progress.complete();
123+
}
124+
return "All done!";
125+
});
168126
}
169127

170128
/**
@@ -173,7 +131,37 @@ public void testDefineTooFewSubTasks() {
173131
*/
174132
@Test
175133
public void testDefineTooManySubTasks() {
176-
Supplier<Integer> task = () -> tooManySubTaskTask(subtask);
134+
assertISEFromTask( //
135+
// task
136+
() -> {
137+
// define 3 subtasks
138+
Progress.defineTotalProgress(0, 3);
139+
// but run 2 subtasks
140+
for(int i = 0; i < 2; i++) {
141+
// Call subtask
142+
Progress.register(subtask);
143+
subtask.apply(4);
144+
Progress.complete();
145+
}
146+
return "All done!";
147+
});
148+
}
149+
150+
/**
151+
* Tests that tasks who update progress without defining total progress result
152+
* in a thrown error.
153+
*/
154+
@Test
155+
public void testUpdateWithoutSetMax() {
156+
Function<Integer, int[]> task = arrayCreator;
157+
Progress.register(task);
158+
Assertions.assertThrows(IllegalStateException.class, () -> task.apply(3));
159+
Progress.complete();
160+
}
161+
162+
// -- Helper methods -- //
163+
164+
private void assertISEFromTask(Supplier<?> task) {
177165
Assertions.assertThrows(IllegalStateException.class, () -> {
178166
Progress.register(task);
179167
task.get();

0 commit comments

Comments
 (0)