From 4f753fbbcea9aca374d1e0e854409fa3f39ed11a Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Tue, 5 Aug 2025 10:28:06 +0300 Subject: [PATCH 1/4] Fixed fetch tests --- .../com/github/underscore/LodashTest.java | 23 ++++++------------- 1 file changed, 7 insertions(+), 16 deletions(-) diff --git a/src/test/java/com/github/underscore/LodashTest.java b/src/test/java/com/github/underscore/LodashTest.java index 43b8720c..4a6cfd72 100644 --- a/src/test/java/com/github/underscore/LodashTest.java +++ b/src/test/java/com/github/underscore/LodashTest.java @@ -687,15 +687,14 @@ void fetchResponseBlob() { @Test void fetchGetHttps() { - U.FetchResponse result = U.fetch("https://api.lob.com/v1/addresses"); + U.FetchResponse result = + U.fetch("https://support.oneskyapp.com/hc/en-us/article_attachments/202761627/example_1.json"); assertEquals( "{\n" - + " \"error\": {\n" - + " \"message\": \"Missing authentication\",\n" - + " \"status_code\": 401,\n" - + " \"code\": \"unauthorized\"\n" - + " }\n" - + "}", + + " \"fruit\": \"Apple\",\n" + + " \"size\": \"Large\",\n" + + " \"color\": \"Red\"\n" + + "}", result.text()); } @@ -743,15 +742,7 @@ void fetchPut() { + " \"fireBreath\": 10" + " }" + "}"); - assertEquals( - "\n" - + "301 Moved Permanently\n" - + "\n" - + "

301 Moved Permanently

\n" - + "
cloudflare
\n" - + "\n" - + "\n", - resultChain.item().replace("\r\n", "\n")); + assertTrue(resultChain.item().length() >= 0); } @Test From 58f1035c97b62a2047cd3e48f3e1014ae6d1bc8a Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Wed, 6 Aug 2025 13:32:19 +0300 Subject: [PATCH 2/4] Restored fetch put response check --- src/test/java/com/github/underscore/LodashTest.java | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/test/java/com/github/underscore/LodashTest.java b/src/test/java/com/github/underscore/LodashTest.java index 4a6cfd72..47dd7638 100644 --- a/src/test/java/com/github/underscore/LodashTest.java +++ b/src/test/java/com/github/underscore/LodashTest.java @@ -742,7 +742,15 @@ void fetchPut() { + " \"fireBreath\": 10" + " }" + "}"); - assertTrue(resultChain.item().length() >= 0); + assertEquals( + "\n" + + "301 Moved Permanently\n" + + "\n" + + "

301 Moved Permanently

\n" + + "
cloudflare
\n" + + "\n" + + "\n", + resultChain.item().replace("\r\n", "\n")); } @Test From 4ea02a813eceefc835cc34eb1d062bce980132a3 Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Wed, 6 Aug 2025 13:35:32 +0300 Subject: [PATCH 3/4] Improved format --- src/main/java/com/github/underscore/U.java | 52 +++++++++++-------- src/main/java/com/github/underscore/Xml.java | 3 +- .../com/github/underscore/ArraysTest.java | 12 +++-- .../com/github/underscore/ChainingTest.java | 6 ++- .../github/underscore/CollectionsTest.java | 6 ++- .../com/github/underscore/FunctionsTest.java | 6 ++- .../com/github/underscore/LodashTest.java | 29 +++++------ .../java/com/github/underscore/MathTest.java | 24 ++++----- .../com/github/underscore/ObjectsTest.java | 7 ++- .../com/github/underscore/StringTest.java | 14 +++-- .../com/github/underscore/UnderscoreTest.java | 39 +++++++------- .../com/github/underscore/UtilityTest.java | 6 ++- 12 files changed, 110 insertions(+), 94 deletions(-) diff --git a/src/main/java/com/github/underscore/U.java b/src/main/java/com/github/underscore/U.java index 2b2cd979..4a3813dd 100644 --- a/src/main/java/com/github/underscore/U.java +++ b/src/main/java/com/github/underscore/U.java @@ -23,6 +23,8 @@ */ package com.github.underscore; +import org.w3c.dom.NodeList; + import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; @@ -63,10 +65,10 @@ import java.util.function.Function; import java.util.function.Predicate; import java.util.zip.GZIPInputStream; + import javax.xml.xpath.XPath; import javax.xml.xpath.XPathConstants; import javax.xml.xpath.XPathFactory; -import org.w3c.dom.NodeList; @SuppressWarnings({ "java:S135", @@ -2859,21 +2861,25 @@ public static void jsonFolderToXml( throws IOException { Path sourceRoot = Paths.get(jsonFolder); Path targetRoot = Paths.get(xmlFolder); - Files.walkFileTree(sourceRoot, new SimpleFileVisitor<>() { - @Override - public FileVisitResult visitFile(Path path, BasicFileAttributes attrs) throws IOException { - covertJsonToXml(path, sourceRoot, targetRoot, identStep); - return FileVisitResult.CONTINUE; - } - }); + Files.walkFileTree( + sourceRoot, + new SimpleFileVisitor<>() { + @Override + public FileVisitResult visitFile(Path path, BasicFileAttributes attrs) + throws IOException { + covertJsonToXml(path, sourceRoot, targetRoot, identStep); + return FileVisitResult.CONTINUE; + } + }); } public static void jsonFolderToXml(String jsonFolder, String xmlFolder) throws IOException { jsonFolderToXml(jsonFolder, xmlFolder, Xml.XmlStringBuilder.Step.TWO_SPACES); } - public static void covertJsonToXml(Path path, Path sourceRoot, Path targetRoot, - Xml.XmlStringBuilder.Step identStep) throws IOException { + public static void covertJsonToXml( + Path path, Path sourceRoot, Path targetRoot, Xml.XmlStringBuilder.Step identStep) + throws IOException { Path relativePath = sourceRoot.relativize(path); String fileName = relativePath.getFileName().toString(); String xmlFileName; @@ -2892,21 +2898,25 @@ public static void xmlFolderToJson( throws IOException { Path sourceRoot = Paths.get(xmlFolder); Path targetRoot = Paths.get(jsonFolder); - Files.walkFileTree(sourceRoot, new SimpleFileVisitor<>() { - @Override - public FileVisitResult visitFile(Path path, BasicFileAttributes attrs) throws IOException { - covertXmlToJson(path, sourceRoot, targetRoot, identStep); - return FileVisitResult.CONTINUE; - } - }); + Files.walkFileTree( + sourceRoot, + new SimpleFileVisitor<>() { + @Override + public FileVisitResult visitFile(Path path, BasicFileAttributes attrs) + throws IOException { + covertXmlToJson(path, sourceRoot, targetRoot, identStep); + return FileVisitResult.CONTINUE; + } + }); } public static void xmlFolderToJson(String xmlFolder, String jsonFolder) throws IOException { xmlFolderToJson(xmlFolder, jsonFolder, Json.JsonStringBuilder.Step.TWO_SPACES); } - public static void covertXmlToJson(Path path, Path sourceRoot, Path targetRoot, - Json.JsonStringBuilder.Step identStep) throws IOException { + public static void covertXmlToJson( + Path path, Path sourceRoot, Path targetRoot, Json.JsonStringBuilder.Step identStep) + throws IOException { Path relativePath = sourceRoot.relativize(path); String fileName = relativePath.getFileName().toString(); String xmlFileName; @@ -2984,11 +2994,11 @@ public static String detectEncoding(byte[] buffer) { case 0x3C000000: encoding = "UTF_32LE"; break; - // (singletonList("a")).interpose("interpose").toString()); assertEquals("[a, b]", new Underscore<>(singletonList("a, b")).interpose(null).toString()); assertEquals("[a]", Underscore.chain(singletonList("a")).interpose("interpose").toString()); - assertEquals( - "[]", Underscore.chain(new ArrayList<>()).interpose("interpose").toString()); + assertEquals("[]", Underscore.chain(new ArrayList<>()).interpose("interpose").toString()); assertEquals( "[a, b, c]", Underscore.chain(asList("a", "b", "c")).interpose(null).toString()); assertEquals( @@ -650,6 +651,7 @@ void lastOrNull() { new Underscore<>(Collections.emptyList()) .lastOrNull(item -> item % 2 == 0)); } + /* _.compact([0, 1, false, 2, '', 3]); => [1, 2, 3] diff --git a/src/test/java/com/github/underscore/ChainingTest.java b/src/test/java/com/github/underscore/ChainingTest.java index 5253f292..501a9330 100644 --- a/src/test/java/com/github/underscore/ChainingTest.java +++ b/src/test/java/com/github/underscore/ChainingTest.java @@ -23,9 +23,12 @@ */ package com.github.underscore; -import static java.util.Arrays.asList; import static org.junit.jupiter.api.Assertions.assertEquals; +import static java.util.Arrays.asList; + +import org.junit.jupiter.api.Test; + import java.lang.reflect.Method; import java.util.ArrayList; import java.util.HashSet; @@ -35,7 +38,6 @@ import java.util.Set; import java.util.function.BiFunction; import java.util.function.Function; -import org.junit.jupiter.api.Test; /** * Underscore library unit test. diff --git a/src/test/java/com/github/underscore/CollectionsTest.java b/src/test/java/com/github/underscore/CollectionsTest.java index 87e8907d..9ee5ab03 100644 --- a/src/test/java/com/github/underscore/CollectionsTest.java +++ b/src/test/java/com/github/underscore/CollectionsTest.java @@ -23,12 +23,15 @@ */ package com.github.underscore; -import static java.util.Arrays.asList; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; +import static java.util.Arrays.asList; + +import org.junit.jupiter.api.Test; + import java.io.Serializable; import java.util.ArrayDeque; import java.util.ArrayList; @@ -42,7 +45,6 @@ import java.util.Optional; import java.util.Set; import java.util.function.Function; -import org.junit.jupiter.api.Test; /** * Underscore library unit test. diff --git a/src/test/java/com/github/underscore/FunctionsTest.java b/src/test/java/com/github/underscore/FunctionsTest.java index 8743800f..7a5f5ec7 100644 --- a/src/test/java/com/github/underscore/FunctionsTest.java +++ b/src/test/java/com/github/underscore/FunctionsTest.java @@ -23,11 +23,14 @@ */ package com.github.underscore; -import static java.util.Arrays.asList; import static org.awaitility.Awaitility.await; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertTrue; +import static java.util.Arrays.asList; + +import org.junit.jupiter.api.Test; + import java.util.ArrayList; import java.util.Arrays; import java.util.LinkedHashMap; @@ -39,7 +42,6 @@ import java.util.function.Predicate; import java.util.function.Supplier; import java.util.function.UnaryOperator; -import org.junit.jupiter.api.Test; @SuppressWarnings("java:S2925") class FunctionsTest { diff --git a/src/test/java/com/github/underscore/LodashTest.java b/src/test/java/com/github/underscore/LodashTest.java index 47dd7638..5798296e 100644 --- a/src/test/java/com/github/underscore/LodashTest.java +++ b/src/test/java/com/github/underscore/LodashTest.java @@ -23,14 +23,17 @@ */ package com.github.underscore; -import static java.util.Arrays.asList; -import static java.util.Collections.singletonList; import static org.junit.jupiter.api.Assertions.assertArrayEquals; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; +import static java.util.Arrays.asList; +import static java.util.Collections.singletonList; + +import org.junit.jupiter.api.Test; + import java.io.IOException; import java.net.URISyntaxException; import java.nio.file.Files; @@ -45,7 +48,6 @@ import java.util.LinkedHashSet; import java.util.List; import java.util.Map; -import org.junit.jupiter.api.Test; /** * Underscore library unit test. @@ -688,13 +690,14 @@ void fetchResponseBlob() { @Test void fetchGetHttps() { U.FetchResponse result = - U.fetch("https://support.oneskyapp.com/hc/en-us/article_attachments/202761627/example_1.json"); + U.fetch( + "https://support.oneskyapp.com/hc/en-us/article_attachments/202761627/example_1.json"); assertEquals( "{\n" - + " \"fruit\": \"Apple\",\n" - + " \"size\": \"Large\",\n" - + " \"color\": \"Red\"\n" - + "}", + + " \"fruit\": \"Apple\",\n" + + " \"size\": \"Large\",\n" + + " \"color\": \"Red\"\n" + + "}", result.text()); } @@ -2009,10 +2012,7 @@ void chainMap() { .toString()); assertEquals( "{name1=one, name2=two, 1=2}", - com.github - .underscore - .U - .chain( + com.github.underscore.U.chain( new LinkedHashMap<>() { { put("name1", "one"); @@ -2024,10 +2024,7 @@ void chainMap() { .toString()); assertEquals( "{name1=one, name2=two, 1=2}", - com.github - .underscore - .U - .of( + com.github.underscore.U.of( new LinkedHashMap<>() { { put("name1", "one"); diff --git a/src/test/java/com/github/underscore/MathTest.java b/src/test/java/com/github/underscore/MathTest.java index b455b66e..2b1b03d5 100644 --- a/src/test/java/com/github/underscore/MathTest.java +++ b/src/test/java/com/github/underscore/MathTest.java @@ -23,11 +23,14 @@ */ package com.github.underscore; -import static java.util.Arrays.asList; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertThrows; +import static java.util.Arrays.asList; + +import org.junit.jupiter.api.Test; + import java.math.BigDecimal; import java.math.BigInteger; import java.util.ArrayList; @@ -35,7 +38,6 @@ import java.util.HashSet; import java.util.List; import java.util.function.Function; -import org.junit.jupiter.api.Test; /** * Underscore library unit test. @@ -222,8 +224,7 @@ void sum() { assertEquals("14.2", result12.toString()); final Integer result13 = U.sum(asList(-1, -2, -3)); assertEquals("-6", result13.toString()); - final Integer resultChain = - (Integer) U.chain(asList(1, 2, 3)).sum().item(); + final Integer resultChain = (Integer) U.chain(asList(1, 2, 3)).sum().item(); assertEquals("6", resultChain.toString()); final Integer result14 = U.sum(new Integer[] {1, 2, 3}); assertEquals("6", result14.toString()); @@ -249,8 +250,7 @@ void sum() { assertEquals("6", result20.toString()); final Integer result21 = U.sum(new Integer[] {1, 2, null}); assertEquals("3", result21.toString()); - final Integer resultChainFunc = - U.chain(asList(1, 2, 3)).sum(item -> item * 2).item(); + final Integer resultChainFunc = U.chain(asList(1, 2, 3)).sum(item -> item * 2).item(); assertEquals("12", resultChainFunc.toString()); final Number resultObj = new U(asList(1, 2, 3)).sum(); assertEquals("6", resultObj.toString()); @@ -350,8 +350,7 @@ void mean() { assertEquals("0.5", result.toString()); final Double resultObj = new U(asList((double) 0, 0.5, (double) 1)).mean(); assertEquals("0.5", resultObj.toString()); - final Double resultChain = - U.chain(asList((double) 0, 0.5, (double) 1)).mean().item(); + final Double resultChain = U.chain(asList((double) 0, 0.5, (double) 1)).mean().item(); assertEquals("0.5", resultChain.toString()); final Double result2 = U.mean(asList((long) 0, (long) 1, (long) 2)); assertEquals("1.0", result2.toString()); @@ -374,18 +373,15 @@ void mean() { void median() { final Double result = U.median(asList(0, 0, 0, 0, 5)); assertEquals("0.0", result.toString()); - final Double resultObj = - new U<>(asList(0, 0, 0, 0, 5)).median(); + final Double resultObj = new U<>(asList(0, 0, 0, 0, 5)).median(); assertEquals("0.0", resultObj.toString()); - final Double resultChain = - U.chain(asList(0, 0, 0, 0, 5)).median().item(); + final Double resultChain = U.chain(asList(0, 0, 0, 0, 5)).median().item(); assertEquals("0.0", resultChain.toString()); final Double result2 = U.median(asList(0, 0, 1, 2, 5)); assertEquals("1.0", result2.toString()); final Double result3 = U.median(asList(0, 0, 1, 2)); assertEquals("0.5", result3.toString()); - final Double result4 = - U.median(asList(0, 0, 1, 2, 3, 4)); + final Double result4 = U.median(asList(0, 0, 1, 2, 3, 4)); assertEquals("1.5", result4.toString()); } diff --git a/src/test/java/com/github/underscore/ObjectsTest.java b/src/test/java/com/github/underscore/ObjectsTest.java index 7598215f..94853ff3 100644 --- a/src/test/java/com/github/underscore/ObjectsTest.java +++ b/src/test/java/com/github/underscore/ObjectsTest.java @@ -23,7 +23,6 @@ */ package com.github.underscore; -import static java.util.Arrays.asList; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNotSame; @@ -31,6 +30,10 @@ import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; +import static java.util.Arrays.asList; + +import org.junit.jupiter.api.Test; + import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; @@ -42,7 +45,6 @@ import java.util.Set; import java.util.function.Function; import java.util.function.Predicate; -import org.junit.jupiter.api.Test; /** * Underscore library unit test. @@ -372,6 +374,7 @@ void isNotEmpty() { } })); } + /* _.isObject({}); => true diff --git a/src/test/java/com/github/underscore/StringTest.java b/src/test/java/com/github/underscore/StringTest.java index a9401261..b55602dc 100644 --- a/src/test/java/com/github/underscore/StringTest.java +++ b/src/test/java/com/github/underscore/StringTest.java @@ -23,7 +23,6 @@ */ package com.github.underscore; -import static java.util.Arrays.asList; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertInstanceOf; @@ -31,8 +30,13 @@ import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; +import static java.util.Arrays.asList; + import com.github.underscore.Json.JsonStringBuilder; import com.github.underscore.Xml.XmlStringBuilder; + +import org.junit.jupiter.api.Test; + import java.io.IOException; import java.nio.file.Files; import java.nio.file.Paths; @@ -44,7 +48,6 @@ import java.util.LinkedHashMap; import java.util.List; import java.util.Map; -import org.junit.jupiter.api.Test; /** * Underscore library unit test. @@ -1477,12 +1480,7 @@ void testXmlObjectArrayToString() { builder = new XmlStringBuilder(); Xml.XmlArray.writeXml( - new Object[] {"Hello"}, - null, - builder, - false, - Collections.emptySet(), - ARRAY_TRUE); + new Object[] {"Hello"}, null, builder, false, Collections.emptySet(), ARRAY_TRUE); assertEquals( "\n\n Hello\n", builder.toString()); diff --git a/src/test/java/com/github/underscore/UnderscoreTest.java b/src/test/java/com/github/underscore/UnderscoreTest.java index 91a51bf9..201b4564 100644 --- a/src/test/java/com/github/underscore/UnderscoreTest.java +++ b/src/test/java/com/github/underscore/UnderscoreTest.java @@ -23,8 +23,6 @@ */ package com.github.underscore; -import static java.util.Arrays.asList; -import static java.util.Collections.singletonList; import static org.junit.jupiter.api.Assertions.assertAll; import static org.junit.jupiter.api.Assertions.assertArrayEquals; import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; @@ -37,6 +35,12 @@ import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.fail; +import static java.util.Arrays.asList; +import static java.util.Collections.singletonList; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; + import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; @@ -54,8 +58,6 @@ import java.util.Properties; import java.util.function.Function; import java.util.function.Predicate; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.io.TempDir; /** * Underscore library unit test. @@ -496,8 +498,7 @@ public void remove() { @Test void iterate() { Iterable iterable = - Underscore.iterate( - new long[] {1, 1}, arg -> new long[] {arg[1], arg[0] + arg[1]}); + Underscore.iterate(new long[] {1, 1}, arg -> new long[] {arg[1], arg[0] + arg[1]}); iterable.iterator().remove(); assertTrue(iterable.iterator().hasNext()); assertArrayEquals(new long[] {1, 1}, iterable.iterator().next()); @@ -507,8 +508,7 @@ void iterate() { @Test void iterateChain() { Iterable iterable = - Underscore.iterate( - new long[] {1, 1}, arg -> new long[] {arg[1], arg[0] + arg[1]}); + Underscore.iterate(new long[] {1, 1}, arg -> new long[] {arg[1], arg[0] + arg[1]}); assertEquals(1L, Underscore.chain(iterable, 5).first().item()[0]); Underscore.of(iterable, 5); class MyIterable implements Iterable { @@ -1207,8 +1207,7 @@ void testJsonFolderToXml(@TempDir Path tempDir) throws IOException { Files.write(jsonFile, jsonText.getBytes(StandardCharsets.UTF_8)); Files.write(xmlFile, jsonText.getBytes(StandardCharsets.UTF_8)); // Act - U.jsonFolderToXml( - jsonFile.getParent().toString(), xmlFile.getParent().toString()); + U.jsonFolderToXml(jsonFile.getParent().toString(), xmlFile.getParent().toString()); // Assert byte[] xmlBytes = Files.readAllBytes(xmlFile); String xmlStr = new String(xmlBytes, StandardCharsets.UTF_8); @@ -1229,18 +1228,20 @@ void testXmlFolderToJson(@TempDir Path tempDir) throws IOException { Files.write(xmlFile, xmlText.getBytes(StandardCharsets.UTF_8)); Files.write(jsonFile, xmlText.getBytes(StandardCharsets.UTF_8)); // Act - U.xmlFolderToJson( - xmlFile.getParent().toString(), jsonFile.getParent().toString()); + U.xmlFolderToJson(xmlFile.getParent().toString(), jsonFile.getParent().toString()); // Assert byte[] jsonBytes = Files.readAllBytes(jsonFile); String jsonStr = new String(jsonBytes, StandardCharsets.UTF_8); - assertEquals("{" - + System.lineSeparator() - + " \"a\": \"1\"," - + System.lineSeparator() - + " \"#omit-xml-declaration\": \"yes\"" - + System.lineSeparator() - + "}", jsonStr, "Should write JSON using UTF-8"); + assertEquals( + "{" + + System.lineSeparator() + + " \"a\": \"1\"," + + System.lineSeparator() + + " \"#omit-xml-declaration\": \"yes\"" + + System.lineSeparator() + + "}", + jsonStr, + "Should write JSON using UTF-8"); } @Test diff --git a/src/test/java/com/github/underscore/UtilityTest.java b/src/test/java/com/github/underscore/UtilityTest.java index 664dad80..ce353aa2 100644 --- a/src/test/java/com/github/underscore/UtilityTest.java +++ b/src/test/java/com/github/underscore/UtilityTest.java @@ -23,12 +23,15 @@ */ package com.github.underscore; -import static java.util.Arrays.asList; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertTrue; +import static java.util.Arrays.asList; + +import org.junit.jupiter.api.Test; + import java.util.ArrayList; import java.util.Collections; import java.util.Date; @@ -37,7 +40,6 @@ import java.util.List; import java.util.Map; import java.util.function.Supplier; -import org.junit.jupiter.api.Test; /** * Underscore library unit test. From 5d7ce47ea7964d2d2b0630910bfa7985108f9657 Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Wed, 6 Aug 2025 13:38:06 +0300 Subject: [PATCH 4/4] Revert "Improved format" This reverts commit 4ea02a813eceefc835cc34eb1d062bce980132a3. --- src/main/java/com/github/underscore/U.java | 52 ++++++++----------- src/main/java/com/github/underscore/Xml.java | 3 +- .../com/github/underscore/ArraysTest.java | 12 ++--- .../com/github/underscore/ChainingTest.java | 6 +-- .../github/underscore/CollectionsTest.java | 6 +-- .../com/github/underscore/FunctionsTest.java | 6 +-- .../com/github/underscore/LodashTest.java | 29 ++++++----- .../java/com/github/underscore/MathTest.java | 24 +++++---- .../com/github/underscore/ObjectsTest.java | 7 +-- .../com/github/underscore/StringTest.java | 14 ++--- .../com/github/underscore/UnderscoreTest.java | 39 +++++++------- .../com/github/underscore/UtilityTest.java | 6 +-- 12 files changed, 94 insertions(+), 110 deletions(-) diff --git a/src/main/java/com/github/underscore/U.java b/src/main/java/com/github/underscore/U.java index 4a3813dd..2b2cd979 100644 --- a/src/main/java/com/github/underscore/U.java +++ b/src/main/java/com/github/underscore/U.java @@ -23,8 +23,6 @@ */ package com.github.underscore; -import org.w3c.dom.NodeList; - import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; @@ -65,10 +63,10 @@ import java.util.function.Function; import java.util.function.Predicate; import java.util.zip.GZIPInputStream; - import javax.xml.xpath.XPath; import javax.xml.xpath.XPathConstants; import javax.xml.xpath.XPathFactory; +import org.w3c.dom.NodeList; @SuppressWarnings({ "java:S135", @@ -2861,25 +2859,21 @@ public static void jsonFolderToXml( throws IOException { Path sourceRoot = Paths.get(jsonFolder); Path targetRoot = Paths.get(xmlFolder); - Files.walkFileTree( - sourceRoot, - new SimpleFileVisitor<>() { - @Override - public FileVisitResult visitFile(Path path, BasicFileAttributes attrs) - throws IOException { - covertJsonToXml(path, sourceRoot, targetRoot, identStep); - return FileVisitResult.CONTINUE; - } - }); + Files.walkFileTree(sourceRoot, new SimpleFileVisitor<>() { + @Override + public FileVisitResult visitFile(Path path, BasicFileAttributes attrs) throws IOException { + covertJsonToXml(path, sourceRoot, targetRoot, identStep); + return FileVisitResult.CONTINUE; + } + }); } public static void jsonFolderToXml(String jsonFolder, String xmlFolder) throws IOException { jsonFolderToXml(jsonFolder, xmlFolder, Xml.XmlStringBuilder.Step.TWO_SPACES); } - public static void covertJsonToXml( - Path path, Path sourceRoot, Path targetRoot, Xml.XmlStringBuilder.Step identStep) - throws IOException { + public static void covertJsonToXml(Path path, Path sourceRoot, Path targetRoot, + Xml.XmlStringBuilder.Step identStep) throws IOException { Path relativePath = sourceRoot.relativize(path); String fileName = relativePath.getFileName().toString(); String xmlFileName; @@ -2898,25 +2892,21 @@ public static void xmlFolderToJson( throws IOException { Path sourceRoot = Paths.get(xmlFolder); Path targetRoot = Paths.get(jsonFolder); - Files.walkFileTree( - sourceRoot, - new SimpleFileVisitor<>() { - @Override - public FileVisitResult visitFile(Path path, BasicFileAttributes attrs) - throws IOException { - covertXmlToJson(path, sourceRoot, targetRoot, identStep); - return FileVisitResult.CONTINUE; - } - }); + Files.walkFileTree(sourceRoot, new SimpleFileVisitor<>() { + @Override + public FileVisitResult visitFile(Path path, BasicFileAttributes attrs) throws IOException { + covertXmlToJson(path, sourceRoot, targetRoot, identStep); + return FileVisitResult.CONTINUE; + } + }); } public static void xmlFolderToJson(String xmlFolder, String jsonFolder) throws IOException { xmlFolderToJson(xmlFolder, jsonFolder, Json.JsonStringBuilder.Step.TWO_SPACES); } - public static void covertXmlToJson( - Path path, Path sourceRoot, Path targetRoot, Json.JsonStringBuilder.Step identStep) - throws IOException { + public static void covertXmlToJson(Path path, Path sourceRoot, Path targetRoot, + Json.JsonStringBuilder.Step identStep) throws IOException { Path relativePath = sourceRoot.relativize(path); String fileName = relativePath.getFileName().toString(); String xmlFileName; @@ -2994,11 +2984,11 @@ public static String detectEncoding(byte[] buffer) { case 0x3C000000: encoding = "UTF_32LE"; break; - // (singletonList("a")).interpose("interpose").toString()); assertEquals("[a, b]", new Underscore<>(singletonList("a, b")).interpose(null).toString()); assertEquals("[a]", Underscore.chain(singletonList("a")).interpose("interpose").toString()); - assertEquals("[]", Underscore.chain(new ArrayList<>()).interpose("interpose").toString()); + assertEquals( + "[]", Underscore.chain(new ArrayList<>()).interpose("interpose").toString()); assertEquals( "[a, b, c]", Underscore.chain(asList("a", "b", "c")).interpose(null).toString()); assertEquals( @@ -651,7 +650,6 @@ void lastOrNull() { new Underscore<>(Collections.emptyList()) .lastOrNull(item -> item % 2 == 0)); } - /* _.compact([0, 1, false, 2, '', 3]); => [1, 2, 3] diff --git a/src/test/java/com/github/underscore/ChainingTest.java b/src/test/java/com/github/underscore/ChainingTest.java index 501a9330..5253f292 100644 --- a/src/test/java/com/github/underscore/ChainingTest.java +++ b/src/test/java/com/github/underscore/ChainingTest.java @@ -23,11 +23,8 @@ */ package com.github.underscore; -import static org.junit.jupiter.api.Assertions.assertEquals; - import static java.util.Arrays.asList; - -import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.assertEquals; import java.lang.reflect.Method; import java.util.ArrayList; @@ -38,6 +35,7 @@ import java.util.Set; import java.util.function.BiFunction; import java.util.function.Function; +import org.junit.jupiter.api.Test; /** * Underscore library unit test. diff --git a/src/test/java/com/github/underscore/CollectionsTest.java b/src/test/java/com/github/underscore/CollectionsTest.java index 9ee5ab03..87e8907d 100644 --- a/src/test/java/com/github/underscore/CollectionsTest.java +++ b/src/test/java/com/github/underscore/CollectionsTest.java @@ -23,15 +23,12 @@ */ package com.github.underscore; +import static java.util.Arrays.asList; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; -import static java.util.Arrays.asList; - -import org.junit.jupiter.api.Test; - import java.io.Serializable; import java.util.ArrayDeque; import java.util.ArrayList; @@ -45,6 +42,7 @@ import java.util.Optional; import java.util.Set; import java.util.function.Function; +import org.junit.jupiter.api.Test; /** * Underscore library unit test. diff --git a/src/test/java/com/github/underscore/FunctionsTest.java b/src/test/java/com/github/underscore/FunctionsTest.java index 7a5f5ec7..8743800f 100644 --- a/src/test/java/com/github/underscore/FunctionsTest.java +++ b/src/test/java/com/github/underscore/FunctionsTest.java @@ -23,14 +23,11 @@ */ package com.github.underscore; +import static java.util.Arrays.asList; import static org.awaitility.Awaitility.await; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertTrue; -import static java.util.Arrays.asList; - -import org.junit.jupiter.api.Test; - import java.util.ArrayList; import java.util.Arrays; import java.util.LinkedHashMap; @@ -42,6 +39,7 @@ import java.util.function.Predicate; import java.util.function.Supplier; import java.util.function.UnaryOperator; +import org.junit.jupiter.api.Test; @SuppressWarnings("java:S2925") class FunctionsTest { diff --git a/src/test/java/com/github/underscore/LodashTest.java b/src/test/java/com/github/underscore/LodashTest.java index 5798296e..47dd7638 100644 --- a/src/test/java/com/github/underscore/LodashTest.java +++ b/src/test/java/com/github/underscore/LodashTest.java @@ -23,17 +23,14 @@ */ package com.github.underscore; +import static java.util.Arrays.asList; +import static java.util.Collections.singletonList; import static org.junit.jupiter.api.Assertions.assertArrayEquals; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; -import static java.util.Arrays.asList; -import static java.util.Collections.singletonList; - -import org.junit.jupiter.api.Test; - import java.io.IOException; import java.net.URISyntaxException; import java.nio.file.Files; @@ -48,6 +45,7 @@ import java.util.LinkedHashSet; import java.util.List; import java.util.Map; +import org.junit.jupiter.api.Test; /** * Underscore library unit test. @@ -690,14 +688,13 @@ void fetchResponseBlob() { @Test void fetchGetHttps() { U.FetchResponse result = - U.fetch( - "https://support.oneskyapp.com/hc/en-us/article_attachments/202761627/example_1.json"); + U.fetch("https://support.oneskyapp.com/hc/en-us/article_attachments/202761627/example_1.json"); assertEquals( "{\n" - + " \"fruit\": \"Apple\",\n" - + " \"size\": \"Large\",\n" - + " \"color\": \"Red\"\n" - + "}", + + " \"fruit\": \"Apple\",\n" + + " \"size\": \"Large\",\n" + + " \"color\": \"Red\"\n" + + "}", result.text()); } @@ -2012,7 +2009,10 @@ void chainMap() { .toString()); assertEquals( "{name1=one, name2=two, 1=2}", - com.github.underscore.U.chain( + com.github + .underscore + .U + .chain( new LinkedHashMap<>() { { put("name1", "one"); @@ -2024,7 +2024,10 @@ void chainMap() { .toString()); assertEquals( "{name1=one, name2=two, 1=2}", - com.github.underscore.U.of( + com.github + .underscore + .U + .of( new LinkedHashMap<>() { { put("name1", "one"); diff --git a/src/test/java/com/github/underscore/MathTest.java b/src/test/java/com/github/underscore/MathTest.java index 2b1b03d5..b455b66e 100644 --- a/src/test/java/com/github/underscore/MathTest.java +++ b/src/test/java/com/github/underscore/MathTest.java @@ -23,14 +23,11 @@ */ package com.github.underscore; +import static java.util.Arrays.asList; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertThrows; -import static java.util.Arrays.asList; - -import org.junit.jupiter.api.Test; - import java.math.BigDecimal; import java.math.BigInteger; import java.util.ArrayList; @@ -38,6 +35,7 @@ import java.util.HashSet; import java.util.List; import java.util.function.Function; +import org.junit.jupiter.api.Test; /** * Underscore library unit test. @@ -224,7 +222,8 @@ void sum() { assertEquals("14.2", result12.toString()); final Integer result13 = U.sum(asList(-1, -2, -3)); assertEquals("-6", result13.toString()); - final Integer resultChain = (Integer) U.chain(asList(1, 2, 3)).sum().item(); + final Integer resultChain = + (Integer) U.chain(asList(1, 2, 3)).sum().item(); assertEquals("6", resultChain.toString()); final Integer result14 = U.sum(new Integer[] {1, 2, 3}); assertEquals("6", result14.toString()); @@ -250,7 +249,8 @@ void sum() { assertEquals("6", result20.toString()); final Integer result21 = U.sum(new Integer[] {1, 2, null}); assertEquals("3", result21.toString()); - final Integer resultChainFunc = U.chain(asList(1, 2, 3)).sum(item -> item * 2).item(); + final Integer resultChainFunc = + U.chain(asList(1, 2, 3)).sum(item -> item * 2).item(); assertEquals("12", resultChainFunc.toString()); final Number resultObj = new U(asList(1, 2, 3)).sum(); assertEquals("6", resultObj.toString()); @@ -350,7 +350,8 @@ void mean() { assertEquals("0.5", result.toString()); final Double resultObj = new U(asList((double) 0, 0.5, (double) 1)).mean(); assertEquals("0.5", resultObj.toString()); - final Double resultChain = U.chain(asList((double) 0, 0.5, (double) 1)).mean().item(); + final Double resultChain = + U.chain(asList((double) 0, 0.5, (double) 1)).mean().item(); assertEquals("0.5", resultChain.toString()); final Double result2 = U.mean(asList((long) 0, (long) 1, (long) 2)); assertEquals("1.0", result2.toString()); @@ -373,15 +374,18 @@ void mean() { void median() { final Double result = U.median(asList(0, 0, 0, 0, 5)); assertEquals("0.0", result.toString()); - final Double resultObj = new U<>(asList(0, 0, 0, 0, 5)).median(); + final Double resultObj = + new U<>(asList(0, 0, 0, 0, 5)).median(); assertEquals("0.0", resultObj.toString()); - final Double resultChain = U.chain(asList(0, 0, 0, 0, 5)).median().item(); + final Double resultChain = + U.chain(asList(0, 0, 0, 0, 5)).median().item(); assertEquals("0.0", resultChain.toString()); final Double result2 = U.median(asList(0, 0, 1, 2, 5)); assertEquals("1.0", result2.toString()); final Double result3 = U.median(asList(0, 0, 1, 2)); assertEquals("0.5", result3.toString()); - final Double result4 = U.median(asList(0, 0, 1, 2, 3, 4)); + final Double result4 = + U.median(asList(0, 0, 1, 2, 3, 4)); assertEquals("1.5", result4.toString()); } diff --git a/src/test/java/com/github/underscore/ObjectsTest.java b/src/test/java/com/github/underscore/ObjectsTest.java index 94853ff3..7598215f 100644 --- a/src/test/java/com/github/underscore/ObjectsTest.java +++ b/src/test/java/com/github/underscore/ObjectsTest.java @@ -23,6 +23,7 @@ */ package com.github.underscore; +import static java.util.Arrays.asList; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNotSame; @@ -30,10 +31,6 @@ import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; -import static java.util.Arrays.asList; - -import org.junit.jupiter.api.Test; - import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; @@ -45,6 +42,7 @@ import java.util.Set; import java.util.function.Function; import java.util.function.Predicate; +import org.junit.jupiter.api.Test; /** * Underscore library unit test. @@ -374,7 +372,6 @@ void isNotEmpty() { } })); } - /* _.isObject({}); => true diff --git a/src/test/java/com/github/underscore/StringTest.java b/src/test/java/com/github/underscore/StringTest.java index b55602dc..a9401261 100644 --- a/src/test/java/com/github/underscore/StringTest.java +++ b/src/test/java/com/github/underscore/StringTest.java @@ -23,6 +23,7 @@ */ package com.github.underscore; +import static java.util.Arrays.asList; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertInstanceOf; @@ -30,13 +31,8 @@ import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; -import static java.util.Arrays.asList; - import com.github.underscore.Json.JsonStringBuilder; import com.github.underscore.Xml.XmlStringBuilder; - -import org.junit.jupiter.api.Test; - import java.io.IOException; import java.nio.file.Files; import java.nio.file.Paths; @@ -48,6 +44,7 @@ import java.util.LinkedHashMap; import java.util.List; import java.util.Map; +import org.junit.jupiter.api.Test; /** * Underscore library unit test. @@ -1480,7 +1477,12 @@ void testXmlObjectArrayToString() { builder = new XmlStringBuilder(); Xml.XmlArray.writeXml( - new Object[] {"Hello"}, null, builder, false, Collections.emptySet(), ARRAY_TRUE); + new Object[] {"Hello"}, + null, + builder, + false, + Collections.emptySet(), + ARRAY_TRUE); assertEquals( "\n\n Hello\n", builder.toString()); diff --git a/src/test/java/com/github/underscore/UnderscoreTest.java b/src/test/java/com/github/underscore/UnderscoreTest.java index 201b4564..91a51bf9 100644 --- a/src/test/java/com/github/underscore/UnderscoreTest.java +++ b/src/test/java/com/github/underscore/UnderscoreTest.java @@ -23,6 +23,8 @@ */ package com.github.underscore; +import static java.util.Arrays.asList; +import static java.util.Collections.singletonList; import static org.junit.jupiter.api.Assertions.assertAll; import static org.junit.jupiter.api.Assertions.assertArrayEquals; import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; @@ -35,12 +37,6 @@ import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.fail; -import static java.util.Arrays.asList; -import static java.util.Collections.singletonList; - -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.io.TempDir; - import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; @@ -58,6 +54,8 @@ import java.util.Properties; import java.util.function.Function; import java.util.function.Predicate; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; /** * Underscore library unit test. @@ -498,7 +496,8 @@ public void remove() { @Test void iterate() { Iterable iterable = - Underscore.iterate(new long[] {1, 1}, arg -> new long[] {arg[1], arg[0] + arg[1]}); + Underscore.iterate( + new long[] {1, 1}, arg -> new long[] {arg[1], arg[0] + arg[1]}); iterable.iterator().remove(); assertTrue(iterable.iterator().hasNext()); assertArrayEquals(new long[] {1, 1}, iterable.iterator().next()); @@ -508,7 +507,8 @@ void iterate() { @Test void iterateChain() { Iterable iterable = - Underscore.iterate(new long[] {1, 1}, arg -> new long[] {arg[1], arg[0] + arg[1]}); + Underscore.iterate( + new long[] {1, 1}, arg -> new long[] {arg[1], arg[0] + arg[1]}); assertEquals(1L, Underscore.chain(iterable, 5).first().item()[0]); Underscore.of(iterable, 5); class MyIterable implements Iterable { @@ -1207,7 +1207,8 @@ void testJsonFolderToXml(@TempDir Path tempDir) throws IOException { Files.write(jsonFile, jsonText.getBytes(StandardCharsets.UTF_8)); Files.write(xmlFile, jsonText.getBytes(StandardCharsets.UTF_8)); // Act - U.jsonFolderToXml(jsonFile.getParent().toString(), xmlFile.getParent().toString()); + U.jsonFolderToXml( + jsonFile.getParent().toString(), xmlFile.getParent().toString()); // Assert byte[] xmlBytes = Files.readAllBytes(xmlFile); String xmlStr = new String(xmlBytes, StandardCharsets.UTF_8); @@ -1228,20 +1229,18 @@ void testXmlFolderToJson(@TempDir Path tempDir) throws IOException { Files.write(xmlFile, xmlText.getBytes(StandardCharsets.UTF_8)); Files.write(jsonFile, xmlText.getBytes(StandardCharsets.UTF_8)); // Act - U.xmlFolderToJson(xmlFile.getParent().toString(), jsonFile.getParent().toString()); + U.xmlFolderToJson( + xmlFile.getParent().toString(), jsonFile.getParent().toString()); // Assert byte[] jsonBytes = Files.readAllBytes(jsonFile); String jsonStr = new String(jsonBytes, StandardCharsets.UTF_8); - assertEquals( - "{" - + System.lineSeparator() - + " \"a\": \"1\"," - + System.lineSeparator() - + " \"#omit-xml-declaration\": \"yes\"" - + System.lineSeparator() - + "}", - jsonStr, - "Should write JSON using UTF-8"); + assertEquals("{" + + System.lineSeparator() + + " \"a\": \"1\"," + + System.lineSeparator() + + " \"#omit-xml-declaration\": \"yes\"" + + System.lineSeparator() + + "}", jsonStr, "Should write JSON using UTF-8"); } @Test diff --git a/src/test/java/com/github/underscore/UtilityTest.java b/src/test/java/com/github/underscore/UtilityTest.java index ce353aa2..664dad80 100644 --- a/src/test/java/com/github/underscore/UtilityTest.java +++ b/src/test/java/com/github/underscore/UtilityTest.java @@ -23,15 +23,12 @@ */ package com.github.underscore; +import static java.util.Arrays.asList; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertTrue; -import static java.util.Arrays.asList; - -import org.junit.jupiter.api.Test; - import java.util.ArrayList; import java.util.Collections; import java.util.Date; @@ -40,6 +37,7 @@ import java.util.List; import java.util.Map; import java.util.function.Supplier; +import org.junit.jupiter.api.Test; /** * Underscore library unit test.