Unverified Commit f3e9b096 authored by Beka Westberg's avatar Beka Westberg Committed by GitHub

Fix dictionary to list coercion (#2091)

* Remove recursive dictionary to list coercion.

Before dictionaries that were children of dictionaries would be coerced
into a list when the parent was coerced into a list. This was
determined to not be the expected behavior, so it has been removed.

* Add dict to list coercion test

* Maybe fix dict test?
parent f936ee78
...@@ -1334,4 +1334,20 @@ public class YailEvalTest extends TestCase { ...@@ -1334,4 +1334,20 @@ public class YailEvalTest extends TestCase {
asdict.get("list")); asdict.get("list"));
assertEquals(YailDictionary.makeDictionary("a", "b"), asdict.get("dictionary")); assertEquals(YailDictionary.makeDictionary("a", "b"), asdict.get("dictionary"));
} }
public void testDictToListCoercion() throws Throwable {
/* Tests that coercion to a list only coerces the top-level dictionary */
String schemeInputString = "(call-yail-primitive yail-list-get-item " +
" (*list-for-runtime* (call-yail-primitive make-yail-dictionary " +
" (*list-for-runtime* (call-yail-primitive make-dictionary-pair " +
" (*list-for-runtime* " +
" \"key\" " +
" (call-yail-primitive make-yail-dictionary " +
" (*list-for-runtime* ) '() \"make a dictionary\") ) " +
" '(key any) \"make a pair\") ) " +
" '(pair ) \"make a dictionary\") " +
" 1) '(list number) \"select list item\")";
String schemeResultString = "(key {})";
assertEquals(schemeResultString, scheme.eval(schemeInputString).toString());
}
} }
...@@ -226,25 +226,9 @@ public class YailDictionary extends LinkedHashMap<Object, Object> ...@@ -226,25 +226,9 @@ public class YailDictionary extends LinkedHashMap<Object, Object>
@SuppressWarnings({"unused", "WeakerAccess"}) // Called from runtime.scm @SuppressWarnings({"unused", "WeakerAccess"}) // Called from runtime.scm
public static YailList dictToAlist(YailDictionary dict) { public static YailList dictToAlist(YailDictionary dict) {
List<Object> list = new ArrayList<>(); List<Object> list = new ArrayList<>();
for (Map.Entry<Object, Object> entry : dict.entrySet()) { for (Map.Entry<Object, Object> entry : dict.entrySet()) {
Object currentKey = entry.getKey(); list.add(YailList.makeList(new Object[] {entry.getKey(), entry.getValue()}));
Object currentValue = entry.getValue();
List<Object> currentPair = new ArrayList<>();
currentPair.add(currentKey);
if (currentValue instanceof YailDictionary) {
currentPair.add(dictToAlist((YailDictionary) currentValue));
} else if (currentValue instanceof YailList) {
currentPair.add(checkListForDicts((YailList) currentValue));
} else {
currentPair.add(currentValue);
} }
list.add(YailList.makeList(currentPair));
}
return YailList.makeList(list); return YailList.makeList(list);
} }
......
...@@ -245,7 +245,7 @@ public class YailDictionaryTest { ...@@ -245,7 +245,7 @@ public class YailDictionaryTest {
@Test @Test
public void testDictToAlist() { public void testDictToAlist() {
YailList target = getTestList(); YailList target = getDictToListTestList();
YailDictionary dict = getTestDict(); YailDictionary dict = getTestDict();
assertEquals(target, YailDictionary.dictToAlist(dict)); assertEquals(target, YailDictionary.dictToAlist(dict));
} }
...@@ -397,4 +397,20 @@ public class YailDictionaryTest { ...@@ -397,4 +397,20 @@ public class YailDictionaryTest {
target.put("list-with-dict", YailList.makeList(singletonList(abdict))); target.put("list-with-dict", YailList.makeList(singletonList(abdict)));
return target; return target;
} }
private static YailList getDictToListTestList() {
// Only the top level is converted to a list.
YailList ablist = YailList.makeList(Arrays.asList("a", "b"));
YailDictionary abdict = YailDictionary.makeDictionary("a", "b");
return YailList.makeList(new Object[] {
YailList.makeList(new Object[] { "number", 1 }),
YailList.makeList(new Object[] { "string", "foo" }),
YailList.makeList(new Object[] { "empty-list", YailList.makeEmptyList() }),
YailList.makeList(new Object[] { "list",
YailList.makeList(asList(ablist, 2, 3)) }),
YailList.makeList(new Object[] { "dict", abdict }),
YailList.makeList(new Object[] { "list-with-dict",
YailList.makeList(singletonList(abdict)) })
});
}
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment