Skip to content

Commit

Permalink
Add unit tests of REPL
Browse files Browse the repository at this point in the history
  • Loading branch information
predatorray committed Feb 21, 2016
1 parent 1122fd3 commit 7b7c8d5
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 3 deletions.
8 changes: 8 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19</version>
<configuration>
<argLine>-Dfile.encoding=UTF-8</argLine>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>cobertura-maven-plugin</artifactId>
Expand Down
10 changes: 8 additions & 2 deletions src/main/java/me/predatorray/bud/lisp/BudRepl.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,13 @@ public class BudRepl {
private final boolean interactive;

public BudRepl() {
this(true);
}

public BudRepl(boolean interactive) {
this.evaluator = new NaiveEvaluator();
this.initial = BuiltinsEnvironment.INSTANCE;
this.interactive = true;
this.interactive = interactive;
}

private class ReplParserCallback implements Parser.ParserCallback {
Expand Down Expand Up @@ -74,6 +78,9 @@ public void execute(Reader reader, final Writer writer, Writer errorWriter) thro

boolean exceptionOccurred = false;
while (true) {
if (exceptionOccurred) {
parser = new Parser(replParserCallback);
}
try {
if (interactive) {
if (replParserCallback.hasAnyExpressionBeenEvaluated() || exceptionOccurred) {
Expand All @@ -89,7 +96,6 @@ public void execute(Reader reader, final Writer writer, Writer errorWriter) thro
break;
}

// TODO if ended with backslash
Lexer lexer = new Lexer(line);
Iterator<Token> tokenIterator = lexer.iterator();
parser.feed(tokenIterator);
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/me/predatorray/bud/lisp/lang/BudString.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package me.predatorray.bud.lisp.lang;

import me.predatorray.bud.lisp.util.StringUtils;
import me.predatorray.bud.lisp.util.Validation;

public class BudString implements BudObject {
Expand Down Expand Up @@ -36,6 +37,6 @@ public int hashCode() {

@Override
public String toString() {
return value;
return StringUtils.quote(value);
}
}
1 change: 1 addition & 0 deletions src/main/java/me/predatorray/bud/lisp/util/Sets.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

public class Sets {

@SafeVarargs
public static <T> Set<T> asSet(T ...ts) {
Validation.notNull(ts, "sets must not be null");
return Collections.unmodifiableSet(new HashSet<T>(Arrays.asList(ts)));
Expand Down
53 changes: 53 additions & 0 deletions src/test/java/me/predatorray/bud/lisp/BudReplTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package me.predatorray.bud.lisp;

import me.predatorray.bud.lisp.test.NullWriter;
import org.junit.Before;
import org.junit.Test;

import java.io.*;

import static org.junit.Assert.*;

public class BudReplTest {

private BudRepl repl;

@Before
public void setUpFixtureOfRepl() {
repl = new BudRepl(false);
}

private String readEvaluateAndPrint(String input) throws IOException {
try (Reader reader = new StringReader(input);
Writer writer = new StringWriter()) {
repl.execute(reader, writer, new NullWriter());
return writer.toString();
}
}

@Test
public void testRepl1() throws Exception {
String output = readEvaluateAndPrint("\"abc\"");
assertEquals("\"abc\"\n", output);
}

@Test
public void testRepl2() throws Exception {
readEvaluateAndPrint("unknown");
String output = readEvaluateAndPrint("\"abc\"");
assertEquals("\"abc\"\n", output);
}

@Test
public void testRepl3() throws Exception {
String output = readEvaluateAndPrint("(+ 1 1) 3");
assertEquals("2\n3\n", output);
}

@Test
public void testRepl4() throws Exception {
String output = readEvaluateAndPrint("(+ 1 1) (\n" +
"- 1 1)\n");
assertEquals("2\n0\n", output);
}
}
19 changes: 19 additions & 0 deletions src/test/java/me/predatorray/bud/lisp/test/NullWriter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package me.predatorray.bud.lisp.test;

import java.io.IOException;
import java.io.Writer;

public class NullWriter extends Writer {

@Override
public void write(char[] cbuf, int off, int len) throws IOException {
}

@Override
public void flush() throws IOException {
}

@Override
public void close() throws IOException {
}
}

0 comments on commit 7b7c8d5

Please sign in to comment.