|
1 | 1 | package tests.wurstscript.tests; |
2 | 2 |
|
3 | 3 | import de.peeeq.wurstio.intermediateLang.interpreter.CompiletimeNatives; |
| 4 | +import de.peeeq.wurstio.intermediateLang.interpreter.ProgramStateIO; |
4 | 5 | import de.peeeq.wurstio.objectreader.ObjectHelper; |
| 6 | +import de.peeeq.wurstscript.ast.Ast; |
| 7 | +import de.peeeq.wurstscript.ast.Element; |
| 8 | +import de.peeeq.wurstscript.gui.WurstGuiLogger; |
| 9 | +import de.peeeq.wurstscript.intermediatelang.ILconstInt; |
5 | 10 | import de.peeeq.wurstscript.intermediatelang.ILconstString; |
| 11 | +import de.peeeq.wurstscript.jassIm.ImProg; |
| 12 | +import de.peeeq.wurstscript.jassIm.JassIm; |
6 | 13 | import net.moonlightflower.wc3libs.bin.ObjMod; |
7 | 14 | import net.moonlightflower.wc3libs.bin.app.objMod.W3A; |
8 | 15 | import net.moonlightflower.wc3libs.bin.app.objMod.W3U; |
9 | 16 | import net.moonlightflower.wc3libs.dataTypes.DataType; |
| 17 | +import net.moonlightflower.wc3libs.dataTypes.app.War3String; |
10 | 18 | import net.moonlightflower.wc3libs.dataTypes.app.War3Real; |
11 | 19 | import net.moonlightflower.wc3libs.misc.MetaFieldId; |
12 | 20 | import net.moonlightflower.wc3libs.misc.ObjId; |
13 | 21 | import org.testng.annotations.Test; |
14 | 22 |
|
15 | 23 | import java.lang.reflect.Method; |
| 24 | +import java.util.HashMap; |
16 | 25 | import java.util.List; |
| 26 | +import java.util.Optional; |
17 | 27 | import java.util.Set; |
18 | 28 | import java.util.stream.Collectors; |
19 | 29 |
|
20 | 30 | import static org.testng.Assert.assertEquals; |
| 31 | +import static org.testng.Assert.assertFalse; |
21 | 32 | import static org.testng.Assert.assertTrue; |
22 | 33 |
|
23 | 34 | public class CompiletimeNativesTest { |
@@ -104,4 +115,74 @@ public void differentIdsObjectDefinitionUsesCustomTable() throws Exception { |
104 | 115 | assertEquals(w3u.getOrigObjs().size(), 0); |
105 | 116 | assertEquals(w3u.getCustomObjs().size(), 1); |
106 | 117 | } |
| 118 | + |
| 119 | + @Test |
| 120 | + public void differentIdsObjectDefinitionOverwritesExistingMapObject() throws Exception { |
| 121 | + CompiletimeNatives natives = new CompiletimeNatives(null, null, false); |
| 122 | + W3U w3u = new W3U(); |
| 123 | + int hfoo = ObjectHelper.objectIdStringToInt("hfoo"); |
| 124 | + int hf01 = ObjectHelper.objectIdStringToInt("hf01"); |
| 125 | + W3U.Obj existing = w3u.addObj(ObjId.valueOf("hf01"), ObjId.valueOf("hpea")); |
| 126 | + existing.addMod(new ObjMod.Obj.Mod(MetaFieldId.valueOf("unam"), ObjMod.ValType.STRING, War3String.valueOf("old map object"))); |
| 127 | + |
| 128 | + Method newDefFromFiletype = CompiletimeNatives.class.getDeclaredMethod( |
| 129 | + "newDefFromFiletype", |
| 130 | + ObjMod.class, |
| 131 | + int.class, |
| 132 | + int.class, |
| 133 | + boolean.class |
| 134 | + ); |
| 135 | + newDefFromFiletype.setAccessible(true); |
| 136 | + |
| 137 | + ObjMod.Obj obj = (ObjMod.Obj) newDefFromFiletype.invoke(natives, w3u, hfoo, hf01, false); |
| 138 | + |
| 139 | + assertEquals(w3u.getCustomObjs().size(), 1, "Existing map object should be replaced instead of duplicated"); |
| 140 | + assertEquals(obj.getId().getVal(), "hf01"); |
| 141 | + assertEquals(obj.getBaseId().getVal(), "hfoo"); |
| 142 | + assertEquals(obj.getMods().size(), 0, "Overwritten map object mods should not leak into the Wurst-created object"); |
| 143 | + } |
| 144 | + |
| 145 | + @Test |
| 146 | + public void duplicateCodeObjectDefinitionsReportError() { |
| 147 | + WurstGuiLogger gui = new WurstGuiLogger(); |
| 148 | + ProgramStateIO state = new ProgramStateIO(Optional.empty(), null, gui, emptyProg(), true); |
| 149 | + CompiletimeNatives natives = new CompiletimeNatives(state, null, false); |
| 150 | + int hfoo = ObjectHelper.objectIdStringToInt("hfoo"); |
| 151 | + int hf01 = ObjectHelper.objectIdStringToInt("hf01"); |
| 152 | + |
| 153 | + natives.createObjectDefinition(new ILconstString("w3u"), new ILconstInt(hf01), new ILconstInt(hfoo)); |
| 154 | + natives.createObjectDefinition(new ILconstString("w3u"), new ILconstInt(hf01), new ILconstInt(hfoo)); |
| 155 | + |
| 156 | + assertEquals(gui.getErrorCount(), 1); |
| 157 | + assertTrue(gui.getErrors().contains("Object definition with id hf01 is defined more than once.")); |
| 158 | + } |
| 159 | + |
| 160 | + @Test |
| 161 | + public void createObjectDefinitionDoesNotReportExistingMapObjectAsError() throws Exception { |
| 162 | + WurstGuiLogger gui = new WurstGuiLogger(); |
| 163 | + ProgramStateIO state = new ProgramStateIO(Optional.empty(), null, gui, emptyProg(), true); |
| 164 | + CompiletimeNatives natives = new CompiletimeNatives(state, null, false); |
| 165 | + int hfoo = ObjectHelper.objectIdStringToInt("hfoo"); |
| 166 | + int hf01 = ObjectHelper.objectIdStringToInt("hf01"); |
| 167 | + |
| 168 | + Method getDataStore = ProgramStateIO.class.getDeclaredMethod("getDataStore", String.class); |
| 169 | + getDataStore.setAccessible(true); |
| 170 | + W3U w3u = (W3U) getDataStore.invoke(state, "w3u"); |
| 171 | + W3U.Obj existing = w3u.addObj(ObjId.valueOf("hf01"), ObjId.valueOf("hpea")); |
| 172 | + existing.addMod(new ObjMod.Obj.Mod(MetaFieldId.valueOf("unam"), ObjMod.ValType.STRING, War3String.valueOf("old map object"))); |
| 173 | + |
| 174 | + natives.createObjectDefinition(new ILconstString("w3u"), new ILconstInt(hf01), new ILconstInt(hfoo)); |
| 175 | + |
| 176 | + assertEquals(gui.getErrorCount(), 0); |
| 177 | + assertEquals(w3u.getCustomObjs().size(), 1); |
| 178 | + ObjMod.Obj obj = w3u.getCustomObjs().get(0); |
| 179 | + assertEquals(obj.getId().getVal(), "hf01"); |
| 180 | + assertEquals(obj.getBaseId().getVal(), "hfoo"); |
| 181 | + assertFalse(obj.getMods().stream().anyMatch(m -> m.getId().getVal().equals("unam"))); |
| 182 | + } |
| 183 | + |
| 184 | + private ImProg emptyProg() { |
| 185 | + Element trace = Ast.NoExpr(); |
| 186 | + return JassIm.ImProg(trace, JassIm.ImVars(), JassIm.ImFunctions(), JassIm.ImMethods(), JassIm.ImClasses(), JassIm.ImTypeClassFuncs(), new HashMap<>()); |
| 187 | + } |
107 | 188 | } |
0 commit comments