mirror of
https://github.com/garbagemule/MobArena.git
synced 2024-11-22 18:46:45 +01:00
Make ThingGroupPicker aware of result set size.
This commit makes the ThingGroupPicker return different values depending on the size of the picked result set. If the set is empty, the picker returns null, emulating a NothingPicker. If the set is a singleton, the element itself is returned, emulating the SingleThingPicker. Finally, if the set contains more than one element, a ThingGroup containing those elements is returned.
This commit is contained in:
parent
823be96b4e
commit
9fec49cc58
@ -19,6 +19,14 @@ public class ThingGroupPicker implements ThingPicker {
|
|||||||
.filter(Objects::nonNull)
|
.filter(Objects::nonNull)
|
||||||
.collect(Collectors.toList());
|
.collect(Collectors.toList());
|
||||||
|
|
||||||
|
if (things.isEmpty()) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (things.size() == 1) {
|
||||||
|
return things.get(0);
|
||||||
|
}
|
||||||
|
|
||||||
return new ThingGroup(things);
|
return new ThingGroup(things);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2,14 +2,17 @@ package com.garbagemule.MobArena.things;
|
|||||||
|
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.mockito.junit.MockitoJUnitRunner;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import static org.mockito.Mockito.mock;
|
import static org.hamcrest.CoreMatchers.*;
|
||||||
import static org.mockito.Mockito.times;
|
import static org.hamcrest.MatcherAssert.*;
|
||||||
import static org.mockito.Mockito.verify;
|
import static org.mockito.Mockito.*;
|
||||||
|
|
||||||
|
@RunWith(MockitoJUnitRunner.StrictStubs.class)
|
||||||
public class ThingGroupPickerTest {
|
public class ThingGroupPickerTest {
|
||||||
|
|
||||||
private ThingGroupPicker subject;
|
private ThingGroupPicker subject;
|
||||||
@ -22,19 +25,71 @@ public class ThingGroupPickerTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void invokesAllPickers() {
|
public void returnsNullIfResultIsEmpty() {
|
||||||
ThingPicker first = mock(ThingPicker.class);
|
Thing result = subject.pick();
|
||||||
ThingPicker second = mock(ThingPicker.class);
|
|
||||||
ThingPicker third = mock(ThingPicker.class);
|
|
||||||
pickers.add(first);
|
|
||||||
pickers.add(second);
|
|
||||||
pickers.add(third);
|
|
||||||
|
|
||||||
subject.pick();
|
assertThat(result, nullValue());
|
||||||
|
}
|
||||||
|
|
||||||
verify(first, times(1)).pick();
|
@Test
|
||||||
verify(second, times(1)).pick();
|
public void returnsSingletonItemIfResultSizeIsOne() {
|
||||||
verify(third, times(1)).pick();
|
Thing thing = mock(Thing.class);
|
||||||
|
ThingPicker picker = mock(ThingPicker.class);
|
||||||
|
when(picker.pick()).thenReturn(thing);
|
||||||
|
pickers.add(picker);
|
||||||
|
|
||||||
|
Thing result = subject.pick();
|
||||||
|
|
||||||
|
assertThat(result, sameInstance(thing));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void returnsThingGroupIfResultSizeIsGreaterThanOne() {
|
||||||
|
Thing thing1 = mock(Thing.class);
|
||||||
|
Thing thing2 = mock(Thing.class);
|
||||||
|
ThingPicker picker1 = mock(ThingPicker.class);
|
||||||
|
ThingPicker picker2 = mock(ThingPicker.class);
|
||||||
|
when(picker1.pick()).thenReturn(thing1);
|
||||||
|
when(picker2.pick()).thenReturn(thing2);
|
||||||
|
pickers.add(picker1);
|
||||||
|
pickers.add(picker2);
|
||||||
|
|
||||||
|
Thing result = subject.pick();
|
||||||
|
|
||||||
|
assertThat(result, instanceOf(ThingGroup.class));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void skipsNullPickInTwoPickersAndReturnsSingleton() {
|
||||||
|
// This test is kind of "cheating" in that we're building off
|
||||||
|
// the knowledge that a singleton result is returned as-is,
|
||||||
|
// but at the time of writing, there's no straightforward way
|
||||||
|
// to verify that nulls are omitted.
|
||||||
|
Thing thing2 = mock(Thing.class);
|
||||||
|
ThingPicker picker1 = mock(ThingPicker.class);
|
||||||
|
ThingPicker picker2 = mock(ThingPicker.class);
|
||||||
|
when(picker1.pick()).thenReturn(null);
|
||||||
|
when(picker2.pick()).thenReturn(thing2);
|
||||||
|
pickers.add(picker1);
|
||||||
|
pickers.add(picker2);
|
||||||
|
|
||||||
|
Thing result = subject.pick();
|
||||||
|
|
||||||
|
assertThat(result, sameInstance(thing2));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void toStringConcatenatesWithAnd() {
|
||||||
|
ThingPicker picker1 = mock(ThingPicker.class);
|
||||||
|
ThingPicker picker2 = mock(ThingPicker.class);
|
||||||
|
when(picker1.toString()).thenReturn("p1");
|
||||||
|
when(picker2.toString()).thenReturn("p2");
|
||||||
|
pickers.add(picker1);
|
||||||
|
pickers.add(picker2);
|
||||||
|
|
||||||
|
String result = subject.toString();
|
||||||
|
|
||||||
|
assertThat(result, equalTo("(p1 and p2)"));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user