Skip to content

Commit

Permalink
GROOVY-9018: Make Interpreter changeable in Groovysh (closes #884)
Browse files Browse the repository at this point in the history
  • Loading branch information
je-ik authored and paulk-asert committed May 6, 2019
1 parent 6535ee7 commit 1c58239
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -98,18 +98,21 @@ class Groovysh extends Shell {
}

Groovysh(final ClassLoader classLoader, final Binding binding, final IO io, final Closure registrar, CompilerConfiguration configuration) {
this(classLoader, binding, io, registrar, configuration, new Interpreter(classLoader, binding, configuration))
}

Groovysh(final ClassLoader classLoader, final Binding binding, final IO io, final Closure registrar, CompilerConfiguration configuration, Interpreter interpreter) {
super(io)
assert classLoader
assert binding
assert registrar
def actualRegistrar = registrar ?: createDefaultRegistrar(classLoader)
parser = new Parser()
interp = new Interpreter(classLoader, binding, configuration)
registrar.call(this)
interp = interpreter
actualRegistrar.call(this)
this.packageHelper = new PackageHelperImpl(classLoader)
}

private static Closure createDefaultRegistrar(final ClassLoader classLoader) {

return {Groovysh shell ->
URL xmlCommandResource = getClass().getResource('commands.xml')
if (xmlCommandResource != null) {
Expand All @@ -122,7 +125,7 @@ class Groovysh extends Shell {
}

Groovysh(final ClassLoader classLoader, final Binding binding, final IO io) {
this(classLoader, binding, io, createDefaultRegistrar(classLoader))
this(classLoader, binding, io, null)
}

Groovysh(final Binding binding, final IO io) {
Expand All @@ -134,8 +137,7 @@ class Groovysh extends Shell {
}

Groovysh(final IO io, CompilerConfiguration configuration) {
this(Thread.currentThread().contextClassLoader, new Binding(), io,
createDefaultRegistrar(Thread.currentThread().contextClassLoader), configuration)
this(Thread.currentThread().contextClassLoader, new Binding(), io, null, configuration)
}

Groovysh() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ class Interpreter implements Evaluator
return shell.classLoader
}

GroovyShell getShell() {
return shell
}

@Override
def evaluate(final Collection<String> buffer) {
assert buffer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,12 @@ abstract class CompletorTestSupport extends GroovyTestCase {
idCompletorMocker = new MockFor(IdentifierCompletor)

groovyshMocker = new MockFor(Groovysh)
groovyshMocker.demand.getClass(0..1) { Groovysh }
groovyshMocker.demand.createDefaultRegistrar { { shell -> null } }
groovyshMocker.demand.getIo(0..2) { testio }
packageHelperMocker = new MockFor(PackageHelperImpl)
def registry = new CommandRegistry()
groovyshMocker.demand.getRegistry(0..1) { registry }
groovyshMocker.demand.getClass(0..1) { Groovysh }
packageHelperMocker.demand.getContents(6) { ['java', 'test'] }
groovyshMocker.demand.getIo(0..2) { testio }
for (i in 1..19) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ abstract class ShellRunnerTestSupport extends GroovyTestCase {
// setup mock and stub with calls expected from InteractiveShellRunner Constructor

shellMocker = new MockFor(Groovysh)
shellMocker.demand.createDefaultRegistrar(1) { {Shell shell -> null} }
// when run with compileStatic
shellMocker.demand.getClass(0..1) {Groovysh}
shellMocker.demand.createDefaultRegistrar(1) { {Shell shell -> null} }
shellMocker.demand.getIo(2) { testio }
shellMocker.demand.getRegistry(1) {new Object() {def commands() {[]} }}
shellMocker.demand.getHistory(1) {new Serializable(){def size() {0}; def getMaxSize() {1}}}
Expand Down

0 comments on commit 1c58239

Please sign in to comment.