Echo working.
This commit is contained in:
parent
d05b4f4c0f
commit
6af7e4fd82
@ -14,12 +14,15 @@ public class StandardGStringTemplateViewMetaClass extends ExpandoMetaClass {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(StandardGStringTemplateViewMetaClass.class);
|
||||
|
||||
private static StandardGStringTemplateView asView(Object object) {
|
||||
private static StandardGStringTemplateView asStandardGStringTemplateView(Object object) {
|
||||
return object instanceof StandardGStringTemplateView view ? view : null;
|
||||
}
|
||||
|
||||
private static Object[] asArgsArray(Object object) {
|
||||
return object instanceof Object[] objects ? objects : new Object[] { object };
|
||||
return object instanceof Object[] objects
|
||||
? objects
|
||||
: new Object[] { object }
|
||||
;
|
||||
}
|
||||
|
||||
private static MetaMethod findMetaMethod(MetaClass metaClass, String methodName, Object[] argsArray) {
|
||||
@ -57,9 +60,9 @@ public class StandardGStringTemplateViewMetaClass extends ExpandoMetaClass {
|
||||
|
||||
@Override
|
||||
public Object invokeMethod(Object object, String methodName, Object arguments) {
|
||||
final StandardGStringTemplateView view = asView(object);
|
||||
final StandardGStringTemplateView view = asStandardGStringTemplateView(object);
|
||||
if (view == null) {
|
||||
logger.warn("StandardGStringTemplateViewMetaClass should only be used as a MetaClass of StandardGStringTemplateViewMetaClass.");
|
||||
warnWrongType(object);
|
||||
return super.invokeMethod(object, methodName, arguments);
|
||||
}
|
||||
|
||||
@ -76,7 +79,7 @@ public class StandardGStringTemplateViewMetaClass extends ExpandoMetaClass {
|
||||
final Object[] argsArray = asArgsArray(arguments);
|
||||
|
||||
// self
|
||||
final MetaMethod selfMethod = findMetaMethod(this, methodName, argsArray);
|
||||
final MetaMethod selfMethod = this.getMetaMethod(methodName, argsArray);
|
||||
if (selfMethod != null) {
|
||||
return selfMethod.invoke(view, argsArray);
|
||||
}
|
||||
@ -84,11 +87,12 @@ public class StandardGStringTemplateViewMetaClass extends ExpandoMetaClass {
|
||||
// parent hierarchy
|
||||
View parent = view.getParent();
|
||||
while (parent != null) {
|
||||
final var parentMetaMethod = findMetaMethod(findMetaClass(parent), methodName, argsArray);
|
||||
final var parentMetaClass = findMetaClass(parent);
|
||||
final var parentMetaMethod = parentMetaClass.getMetaMethod(methodName, argsArray);
|
||||
if (parentMetaMethod != null) {
|
||||
return parentMetaMethod.invoke(parent, argsArray);
|
||||
} else if (parent instanceof StandardGStringTemplateView) {
|
||||
parent = ((StandardGStringTemplateView) parent).getParent();
|
||||
} else if (parent instanceof StandardGStringTemplateView standardGStringTemplateView) {
|
||||
parent = standardGStringTemplateView.getParent();
|
||||
} else {
|
||||
parent = null;
|
||||
}
|
||||
@ -99,7 +103,7 @@ public class StandardGStringTemplateViewMetaClass extends ExpandoMetaClass {
|
||||
|
||||
@Override
|
||||
public Object getProperty(Object object, String name) {
|
||||
final StandardGStringTemplateView view = asView(object);
|
||||
final StandardGStringTemplateView view = asStandardGStringTemplateView(object);
|
||||
if (view == null) {
|
||||
warnWrongType(object);
|
||||
return super.getProperty(object, name);
|
||||
@ -136,7 +140,7 @@ public class StandardGStringTemplateViewMetaClass extends ExpandoMetaClass {
|
||||
|
||||
@Override
|
||||
public void setProperty(Object object, String name, Object value) {
|
||||
final StandardGStringTemplateView view = asView(object);
|
||||
final StandardGStringTemplateView view = asStandardGStringTemplateView(object);
|
||||
if (view == null) {
|
||||
warnWrongType(object);
|
||||
super.setProperty(object, name, value);
|
||||
|
@ -10,37 +10,32 @@ class Echo extends DelegatingWebViewComponent {
|
||||
|
||||
static final class EchoFactory implements ComponentFactory<Echo> {
|
||||
|
||||
Echo doCreate(String typeName, ComponentContext context, Map<String, Object> attr) {
|
||||
doCreate(typeName, context, attr, true)
|
||||
Echo doCreate(String typeName, boolean selfClose) {
|
||||
doCreate(typeName, [:], selfClose)
|
||||
}
|
||||
|
||||
Echo doCreate(String typeName, ComponentContext context, Map<String, Object> attr, boolean selfClose) {
|
||||
Echo doCreate(String typeName, Map<String, Object> attr) {
|
||||
doCreate(typeName, attr, true)
|
||||
}
|
||||
|
||||
Echo doCreate(String typeName, Map<String, Object> attr, boolean selfClose) {
|
||||
def echo = new Echo(attr, typeName, selfClose)
|
||||
echo.context = context
|
||||
echo
|
||||
}
|
||||
|
||||
Echo doCreate(
|
||||
String typeName,
|
||||
ComponentContext context,
|
||||
Map<String, Object> attr,
|
||||
List<WebViewChildComponentRenderer> children
|
||||
) {
|
||||
def echo = new Echo(attr, typeName, false)
|
||||
echo.context = context
|
||||
echo.childRenderers = children
|
||||
echo
|
||||
}
|
||||
|
||||
@Override
|
||||
Echo create(String type, ComponentContext componentContext, Object... args) {
|
||||
if (args == null || args.length < 1) {
|
||||
throw new IllegalArgumentException(
|
||||
'<Echo> must have at least one attribute. ' +
|
||||
'If you are just echoing children, use a fragment (<>...</>) instead. '
|
||||
)
|
||||
}
|
||||
this.invokeMethod('doCreate', type as String, componentContext, *args) as Echo
|
||||
this.doCreate(type, *args) as Echo
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -50,12 +45,13 @@ class Echo extends DelegatingWebViewComponent {
|
||||
|
||||
}
|
||||
|
||||
String typeName
|
||||
String name
|
||||
boolean selfClose
|
||||
|
||||
Echo(Map<String, Object> attr, String typeName, boolean selfClose) {
|
||||
Echo(Map<String, Object> attr, String name, boolean selfClose) {
|
||||
super(attr)
|
||||
this.typeName = typeName
|
||||
this.name = name
|
||||
this.selfClose = selfClose
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -66,18 +62,20 @@ class Echo extends DelegatingWebViewComponent {
|
||||
)
|
||||
}
|
||||
|
||||
void renderAttr(Writer out) {
|
||||
String formatAttr() {
|
||||
def sb = new StringBuilder()
|
||||
def iter = this.attr.iterator()
|
||||
while (iter.hasNext()) {
|
||||
def entry = iter.next()
|
||||
out << entry.key
|
||||
out << '="'
|
||||
out << entry.value
|
||||
out << '"'
|
||||
sb << entry.key
|
||||
sb << '="'
|
||||
sb << entry.value
|
||||
sb << '"'
|
||||
if (iter.hasNext()) {
|
||||
out << ' '
|
||||
sb << ' '
|
||||
}
|
||||
}
|
||||
sb.toString()
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1 +1 @@
|
||||
<$typeName ${renderAttr()}${selfClose ? ' /' : ''}>${renderChildren()}${!selfClose ? "</$typeName>" : ''}
|
||||
<$name ${formatAttr()}${selfClose ? '/' : ''}>${hasChildren() ? renderChildren() : ''}${hasChildren() || !selfClose ? "</$name>" : ''}
|
||||
|
@ -0,0 +1,16 @@
|
||||
package groowt.view.web.lib
|
||||
|
||||
import groowt.view.web.DefaultWebViewComponentContext
|
||||
import org.junit.jupiter.api.Test
|
||||
|
||||
class EchoTests extends AbstractWebViewComponentTests {
|
||||
|
||||
@Test
|
||||
void typeOnlySelfClose() {
|
||||
def context = new DefaultWebViewComponentContext()
|
||||
context.pushDefaultScope()
|
||||
context.currentScope.add('Echo', new Echo.EchoFactory())
|
||||
this.doTest('<Echo(true) />', '<Echo />', context)
|
||||
}
|
||||
|
||||
}
|
@ -7,7 +7,7 @@ import groowt.view.web.DefaultWebViewComponentContext
|
||||
import groowt.view.web.WebViewTemplateComponentSource
|
||||
import org.junit.jupiter.api.Test
|
||||
|
||||
class FragmentTests extends AbstractComponentTests {
|
||||
class FragmentTests extends AbstractWebViewComponentTests {
|
||||
|
||||
static class Greeter extends DefaultWebViewComponent {
|
||||
|
||||
|
@ -14,7 +14,7 @@ import java.io.StringWriter;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
public abstract class AbstractComponentTests {
|
||||
public abstract class AbstractWebViewComponentTests {
|
||||
|
||||
protected void doTest(Reader source, String expected, ComponentContext context) {
|
||||
final var compiler = new DefaultWebComponentTemplateCompiler(
|
||||
@ -25,7 +25,7 @@ public abstract class AbstractComponentTests {
|
||||
final StringWriter sw = new StringWriter();
|
||||
final WebViewComponentWriter out = new WebViewComponentWriter(sw);
|
||||
renderer.call(context, out);
|
||||
assertEquals(expected, sw.toString());
|
||||
assertEquals(expected, sw.toString().trim());
|
||||
}
|
||||
|
||||
protected void doTest(String source, String expected, ComponentContext context) {
|
Loading…
Reference in New Issue
Block a user