Skip to content

Commit

Permalink
[#314] fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
vbmacher committed Aug 29, 2023
1 parent 25e2a2a commit 28a6278
Show file tree
Hide file tree
Showing 13 changed files with 195 additions and 278 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.junit.rules.TemporaryFolder;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.StandardOpenOption;
import java.util.Optional;
Expand Down Expand Up @@ -84,14 +85,18 @@ public void onFinish() {
compiler.initialize();
}

protected void compile(String content) throws Exception {
File sourceFile = folder.newFile();
Files.write(sourceFile.toPath(), content.getBytes(), StandardOpenOption.WRITE);
protected void compile(String content) {
try {
File sourceFile = folder.newFile();
Files.write(sourceFile.toPath(), content.getBytes(), StandardOpenOption.WRITE);

File outputFile = folder.newFile();
compiler.compile(sourceFile.toPath(), Optional.of(outputFile.toPath()));
if (errorCount > 0) {
throw new Exception("Compilation failed with " + errorCount + " errors");
File outputFile = folder.newFile();
compiler.compile(sourceFile.toPath(), Optional.of(outputFile.toPath()));
if (errorCount > 0) {
throw new RuntimeException("Compilation failed with " + errorCount + " errors");
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public void testCopyrightIsKnown() {
}

@Test
public void testForwardAbsoluteJump() throws Exception {
public void testForwardAbsoluteJump() {
compile(
"now: mov a,b\n" +
"cpi 'C'\n" +
Expand All @@ -49,7 +49,7 @@ public void testForwardAbsoluteJump() throws Exception {
}

@Test
public void testBackwardAbsoluteJump() throws Exception {
public void testBackwardAbsoluteJump() {
compile(
"now: mov a,b\n" +
"cpi 'C'\n" +
Expand All @@ -63,7 +63,7 @@ public void testBackwardAbsoluteJump() throws Exception {
}

@Test
public void testCallBackward() throws Exception {
public void testCallBackward() {
compile(
"dcx sp\n" +
"now: mov a,b\n" +
Expand All @@ -78,7 +78,7 @@ public void testCallBackward() throws Exception {
}

@Test
public void testCallForward() throws Exception {
public void testCallForward() {
compile(
"dcx sp\n" +
"now: mov a,b\n" +
Expand All @@ -93,12 +93,12 @@ public void testCallForward() throws Exception {
}

@Test(expected = Exception.class)
public void testRSTtooBigArgument() throws Exception {
public void testRSTtooBigArgument() {
compile("rst 10");
}

@Test
public void testDCXwithLXI() throws Exception {
public void testDCXwithLXI() {
compile(
"dcx sp\n"
+ "lxi h, text\n"
Expand All @@ -112,7 +112,7 @@ public void testDCXwithLXI() throws Exception {
}

@Test
public void testINthenJMP() throws Exception {
public void testINthenJMP() {
compile(
"jmp sample\n"
+ "in 10h\n"
Expand All @@ -126,7 +126,7 @@ public void testINthenJMP() throws Exception {
}

@Test
public void testGetChar() throws Exception {
public void testGetChar() {
compile(
"jmp sample\n"
+ "getchar:\n"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
public class ConstantsAndVariablesTest extends AbstractCompilerTest {

@Test
public void testLabelAsConstantWorks() throws Exception {
public void testLabelAsConstantWorks() {
compile(
"here equ 0\n"
+ "mov a,b\n"
Expand All @@ -36,7 +36,7 @@ public void testLabelAsConstantWorks() throws Exception {
}

@Test
public void testConstantAsLabelWorks() throws Exception {
public void testConstantAsLabelWorks() {
compile(
"here equ there\n"
+ "there: mov a,b\n"
Expand All @@ -49,7 +49,7 @@ public void testConstantAsLabelWorks() throws Exception {
}

@Test(expected = Exception.class)
public void testRecursiveConstantDefinitionsDoesNotWork() throws Exception {
public void testRecursiveConstantDefinitionsDoesNotWork() {
compile(
"here equ there\n"
+ "there equ here\n"
Expand All @@ -58,21 +58,21 @@ public void testRecursiveConstantDefinitionsDoesNotWork() throws Exception {
}

@Test(expected = Exception.class)
public void testTwoSameLabelsDoNotWork() throws Exception {
public void testTwoSameLabelsDoNotWork() {
compile(
"here:\nhere:\njz here"
);
}

@Test(expected = Exception.class)
public void testTwoSameConstantsDoNotWork() throws Exception {
public void testTwoSameConstantsDoNotWork() {
compile(
"here equ 0\nhere equ 1"
);
}

@Test
public void testVariableCanBeOverwritten() throws Exception {
public void testVariableCanBeOverwritten() {
compile(
"here set 0\nhere set 1\ncpi here"
);
Expand All @@ -82,21 +82,21 @@ public void testVariableCanBeOverwritten() throws Exception {
}

@Test(expected = Exception.class)
public void testCannotSetVariableBecauseIdentifierIsAlreadyDefined() throws Exception {
public void testCannotSetVariableBecauseIdentifierIsAlreadyDefined() {
compile(
"here equ 0\nhere set 1\n"
);
}

@Test(expected = Exception.class)
public void testCannotDefineConstantBecauseIdentifierIsAlreadyDefined() throws Exception {
public void testCannotDefineConstantBecauseIdentifierIsAlreadyDefined() {
compile(
"here: db 4\nhere equ 1\n"
);
}

@Test
public void testForwardReferenceOfConstantShouldWork() throws Exception {
public void testForwardReferenceOfConstantShouldWork() {
compile("LXI SP,STACK\n" +
"TEMPP: DW TEMP0\n" +
"TEMP0: DS 1\n" +
Expand All @@ -108,7 +108,7 @@ public void testForwardReferenceOfConstantShouldWork() throws Exception {
}

@Test(expected = Exception.class)
public void testUnknownIdentifier() throws Exception {
public void testUnknownIdentifier() {
compile("LXI SP,STACK");
}
}
Loading

0 comments on commit 28a6278

Please sign in to comment.