core: fix switch statement processing (issue #9 case 2)

This commit is contained in:
Skylot
2014-06-23 22:26:30 +04:00
parent 6fbcf46a8b
commit 37857e88ea
7 changed files with 122 additions and 20 deletions
@@ -1,4 +1,4 @@
package jadx.tests.internal;
package jadx.tests.internal.switches;
import jadx.api.InternalJadxTest;
import jadx.core.dex.nodes.ClassNode;
@@ -6,6 +6,7 @@ import jadx.core.dex.nodes.ClassNode;
import org.junit.Test;
import static org.hamcrest.CoreMatchers.containsString;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
public class TestSwitch extends InternalJadxTest {
@@ -45,5 +46,9 @@ public class TestSwitch extends InternalJadxTest {
assertThat(code, containsString("case '/':"));
assertThat(code, containsString(indent(5) + "break;"));
assertThat(code, containsString(indent(4) + "default:"));
assertEquals(1, count(code, "i++"));
assertEquals(4, count(code, "break;"));
}
}
@@ -1,4 +1,4 @@
package jadx.tests.internal;
package jadx.tests.internal.switches;
import jadx.api.InternalJadxTest;
import jadx.core.dex.nodes.ClassNode;
@@ -0,0 +1,42 @@
package jadx.tests.internal.switches;
import jadx.api.InternalJadxTest;
import jadx.core.dex.nodes.ClassNode;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class TestSwitchNoDefault extends InternalJadxTest {
public static class TestCls {
public void test(int a) {
String s = null;
switch (a) {
case 1:
s = "1";
break;
case 2:
s = "2";
break;
case 3:
s = "3";
break;
case 4:
s = "4";
break;
}
System.out.println(s);
}
}
@Test
public void test() {
ClassNode cls = getClassNode(TestCls.class);
String code = cls.getCode().toString();
System.out.println(code);
assertEquals(4, count(code, "break;"));
assertEquals(1, count(code, "System.out.println(s);"));
}
}
@@ -0,0 +1,52 @@
package jadx.tests.internal.switches;
import jadx.api.InternalJadxTest;
import jadx.core.dex.nodes.ClassNode;
import org.junit.Test;
import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.not;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
public class TestSwitchSimple extends InternalJadxTest {
public static class TestCls {
public void test(int a) {
String s = null;
switch (a % 4) {
case 1:
s = "1";
break;
case 2:
s = "2";
break;
case 3:
s = "3";
break;
case 4:
s = "4";
break;
default:
System.out.println("Not Reach");
break;
}
System.out.println(s);
}
}
@Test
public void test() {
ClassNode cls = getClassNode(TestCls.class);
String code = cls.getCode().toString();
System.out.println(code);
assertEquals(5, count(code, "break;"));
assertEquals(1, count(code, "System.out.println(s);"));
assertEquals(1, count(code, "System.out.println(\"Not Reach\");"));
assertThat(code, not(containsString("switch ((a % 4)) {")));
assertThat(code, containsString("switch (a % 4) {"));
}
}