Skip to content

Commit

Permalink
Refactor Utils.print* a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
espenhw committed Mar 5, 2009
1 parent b5f0542 commit 0283007
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 25 deletions.
10 changes: 2 additions & 8 deletions src/main/groovy/org/grumblesmurf/malabar/GroovyServer.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,6 @@ class GroovyServer
{
static ready = new CountDownLatch(2);

static ThreadLocal<IO> io = new ThreadLocal<IO>();

static def getOut() {
return io.get()?.out ?: System.out
}

static void main(String[] args) {
ExpandoMetaClass.enableGlobally();

Expand Down Expand Up @@ -72,7 +66,7 @@ class GroovyServer

static startConsole() {
IO io = new IO();
GroovyServer.io.set(io);
Utils.setOut(io.out);
Binding binding = new Binding();
binding['io'] = io;
new Groovysh(binding, io).run();
Expand Down Expand Up @@ -112,7 +106,7 @@ class GroovySocketServer
Socket client = server.accept();
try {
IO io = new IO(client.inputStream, client.outputStream, client.outputStream);
GroovyServer.io.set(io);
Utils.setOut(io.out);
Binding binding = new Binding();
binding['io'] = io;
new Groovysh(binding, io).run();
Expand Down
36 changes: 25 additions & 11 deletions src/main/groovy/org/grumblesmurf/malabar/Utils.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -20,26 +20,40 @@ package org.grumblesmurf.malabar

class Utils
{
static ThreadLocal<PrintWriter> _out = new ThreadLocal<PrintWriter>();

static getOut() {
return _out.get() ?: System.out
}

static setOut(PrintWriter out) {
_out.set(out);
}

static print(Object v) {
GroovyServer.getOut().print(v);
getOut().print(v);
}

static println(Object v) {
GroovyServer.getOut().println(v);
getOut().println(v);
}

static printAsLispList(List list) {
print "("
list.each {
print asLispList(list)
}

static asLispList(List list) {
def result = new StringBuilder("(");
result << list.collect {
if (it instanceof String) {
print '"' + it + '"'
"\"${it}\""
} else if (it instanceof List) {
printAsLispList(it)
asLispList(it)
} else {
print it
it
}
print " "
}
print ")"
}.join(" ");
result << ")";
return result as String;
}
}
11 changes: 5 additions & 6 deletions src/test/groovy/org/grumblesmurf/malabar/ClasspathTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,9 @@ class ClasspathTest
@Before
void setIO() {
out = new ByteArrayOutputStream();
GroovyServer.io.set(new IO(System.in, out, out));
Utils.setOut(new IO(System.in, out, out).out)
}


@AfterClass
static void setExceptionHandler() {
Thread[] threads = new Thread[Thread.activeCount() * 2];
Expand Down Expand Up @@ -76,25 +75,25 @@ class ClasspathTest
@Test
void inputStreamImplementsCloseable() {
String result = classInfo("java.io.InputStream");
assertThat(result, containsString(':interfaces ("java.io.Closeable" )'));
assertThat(result, containsString(':interfaces ("java.io.Closeable")'));
}

@Test
void outputStreamImplementsCloseableAndFlushable() {
String result = classInfo("java.io.OutputStream");
assertThat(result, containsString(':interfaces ("java.io.Closeable" "java.io.Flushable" )'));
assertThat(result, containsString(':interfaces ("java.io.Closeable" "java.io.Flushable")'));
}

@Test
void collectionImplementsIterableE() {
String result = classInfo("java.util.Collection");
assertThat(result, containsString(':interfaces ("java.lang.Iterable<E>" )'));
assertThat(result, containsString(':interfaces ("java.lang.Iterable<E>")'));
}

@Test
void scannerImplementsIteratorString() {
String result = classInfo("java.util.Scanner");
assertThat(result, containsString(':interfaces ("java.util.Iterator<java.lang.String>" )'));
assertThat(result, containsString(':interfaces ("java.util.Iterator<java.lang.String>")'));
}

def classInfo(name) {
Expand Down
33 changes: 33 additions & 0 deletions src/test/groovy/org/grumblesmurf/malabar/UtilsTest.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* Copyright (c) 2009 Espen Wiborg <espenhw@grumblesmurf.org>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301 USA.
*/
package org.grumblesmurf.malabar;

import org.junit.Test;

import static org.junit.Assert.*
import static org.junit.matchers.JUnitMatchers.*
import static org.hamcrest.CoreMatchers.*

class UtilsTest
{
@Test
void emptyListIsEmpty() {
assertThat(Utils.asLispList([]), is("()"))
}
}

0 comments on commit 0283007

Please sign in to comment.