From 251f2381cd1d1e917215b77708b1cee214ba1f3c Mon Sep 17 00:00:00 2001 From: ericlam Date: Wed, 13 Nov 2024 17:23:42 +0800 Subject: [PATCH] added unit test for delegate --- .../mc/groovytest/TestGroovyDelegate.groovy | 80 +++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 groovier-plugin/src/test/groovy/com/ericlam/mc/groovytest/TestGroovyDelegate.groovy diff --git a/groovier-plugin/src/test/groovy/com/ericlam/mc/groovytest/TestGroovyDelegate.groovy b/groovier-plugin/src/test/groovy/com/ericlam/mc/groovytest/TestGroovyDelegate.groovy new file mode 100644 index 0000000..b34255e --- /dev/null +++ b/groovier-plugin/src/test/groovy/com/ericlam/mc/groovytest/TestGroovyDelegate.groovy @@ -0,0 +1,80 @@ +package com.ericlam.mc.groovytest + +import org.bukkit.Material +import org.junit.jupiter.api.Test + +class TestGroovyDelegate { + + @Test + void testGroovyDelegate() { + def builder = new ItemBuilderImpl() as ItemBuilder + builder.item = new Item() + builder.meta = builder.item.meta + + builder.with { + type = Material.DIAMOND + displayName = "Hello Diamond!" + lore = ["Hello", "Diamond!"] + } + + println builder.item.toString() + } + + + trait ItemBuilder { + @Delegate Item item + @Delegate ItemMeta meta + } + + class ItemBuilderImpl implements ItemBuilder { + ItemBuilderImpl(){ + this.item = new Item() + this.meta = item.meta + } + } + + + class Item { + private Material type = Material.STONE + private ItemMeta meta = new ItemMeta() + + void setType(Material type) { + this.type = type + } + + ItemMeta getMeta() { + return meta + } + + + @Override + public String toString() { + return "Item{" + + "type=" + type + + ", meta=" + meta + + '}'; + } + } + + class ItemMeta { + private String displayName; + private List lore; + + void setDisplayName(String displayName) { + this.displayName = displayName + } + + void setLore(List lore) { + this.lore = lore + } + + + @Override + public String toString() { + return "ItemMeta{" + + "displayName='" + displayName + '\'' + + ", lore=" + lore + + '}'; + } + } +}