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:
Andreas Troelsen 2021-08-07 01:25:30 +02:00
parent 823be96b4e
commit 9fec49cc58
2 changed files with 77 additions and 14 deletions

View File

@ -19,6 +19,14 @@ public class ThingGroupPicker implements ThingPicker {
.filter(Objects::nonNull)
.collect(Collectors.toList());
if (things.isEmpty()) {
return null;
}
if (things.size() == 1) {
return things.get(0);
}
return new ThingGroup(things);
}

View File

@ -2,14 +2,17 @@ package com.garbagemule.MobArena.things;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.junit.MockitoJUnitRunner;
import java.util.ArrayList;
import java.util.List;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.hamcrest.CoreMatchers.*;
import static org.hamcrest.MatcherAssert.*;
import static org.mockito.Mockito.*;
@RunWith(MockitoJUnitRunner.StrictStubs.class)
public class ThingGroupPickerTest {
private ThingGroupPicker subject;
@ -22,19 +25,71 @@ public class ThingGroupPickerTest {
}
@Test
public void invokesAllPickers() {
ThingPicker first = mock(ThingPicker.class);
ThingPicker second = mock(ThingPicker.class);
ThingPicker third = mock(ThingPicker.class);
pickers.add(first);
pickers.add(second);
pickers.add(third);
public void returnsNullIfResultIsEmpty() {
Thing result = subject.pick();
subject.pick();
assertThat(result, nullValue());
}
verify(first, times(1)).pick();
verify(second, times(1)).pick();
verify(third, times(1)).pick();
@Test
public void returnsSingletonItemIfResultSizeIsOne() {
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)"));
}
}