Grails的 - 你怎么传递非字符串变量的taglib ATTRS地图使用的测试与ApplyTemplate在?

问题描述 投票:-1回答:1

Grails的3.3.9,+标记库

我创建这样一个新的标签库

import java.time.LocalDateTime
import java.time.format.DateTimeFormatter

class JavaDateTimeTagLib {
    static defaultEncodeAs = [taglib:'html']
    //static encodeAsForTags = [tagName: [taglib:'html'], otherTagName: [taglib:'none']]
    static encodeAsForTags = [testCall: [taglib:'none']]

    static namespace = "jdt"        //java8 date time name space for tags

    def testCall = { attrs ->

        def p1 = attrs.p1
        def p2 = attrs.p2
        out << "p1:'$p1' with class ${p1.getClass()}"
        out << "p2:'$p2' with class ${p2.getClass()}"
    }
}

在这里我想非字符串变量传递给ATTRS地图。

i,那么设置测试像这样的

class JavaDateTimeTagLibSpec extends Specification implements TagLibUnitTest<JavaDateTimeTagLib> {

    def setup() {
    }

    def cleanup() {
    }

    /**
     * restriction params must be quoted values - esentially strings
     * taglib has to take that and do any conversions in the taglib
     * output by defaults is encoded html using std codec
     */
    void "call displayDateTime tag "() {
        given:

        String result = applyTemplate('<jdt:testCall p1="p1-string" p2="$now"  />' , [now:LocalDateTime.now()])

        when :
        println "$result "

        then:
        result
    }
}

我想要做的是一个LocalDateTime变量传递给ATTRS地图。

如果使用ApplyTemplate在与通P2 = LDT和测试图[LDT:LocalDateTime.now()]的测试失败称变量必须“引

[Byte array resource [test_1549319950845]:1] Attribute value must be quoted (p1="p1-string" p2=now).

如果使用P2 =“$ LDT”并测试映射SNAME作为引用变量P2 [LDT:LocalDateTime.now()],则测试将工作 - 但是传递给ATTRS地图类型是GStringImpl

但是看完OCI引导oci tag lib guide

这意味着4页,你可以通过attrs.employees作为domainobjects的列表,并使用在你的标记上

但没有办法,这个使用测试,一切都必须串报价调用 - 这使得它GStringImpl

你如何从appyTemplate传递非字符串变量的标记库ATTRS地图(..我想,不管restiction适用于现场GSP,而不仅仅是测试框架)

unit-testing grails taglib
1个回答
0
投票

哈!它的这样一个小东西

如果你改变了测试,并写这个

    String result = applyTemplate('<jdt:testCall p1="p1-string" p2="${now}"  />' , [now:LocalDateTime.now()])

用大括号轮从地图变量的键名 - 然后输入变量的实际值被传递到的taglib。所以,如果你在attrs.p2值看它实际的LocalDateTime实例。

现在很清楚如何ApplyTemplate在工作不知道,但希望它试图做的像常规外壳soemthing评估字符串 - 因此需要使用单“”像'

希望是有道理的

啊呀足以让你的头,有时发生爆炸

© www.soinside.com 2019 - 2024. All rights reserved.