Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
  • 4 commits
  • 10 files changed
  • 0 commit comments
  • 1 contributor
Commits on Sep 22, 2012
Rodrigo B. de Oliveira split scripting engine instantiation from evaluation 72df323
Rodrigo B. de Oliveira preserve variables between evaluations f168528
Rodrigo B. de Oliveira code gardening 03c83e7
Rodrigo B. de Oliveira groovy progress 7e611e4
View
7 code/src/sneer/bricks/software/bricks/repl/Evaluator.java
@@ -0,0 +1,7 @@
+package sneer.bricks.software.bricks.repl;
+
+public interface Evaluator {
+
+ Object eval(String code);
+
+}
View
7 code/src/sneer/bricks/software/bricks/repl/Repl.java
@@ -2,9 +2,14 @@
import basis.brickness.Brick;
+// TODO: catch exceptions
+// TODO: add imports
+// TODO: reset engine
@Brick
public interface Repl {
- Object evaluate(ReplLang lang, String text);
+ Evaluator newEvaluatorFor(ReplLang groovy);
+
+ ReplConsole newConsoleFor(Evaluator evaluator);
}
View
8 code/src/sneer/bricks/software/bricks/repl/ReplConsole.java
@@ -0,0 +1,8 @@
+package sneer.bricks.software.bricks.repl;
+
+
+public interface ReplConsole {
+
+ String eval(String code);
+
+}
View
6 code/src/sneer/bricks/software/bricks/repl/gui/impl/ReplWindowImpl.java
@@ -28,7 +28,7 @@
public ReplWindowImpl() {
setLayout(new BorderLayout());
- add (_title , BorderLayout.NORTH);
+ add(_title , BorderLayout.NORTH);
super.add(textArea);
super.setSize(300, 300);
@@ -36,7 +36,7 @@ public ReplWindowImpl() {
textArea.addKeyListener(new KeyAdapter() {
@Override
public void keyReleased(KeyEvent e) {
- if(e.isControlDown() && e.getKeyCode() == KeyEvent.VK_ENTER) {
+ if (e.isControlDown() && e.getKeyCode() == KeyEvent.VK_ENTER) {
executeAndShowResult();
}
}
@@ -58,7 +58,7 @@ public void open() {
private void executeAndShowResult() {
String selectedText = textArea.getSelectedText();
if (selectedText != null && !selectedText.isEmpty()) {
- Object result = my(Repl.class).evaluate(ReplLang.groovy, selectedText);
+ Object result = my(Repl.class).newEvaluatorFor(ReplLang.groovy).eval(selectedText);
String resultString = "\n --> " + (result == null ? "null" : result.toString());
textArea.append(resultString);
}
View
14 code/src/sneer/bricks/software/bricks/repl/gui/tests/ReplWindowTest.java
@@ -0,0 +1,14 @@
+package sneer.bricks.software.bricks.repl.gui.tests;
+
+import org.junit.Test;
+
+import sneer.bricks.software.folderconfig.testsupport.BrickTestBase;
+
+public class ReplWindowTest extends BrickTestBase {
+
+ @Test
+ void exceptionsAreReportedToTheConsole() {
+ assertFalse(true);
+ }
+
+}
View
15 code/src/sneer/bricks/software/bricks/repl/impl/GroovyReplConsole.java
@@ -0,0 +1,15 @@
+package sneer.bricks.software.bricks.repl.impl;
+
+import groovy.lang.GroovyShell;
+import sneer.bricks.software.bricks.repl.Evaluator;
+
+public class GroovyReplConsole implements Evaluator {
+
+ private final GroovyShell shell = new GroovyShell();
+
+ @Override
+ public Object eval(String code) {
+ return shell.evaluate(code);
+ }
+
+}
View
13 code/src/sneer/bricks/software/bricks/repl/impl/ReplImpl.java
@@ -1,17 +1,20 @@
package sneer.bricks.software.bricks.repl.impl;
-import groovy.lang.GroovyShell;
import sneer.bricks.software.bricks.repl.Repl;
+import sneer.bricks.software.bricks.repl.Evaluator;
+import sneer.bricks.software.bricks.repl.ReplConsole;
import sneer.bricks.software.bricks.repl.ReplLang;
public class ReplImpl implements Repl {
- private GroovyShell shell = new GroovyShell();
+ @Override
+ public Evaluator newEvaluatorFor(ReplLang language) {
+ return new GroovyReplConsole();
+ }
@Override
- public Object evaluate(ReplLang lang, String text) {
- // binding.setVariable("foo", new Integer(2));
- return shell.evaluate(text);
+ public ReplConsole newConsoleFor(Evaluator evaluator) {
+ throw new basis.lang.exceptions.NotImplementedYet(); // Implement
}
}
View
36 code/src/sneer/bricks/software/bricks/repl/tests/GroovyEvaluatorTest.java
@@ -0,0 +1,36 @@
+package sneer.bricks.software.bricks.repl.tests;
+
+import static basis.environments.Environments.my;
+
+import org.junit.Test;
+
+import sneer.bricks.software.bricks.repl.Repl;
+import sneer.bricks.software.bricks.repl.Evaluator;
+import sneer.bricks.software.bricks.repl.ReplLang;
+import sneer.bricks.software.folderconfig.testsupport.BrickTestBase;
+
+public class GroovyEvaluatorTest extends BrickTestBase {
+
+ private final Repl subject = my(Repl.class);
+
+ @Test
+ public void groovyRepl() {
+ Evaluator evaluator = createEvaluator();
+ String text = "hello";
+ Object result = evaluator.eval("'" + text + "'");
+ assertEquals(text, result);
+ }
+
+ @Test
+ public void variablesArePreservedBetweenEvaluations() {
+ Evaluator evaluator = createEvaluator();
+ String text = "foo = 42";
+ Object result = evaluator.eval(text);
+ assertEquals(42, result);
+ assertEquals(42, evaluator.eval("foo"));
+ }
+
+ private Evaluator createEvaluator() {
+ return subject.newEvaluatorFor(ReplLang.groovy);
+ }
+}
View
32 code/src/sneer/bricks/software/bricks/repl/tests/ReplConsoleTest.java
@@ -0,0 +1,32 @@
+package sneer.bricks.software.bricks.repl.tests;
+
+
+import static basis.environments.Environments.my;
+
+import org.jmock.Expectations;
+import org.junit.Ignore;
+import org.junit.Test;
+
+import sneer.bricks.software.bricks.repl.Evaluator;
+import sneer.bricks.software.bricks.repl.Repl;
+import sneer.bricks.software.bricks.repl.ReplConsole;
+import sneer.bricks.software.folderconfig.testsupport.BrickTestBase;
+@Ignore
+public class ReplConsoleTest extends BrickTestBase {
+
+ @Test
+ public void exceptionsAreReported() {
+ final String code = "code";
+ final IllegalStateException expectedException = new IllegalStateException("oops...");
+
+ final Evaluator evaluator = mock(Evaluator.class);
+ ReplConsole console = my(Repl.class).newConsoleFor(evaluator);
+ checking(new Expectations() {{
+ exactly(1).of(evaluator).eval(code);
+ will(throwException(expectedException));
+ }});
+
+ String resultingText = console.eval(code);
+ assertTrue(resultingText.startsWith(expectedException.toString()));
+ }
+}
View
22 code/src/sneer/bricks/software/bricks/repl/tests/ReplTest.java
@@ -1,22 +0,0 @@
-package sneer.bricks.software.bricks.repl.tests;
-
-import static basis.environments.Environments.my;
-
-import org.junit.Test;
-
-import sneer.bricks.software.bricks.repl.Repl;
-import sneer.bricks.software.bricks.repl.ReplLang;
-import sneer.bricks.software.folderconfig.testsupport.BrickTestBase;
-
-public class ReplTest extends BrickTestBase {
-
- private final Repl subject = my(Repl.class);
-
- @Test
- public void testHello() {
- String text = "hello";
- Object result = subject.evaluate(ReplLang.groovy, "'" + text + "'");
- assertEquals(text, result);
- }
-
-}

No commit comments for this range

Something went wrong with that request. Please try again.