package triangle.syntacticAnalyser; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertThrows; import org.junit.Test; import org.junit.function.ThrowingRunnable; import triangle.ErrorReporter; import triangle.abstractSyntaxTrees.AbstractSyntaxTree; import triangle.syntacticAnalyzer.Parser; import triangle.syntacticAnalyzer.Scanner; import triangle.syntacticAnalyzer.SourceFile; public class TestScanner { @Test public void testHi() { // build.gradle has a line sourceSets.test.resources.srcDir file("$rootDir/programs") // which adds the programs directory to the list of places Java can easily find files // getResource() below searches for a file, which is in /programs SourceFile source = SourceFile.ofPath(this.getClass().getResource("/hi.tri").getFile().toString()); Scanner scanner = new Scanner(source); ErrorReporter reporter = new ErrorReporter(true); Parser parser = new Parser(scanner, reporter); parser.parseProgram(); // we should get to here with no exceptions assertEquals("Problem compiling hi.tri", 0, reporter.getNumErrors()); } @Test public void testHiNewComment() { SourceFile source = SourceFile.ofPath(this.getClass().getResource("/hi-newcomment.tri").getFile().toString()); Scanner scanner = new Scanner(source); ErrorReporter reporter = new ErrorReporter(true); Parser parser = new Parser(scanner, reporter); // we expect an exception here as the program has invalid syntax assertThrows(RuntimeException.class, new ThrowingRunnable() { public void run(){ parser.parseProgram(); } }); // currently this program will fail assertNotEquals("Problem compiling hi-newcomment.tri", 0, reporter.getNumErrors()); } }