Skip to content

Commit

Permalink
Small cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
madsravn committed Nov 12, 2023
1 parent 384a2da commit f903c9f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ public void testBangOperator() {
new EvalBooleanData("!!0", false),
new EvalBooleanData("!0", true)
);

for(EvalBooleanData input : inputs) {
IObject object = testEval(input.input);
testBooleanObject(object, input.value, input.input);
Expand All @@ -107,6 +108,7 @@ public void testIfElseExpression() {
new EvalIntegerData("if (1 > 2) { 10 } else { 20 }", 20),
new EvalIntegerData("if (1 < 2) { 10 } else { 20 }", 10)
);

for(EvalIntegerData input : inputs) {
IObject object = testEval(input.input);
if(input.value != null) {
Expand Down Expand Up @@ -160,6 +162,7 @@ public void testLetStatements() {
new EvalIntegerData("let a = 5; let b = a; b;", 5),
new EvalIntegerData("let a = 5; let b = a; let c = a + b + 5; c;", 15)
);

for(EvalIntegerData input : inputs) {
IObject object = testEval(input.input);
testIntegerObject(object, input.value);
Expand Down Expand Up @@ -224,6 +227,7 @@ public void testBuiltinFunctionsGood() {
new EvalIntegerData("len(\"four\")", 4),
new EvalIntegerData("len(\"Hello world\")", 11)
);

for(EvalIntegerData input : inputs) {
IObject object = testEval(input.input);
testIntegerObject(object, input.value);
Expand All @@ -236,6 +240,7 @@ public void testBuiltinFunctionsBad() {
new EvalStringData("len(1)","argument to `len` not supported, got INTEGER"),
new EvalStringData("len(\"one\", \"two\")", "wrong number of arguments. got=2, want=1")
);

for(EvalStringData input : inputs) {
IObject object = testEval(input.input);
testErrorObject(object, input.value);
Expand Down Expand Up @@ -294,9 +299,8 @@ public void testArrayBuiltins() {
new EvalIntegerArrayData("last([]);", null),
new EvalIntegerArrayData("push([2], 3);", Arrays.asList(2, 3)),
new EvalIntegerArrayData("first([]);", null)


);

for(EvalIntegerArrayData input : inputs) {
var evaluated = testEval(input.input);
if(input.value != null) {
Expand Down Expand Up @@ -348,6 +352,7 @@ public void testHashIndexExpressions() {
new EvalIntegerData("{true: 5}[true]", 5),
new EvalIntegerData("{false: 5}[false]", 5)
);

for(EvalIntegerData input: inputs) {
var evaluated = testEval(input.input);
if(input.value != null) {
Expand Down Expand Up @@ -378,6 +383,7 @@ private void testIntegerArrayObject(IObject object, List<Integer> elements) {
assertTrue(object instanceof ArrayObject);
ArrayObject arrayObject = (ArrayObject) object;
assertEquals(arrayObject.getElementsLength(), elements.size());

for (int i = 0; i < elements.size(); i++) {
assertTrue(arrayObject.getElements().get(i) instanceof IntegerObject);
IntegerObject integerObject = (IntegerObject) arrayObject.getElements().get(i);
Expand Down
13 changes: 5 additions & 8 deletions src/test/java/dk/madsravn/interpreter/parser/ParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ public void TestLetStatements() {
assertEquals(((LetStatement)program.getStatements().get(1)).getName().getValue(), "y", "Second LET identifier expected to be 'x', but was " + ((LetStatement)program.getStatements().get(1)).getName().getValue());
assertEquals(((LetStatement)program.getStatements().get(2)).getName().getValue(), "foobar", "Third LET identifier expected to be 'x', but was " + ((LetStatement)program.getStatements().get(2)).getName().getValue());

assertEquals(program.getStatements().get(0).tokenLiteral(), "let", "First LET identifier expected to be 'x', but was " + program.getStatements().get(0).tokenLiteral());
assertEquals(program.getStatements().get(1).tokenLiteral(), "let", "Second LET identifier expected to be 'x', but was " + program.getStatements().get(1).tokenLiteral());
assertEquals(program.getStatements().get(2).tokenLiteral(), "let", "Third LET identifier expected to be 'x', but was " + program.getStatements().get(2).tokenLiteral());
assertEquals(program.getStatements().get(0).tokenLiteral(), "let");
assertEquals(program.getStatements().get(1).tokenLiteral(), "let");
assertEquals(program.getStatements().get(2).tokenLiteral(), "let");

}

Expand Down Expand Up @@ -71,7 +71,6 @@ public void TestLetStatementWithErrors() {
Lexer lexer = new Lexer(input);
Parser parser = new Parser(lexer);

// TODO: Should the errors be on the program or on the parser?
Program program = parser.parseProgram();
assertEquals(parser.getErrors().size(), 5);
}
Expand All @@ -82,8 +81,8 @@ public void testStringMethods() {
Lexer lexer = new Lexer(input);
Parser parser = new Parser(lexer);
Program program = parser.parseProgram();
checkForParseErrors(parser, input);

assertEquals(parser.getErrors().size(), 0);
assertEquals(program.string(), "let myVar = 5;");
}

Expand All @@ -92,9 +91,8 @@ public void testIdentifierExpression() {
String input = "foobar;";
Lexer lexer = new Lexer(input);
Parser parser = new Parser(lexer);

Program program = parser.parseProgram();
assertEquals(parser.getErrors().size(), 0);
checkForParseErrors(parser, input);

assertEquals(program.getStatementsLength(), 1);
IStatement statement = program.getStatements().get(0);
Expand Down Expand Up @@ -171,7 +169,6 @@ public void testParsingBooleanInfixExpressions() {
assertEquals(leftBooleanLiteral.getValue(), infixData.leftValue);
assertEquals(leftBooleanLiteral.tokenLiteral(), "" + infixData.leftValue);
}

}

private record InfixDataInteger(String input, int leftValue, String operator, int rightValue) { }
Expand Down

0 comments on commit f903c9f

Please sign in to comment.