Add more tests
This commit is contained in:
parent
5590cd007a
commit
d288fd6fce
5 changed files with 577 additions and 170 deletions
|
@ -40,7 +40,12 @@ class TestCase {
|
||||||
var bean = new ValueBean("this IS a TEST.");
|
var bean = new ValueBean("this IS a TEST.");
|
||||||
t.setBean(bean);
|
t.setBean(bean);
|
||||||
assertThat(t.getContent()).isEqualTo(bean.getValue() + ": this is a test.");
|
assertThat(t.getContent()).isEqualTo(bean.getValue() + ": this is a test.");
|
||||||
bean = new ValueBean("");
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void lowercaseWithEmpty() {
|
||||||
|
var t = TemplateFactory.TXT.get("lowercase");
|
||||||
|
var bean = new ValueBean("");
|
||||||
t.setBean(bean);
|
t.setBean(bean);
|
||||||
assertThat(t.getContent()).isEqualTo(bean.getValue() + ": ");
|
assertThat(t.getContent()).isEqualTo(bean.getValue() + ": ");
|
||||||
}
|
}
|
||||||
|
@ -57,6 +62,11 @@ class TestCase {
|
||||||
var t = TemplateFactory.TXT.get("trim");
|
var t = TemplateFactory.TXT.get("trim");
|
||||||
t.setAttribute(FOO, "\t" + SAMPLE_TEXT + " \n");
|
t.setAttribute(FOO, "\t" + SAMPLE_TEXT + " \n");
|
||||||
assertThat(t.getContent()).isEqualTo(SAMPLE_TEXT);
|
assertThat(t.getContent()).isEqualTo(SAMPLE_TEXT);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void trimWithEmpty() {
|
||||||
|
var t = TemplateFactory.TXT.get("trim");
|
||||||
t.setAttribute(FOO, "");
|
t.setAttribute(FOO, "");
|
||||||
assertThat(t.getContent()).isEmpty();
|
assertThat(t.getContent()).isEmpty();
|
||||||
}
|
}
|
||||||
|
@ -73,6 +83,11 @@ class TestCase {
|
||||||
var t = TemplateFactory.TXT.get("uppercase");
|
var t = TemplateFactory.TXT.get("uppercase");
|
||||||
t.setAttribute("bar", SAMPLE_TEXT);
|
t.setAttribute("bar", SAMPLE_TEXT);
|
||||||
assertThat(t.getContent()).isEqualTo(SAMPLE_TEXT.toUpperCase(Localization.getLocale()));
|
assertThat(t.getContent()).isEqualTo(SAMPLE_TEXT.toUpperCase(Localization.getLocale()));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void uppercaseWithEmpty() {
|
||||||
|
var t = TemplateFactory.TXT.get("uppercase");
|
||||||
t.setAttribute("bar", "");
|
t.setAttribute("bar", "");
|
||||||
assertThat(t.getContent()).isEmpty();
|
assertThat(t.getContent()).isEmpty();
|
||||||
}
|
}
|
||||||
|
|
|
@ -23,15 +23,28 @@ import rife.template.TemplateFactory;
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
class TestEncode {
|
class TestEncode {
|
||||||
|
@Test
|
||||||
|
void decodeRot13() {
|
||||||
|
var t = TemplateFactory.TXT.get("rot13");
|
||||||
|
var rot13 = "Guvf vf n grfg.";
|
||||||
|
|
||||||
|
t.setValue("value", rot13);
|
||||||
|
assertThat(t.getContent()).isEqualTo(rot13 + ": " + TestCase.SAMPLE_TEXT);
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void encodeBase64() {
|
void encodeBase64() {
|
||||||
var t = TemplateFactory.TXT.get("encodeBase64");
|
var t = TemplateFactory.TXT.get("encodeBase64");
|
||||||
t.setValue(TestCase.FOO, TestCase.SAMPLE_TEXT);
|
t.setValue(TestCase.FOO, TestCase.SAMPLE_TEXT);
|
||||||
assertThat(t.getContent()).isEqualTo(t.getValue(TestCase.FOO) + ": VGhpcyBpcyBhIHRlc3Qu");
|
assertThat(t.getContent()).isEqualTo(t.getValue(TestCase.FOO) + ": VGhpcyBpcyBhIHRlc3Qu");
|
||||||
|
}
|
||||||
|
|
||||||
t = TemplateFactory.HTML.get("encodeBase64");
|
@Test
|
||||||
|
void encodeBase64WithUrlEncoding() {
|
||||||
|
var t = TemplateFactory.HTML.get("encodeBase64");
|
||||||
t.setValue(TestCase.FOO, TestCase.SAMPLE_TEXT + " URL Encoded.");
|
t.setValue(TestCase.FOO, TestCase.SAMPLE_TEXT + " URL Encoded.");
|
||||||
assertThat(t.getContent()).as("with URL encoding").contains("VGhpcyBpcyBhIHRlc3QuIFVSTCBFbmNvZGVkLg%3D%3D");
|
assertThat(t.getContent()).as("with URL encoding")
|
||||||
|
.contains("VGhpcyBpcyBhIHRlc3QuIFVSTCBFbmNvZGVkLg%3D%3D");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -54,15 +67,21 @@ class TestEncode {
|
||||||
var t = TemplateFactory.TXT.get("encodeJs");
|
var t = TemplateFactory.TXT.get("encodeJs");
|
||||||
t.setAttribute(TestCase.FOO, "'\"\\/");
|
t.setAttribute(TestCase.FOO, "'\"\\/");
|
||||||
assertThat(t.getContent()).isEqualTo("\\'\\\"\\\\\\/");
|
assertThat(t.getContent()).isEqualTo("\\'\\\"\\\\\\/");
|
||||||
|
}
|
||||||
|
|
||||||
t = TemplateFactory.TXT.get("encodeJs");
|
@Test
|
||||||
|
void encodeJsWithSpecialCharacters() {
|
||||||
|
var t = TemplateFactory.TXT.get("encodeJs");
|
||||||
t.setAttribute(TestCase.FOO, "This is\f\b a\r\n\ttest");
|
t.setAttribute(TestCase.FOO, "This is\f\b a\r\n\ttest");
|
||||||
assertThat(t.getContent()).isEqualTo("This is\\f\\b a\\r\\n\\ttest");
|
assertThat(t.getContent()).isEqualTo("This is\\f\\b a\\r\\n\\ttest");
|
||||||
|
}
|
||||||
|
|
||||||
t = TemplateFactory.HTML.get("encodeJs");
|
@Test
|
||||||
|
void encodeJsWithUnicode() {
|
||||||
|
var t = TemplateFactory.HTML.get("encodeJs");
|
||||||
t.setAttribute(TestCase.FOO, '"' + TestCase.SAMPLE_TEXT + '"');
|
t.setAttribute(TestCase.FOO, '"' + TestCase.SAMPLE_TEXT + '"');
|
||||||
assertThat(t.getContent()).as("with unicode")
|
assertThat(t.getContent()).isEqualTo(
|
||||||
.isEqualTo("\\u005C\\u0022\\u0054\\u0068\\u0069\\u0073\\u0020\\u0069\\u0073\\u0020\\u0061\\u0020\\u0074\\u0065\\u0073\\u0074\\u002E\\u005C\\u0022");
|
"\\u005C\\u0022\\u0054\\u0068\\u0069\\u0073\\u0020\\u0069\\u0073\\u0020\\u0061\\u0020\\u0074\\u0065\\u0073\\u0074\\u002E\\u005C\\u0022");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -70,10 +89,13 @@ class TestEncode {
|
||||||
var t = TemplateFactory.JSON.get("encodeJson");
|
var t = TemplateFactory.JSON.get("encodeJson");
|
||||||
t.setAttribute(TestCase.FOO, "This is a \"•test\"");
|
t.setAttribute(TestCase.FOO, "This is a \"•test\"");
|
||||||
assertThat(t.getContent()).isEqualTo("{\n \"foo\": \"This is a \\\"\\u2022test\\\"\"\n}");
|
assertThat(t.getContent()).isEqualTo("{\n \"foo\": \"This is a \\\"\\u2022test\\\"\"\n}");
|
||||||
|
}
|
||||||
|
|
||||||
t = TemplateFactory.HTML.get("encodeJson");
|
@Test
|
||||||
|
void encodeJsonWithHtml() {
|
||||||
|
var t = TemplateFactory.HTML.get("encodeJson");
|
||||||
t.setAttribute(TestCase.FOO, "\"<test>\"");
|
t.setAttribute(TestCase.FOO, "\"<test>\"");
|
||||||
assertThat(t.getContent()).as("with html").isEqualTo("\\"<test>\\"");
|
assertThat(t.getContent()).isEqualTo("\\"<test>\\"");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -81,14 +103,9 @@ class TestEncode {
|
||||||
var t = TemplateFactory.TXT.get("rot13");
|
var t = TemplateFactory.TXT.get("rot13");
|
||||||
var rot13 = "Guvf vf n grfg.";
|
var rot13 = "Guvf vf n grfg.";
|
||||||
|
|
||||||
// Encode
|
|
||||||
var bean = new ValueBean(TestCase.SAMPLE_TEXT);
|
var bean = new ValueBean(TestCase.SAMPLE_TEXT);
|
||||||
t.setBean(bean);
|
t.setBean(bean);
|
||||||
assertThat(t.getContent()).as("encode").isEqualTo(bean.getValue() + ": " + rot13);
|
assertThat(t.getContent()).isEqualTo(bean.getValue() + ": " + rot13);
|
||||||
|
|
||||||
// Decode
|
|
||||||
t.setValue("value", rot13);
|
|
||||||
assertThat(t.getContent()).as("decode").isEqualTo(rot13 + ": " + TestCase.SAMPLE_TEXT);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -97,11 +114,14 @@ class TestEncode {
|
||||||
t.setAttribute(TestCase.FOO, TestCase.SAMPLE_TEXT);
|
t.setAttribute(TestCase.FOO, TestCase.SAMPLE_TEXT);
|
||||||
assertThat(t.getContent()).isEqualTo(
|
assertThat(t.getContent()).isEqualTo(
|
||||||
"\\u0054\\u0068\\u0069\\u0073\\u0020\\u0069\\u0073\\u0020\\u0061\\u0020\\u0074\\u0065\\u0073\\u0074\\u002E");
|
"\\u0054\\u0068\\u0069\\u0073\\u0020\\u0069\\u0073\\u0020\\u0061\\u0020\\u0074\\u0065\\u0073\\u0074\\u002E");
|
||||||
|
}
|
||||||
|
|
||||||
t = TemplateFactory.HTML.get("encodeUnicode");
|
@Test
|
||||||
|
void encodeUnicodeWithJs() {
|
||||||
|
var t = TemplateFactory.HTML.get("encodeUnicode");
|
||||||
t.setAttribute(TestCase.FOO, '"' + TestCase.SAMPLE_TEXT + '"');
|
t.setAttribute(TestCase.FOO, '"' + TestCase.SAMPLE_TEXT + '"');
|
||||||
assertThat(t.getContent()).as("with js")
|
assertThat(t.getContent()).contains(
|
||||||
.contains("'\\\\u0022\\\\u0054\\\\u0068\\\\u0069\\\\u0073\\\\u0020\\\\u0069\\\\u0073\\\\u0020\\\\u0061\\\\u0020\\\\u0074\\\\u0065\\\\u0073\\\\u0074\\\\u002E\\\\u0022'");
|
"'\\\\u0022\\\\u0054\\\\u0068\\\\u0069\\\\u0073\\\\u0020\\\\u0069\\\\u0073\\\\u0020\\\\u0061\\\\u0020\\\\u0074\\\\u0065\\\\u0073\\\\u0074\\\\u002E\\\\u0022'");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -109,11 +129,13 @@ class TestEncode {
|
||||||
var t = TemplateFactory.HTML.get("encodeUrl");
|
var t = TemplateFactory.HTML.get("encodeUrl");
|
||||||
t.setAttribute(TestCase.FOO, "a test &");
|
t.setAttribute(TestCase.FOO, "a test &");
|
||||||
assertThat(t.getContent()).isEqualTo("<a href=\"https://example.com/a%20test%20%26\">a test &</a>");
|
assertThat(t.getContent()).isEqualTo("<a href=\"https://example.com/a%20test%20%26\">a test &</a>");
|
||||||
|
}
|
||||||
|
|
||||||
t = TemplateFactory.HTML.get("encodeUrlwithUnicode");
|
@Test
|
||||||
|
void encodeUrlWithUnicode() {
|
||||||
|
var t = TemplateFactory.HTML.get("encodeUrlwithUnicode");
|
||||||
t.setAttribute(TestCase.FOO, "a=test");
|
t.setAttribute(TestCase.FOO, "a=test");
|
||||||
assertThat(t.getContent()).as("with unicode")
|
assertThat(t.getContent()).contains("https://foo.com/\\u0061\\u0025\\u0033\\u0044\\u0074\\u0065\\u0073\\u0074");
|
||||||
.contains("https://foo.com/\\u0061\\u0025\\u0033\\u0044\\u0074\\u0065\\u0073\\u0074");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|
|
@ -17,49 +17,43 @@
|
||||||
|
|
||||||
package rife.render;
|
package rife.render;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.DisplayName;
|
||||||
|
import org.junit.jupiter.api.Nested;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import rife.template.TemplateFactory;
|
import rife.template.TemplateFactory;
|
||||||
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
|
@SuppressWarnings("PMD.AvoidDuplicateLiterals")
|
||||||
class TestFormat {
|
class TestFormat {
|
||||||
@Test
|
@Test
|
||||||
void abbreviate() {
|
void abbreviate() {
|
||||||
var t = TemplateFactory.HTML.get("abbreviate");
|
var t = TemplateFactory.TXT.get("abbreviate");
|
||||||
t.setAttribute(TestCase.FOO, TestCase.SAMPLE_TEXT);
|
t.setAttribute(TestCase.FOO, TestCase.SAMPLE_TEXT);
|
||||||
assertThat(t.getContent()).as("activate.html").endsWith("…").hasSize(19);
|
assertThat(t.getContent()).endsWith("...").hasSize(8);
|
||||||
|
|
||||||
t = TemplateFactory.TXT.get("abbreviate");
|
|
||||||
t.setAttribute(TestCase.FOO, TestCase.SAMPLE_TEXT);
|
|
||||||
assertThat(t.getContent()).as("activate.txt").endsWith("...").hasSize(8);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void formatCreditCard() {
|
void abbreviateHtml() {
|
||||||
var t = TemplateFactory.TXT.get("formatCreditCard");
|
var t = TemplateFactory.HTML.get("abbreviate");
|
||||||
t.setAttribute(TestCase.FOO, "4342 2565 6244 0179");
|
t.setAttribute(TestCase.FOO, TestCase.SAMPLE_TEXT);
|
||||||
assertThat(t.getContent()).as("US VISA").isEqualTo("0179");
|
assertThat(t.getContent()).hasSize(19);
|
||||||
t.setAttribute(TestCase.FOO, "5130-3899-9169-8324");
|
|
||||||
assertThat(t.getContent()).as("FR MASTERCARD").isEqualTo("8324");
|
|
||||||
t.setAttribute(TestCase.FOO, "374380141731053");
|
|
||||||
assertThat(t.getContent()).as("UK AMEX").isEqualTo("1053");
|
|
||||||
t.setAttribute(TestCase.FOO, "000000000000001");
|
|
||||||
assertThat(t.getContent()).as("000000000000001").isEmpty();
|
|
||||||
t.setAttribute(TestCase.FOO, "");
|
|
||||||
assertThat(t.getContent()).as("").isEmpty();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void mask() {
|
void mask() {
|
||||||
|
var t = TemplateFactory.TXT.get("mask");
|
||||||
|
var foo = "374380141731053";
|
||||||
|
t.setAttribute(TestCase.FOO, foo);
|
||||||
|
assertThat(t.getContent()).isEqualTo("***************");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void maskHtml() {
|
||||||
var t = TemplateFactory.HTML.get("mask");
|
var t = TemplateFactory.HTML.get("mask");
|
||||||
var foo = "374380141731053";
|
var foo = "374380141731053";
|
||||||
t.setAttribute(TestCase.FOO, foo);
|
t.setAttribute(TestCase.FOO, foo);
|
||||||
assertThat(t.getContent()).as("mask.html")
|
assertThat(t.getContent()).isEqualTo("3743•••••••••••");
|
||||||
.isEqualTo("3743•••••••••••");
|
|
||||||
|
|
||||||
t = TemplateFactory.TXT.get("mask");
|
|
||||||
t.setAttribute(TestCase.FOO, foo);
|
|
||||||
assertThat(t.getContent()).as("mask.txt").isEqualTo("***************");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -67,8 +61,7 @@ class TestFormat {
|
||||||
var t = TemplateFactory.HTML.get("normalize");
|
var t = TemplateFactory.HTML.get("normalize");
|
||||||
var foo = "News for January 6, 2023 (Paris)";
|
var foo = "News for January 6, 2023 (Paris)";
|
||||||
t.setValue(TestCase.FOO, foo);
|
t.setValue(TestCase.FOO, foo);
|
||||||
assertThat(t.getContent()).isEqualTo("<a href=\"news/20230106/news-for-january-6-2023-paris\">"
|
assertThat(t.getContent()).isEqualTo("<a href=\"news/20230106/news-for-january-6-2023-paris\">" + foo + "</a>");
|
||||||
+ foo + "</a>");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -92,28 +85,108 @@ class TestFormat {
|
||||||
assertThat(t.getContent()).isEqualTo("<a href=\"foo\">foo</a>");
|
assertThat(t.getContent()).isEqualTo("<a href=\"foo\">foo</a>");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Nested
|
||||||
|
@DisplayName("Credit Card Format Tests")
|
||||||
|
class CreditCardFormatTests {
|
||||||
|
@Test
|
||||||
|
void amexCreditCard() {
|
||||||
|
var t = TemplateFactory.TXT.get("formatCreditCard");
|
||||||
|
t.setAttribute(TestCase.FOO, "374380141731053");
|
||||||
|
assertThat(t.getContent()).isEqualTo("1053");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void creditCardWithEmpty() {
|
||||||
|
var t = TemplateFactory.TXT.get("formatCreditCard");
|
||||||
|
t.setAttribute(TestCase.FOO, "");
|
||||||
|
assertThat(t.getContent()).isEmpty();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void discoverCreditCard() {
|
||||||
|
var t = TemplateFactory.TXT.get("formatCreditCard");
|
||||||
|
t.setAttribute(TestCase.FOO, "6011 1076-8252 0629");
|
||||||
|
assertThat(t.getContent()).isEqualTo("0629");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void invalidCreditCard() {
|
||||||
|
var t = TemplateFactory.TXT.get("formatCreditCard");
|
||||||
|
t.setAttribute(TestCase.FOO, "000000000000001");
|
||||||
|
assertThat(t.getContent()).isEmpty();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void mastercardCreditCard() {
|
||||||
|
var t = TemplateFactory.TXT.get("formatCreditCard");
|
||||||
|
t.setAttribute(TestCase.FOO, "5130-3899-9169-8324");
|
||||||
|
assertThat(t.getContent()).isEqualTo("8324");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void visaCreditCard() {
|
||||||
|
var t = TemplateFactory.TXT.get("formatCreditCard");
|
||||||
|
t.setAttribute(TestCase.FOO, "4342 2565 6244 0179");
|
||||||
|
assertThat(t.getContent()).isEqualTo("0179");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nested
|
||||||
|
@DisplayName("Uptime Tests")
|
||||||
|
class UptimeTests {
|
||||||
@Test
|
@Test
|
||||||
void uptime() {
|
void uptime() {
|
||||||
var t = TemplateFactory.TXT.get("uptime");
|
var t = TemplateFactory.TXT.get("uptime");
|
||||||
assertThat(t.getContent()).as("uptime.txt").isEqualTo("0 minute\n0 minuto\n0 minute");
|
assertThat(t.getContent()).isEqualTo("0 minute\n0 minuto\n0 minute");
|
||||||
|
}
|
||||||
|
|
||||||
t = TemplateFactory.HTML.get("uptime");
|
@Test
|
||||||
|
void uptimeInFrench() {
|
||||||
|
var t = TemplateFactory.HTML.get("uptime");
|
||||||
t.setAttribute(Uptime.class.getName(), 547800300076L);
|
t.setAttribute(Uptime.class.getName(), 547800300076L);
|
||||||
assertThat(t.getContent()).as("uptime.html")
|
assertThat(t.getContent()).isEqualTo("17 années, 4 mois, 2 semaines, 1 jour, 6 heures, 45 minutes");
|
||||||
.isEqualTo("17 années, 4 mois, 2 semaines, 1 jour, 6 heures, 45 minutes");
|
}
|
||||||
t.setAttribute(Uptime.class.getName(), 120000L);
|
|
||||||
assertThat(t.getContent()).as("uptime.html: 2 min").isEqualTo("2 minutes");
|
|
||||||
|
|
||||||
t = TemplateFactory.JSON.get("uptime");
|
@Test
|
||||||
|
void uptimeInJson() {
|
||||||
|
var t = TemplateFactory.JSON.get("uptime");
|
||||||
t.setAttribute(Uptime.class.getName(), 5999964460000L);
|
t.setAttribute(Uptime.class.getName(), 5999964460000L);
|
||||||
assertThat(t.getContent()).as("uptime.json")
|
assertThat(t.getContent()).isEqualTo("190 years 3 months 4 days 47 minutes");
|
||||||
.isEqualTo("190 years 3 months 4 days 47 minutes");
|
}
|
||||||
t.setAttribute(Uptime.class.getName(), 34822860000L);
|
|
||||||
assertThat(t.getContent()).as("uptime.json: 1 year...")
|
|
||||||
.isEqualTo("1 year 1 month 1 week 1 day 1 hour 1 minute");
|
|
||||||
|
|
||||||
t = TemplateFactory.TXT.get("uptime2");
|
@Test
|
||||||
|
void uptimeInMinutes() {
|
||||||
|
var t = TemplateFactory.HTML.get("uptime");
|
||||||
|
t.setAttribute(Uptime.class.getName(), 120000L);
|
||||||
|
assertThat(t.getContent()).isEqualTo("2 minutes");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void uptimeInMonth() {
|
||||||
|
var t = TemplateFactory.JSON.get("uptime");
|
||||||
|
t.setAttribute(Uptime.class.getName(), 2592000000L);
|
||||||
|
assertThat(t.getContent()).isEqualTo("1 month");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void uptimeInWeeks() {
|
||||||
|
var t = TemplateFactory.TXT.get("uptime");
|
||||||
|
t.setAttribute(Uptime.class.getName(), 1209600000L);
|
||||||
|
assertThat(t.getContent()).isEqualTo("2 weeks\n2 semanas\n2 weeks");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void uptimeWithFormatting() {
|
||||||
|
var t = TemplateFactory.TXT.get("uptime2");
|
||||||
t.setAttribute(Uptime.class.getName(), 547800388076L);
|
t.setAttribute(Uptime.class.getName(), 547800388076L);
|
||||||
assertThat(t.getContent()).as("uptime2.txt").isEqualTo("17YRS-4MOS-2WKS-1D-6H-46M");
|
assertThat(t.getContent()).isEqualTo("17YRS-4MOS-2WKS-1D-6H-46M");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void uptimeWithZero() {
|
||||||
|
var t = TemplateFactory.JSON.get("uptime");
|
||||||
|
t.setAttribute(Uptime.class.getName(), 0L);
|
||||||
|
assertThat(t.getContent()).isEqualTo("0 minute");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -17,136 +17,431 @@
|
||||||
|
|
||||||
package rife.render;
|
package rife.render;
|
||||||
|
|
||||||
import org.assertj.core.api.AutoCloseableSoftAssertions;
|
import org.junit.jupiter.api.DisplayName;
|
||||||
|
import org.junit.jupiter.api.Nested;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.junit.jupiter.params.ParameterizedTest;
|
||||||
|
import org.junit.jupiter.params.provider.Arguments;
|
||||||
|
import org.junit.jupiter.params.provider.MethodSource;
|
||||||
|
import org.junit.jupiter.params.provider.NullAndEmptySource;
|
||||||
|
import org.junit.jupiter.params.provider.ValueSource;
|
||||||
|
|
||||||
import java.util.Properties;
|
import java.util.Properties;
|
||||||
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
class TestRenderUtils {
|
class
|
||||||
|
TestRenderUtils {
|
||||||
static final String SAMPLE_GERMAN = "Möchten Sie ein paar Äpfel?";
|
static final String SAMPLE_GERMAN = "Möchten Sie ein paar Äpfel?";
|
||||||
|
|
||||||
@Test
|
|
||||||
void abbreviate() {
|
|
||||||
assertThat(RenderUtils.abbreviate(TestCase.SAMPLE_TEXT, 9, "")).as("max=9")
|
|
||||||
.isEqualTo("This is a");
|
|
||||||
|
|
||||||
assertThat(RenderUtils.abbreviate(TestCase.SAMPLE_TEXT, 0, "")).as("max=0").isEmpty();
|
|
||||||
|
|
||||||
assertThat(RenderUtils.abbreviate(TestCase.SAMPLE_TEXT, -1, "")).as("max=-1")
|
|
||||||
.isEqualTo(TestCase.SAMPLE_TEXT);
|
|
||||||
|
|
||||||
assertThat(RenderUtils.abbreviate("", 10, "")).as("").isEmpty();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void capitalizeWords() {
|
|
||||||
assertThat(RenderUtils.capitalizeWords("hello world")).isEqualTo("Hello World");
|
|
||||||
assertThat(RenderUtils.capitalizeWords("java programming")).isEqualTo("Java Programming");
|
|
||||||
assertThat(RenderUtils.capitalizeWords("TEST")).isEqualTo("Test");
|
|
||||||
assertThat(RenderUtils.capitalizeWords("multiple spaces")).isEqualTo("Multiple Spaces");
|
|
||||||
assertThat(RenderUtils.capitalizeWords("white\t\fspaces")).isEqualTo("White\t\fSpaces");
|
|
||||||
assertThat(RenderUtils.capitalizeWords("")).isEmpty();
|
|
||||||
assertThat(RenderUtils.capitalizeWords(null)).isNull();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void encode() {
|
|
||||||
var p = new Properties();
|
|
||||||
p.put(RenderUtils.ENCODING_PROPERTY, "blah");
|
|
||||||
assertThat(RenderUtils.encode(TestCase.SAMPLE_TEXT, p)).as("invalid encoding").isEqualTo(TestCase.SAMPLE_TEXT);
|
|
||||||
p.put(RenderUtils.ENCODING_PROPERTY, "json");
|
|
||||||
assertThat(RenderUtils.encode("This is a \"•test\"", p)).as("json").isEqualTo("This is a \\\"\\u2022test\\\"");
|
|
||||||
p.put(RenderUtils.ENCODING_PROPERTY, "html");
|
|
||||||
assertThat(RenderUtils.encode("<a test &>", p)).as("html").isEqualTo("<a test &>");
|
|
||||||
p.put(RenderUtils.ENCODING_PROPERTY, "js");
|
|
||||||
assertThat(RenderUtils.encode("\"test'", p)).as("js").isEqualTo("\\\"test\\'");
|
|
||||||
p.put(RenderUtils.ENCODING_PROPERTY, "unicode");
|
|
||||||
assertThat(RenderUtils.encode("test", p)).as("unicode").isEqualTo("\\u0074\\u0065\\u0073\\u0074");
|
|
||||||
p.put(RenderUtils.ENCODING_PROPERTY, "url");
|
|
||||||
assertThat(RenderUtils.encode("a = test", p)).as("url").isEqualTo("a%20%3D%20test");
|
|
||||||
p.put(RenderUtils.ENCODING_PROPERTY, "xml");
|
|
||||||
assertThat(RenderUtils.encode("Joe's Café & Bar", p)).as("xml").isEqualTo("Joe's Café & Bar");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void encodeJs() {
|
|
||||||
assertThat(RenderUtils.encodeJs("")).isEmpty();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void fetchUrl() {
|
|
||||||
var s = "default";
|
|
||||||
assertThat(RenderUtils.fetchUrl("blah", s)).isEqualTo(s);
|
|
||||||
assertThat(RenderUtils.fetchUrl("https://www.google.com/404", s)).isEqualTo(s);
|
|
||||||
assertThat(RenderUtils.fetchUrl("https://www.notreallythere.com/", s)).isEqualTo(s);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void htmlEntities() {
|
void htmlEntities() {
|
||||||
|
assertThat(RenderUtils.htmlEntities(SAMPLE_GERMAN)).isEqualTo(
|
||||||
|
"Möchten Sie ein paar Äpfel?");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void htmlEntitiesWithEmpty() {
|
||||||
assertThat(RenderUtils.htmlEntities("")).isEmpty();
|
assertThat(RenderUtils.htmlEntities("")).isEmpty();
|
||||||
assertThat(RenderUtils.htmlEntities(SAMPLE_GERMAN))
|
|
||||||
.isEqualTo("Möchten Sie ein paar Äpfel?");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void mask() {
|
|
||||||
var foo = "4342256562440179";
|
|
||||||
|
|
||||||
assertThat(RenderUtils.mask("", " ", 2, false)).isEmpty();
|
|
||||||
|
|
||||||
assertThat(RenderUtils.mask(foo, "?", 4, false)).as("mask=?")
|
|
||||||
.isEqualTo("????????????0179");
|
|
||||||
|
|
||||||
assertThat(RenderUtils.mask(foo, "-", 22, true)).as("unmasked=22")
|
|
||||||
.isEqualTo("----------------");
|
|
||||||
|
|
||||||
assertThat(RenderUtils.mask(foo, "•", -1, false)).as("mask=•")
|
|
||||||
.isEqualTo("••••••••••••••••");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void normalize() {
|
|
||||||
assertThat(RenderUtils.normalize("")).as("empty").isEmpty();
|
|
||||||
assertThat(RenderUtils.normalize(" &()-_=[{]}\\|;:,<.>/")).as("blank").isEmpty();
|
|
||||||
assertThat(RenderUtils.normalize(SAMPLE_GERMAN)).as("greman").isEqualTo("mochten-sie-ein-paar-apfel");
|
|
||||||
assertThat(RenderUtils.normalize("foo bar, <foo-bar>,foo:bar,foo;(bar), {foo} & bar=foo.bar[foo|bar]"))
|
|
||||||
.as("foo-bar")
|
|
||||||
.isEqualTo("foo-bar-foo-bar-foo-bar-foo-bar-foo-bar-foo-bar-foo-bar");
|
|
||||||
assertThat(RenderUtils.normalize("News for January 6, 2023 (Paris)")).as("docs example")
|
|
||||||
.isEqualTo("news-for-january-6-2023-paris");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void qrCode() {
|
void qrCode() {
|
||||||
assertThat(RenderUtils.qrCode("", "12")).as("empty").isEmpty();
|
|
||||||
assertThat(RenderUtils.qrCode("erik", "24")).as("svg")
|
assertThat(RenderUtils.qrCode("erik", "24")).as("svg")
|
||||||
.startsWith("<?xml").contains("<svg").contains("<desc>erik");
|
.startsWith("<?xml").contains("<svg").contains("<desc>erik");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void rot13() {
|
void qrCodeWithEmpty() {
|
||||||
var encoded = "Zöpugra Fvr rva cnne Äcsry?";
|
assertThat(RenderUtils.qrCode("", "12")).as("empty").isEmpty();
|
||||||
assertThat(RenderUtils.rot13("")).isEmpty();
|
|
||||||
assertThat(RenderUtils.rot13(SAMPLE_GERMAN)).as("encode").isEqualTo(encoded);
|
|
||||||
assertThat(RenderUtils.rot13(encoded)).as("decode").isEqualTo(SAMPLE_GERMAN);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void swapCase() {
|
void swapCase() {
|
||||||
assertThat(RenderUtils.swapCase("")).isEmpty();
|
|
||||||
assertThat(RenderUtils.swapCase(SAMPLE_GERMAN)).isEqualTo("mÖCHTEN sIE EIN PAAR äPFEL?");
|
assertThat(RenderUtils.swapCase(SAMPLE_GERMAN)).isEqualTo("mÖCHTEN sIE EIN PAAR äPFEL?");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void validateCreditCard() {
|
void swapCaseWithEmpty() {
|
||||||
try (var softly = new AutoCloseableSoftAssertions()) {
|
assertThat(RenderUtils.swapCase("")).isEmpty();
|
||||||
softly.assertThat(RenderUtils.validateCreditCard("4505 4672 3366 6430")).as("visa").isTrue();
|
}
|
||||||
softly.assertThat(RenderUtils.validateCreditCard("5189-5923-3915-0425")).as("mastercard").isTrue();
|
|
||||||
softly.assertThat(RenderUtils.validateCreditCard("3433634926643302")).as("amex").isTrue();
|
@Nested
|
||||||
softly.assertThat(RenderUtils.validateCreditCard("6011 1076-8252 0629")).as("discover").isTrue();
|
@DisplayName("Abbreviate Tests")
|
||||||
softly.assertThat(RenderUtils.validateCreditCard("0123456789012345")).as("invalid").isFalse();
|
class AbbreviateTests {
|
||||||
|
@Test
|
||||||
|
void abbreviateWithEllipsis() {
|
||||||
|
assertThat(RenderUtils.abbreviate(TestCase.SAMPLE_TEXT, 10, "…")).isEqualTo("This is a…");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void abbreviateWithEmpty() {
|
||||||
|
assertThat(RenderUtils.abbreviate("", 10, "")).as("").isEmpty();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void abbreviateWithMarker() {
|
||||||
|
assertThat(RenderUtils.abbreviate(TestCase.SAMPLE_TEXT, 12, "...")).isEqualTo("This is a...");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void abbreviateWithMax() {
|
||||||
|
assertThat(RenderUtils.abbreviate(TestCase.SAMPLE_TEXT, 9, "")).isEqualTo("This is a");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void abbreviateWithMaxNegative() {
|
||||||
|
assertThat(RenderUtils.abbreviate(TestCase.SAMPLE_TEXT, -1, ""))
|
||||||
|
.isEqualTo(TestCase.SAMPLE_TEXT);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void abbreviateWithMaxZero() {
|
||||||
|
assertThat(RenderUtils.abbreviate(TestCase.SAMPLE_TEXT, 0, "")).isEmpty();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nested
|
||||||
|
@DisplayName("Capitalize Tests")
|
||||||
|
class CapitalizeTests {
|
||||||
|
@Test
|
||||||
|
void capitalizeWords() {
|
||||||
|
assertThat(RenderUtils.capitalizeWords("hello world")).isEqualTo("Hello World");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void capitalizeWordsWithEmpty() {
|
||||||
|
assertThat(RenderUtils.capitalizeWords("")).isEmpty();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void capitalizeWordsWithMultipleSpaces() {
|
||||||
|
assertThat(RenderUtils.capitalizeWords("multiple spaces")).isEqualTo("Multiple Spaces");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void capitalizeWordsWithNull() {
|
||||||
|
assertThat(RenderUtils.capitalizeWords(null)).isNull();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void capitalizeWordsWithSpecialCharacters() {
|
||||||
|
assertThat(RenderUtils.capitalizeWords("white\t\fspaces")).isEqualTo("White\t\fSpaces");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void capitalizeWordsWithUppercase() {
|
||||||
|
assertThat(RenderUtils.capitalizeWords("HELLO World")).isEqualTo("Hello World");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nested
|
||||||
|
@DisplayName("Encode JavaScript Tests")
|
||||||
|
class EncodeJavaScriptTests {
|
||||||
|
private static Stream<Arguments> javascriptEscapeTestCases() {
|
||||||
|
return Stream.of(
|
||||||
|
Arguments.of("test's", "test\\'s"),
|
||||||
|
Arguments.of("test\"s", "test\\\"s"),
|
||||||
|
Arguments.of("test\\s", "test\\\\s"),
|
||||||
|
Arguments.of("test/s", "test\\/s"),
|
||||||
|
Arguments.of("test\bs", "test\\bs"),
|
||||||
|
Arguments.of("test\ns", "test\\ns"),
|
||||||
|
Arguments.of("test\ts", "test\\ts"),
|
||||||
|
Arguments.of("test\fs", "test\\fs"),
|
||||||
|
Arguments.of("test\rs", "test\\rs"),
|
||||||
|
Arguments.of("a'b\"c\\d/e\bf\ng\th\fi\rj", "a\\'b\\\"c\\\\d\\/e\\bf\\ng\\th\\fi\\rj")
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void encodeJsAllSpecialChars() {
|
||||||
|
String input = "'\"\\/\b\n\t\f\r";
|
||||||
|
String expected = "\\'\\\"\\\\\\/\\b\\n\\t\\f\\r";
|
||||||
|
assertThat(RenderUtils.encodeJs(input)).isEqualTo(expected);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void encodeJsBackslash() {
|
||||||
|
assertThat(RenderUtils.encodeJs("This is a test \\string\\"))
|
||||||
|
.isEqualTo("This is a test \\\\string\\\\");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void encodeJsBackspace() {
|
||||||
|
assertThat(RenderUtils.encodeJs("abc\bdef")).isEqualTo("abc\\bdef");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void encodeJsBlankInput() {
|
||||||
|
assertThat(RenderUtils.encodeJs(" ")).isEqualTo(" ");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void encodeJsCarriageReturn() {
|
||||||
|
assertThat(RenderUtils.encodeJs("abc\rdef")).isEqualTo("abc\\rdef");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void encodeJsConsecutiveSpecialChars() {
|
||||||
|
assertThat(RenderUtils.encodeJs("''\"\"\\\\")).isEqualTo("\\'\\'\\\"\\\"\\\\\\\\");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void encodeJsDoubleQuote() {
|
||||||
|
assertThat(RenderUtils.encodeJs("This is a test \"string\"")).isEqualTo("This is a test \\\"string\\\"");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void encodeJsEmptyInput() {
|
||||||
|
assertThat(RenderUtils.encodeJs("")).isEqualTo("");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void encodeJsFormFeed() {
|
||||||
|
assertThat(RenderUtils.encodeJs("abc\fdef")).isEqualTo("abc\\fdef");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void encodeJsForwardSlash() {
|
||||||
|
assertThat(RenderUtils.encodeJs("This is a test /string/")).isEqualTo("This is a test \\/string\\/");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void encodeJsMixedChars() {
|
||||||
|
String input = "Hello 'World' and \"JavaScript\" with \\slashes/ and \nnewlines.";
|
||||||
|
String expected = "Hello \\'World\\' and \\\"JavaScript\\\" with \\\\slashes\\/ and \\nnewlines.";
|
||||||
|
assertThat(RenderUtils.encodeJs(input)).isEqualTo(expected);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void encodeJsNewline() {
|
||||||
|
assertThat(RenderUtils.encodeJs("abc\ndef")).isEqualTo("abc\\ndef");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void encodeJsNoSpecialChars() {
|
||||||
|
String input = "Hello World 123!";
|
||||||
|
assertThat(input).isEqualTo(RenderUtils.encodeJs(input));
|
||||||
|
}
|
||||||
|
|
||||||
|
@ParameterizedTest
|
||||||
|
@NullAndEmptySource
|
||||||
|
@ValueSource(strings = {" ", " ", "\t", "\n"})
|
||||||
|
// isBlank() handles these
|
||||||
|
void encodeJsNullEmptyOrBlankInputs(String input) {
|
||||||
|
assertThat(RenderUtils.encodeJs(input)).isEqualTo(input);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void encodeJsNullInput() {
|
||||||
|
assertThat(RenderUtils.encodeJs(null)).isNull();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void encodeJsSingleQuote() {
|
||||||
|
assertThat(RenderUtils.encodeJs("This is a test 'string'")).isEqualTo("This is a test \\'string\\'");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void encodeJsSpecialCharsAtStartAndEnd() {
|
||||||
|
assertThat(RenderUtils.encodeJs("'test'")).isEqualTo("\\'test\\'");
|
||||||
|
assertThat(RenderUtils.encodeJs("\"test\"")).isEqualTo("\\\"test\\\"");
|
||||||
|
assertThat(RenderUtils.encodeJs("\\test\\")).isEqualTo("\\\\test\\\\");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void encodeJsTab() {
|
||||||
|
assertThat(RenderUtils.encodeJs("abc\tdef")).isEqualTo("abc\\tdef");
|
||||||
|
}
|
||||||
|
|
||||||
|
@ParameterizedTest
|
||||||
|
@MethodSource("javascriptEscapeTestCases")
|
||||||
|
void encodeJsVariousSpecialCharsParameterized(String input, String expected) {
|
||||||
|
assertThat(RenderUtils.encodeJs(input)).as("encodeJs(%s,%s)", input, expected).isEqualTo(expected);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nested
|
||||||
|
@DisplayName("Encode Tests")
|
||||||
|
class EncodeTests {
|
||||||
|
private final Properties p = new Properties();
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void encodeHtml() {
|
||||||
|
p.put(RenderUtils.ENCODING_PROPERTY, "html");
|
||||||
|
assertThat(RenderUtils.encode("<a test &>", p)).isEqualTo("<a test &>");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void encodeJs() {
|
||||||
|
p.put(RenderUtils.ENCODING_PROPERTY, "js");
|
||||||
|
assertThat(RenderUtils.encode("\"test'", p)).isEqualTo("\\\"test\\'");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void encodeJson() {
|
||||||
|
p.put(RenderUtils.ENCODING_PROPERTY, "json");
|
||||||
|
assertThat(RenderUtils.encode("This is a \"•test\"", p))
|
||||||
|
.isEqualTo("This is a \\\"\\u2022test\\\"");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void encodeUnicode() {
|
||||||
|
p.put(RenderUtils.ENCODING_PROPERTY, "unicode");
|
||||||
|
assertThat(RenderUtils.encode("test", p)).isEqualTo("\\u0074\\u0065\\u0073\\u0074");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void encodeUrl() {
|
||||||
|
p.put(RenderUtils.ENCODING_PROPERTY, "url");
|
||||||
|
assertThat(RenderUtils.encode("a = test", p)).isEqualTo("a%20%3D%20test");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void encodeWithInvalidFormat() {
|
||||||
|
p.put(RenderUtils.ENCODING_PROPERTY, "blah");
|
||||||
|
assertThat(RenderUtils.encode(TestCase.SAMPLE_TEXT, p)).isEqualTo(TestCase.SAMPLE_TEXT);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void encodeXml() {
|
||||||
|
p.put(RenderUtils.ENCODING_PROPERTY, "xml");
|
||||||
|
assertThat(RenderUtils.encode("Joe's Café & Bar", p)).isEqualTo("Joe's Café & Bar");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nested
|
||||||
|
@DisplayName("FetchUrl Tests")
|
||||||
|
class FetchUrlTests {
|
||||||
|
private static final String DEFAULT = "default";
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void fetchUrl() {
|
||||||
|
assertThat(RenderUtils.fetchUrl("https://postman-echo.com/get?foo=bar", DEFAULT))
|
||||||
|
.contains("\"foo\": \"bar\"");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void fetchUrlWith404() {
|
||||||
|
assertThat(RenderUtils.fetchUrl("https://www.google.com/404", DEFAULT)).isEqualTo(DEFAULT);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void fetchUrlWithInvalidHostname() {
|
||||||
|
assertThat(RenderUtils.fetchUrl("https://www.notreallythere.com/", DEFAULT)).isEqualTo(DEFAULT);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void fetchUrlWithInvalidUrl() {
|
||||||
|
assertThat(RenderUtils.fetchUrl("blah", DEFAULT)).isEqualTo(DEFAULT);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nested
|
||||||
|
@DisplayName("Mask Tests")
|
||||||
|
class MaskTests {
|
||||||
|
public static final String FOO = "4342256562440179";
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void maskWithDash() {
|
||||||
|
assertThat(RenderUtils.mask(FOO, "–", 22, true))
|
||||||
|
.isEqualTo("––––––––––––––––");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void maskWithEmpty() {
|
||||||
|
assertThat(RenderUtils.mask("", " ", 2, false)).isEmpty();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void maskWithHtmlBuller() {
|
||||||
|
assertThat(RenderUtils.mask(FOO, "•", -1, false)).isEqualTo(
|
||||||
|
"••••••••••••••••");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void maskWithQuestionMark() {
|
||||||
|
assertThat(RenderUtils.mask(FOO, "?", 4, false)).as("mask=?")
|
||||||
|
.isEqualTo("????????????0179");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nested
|
||||||
|
@DisplayName("Normalize Tests")
|
||||||
|
class NormalizeTests {
|
||||||
|
@Test
|
||||||
|
void normalize() {
|
||||||
|
assertThat(RenderUtils.normalize("News for January 6, 2023 (Paris)")).as("docs example")
|
||||||
|
.isEqualTo("news-for-january-6-2023-paris");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void normalizeWithGerman() {
|
||||||
|
assertThat(RenderUtils.normalize(SAMPLE_GERMAN)).as("greman")
|
||||||
|
.isEqualTo("mochten-sie-ein-paar-apfel");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void normalizeWithMixedPunctuation() {
|
||||||
|
assertThat(RenderUtils.normalize(" &()-_=[{]}\\|;:,<.>/")).as("blank").isEmpty();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void normalizeWithMixedSeparators() {
|
||||||
|
assertThat(RenderUtils.normalize("foo bar, <foo-bar>,foo:bar,foo;(bar), {foo} & bar=foo.bar[foo|bar]"))
|
||||||
|
.as("foo-bar")
|
||||||
|
.isEqualTo("foo-bar-foo-bar-foo-bar-foo-bar-foo-bar-foo-bar-foo-bar");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nested
|
||||||
|
@DisplayName("ROT13 Test")
|
||||||
|
class Rot13Test {
|
||||||
|
private static final String ENCODED = "Zöpugra Fvr rva cnne Äcsry?";
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void rot13Decode() {
|
||||||
|
assertThat(RenderUtils.rot13(ENCODED)).as("decode").isEqualTo(SAMPLE_GERMAN);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void rot13Encode() {
|
||||||
|
assertThat(RenderUtils.rot13(SAMPLE_GERMAN)).as("encode").isEqualTo(ENCODED);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void rot13WithEmpty() {
|
||||||
|
assertThat(RenderUtils.rot13("")).isEmpty();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nested
|
||||||
|
@DisplayName("Validate Credit Card")
|
||||||
|
class ValidateCreditCard {
|
||||||
|
@Test
|
||||||
|
void amexCreditCard() {
|
||||||
|
assertThat(RenderUtils.validateCreditCard("3433634926643302")).as("amex").isTrue();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void discoverCreditCard() {
|
||||||
|
assertThat(RenderUtils.validateCreditCard("6011 1076-8252 0629")).as("discover").isTrue();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void invalidCreditCard() {
|
||||||
|
assertThat(RenderUtils.validateCreditCard("0123456789012345")).as("invalid").isFalse();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void mastercardCreditCard() {
|
||||||
|
assertThat(RenderUtils.validateCreditCard("5189-5923-3915-0425")).as("mastercard").isTrue();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void visaCreditCard() {
|
||||||
|
assertThat(RenderUtils.validateCreditCard("4505 4672 3366 6430")).as("visa").isTrue();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
{{v render:rife.render.Uptime/}}
|
{{v render:rife.render.Uptime/}}
|
||||||
{{v render:rife.render.Uptime:spanish}}
|
{{v render:rife.render.Uptime:spanish}}
|
||||||
minute=\ minuto
|
minute=\ minuto
|
||||||
minutes=\ minutos
|
minutes=\ minutos\
|
||||||
|
week=\ week
|
||||||
|
weeks=\ semanas
|
||||||
{{/v}}
|
{{/v}}
|
||||||
{{v render:rife.render.Uptime:plain}}{{/v}}
|
{{v render:rife.render.Uptime:plain}}{{/v}}
|
Loading…
Add table
Add a link
Reference in a new issue