From e6c3f9693131db18a83cb971c7326798d0101c66 Mon Sep 17 00:00:00 2001 From: Felipe Garcia Date: Tue, 6 Oct 2020 21:56:08 -0300 Subject: [PATCH] Add Groovy syntax test. --- .../highlighted/Groovy/test.groovy | 77 +++++++++++++++++++ tests/syntax-tests/source/Groovy/test.groovy | 77 +++++++++++++++++++ 2 files changed, 154 insertions(+) create mode 100644 tests/syntax-tests/highlighted/Groovy/test.groovy create mode 100644 tests/syntax-tests/source/Groovy/test.groovy diff --git a/tests/syntax-tests/highlighted/Groovy/test.groovy b/tests/syntax-tests/highlighted/Groovy/test.groovy new file mode 100644 index 00000000..0c648df9 --- /dev/null +++ b/tests/syntax-tests/highlighted/Groovy/test.groovy @@ -0,0 +1,77 @@ +interface Display { + String asText() +} + +trait Entity { + Integer id +} + +class Product implements Entity, Display { + public String name + public Boolean available + public Float price + + private String key + protected String data + + /** + * Creates a new product instance. + * @param id Product ID. + * @param name Product name. + * @param available Product availability. + * @param price Product price. + * @param key Product key. + * @param data Product internal data. + */ + Product(id, name, available, price, key = "key", data = "internal") { + this.id = id + this.name = name + this.available = available + this.price = price + this.key = key + this.data = data + } + + /**@ + * Returns product data as text. + * @return Data string. + */ + String asText() { + return """ID [${id}] Name [${name}] Available [${available}] Price [${price}]""" + } +} + +/* Creates a new product instance */ +def product = new Product(1, "T-Shirt", true, 15.00) + +println(product.asText()) + +product.available = false +product.price = 0.0 + +// Check values +assert product.asText() == "ID [1] Name [T-Shirt] Available [false] Price [0.0]" + +def factorial(Integer value) { + if (value <= 1) { + return 1 + } else { + return value * factorial(value - 1) + } +} + +assert factorial(5) == 120 + +static String join(List list, String separator) { + String data = "" + + list.each { item -> + data += item + separator + } + + data = data.substring(0, data.length() - 1) + + return data +} + +assert join(["g", "r", "o", "o", "v", "y"], " ") == "g r o o v y" diff --git a/tests/syntax-tests/source/Groovy/test.groovy b/tests/syntax-tests/source/Groovy/test.groovy new file mode 100644 index 00000000..43d1477b --- /dev/null +++ b/tests/syntax-tests/source/Groovy/test.groovy @@ -0,0 +1,77 @@ +interface Display { + String asText() +} + +trait Entity { + Integer id +} + +class Product implements Entity, Display { + public String name + public Boolean available + public Float price + + private String key + protected String data + + /** + * Creates a new product instance. + * @param id Product ID. + * @param name Product name. + * @param available Product availability. + * @param price Product price. + * @param key Product key. + * @param data Product internal data. + */ + Product(id, name, available, price, key = "key", data = "internal") { + this.id = id + this.name = name + this.available = available + this.price = price + this.key = key + this.data = data + } + + /**@ + * Returns product data as text. + * @return Data string. + */ + String asText() { + return """ID [${id}] Name [${name}] Available [${available}] Price [${price}]""" + } +} + +/* Creates a new product instance */ +def product = new Product(1, "T-Shirt", true, 15.00) + +println(product.asText()) + +product.available = false +product.price = 0.0 + +// Check values +assert product.asText() == "ID [1] Name [T-Shirt] Available [false] Price [0.0]" + +def factorial(Integer value) { + if (value <= 1) { + return 1 + } else { + return value * factorial(value - 1) + } +} + +assert factorial(5) == 120 + +static String join(List list, String separator) { + String data = "" + + list.each { item -> + data += item + separator + } + + data = data.substring(0, data.length() - 1) + + return data +} + +assert join(["g", "r", "o", "o", "v", "y"], " ") == "g r o o v y"