在类的所有实例方法中隐式使用 Groovy Category

发布于 2024-10-30 07:25:17 字数 677 浏览 4 评论 0原文

我有简单的 Groovy 类别类,它将方法添加到 String 实例:

final class SampleCategory {

    static String withBraces(String self) {
        "($self)"
    }

}

我想在我的单元测试中使用此类别(例如)。它看起来像这样:

class MyTest {
    @Test
    void shouldDoThis() {
        use (SampleCategory) {
            assert 'this'.withBraces() == '(this)'
        }
    }

    @Test
    void shouldDoThat() {
        use (SampleCategory) {
            assert 'that'.withBraces() == '(that)'
        }
    }
}

但是,我想要实现的是能够指定类别 SampleCategoryMyTest 的每个实例方法的范围内使用,所以我不必在每个方法中指定 use(SampleCategory) { ... }

是否可以?

I have simple Groovy category class which adds method to String instances:

final class SampleCategory {

    static String withBraces(String self) {
        "($self)"
    }

}

I want to use this category in my unit tests (for example). It looks like this:

class MyTest {
    @Test
    void shouldDoThis() {
        use (SampleCategory) {
            assert 'this'.withBraces() == '(this)'
        }
    }

    @Test
    void shouldDoThat() {
        use (SampleCategory) {
            assert 'that'.withBraces() == '(that)'
        }
    }
}

What I'd like to achieve, however, is ability to specify that category SampleCategory is used in scope of each and every instance method of MyTest so I don't have to specify use(SampleCategory) { ... } in every method.

Is it possible?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

你没皮卡萌 2024-11-06 07:25:17

您可以使用 mixin 将类别直接应用于 String 的元类。将 null 分配给元类以将其重置为常规默认值。例如:

@Before void setUp() { 
    String.mixin(SampleCategory)
}

@After void tearDown() {
    String.metaClass = null
}

@Test
void shouldDoThat() {
    assert 'that'.withBraces() == '(that)'
}

You can use mixin to apply the category directly to String's metaClass. Assign null to the metaClass to reset it to groovy defaults. For example:

@Before void setUp() { 
    String.mixin(SampleCategory)
}

@After void tearDown() {
    String.metaClass = null
}

@Test
void shouldDoThat() {
    assert 'that'.withBraces() == '(that)'
}
吐个泡泡 2024-11-06 07:25:17

现在您可以选择使用扩展模块而不是类别:
http://mrhaki.blogspot.se/2013/01 /groovy-goodness-adding-extra-methods.html

从好的方面来说,Intellij 会识别扩展。我刚刚注意到它甚至不需要像链接所建议的那样是一个单独的模块,只需将 META-INF/services/org.codehaus.groovy.runtime.ExtensionModule 添加到项目中:

# File: src/main/resources/META-INF/services/org.codehaus.groovy.runtime.ExtensionModule
moduleName = module
moduleVersion = 1.0
extensionClasses = SampleExtension

扩展类几乎是像普通类别一样定义:

class SampleExtension {
    static String withBraces(String self) {
        "($self)"
    }
}

可以这样使用:

def "Sample extension"() {
    expect: 'this'.withBraces() == '(this)'
}

如果您使用 Spock,则可以在规范上使用 @Use 注释。这样做的缺点是 Intellij 无法识别它。

Now you have the option to use extension modules instead of categories:
http://mrhaki.blogspot.se/2013/01/groovy-goodness-adding-extra-methods.html

On the plus side Intellij will recognize the extensions. I've just noticed that it doesn't even need to be a separate module as suggested by the link, just add META-INF/services/org.codehaus.groovy.runtime.ExtensionModule to the project:

# File: src/main/resources/META-INF/services/org.codehaus.groovy.runtime.ExtensionModule
moduleName = module
moduleVersion = 1.0
extensionClasses = SampleExtension

The extension class is pretty much defined like a normal category:

class SampleExtension {
    static String withBraces(String self) {
        "($self)"
    }
}

Can be used like:

def "Sample extension"() {
    expect: 'this'.withBraces() == '(this)'
}

If you are using Spock there is a @Use annotation that can be used on the specifications. The drawback with that is that Intellij will not recognize it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文