Skip to content

Commit eeb67e4

Browse files
committed
Fix and test for GRAILS-9084 Implement asBoolean (Groovy truth) for GroovyPage.ConstantClosure
1 parent 9a0f2e2 commit eeb67e4

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package org.codehaus.groovy.grails.web.pages
2+
3+
import grails.test.AbstractGrailsEnvChangingSpec
4+
5+
import spock.lang.Specification
6+
import grails.test.mixin.TestFor
7+
import grails.util.Environment;
8+
import grails.artefact.Artefact
9+
10+
11+
@TestFor(CustomTagLib)
12+
class OptionalTagBodySpec extends AbstractGrailsEnvChangingSpec {
13+
def "Test that the existence of a body can be tested with groovy truth"(grailsEnv) {
14+
when:
15+
changeGrailsEnv(grailsEnv)
16+
def content = applyTemplate("<a:myBody />")
17+
def content2 = applyTemplate("<a:myBody>Hello</a:myBody>")
18+
then:
19+
content == 'nobody'
20+
content2 == 'Hello'
21+
where:
22+
grailsEnv << grailsEnvs
23+
}
24+
}
25+
@Artefact("TagLibrary")
26+
class CustomTagLib {
27+
static namespace = "a"
28+
29+
def myBody = { attrs, body ->
30+
if(body) {
31+
out << body()
32+
} else {
33+
out << 'nobody'
34+
}
35+
}
36+
}

grails-web/src/main/groovy/org/codehaus/groovy/grails/web/pages/GroovyPage.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
import org.codehaus.groovy.grails.web.taglib.exceptions.GrailsTagException;
4848
import org.codehaus.groovy.grails.web.util.CodecPrintWriter;
4949
import org.codehaus.groovy.grails.web.util.GrailsPrintWriter;
50+
import org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation;
5051

5152
/**
5253
* NOTE: Based on work done by on the GSP standalone project (https://gsp.dev.java.net/)
@@ -152,6 +153,10 @@ public Object doCall(@SuppressWarnings("unused") Object[] args) {
152153
public Object call(Object... args) {
153154
return retval;
154155
}
156+
157+
public boolean asBoolean() {
158+
return DefaultTypeTransformation.castToBoolean(retval);
159+
}
155160
}
156161

157162
protected static final Closure<?> EMPTY_BODY_CLOSURE = new ConstantClosure(BLANK_STRING);

0 commit comments

Comments
 (0)