Skip to content

Commit

Permalink
added unit test for delegate
Browse files Browse the repository at this point in the history
  • Loading branch information
eric2788 committed Nov 13, 2024
1 parent fd2d9ea commit 251f238
Showing 1 changed file with 80 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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<String> lore;

void setDisplayName(String displayName) {
this.displayName = displayName
}

void setLore(List<String> lore) {
this.lore = lore
}


@Override
public String toString() {
return "ItemMeta{" +
"displayName='" + displayName + '\'' +
", lore=" + lore +
'}';
}
}
}

0 comments on commit 251f238

Please sign in to comment.