Skip to content

Commit

Permalink
Test drive reimplementation of typeString
Browse files Browse the repository at this point in the history
[#36 state:resolved]
  • Loading branch information
espenhw committed Mar 17, 2009
1 parent 66754f2 commit f241337
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 36 deletions.
60 changes: 25 additions & 35 deletions src/main/groovy/org/grumblesmurf/malabar/Classpath.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -334,46 +334,36 @@ class Classpath
}
}

def typeString(type) {
return typeString(type, false);
}

def typeString(type, qualify) {
def typeString(type, qualify=false) {
def str;

if (type instanceof Class) {
def name = qualify ? type.name : type.simpleName
if (type.enclosingClass) {
return typeString(type.enclosingClass, qualify) + "." + name
}
return name
}
if (type instanceof GenericArrayType) {
return typeString(type.genericComponentType, qualify) + "[]"
}
if (type instanceof ParameterizedType) {
def str = typeString(type.rawType, qualify)
str += "<";
type.actualTypeArguments.eachWithIndex{ it, i ->
if (i > 0) {
str += ", "
}
str += typeString(it, qualify)
str = type.name;

if (type.typeParameters) {
str += "<"
str += type.typeParameters.join(", ")
str += ">"
}
str += ">"
return str;
}
if (type instanceof TypeVariable) {
def str = type.name
if (type.bounds) {
if (type.bounds.length > 1 ||
type.bounds[0] != Object.class) {
str += " extends "
str += type.bounds.collect{ typeString(it, qualify) }.join(" & ")
}
} else if (type instanceof TypeVariable) {
str = type.name;
if (type.bounds.length > 1 ||
type.bounds[0] != Object) {
str += " extends "
str += type.bounds.collect{ typeString(it, qualify) }.join(" & ")
}
return str
} else {
str = type.toString();
}

return type.toString()
if (qualify) {
return str;
}
def brackpos = str.indexOf("<")
if (brackpos < 0)
brackpos = str.length()

return str.substring(str.lastIndexOf(".", brackpos) + 1)
}

def methodDescriptor(Constructor cons) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class ClasspathTest
}
}
}

@Test
void objectHasNoSuper() {
String result = classInfo("java.lang.Object");
Expand Down Expand Up @@ -96,6 +96,12 @@ class ClasspathTest
assertThat(result, containsString(':interfaces ("java.util.Iterator<java.lang.String>")'));
}

@Test
void classInfoOfEnumDoesntInfLoop() {
String result = classInfo("java.lang.Enum");
assertThat(result, containsString('valueOf'));
}

def classInfo(name) {
new Classpath().getClassInfo(name);
return out.toString();
Expand Down
96 changes: 96 additions & 0 deletions src/test/groovy/org/grumblesmurf/malabar/TypestringTest.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/**
* 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 org.junit.Before;

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

class TypestringTest
{
def cp;

@Before
void createClasspath() {
cp = new Classpath();
}

@Test
void typeStringOfObject() {
assertThat(cp.typeString(Object.class), is("Object"))
}

@Test
void qualifiedTypeStringOfObject() {
assertThat(cp.typeString(Object.class, true), is("java.lang.Object"))
}

@Test
void typeStringOfCollections() {
assertThat(cp.typeString(Collections.class), is("Collections"))
}

@Test
void qualifiedTypeStringOfCollections() {
assertThat(cp.typeString(Collections.class, true), is("java.util.Collections"))
}

@Test
void collectionIsGeneric() {
assertThat(cp.typeString(Collection.class, true), is("java.util.Collection<E>"))
}

@Test
void mapHasTwoTypeparams() {
assertThat(cp.typeString(Map.class, true), is("java.util.Map<K, V>"))
}

@Test
void collectionsAddAllHasGenericFirstParam() {
assertThat(cp.typeString(Collections.methods.find {it.name == 'addAll'}.genericParameterTypes[0], true),
is("java.util.Collection<? super T>"))
}

@Test
void collectionsBinarySearchHasComplicatedFirstParam() {
assertThat(cp.typeString(Collections.methods.find {it.name == 'binarySearch'}.genericParameterTypes[0], true),
is("java.util.List<? extends java.lang.Comparable<? super T>>"))
}

@Test
void collectionsMaxHasAbsurdlyComplicatedTypeParam() {
assertThat(cp.typeString(Collections.getMethod("max", [ Collection ] as Class[]).typeParameters[0], true),
is("T extends java.lang.Object & java.lang.Comparable<? super T>"))
}

@Test
void collectionsMaxHasAbsurdlyComplicatedTypeParamUnqualified() {
assertThat(cp.typeString(Collections.getMethod("max", [ Collection ] as Class[]).typeParameters[0]),
is("T extends Object & Comparable<? super T>"))
}

@Test
void enumHasRecursiveInterface() {
assertThat(cp.typeString(Enum.genericInterfaces[0], true),
is("java.lang.Comparable<E>"));
}
}

0 comments on commit f241337

Please sign in to comment.