Added basic tests

This commit is contained in:
Jesper Saastamoinen 2024-09-17 13:35:30 +02:00
parent 2e96a26de1
commit 5cbacbc251
11 changed files with 150 additions and 6 deletions

View file

@ -26,7 +26,7 @@ public class Froj {
}
private void runFile(String path) throws IOException {
public void runFile(String path) throws IOException {
try {
byte[] bytes = Files.readAllBytes(Paths.get(path));
run(new String(bytes, Charset.defaultCharset()));

View file

@ -14,7 +14,7 @@ public class Parser {
private final List<Token> tokens;
private int current = 0;
Parser(List<Token> tokens) {
public Parser(List<Token> tokens) {
this.tokens = tokens;
}

View file

@ -1,7 +1,7 @@
package com.jsaasta.froj;
public class RuntimeError extends RuntimeException {
final Token token;
public final Token token;
public RuntimeError(Token token, String message) {
super(message);

View file

@ -37,7 +37,7 @@ public class Scanner {
private int line = 1;
Scanner(String source) {
public Scanner(String source) {
this.source = source;
}

View file

@ -2,7 +2,7 @@ package com.jsaasta.froj;
import java.util.List;
abstract class Stmt {
public abstract class Stmt {
interface Visitor<R> {
R visitBlockStmt(Block stmt);

View file

@ -4,7 +4,7 @@ public class Token {
public final TokenType type;
public final String lexeme;
public final Object literal;
final int line;
public final int line;
public Token(TokenType type, String lexeme, Object literal, int line) {
this.type = type;

View file

@ -0,0 +1,15 @@
package test.com.saasta.froj;
import com.jsaasta.froj.*;
import org.junit.Test;
import java.io.IOException;
public class FrojTest {
@Test
public void testHelloWorld() throws IOException {
Froj froj = new Froj();
froj.runFile("src/test/com/saasta/froj/froj_test.froj");
}
}

View file

@ -0,0 +1,109 @@
class HelloParent {
welcome(){
print "Hello from Parent";
}
}
class HelloWorld < HelloParent {
init(helloString){
this.helloString = helloString;
}
welcome(){
super.welcome();
print this.helloString;
}
}
var hello = "Hello World!";
var helloWorldObject = HelloWorld(hello);
helloWorldObject.welcome();
// Or just a simple:
print "Hello World";
// -----------------------------------------
class Test {
init(aBoolean){
if(aBoolean){
print "in true";
} else {
print "in false";
}
}
}
Test(false);
// ----------------------------------------
function controlFlowTest(someValue){
if(someValue == "if"){
print "in " + someValue;
} else if(someValue == "else if") {
print "in " + someValue;
} else {
print "in 'else', because the value was: " + someValue;
}
}
controlFlowTest("if");
controlFlowTest("else if");
controlFlowTest("something else entirely");
// ----------------------------------------
function testWhile(){
var a = 0;
while(a < 10){
a = a+1;
}
return a;
}
function testFor() {
var a = "";
for(var i = 0; i < 10; i = i + 1){
a = a + "a";
}
return a;
}
print testWhile();
print testFor();
// ---------------------------------------
function addTwoNumbers(a, b) {
return a + b;
}
function testCallBack(aFunction){
var a = 1;
var b = 2;
return aFunction(a, b);
}
print addTwoNumbers(4,5);
print testCallBack(addTwoNumbers);
// ---------------------------------------
class Person {
init(name){
this.name = name;
}
myNameIs() {
print this.name;
}
}
var janne = Person("Janne");
janne.name = "Janne";
janne.otherName = "Totally Not Janne";
janne.myNameIs();
print janne.otherName;
// ---------------------------------------

View file

@ -0,0 +1,18 @@
package test.com.saasta.froj.stdlib;
import com.jsaasta.froj.Froj;
import org.junit.Test;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintStream;
public class FrojStdlibTest {
@Test
public void test_Stdlib_Clock() throws IOException {
Froj froj = new Froj();
froj.runFile("src/test/com/saasta/froj/stdlib/froj_stdlib_clock.froj");
}
}

View file

@ -0,0 +1 @@
clock();

View file

@ -0,0 +1 @@
input();