fix: isList check in Value checks type of list (#70)
* isList check of Value checks type of list Signed-off-by: Robert Grassian <robert.grassian@split.io> * test for empty list Signed-off-by: Robert Grassian <robert.grassian@split.io> Signed-off-by: Robert Grassian <robert.grassian@split.io>
This commit is contained in:
parent
00af2f88f8
commit
81ab0710ea
|
|
@ -125,7 +125,9 @@ public class Value {
|
|||
* @return boolean
|
||||
*/
|
||||
public boolean isList() {
|
||||
return this.innerObject instanceof List;
|
||||
return this.innerObject instanceof List
|
||||
&& (((List) this.innerObject).isEmpty()
|
||||
|| ((List) this.innerObject).get(0) instanceof Value);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -110,4 +110,28 @@ public class ValueTest {
|
|||
assertTrue(value.isList());
|
||||
assertEquals(ITEM_VALUE, value.asList().get(0).asString());
|
||||
}
|
||||
|
||||
@Test public void listMustBeOfValues() {
|
||||
String item = "item";
|
||||
List<String> list = new ArrayList<>();
|
||||
list.add(item);
|
||||
try {
|
||||
new Value((Object) list);
|
||||
fail("Should fail due to creation of list of non-values.");
|
||||
} catch (InstantiationException e) {
|
||||
assertEquals("Invalid value type: class java.util.ArrayList", e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@Test public void emptyListAllowed() {
|
||||
List<String> list = new ArrayList<>();
|
||||
try {
|
||||
Value value = new Value((Object) list);
|
||||
assertTrue(value.isList());
|
||||
List<Value> values = value.asList();
|
||||
assertTrue(values.isEmpty());
|
||||
} catch (Exception e) {
|
||||
fail("Unexpected exception occurred.", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue