fixed problem with singletonMap macro

This commit is contained in:
Renaud Pawlak 2017-01-17 09:53:46 +01:00
parent 87541b8d14
commit fc825bb64e
3 changed files with 20 additions and 7 deletions

View File

@ -217,7 +217,13 @@ public class RemoveJavaDependenciesAdapter<C extends JSweetContext> extends Java
return true;
case "singletonMap":
printMacroName(targetMethodName);
print("{ ").print(invocation.args.head).print(": ").print(invocation.args.tail.head).print(" }");
if (invocation.args.head instanceof JCLiteral) {
print("{ ").print(invocation.args.head).print(": ").print(invocation.args.tail.head)
.print(" }");
} else {
print("(k => { let o = {}; o[k] = ").print(invocation.args.tail.head).print("; return o; })(")
.print(invocation.args.head).print(")");
}
return true;
}
break;

View File

@ -4,10 +4,8 @@ import static org.junit.Assert.assertEquals;
import org.jsweet.transpiler.JSweetFactory;
import org.jsweet.transpiler.extensions.RemoveJavaDependenciesFactory;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
@ -50,7 +48,7 @@ public class NativeStructuresTests extends AbstractTest {
public void testMaps() {
eval((logHandler, result) -> {
Assert.assertEquals("There should be no errors", 0, logHandler.reportedProblems.size());
assertEquals("2,a,true,[1,2],[a,b],1,true,1", result.<String> get("trace"));
assertEquals("2,a,true,[1,2],[a,b],1,true,1,2", result.<String> get("trace"));
}, getSourceFile(Maps.class));
}

View File

@ -15,10 +15,18 @@ public class Maps {
static Array<String> trace = new Array<>();
static String key1() {
return "1";
}
static String key2() {
return "a";
}
public static void main(String[] args) {
Map<String, String> m = new HashMap<>();
m.put("1", "a");
m.put(key1(), "a");
m.put("2", "b");
trace.push("" + m.size());
@ -35,8 +43,9 @@ public class Maps {
trace.push("" + m.size());
trace.push("" + (m.get("1") == null));
trace.push(Collections.singletonMap("a", "1").get("a"));
trace.push(Collections.singletonMap(key2(), "1").get("a"));
trace.push(Collections.singletonMap("b", "2").get("b"));
$export("trace", trace.join(","));
}