Fix build fail and refactor config file in tests

This commit is contained in:
simon987 2019-04-23 17:04:20 -04:00
parent 6e4b2358af
commit 2f7d2cd85a
6 changed files with 36 additions and 67 deletions

View File

@ -237,7 +237,7 @@ public class VaultDimension {
openings.add(Direction.WEST);
attachedWorld.openings.add(Direction.EAST);
break;
case default;
default:
break;
}

View File

@ -0,0 +1,24 @@
package net.simon987.server;
import java.io.File;
public class ConfigHelper {
public static ServerConfiguration getConfig() {
File workingDir = new File("");
File file = new File(workingDir.getAbsolutePath(), "config.properties");
if (!file.exists()) {
File fallback = new File("Server/src/main/resources/", file.getName());
if (fallback.exists()) {
file = fallback;
} else {
throw new AssertionError("'config.properties' and " +
"'Server/src/main/resources/config.properties' cannot be found with working directory: " +
workingDir.getAbsolutePath());
}
}
return new ServerConfiguration(file.getAbsolutePath());
}
}

View File

@ -1,33 +1,15 @@
package net.simon987.server.assembly;
import net.simon987.server.ServerConfiguration;
import net.simon987.server.ConfigHelper;
import org.junit.Test;
import static org.junit.Assert.*;
import java.io.File;
public class MemoryTest {
ServerConfiguration getConfig() {
String filePath = "config.properties";
if (!new File(filePath).exists()) {
File fallback = new File("Server/src/main/resources/", filePath);
if (fallback.exists()) {
filePath = fallback.getAbsolutePath();
} else {
throw new AssertionError("'config.properties' and 'Server/src/main/resources/config.properties' cannot be found with working directory: " + new File("").getAbsolutePath());
}
}
ServerConfiguration config = new ServerConfiguration(filePath);
return config;
}
@Test
public void getSet() {
int memorySize = getConfig().getInt("memory_size");
int memorySize = ConfigHelper.getConfig().getInt("memory_size");
Memory memory = new Memory(memorySize);
memory.set(1, 1);
@ -45,7 +27,7 @@ public class MemoryTest {
@Test
public void write() {
int memorySize = getConfig().getInt("memory_size");
int memorySize = ConfigHelper.getConfig().getInt("memory_size");
Memory memory = new Memory(memorySize);
assertTrue(memory.write(0, new char[memorySize], 0, memorySize));

View File

@ -1,6 +1,6 @@
package net.simon987.server.assembly.instruction;
import net.simon987.server.ServerConfiguration;
import net.simon987.server.ConfigHelper;
import net.simon987.server.assembly.Memory;
import net.simon987.server.assembly.Register;
import net.simon987.server.assembly.RegisterSet;
@ -9,32 +9,14 @@ import org.junit.Test;
import static org.junit.Assert.*;
import java.io.File;
public class AddInstructionTest {
ServerConfiguration getConfig() {
String filePath = "config.properties";
if (!new File(filePath).exists()) {
File fallback = new File("Server/src/main/resources/", filePath);
if (fallback.exists()) {
filePath = fallback.getAbsolutePath();
} else {
throw new AssertionError("'config.properties' and 'Server/src/main/resources/config.properties' cannot be found with working directory: " + new File("").getAbsolutePath());
}
}
ServerConfiguration config = new ServerConfiguration(filePath);
return config;
}
/**
* ADD mem/reg, mem/reg
*/
@Test
public void addTargetTarget() {
int memorySize = getConfig().getInt("memory_size");
int memorySize = ConfigHelper.getConfig().getInt("memory_size");
//Memory
Memory memory = new Memory(memorySize);
@ -144,7 +126,7 @@ public class AddInstructionTest {
*/
@Test
public void addTargetImm() {
int memorySize = getConfig().getInt("memory_size");
int memorySize = ConfigHelper.getConfig().getInt("memory_size");
//Memory
Memory memory = new Memory(memorySize);

View File

@ -1,35 +1,17 @@
package net.simon987.server.assembly.instruction;
import net.simon987.server.ServerConfiguration;
import net.simon987.server.ConfigHelper;
import net.simon987.server.assembly.Memory;
import net.simon987.server.assembly.Status;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import java.io.File;
public class AndInstructionTest {
ServerConfiguration getConfig() {
String filePath = "config.properties";
if (!new File(filePath).exists()) {
File fallback = new File("Server/src/main/resources/", filePath);
if (fallback.exists()) {
filePath = fallback.getAbsolutePath();
} else {
throw new AssertionError("'config.properties' and 'Server/src/main/resources/config.properties' cannot be found with working directory: " + new File("").getAbsolutePath());
}
}
ServerConfiguration config = new ServerConfiguration(filePath);
return config;
}
@Test
public void executeTargetTarget() {
int memorySize = getConfig().getInt("memory_size");
int memorySize = ConfigHelper.getConfig().getInt("memory_size");
//Memory
Memory memory = new Memory(memorySize);
@ -69,7 +51,7 @@ public class AndInstructionTest {
@Test
public void executeTargetImm() {
int memorySize = getConfig().getInt("memory_size");
int memorySize = ConfigHelper.getConfig().getInt("memory_size");
//Memory
Memory memory = new Memory(memorySize);

View File

@ -3,7 +3,7 @@ package net.simon987.server.assembly.instruction;
import net.simon987.server.assembly.Status;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
public class BrkInstructionTest {
@Test
@ -17,8 +17,7 @@ public class BrkInstructionTest {
brkInstruction.execute(status);
assertEquals(true, status.isBreakFlag());
assertTrue(status.isBreakFlag());
}
}