diff options
Diffstat (limited to 'test/cuchaz/enigma/TestSignature.java')
| -rw-r--r-- | test/cuchaz/enigma/TestSignature.java | 268 |
1 files changed, 268 insertions, 0 deletions
diff --git a/test/cuchaz/enigma/TestSignature.java b/test/cuchaz/enigma/TestSignature.java new file mode 100644 index 0000000..8537adf --- /dev/null +++ b/test/cuchaz/enigma/TestSignature.java | |||
| @@ -0,0 +1,268 @@ | |||
| 1 | /******************************************************************************* | ||
| 2 | * Copyright (c) 2015 Jeff Martin. | ||
| 3 | * All rights reserved. This program and the accompanying materials | ||
| 4 | * are made available under the terms of the GNU Lesser General Public | ||
| 5 | * License v3.0 which accompanies this distribution, and is available at | ||
| 6 | * http://www.gnu.org/licenses/lgpl.html | ||
| 7 | * | ||
| 8 | * Contributors: | ||
| 9 | * Jeff Martin - initial API and implementation | ||
| 10 | ******************************************************************************/ | ||
| 11 | package cuchaz.enigma; | ||
| 12 | |||
| 13 | import static org.hamcrest.MatcherAssert.*; | ||
| 14 | import static org.hamcrest.Matchers.*; | ||
| 15 | |||
| 16 | import org.junit.Test; | ||
| 17 | |||
| 18 | import cuchaz.enigma.mapping.ClassNameReplacer; | ||
| 19 | import cuchaz.enigma.mapping.Signature; | ||
| 20 | import cuchaz.enigma.mapping.Type; | ||
| 21 | |||
| 22 | |||
| 23 | public class TestSignature { | ||
| 24 | |||
| 25 | @Test | ||
| 26 | public void easiest() { | ||
| 27 | final Signature sig = new Signature("()V"); | ||
| 28 | assertThat(sig.getArgumentTypes(), is(empty())); | ||
| 29 | assertThat(sig.getReturnType(), is(new Type("V"))); | ||
| 30 | } | ||
| 31 | |||
| 32 | @Test | ||
| 33 | public void primitives() { | ||
| 34 | { | ||
| 35 | final Signature sig = new Signature("(I)V"); | ||
| 36 | assertThat(sig.getArgumentTypes(), contains( | ||
| 37 | new Type("I") | ||
| 38 | )); | ||
| 39 | assertThat(sig.getReturnType(), is(new Type("V"))); | ||
| 40 | } | ||
| 41 | { | ||
| 42 | final Signature sig = new Signature("(I)I"); | ||
| 43 | assertThat(sig.getArgumentTypes(), contains( | ||
| 44 | new Type("I") | ||
| 45 | )); | ||
| 46 | assertThat(sig.getReturnType(), is(new Type("I"))); | ||
| 47 | } | ||
| 48 | { | ||
| 49 | final Signature sig = new Signature("(IBCJ)Z"); | ||
| 50 | assertThat(sig.getArgumentTypes(), contains( | ||
| 51 | new Type("I"), | ||
| 52 | new Type("B"), | ||
| 53 | new Type("C"), | ||
| 54 | new Type("J") | ||
| 55 | )); | ||
| 56 | assertThat(sig.getReturnType(), is(new Type("Z"))); | ||
| 57 | } | ||
| 58 | } | ||
| 59 | |||
| 60 | @Test | ||
| 61 | public void classes() { | ||
| 62 | { | ||
| 63 | final Signature sig = new Signature("([LFoo;)V"); | ||
| 64 | assertThat(sig.getArgumentTypes().size(), is(1)); | ||
| 65 | assertThat(sig.getArgumentTypes().get(0), is(new Type("[LFoo;"))); | ||
| 66 | assertThat(sig.getReturnType(), is(new Type("V"))); | ||
| 67 | } | ||
| 68 | { | ||
| 69 | final Signature sig = new Signature("(LFoo;)LBar;"); | ||
| 70 | assertThat(sig.getArgumentTypes(), contains( | ||
| 71 | new Type("LFoo;") | ||
| 72 | )); | ||
| 73 | assertThat(sig.getReturnType(), is(new Type("LBar;"))); | ||
| 74 | } | ||
| 75 | { | ||
| 76 | final Signature sig = new Signature("(LFoo;LMoo;LZoo;)LBar;"); | ||
| 77 | assertThat(sig.getArgumentTypes(), contains( | ||
| 78 | new Type("LFoo;"), | ||
| 79 | new Type("LMoo;"), | ||
| 80 | new Type("LZoo;") | ||
| 81 | )); | ||
| 82 | assertThat(sig.getReturnType(), is(new Type("LBar;"))); | ||
| 83 | } | ||
| 84 | } | ||
| 85 | |||
| 86 | @Test | ||
| 87 | public void arrays() { | ||
| 88 | { | ||
| 89 | final Signature sig = new Signature("([I)V"); | ||
| 90 | assertThat(sig.getArgumentTypes(), contains( | ||
| 91 | new Type("[I") | ||
| 92 | )); | ||
| 93 | assertThat(sig.getReturnType(), is(new Type("V"))); | ||
| 94 | } | ||
| 95 | { | ||
| 96 | final Signature sig = new Signature("([I)[J"); | ||
| 97 | assertThat(sig.getArgumentTypes(), contains( | ||
| 98 | new Type("[I") | ||
| 99 | )); | ||
| 100 | assertThat(sig.getReturnType(), is(new Type("[J"))); | ||
| 101 | } | ||
| 102 | { | ||
| 103 | final Signature sig = new Signature("([I[Z[F)[D"); | ||
| 104 | assertThat(sig.getArgumentTypes(), contains( | ||
| 105 | new Type("[I"), | ||
| 106 | new Type("[Z"), | ||
| 107 | new Type("[F") | ||
| 108 | )); | ||
| 109 | assertThat(sig.getReturnType(), is(new Type("[D"))); | ||
| 110 | } | ||
| 111 | } | ||
| 112 | |||
| 113 | @Test | ||
| 114 | public void mixed() { | ||
| 115 | { | ||
| 116 | final Signature sig = new Signature("(I[JLFoo;)Z"); | ||
| 117 | assertThat(sig.getArgumentTypes(), contains( | ||
| 118 | new Type("I"), | ||
| 119 | new Type("[J"), | ||
| 120 | new Type("LFoo;") | ||
| 121 | )); | ||
| 122 | assertThat(sig.getReturnType(), is(new Type("Z"))); | ||
| 123 | } | ||
| 124 | { | ||
| 125 | final Signature sig = new Signature("(III)[LFoo;"); | ||
| 126 | assertThat(sig.getArgumentTypes(), contains( | ||
| 127 | new Type("I"), | ||
| 128 | new Type("I"), | ||
| 129 | new Type("I") | ||
| 130 | )); | ||
| 131 | assertThat(sig.getReturnType(), is(new Type("[LFoo;"))); | ||
| 132 | } | ||
| 133 | } | ||
| 134 | |||
| 135 | @Test | ||
| 136 | public void replaceClasses() { | ||
| 137 | { | ||
| 138 | final Signature oldSig = new Signature("()V"); | ||
| 139 | final Signature sig = new Signature(oldSig, new ClassNameReplacer() { | ||
| 140 | @Override | ||
| 141 | public String replace(String val) { | ||
| 142 | return null; | ||
| 143 | } | ||
| 144 | }); | ||
| 145 | assertThat(sig.getArgumentTypes(), is(empty())); | ||
| 146 | assertThat(sig.getReturnType(), is(new Type("V"))); | ||
| 147 | } | ||
| 148 | { | ||
| 149 | final Signature oldSig = new Signature("(IJLFoo;)V"); | ||
| 150 | final Signature sig = new Signature(oldSig, new ClassNameReplacer() { | ||
| 151 | @Override | ||
| 152 | public String replace(String val) { | ||
| 153 | return null; | ||
| 154 | } | ||
| 155 | }); | ||
| 156 | assertThat(sig.getArgumentTypes(), contains( | ||
| 157 | new Type("I"), | ||
| 158 | new Type("J"), | ||
| 159 | new Type("LFoo;") | ||
| 160 | )); | ||
| 161 | assertThat(sig.getReturnType(), is(new Type("V"))); | ||
| 162 | } | ||
| 163 | { | ||
| 164 | final Signature oldSig = new Signature("(LFoo;LBar;)LMoo;"); | ||
| 165 | final Signature sig = new Signature(oldSig, new ClassNameReplacer() { | ||
| 166 | @Override | ||
| 167 | public String replace(String val) { | ||
| 168 | if (val.equals("Foo")) { | ||
| 169 | return "Bar"; | ||
| 170 | } | ||
| 171 | return null; | ||
| 172 | } | ||
| 173 | }); | ||
| 174 | assertThat(sig.getArgumentTypes(), contains( | ||
| 175 | new Type("LBar;"), | ||
| 176 | new Type("LBar;") | ||
| 177 | )); | ||
| 178 | assertThat(sig.getReturnType(), is(new Type("LMoo;"))); | ||
| 179 | } | ||
| 180 | { | ||
| 181 | final Signature oldSig = new Signature("(LFoo;LBar;)LMoo;"); | ||
| 182 | final Signature sig = new Signature(oldSig, new ClassNameReplacer() { | ||
| 183 | @Override | ||
| 184 | public String replace(String val) { | ||
| 185 | if (val.equals("Moo")) { | ||
| 186 | return "Cow"; | ||
| 187 | } | ||
| 188 | return null; | ||
| 189 | } | ||
| 190 | }); | ||
| 191 | assertThat(sig.getArgumentTypes(), contains( | ||
| 192 | new Type("LFoo;"), | ||
| 193 | new Type("LBar;") | ||
| 194 | )); | ||
| 195 | assertThat(sig.getReturnType(), is(new Type("LCow;"))); | ||
| 196 | } | ||
| 197 | } | ||
| 198 | |||
| 199 | @Test | ||
| 200 | public void replaceArrayClasses() { | ||
| 201 | { | ||
| 202 | final Signature oldSig = new Signature("([LFoo;)[[[LBar;"); | ||
| 203 | final Signature sig = new Signature(oldSig, new ClassNameReplacer() { | ||
| 204 | @Override | ||
| 205 | public String replace(String val) { | ||
| 206 | if (val.equals("Foo")) { | ||
| 207 | return "Food"; | ||
| 208 | } else if (val.equals("Bar")) { | ||
| 209 | return "Beer"; | ||
| 210 | } | ||
| 211 | return null; | ||
| 212 | } | ||
| 213 | }); | ||
| 214 | assertThat(sig.getArgumentTypes(), contains( | ||
| 215 | new Type("[LFood;") | ||
| 216 | )); | ||
| 217 | assertThat(sig.getReturnType(), is(new Type("[[[LBeer;"))); | ||
| 218 | } | ||
| 219 | } | ||
| 220 | |||
| 221 | @Test | ||
| 222 | public void equals() { | ||
| 223 | |||
| 224 | // base | ||
| 225 | assertThat(new Signature("()V"), is(new Signature("()V"))); | ||
| 226 | |||
| 227 | // arguments | ||
| 228 | assertThat(new Signature("(I)V"), is(new Signature("(I)V"))); | ||
| 229 | assertThat(new Signature("(ZIZ)V"), is(new Signature("(ZIZ)V"))); | ||
| 230 | assertThat(new Signature("(LFoo;)V"), is(new Signature("(LFoo;)V"))); | ||
| 231 | assertThat(new Signature("(LFoo;LBar;)V"), is(new Signature("(LFoo;LBar;)V"))); | ||
| 232 | assertThat(new Signature("([I)V"), is(new Signature("([I)V"))); | ||
| 233 | assertThat(new Signature("([[D[[[J)V"), is(new Signature("([[D[[[J)V"))); | ||
| 234 | |||
| 235 | assertThat(new Signature("()V"), is(not(new Signature("(I)V")))); | ||
| 236 | assertThat(new Signature("(I)V"), is(not(new Signature("()V")))); | ||
| 237 | assertThat(new Signature("(IJ)V"), is(not(new Signature("(JI)V")))); | ||
| 238 | assertThat(new Signature("([[Z)V"), is(not(new Signature("([[LFoo;)V")))); | ||
| 239 | assertThat(new Signature("(LFoo;LBar;)V"), is(not(new Signature("(LFoo;LCow;)V")))); | ||
| 240 | assertThat(new Signature("([LFoo;LBar;)V"), is(not(new Signature("(LFoo;LCow;)V")))); | ||
| 241 | |||
| 242 | // return type | ||
| 243 | assertThat(new Signature("()I"), is(new Signature("()I"))); | ||
| 244 | assertThat(new Signature("()Z"), is(new Signature("()Z"))); | ||
| 245 | assertThat(new Signature("()[D"), is(new Signature("()[D"))); | ||
| 246 | assertThat(new Signature("()[[[Z"), is(new Signature("()[[[Z"))); | ||
| 247 | assertThat(new Signature("()LFoo;"), is(new Signature("()LFoo;"))); | ||
| 248 | assertThat(new Signature("()[LFoo;"), is(new Signature("()[LFoo;"))); | ||
| 249 | |||
| 250 | assertThat(new Signature("()I"), is(not(new Signature("()Z")))); | ||
| 251 | assertThat(new Signature("()Z"), is(not(new Signature("()I")))); | ||
| 252 | assertThat(new Signature("()[D"), is(not(new Signature("()[J")))); | ||
| 253 | assertThat(new Signature("()[[[Z"), is(not(new Signature("()[[Z")))); | ||
| 254 | assertThat(new Signature("()LFoo;"), is(not(new Signature("()LBar;")))); | ||
| 255 | assertThat(new Signature("()[LFoo;"), is(not(new Signature("()[LBar;")))); | ||
| 256 | } | ||
| 257 | |||
| 258 | @Test | ||
| 259 | public void testToString() { | ||
| 260 | assertThat(new Signature("()V").toString(), is("()V")); | ||
| 261 | assertThat(new Signature("(I)V").toString(), is("(I)V")); | ||
| 262 | assertThat(new Signature("(ZIZ)V").toString(), is("(ZIZ)V")); | ||
| 263 | assertThat(new Signature("(LFoo;)V").toString(), is("(LFoo;)V")); | ||
| 264 | assertThat(new Signature("(LFoo;LBar;)V").toString(), is("(LFoo;LBar;)V")); | ||
| 265 | assertThat(new Signature("([I)V").toString(), is("([I)V")); | ||
| 266 | assertThat(new Signature("([[D[[[J)V").toString(), is("([[D[[[J)V")); | ||
| 267 | } | ||
| 268 | } | ||