From 85ce4c3c8c45dba7a1cff611c71d029602224cce Mon Sep 17 00:00:00 2001 From: Roman Korostinskiy <70313618+c71n93@users.noreply.github.com> Date: Tue, 5 Mar 2024 14:56:07 +0300 Subject: [PATCH 1/7] #2863 replace anonymous class by single implementation to reduce code duplicatoin --- .github/workflows/simian.yml | 2 +- .../test/java/org/eolang/DataizedTest.java | 92 ++++++------------- 2 files changed, 29 insertions(+), 65 deletions(-) diff --git a/.github/workflows/simian.yml b/.github/workflows/simian.yml index 2f1afb342c..6c2e2d85e2 100644 --- a/.github/workflows/simian.yml +++ b/.github/workflows/simian.yml @@ -24,4 +24,4 @@ jobs: - run: | wget --quiet http://public.yegor256.com/simian.jar -O /tmp/simian.jar - run: | - java -jar /tmp/simian.jar -threshold=25 -excludes=**/gen -excludes=**/it **/*.java + java -jar /tmp/simian.jar -threshold=16 -excludes=**/gen -excludes=**/it **/*.java diff --git a/eo-runtime/src/test/java/org/eolang/DataizedTest.java b/eo-runtime/src/test/java/org/eolang/DataizedTest.java index 543c129406..c940a9fc94 100644 --- a/eo-runtime/src/test/java/org/eolang/DataizedTest.java +++ b/eo-runtime/src/test/java/org/eolang/DataizedTest.java @@ -48,22 +48,7 @@ void logsCorrectly() { final Level before = log.getLevel(); log.setLevel(Level.ALL); final List logs = new LinkedList<>(); - final Handler hnd = new Handler() { - @Override - public void publish(final LogRecord record) { - logs.add(record); - } - - @Override - public void flush() { - throw new UnsupportedOperationException("#flush()"); - } - - @Override - public void close() throws SecurityException { - throw new UnsupportedOperationException("#close()"); - } - }; + final Handler hnd = new TestHandler(logs); log.addHandler(hnd); new Dataized(new Data.ToPhi(1L), log).take(); log.setLevel(before); @@ -83,22 +68,7 @@ void logsWhenException() { final Level before = log.getLevel(); log.setLevel(Level.ALL); final List logs = new LinkedList<>(); - final Handler hnd = new Handler() { - @Override - public void publish(final LogRecord record) { - logs.add(record); - } - - @Override - public void flush() { - throw new UnsupportedOperationException("#flush()"); - } - - @Override - public void close() throws SecurityException { - throw new UnsupportedOperationException("#close()"); - } - }; + final Handler hnd = new TestHandler(logs); log.addHandler(hnd); final Phi wrong = new PhIncorrect(Phi.Φ); IntStream.range(0, 5).forEach( @@ -125,22 +95,7 @@ void printsShortLogs() throws InterruptedException { final Level before = log.getLevel(); log.setLevel(Level.ALL); final List logs = new LinkedList<>(); - final Handler hnd = new Handler() { - @Override - public void publish(final LogRecord record) { - logs.add(record); - } - - @Override - public void flush() { - throw new UnsupportedOperationException("#flush()"); - } - - @Override - public void close() throws SecurityException { - throw new UnsupportedOperationException("#close()"); - } - }; + final Handler hnd = new TestHandler(logs); log.addHandler(hnd); final Thread thread = new Thread( () -> { @@ -170,22 +125,7 @@ void printsLongLogs() throws InterruptedException { final Level before = log.getLevel(); log.setLevel(Level.ALL); final List logs = new LinkedList<>(); - final Handler hnd = new Handler() { - @Override - public void publish(final LogRecord record) { - logs.add(record); - } - - @Override - public void flush() { - throw new UnsupportedOperationException("#flush()"); - } - - @Override - public void close() throws SecurityException { - throw new UnsupportedOperationException("#close()"); - } - }; + final Handler hnd = new TestHandler(logs); log.addHandler(hnd); final Thread thread = new Thread( () -> { @@ -261,4 +201,28 @@ public static class PhiDec extends PhDefault { } } + private static class TestHandler extends Handler { + final List logs; + + TestHandler(final List logs) { + this.logs = logs; + } + + @Override + public void publish(final LogRecord record) { + logs.add(record); + } + + @Override + public void flush() { + throw new UnsupportedOperationException("#flush()"); + } + + @Override + public void close() throws SecurityException { + throw new UnsupportedOperationException("#close()"); + } + + } + } From 333e32f7b336f0c3a086de488b1f8798acc2482c Mon Sep 17 00:00:00 2001 From: Roman Korostinskiy <70313618+c71n93@users.noreply.github.com> Date: Tue, 5 Mar 2024 15:58:58 +0300 Subject: [PATCH 2/7] #2863 fix qulice warnings --- .../test/java/org/eolang/DataizedTest.java | 29 ++++++++++++++----- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/eo-runtime/src/test/java/org/eolang/DataizedTest.java b/eo-runtime/src/test/java/org/eolang/DataizedTest.java index c940a9fc94..65e8d24aab 100644 --- a/eo-runtime/src/test/java/org/eolang/DataizedTest.java +++ b/eo-runtime/src/test/java/org/eolang/DataizedTest.java @@ -48,7 +48,7 @@ void logsCorrectly() { final Level before = log.getLevel(); log.setLevel(Level.ALL); final List logs = new LinkedList<>(); - final Handler hnd = new TestHandler(logs); + final Handler hnd = new Hnd(logs); log.addHandler(hnd); new Dataized(new Data.ToPhi(1L), log).take(); log.setLevel(before); @@ -68,7 +68,7 @@ void logsWhenException() { final Level before = log.getLevel(); log.setLevel(Level.ALL); final List logs = new LinkedList<>(); - final Handler hnd = new TestHandler(logs); + final Handler hnd = new Hnd(logs); log.addHandler(hnd); final Phi wrong = new PhIncorrect(Phi.Φ); IntStream.range(0, 5).forEach( @@ -95,7 +95,7 @@ void printsShortLogs() throws InterruptedException { final Level before = log.getLevel(); log.setLevel(Level.ALL); final List logs = new LinkedList<>(); - final Handler hnd = new TestHandler(logs); + final Handler hnd = new Hnd(logs); log.addHandler(hnd); final Thread thread = new Thread( () -> { @@ -125,7 +125,7 @@ void printsLongLogs() throws InterruptedException { final Level before = log.getLevel(); log.setLevel(Level.ALL); final List logs = new LinkedList<>(); - final Handler hnd = new TestHandler(logs); + final Handler hnd = new Hnd(logs); log.addHandler(hnd); final Thread thread = new Thread( () -> { @@ -201,16 +201,29 @@ public static class PhiDec extends PhDefault { } } - private static class TestHandler extends Handler { - final List logs; + /** + * Handler implementation for tests. + * + * @since 1.0 + */ + private static class Hnd extends Handler { + /** + * Logs. + */ + private final List logs; - TestHandler(final List logs) { + /** + * Ctor. + * + * @param logs Logs + */ + Hnd(final List logs) { this.logs = logs; } @Override public void publish(final LogRecord record) { - logs.add(record); + this.logs.add(record); } @Override From 153aeb969971132b780ec7272affa6516b4e864d Mon Sep 17 00:00:00 2001 From: Roman Korostinskiy <70313618+c71n93@users.noreply.github.com> Date: Tue, 5 Mar 2024 17:32:54 +0300 Subject: [PATCH 3/7] #2863 create additional object for test to reduce code duplication --- .../EOorg/EOeolang/EOio/EOstdoutTest.java | 91 ++++++++++++------- 1 file changed, 59 insertions(+), 32 deletions(-) diff --git a/eo-runtime/src/test/java/EOorg/EOeolang/EOio/EOstdoutTest.java b/eo-runtime/src/test/java/EOorg/EOeolang/EOio/EOstdoutTest.java index 76224ac252..aabd1eb58a 100644 --- a/eo-runtime/src/test/java/EOorg/EOeolang/EOio/EOstdoutTest.java +++ b/eo-runtime/src/test/java/EOorg/EOeolang/EOio/EOstdoutTest.java @@ -32,9 +32,11 @@ import EOorg.EOeolang.EOtuple$EOempty; import java.io.ByteArrayOutputStream; import java.io.PrintStream; +import org.eolang.AtComposite; import org.eolang.Data; import org.eolang.Dataized; import org.eolang.PhCopy; +import org.eolang.PhDefault; import org.eolang.PhMethod; import org.eolang.PhWith; import org.eolang.Phi; @@ -96,27 +98,16 @@ public void doesNotPrintTwiceOnIntComparisonMethods(final String method) { final ByteArrayOutputStream stream = new ByteArrayOutputStream(); final String str = "Hello world"; new Dataized( - new PhWith( + new PrintWithCmp( new PhMethod( new Data.ToPhi(1L), method ), - 0, + new Data.ToPhi(2L), new PhWith( - new EOseq(Phi.Φ), - 0, - new PhWith( - new PhWith( - new EOtuple$EOempty(Phi.Φ).attr("with").get().copy(), - 0, - new PhWith( - new EOstdout(Phi.Φ, new PrintStream(stream)), - "text", - new Data.ToPhi(str) - ) - ).attr("with").get().copy(), - 0, new Data.ToPhi(2L) - ) + new EOstdout(Phi.Φ, new PrintStream(stream)), + "text", + new Data.ToPhi(str) ) ) ).take(); @@ -132,27 +123,16 @@ public void doesNotPrintTwiceOnFloatComparisonMethods(final String method) { final ByteArrayOutputStream stream = new ByteArrayOutputStream(); final String str = "Hello world"; new Dataized( - new PhWith( + new PrintWithCmp( new PhMethod( new Data.ToPhi(1.0), method ), - 0, + new Data.ToPhi(3.0), new PhWith( - new EOseq(Phi.Φ), - 0, - new PhWith( - new PhWith( - new EOtuple$EOempty(Phi.Φ).attr("with").get().copy(), - 0, - new PhWith( - new EOstdout(Phi.Φ, new PrintStream(stream)), - "text", - new Data.ToPhi(str) - ) - ).attr("with").get().copy(), - 0, new Data.ToPhi(3.0) - ) + new EOstdout(Phi.Φ, new PrintStream(stream)), + "text", + new Data.ToPhi(str) ) ) ).take(); @@ -161,4 +141,51 @@ public void doesNotPrintTwiceOnFloatComparisonMethods(final String method) { Matchers.equalTo(str) ); } + + /** + * Handler implementation for tests. + * + * @since 1.0 + */ + private static class PrintWithCmp extends PhDefault { + /** + * Ctor. + * + * @param method Comparison PhMethod ("lt", "gt", "lte", "gte") + * @param value Phi value to be compared + * @param stdout Phi object with printing a string via {@link EOstdout} object + */ + PrintWithCmp(final Phi method, final Phi value, final Phi stdout) { + super(Phi.Φ); + this.add( + "φ", + new AtComposite( + this, + self -> new Data.ToPhi( + new Dataized( + new PhWith( + method, + 0, + new PhWith( + new EOseq(Phi.Φ), + 0, + new PhWith( + new PhWith( + new EOtuple$EOempty(Phi.Φ) + .attr("with") + .get() + .copy(), + 0, + stdout + ).attr("with").get().copy(), + 0, value + ) + ) + ) + ).take() + ) + ) + ); + } + } } From 3be2745e68afcfb14e2e0e2fa678175b1bfe8c76 Mon Sep 17 00:00:00 2001 From: Roman Korostinskiy <70313618+c71n93@users.noreply.github.com> Date: Fri, 8 Mar 2024 21:56:20 +0300 Subject: [PATCH 4/7] #2863 use dirty hacks to reduce code duplication --- .../src/main/java/org/eolang/maven/rust/Names.java | 11 ++--------- eo-runtime/src/main/java/EOorg/EOeolang/EOrust.java | 11 ++--------- 2 files changed, 4 insertions(+), 18 deletions(-) diff --git a/eo-maven-plugin/src/main/java/org/eolang/maven/rust/Names.java b/eo-maven-plugin/src/main/java/org/eolang/maven/rust/Names.java index ab362fbdd4..2154266e86 100644 --- a/eo-maven-plugin/src/main/java/org/eolang/maven/rust/Names.java +++ b/eo-maven-plugin/src/main/java/org/eolang/maven/rust/Names.java @@ -189,20 +189,13 @@ private static ConcurrentHashMap load(final Path src) throws IOE final Object result = map.readObject(); if (result.getClass() != ConcurrentHashMap.class) { throw new ClassCastException( - String.format( - "Object inside %s has wrong class %s", - src, - result.getClass() - ) + String.format("Object inside %s has wrong class %s", src, result.getClass()) ); } return (ConcurrentHashMap) result; } catch (final ClassNotFoundException exc) { throw new IllegalArgumentException( - String.format( - "File %s contains invalid data", - src - ), + String.format("File %s contains invalid data", src), exc ); } diff --git a/eo-runtime/src/main/java/EOorg/EOeolang/EOrust.java b/eo-runtime/src/main/java/EOorg/EOeolang/EOrust.java index bef9fcaa8b..ed9af8e97e 100644 --- a/eo-runtime/src/main/java/EOorg/EOeolang/EOrust.java +++ b/eo-runtime/src/main/java/EOorg/EOeolang/EOrust.java @@ -189,20 +189,13 @@ private static ConcurrentHashMap load(final String src) throws I final Object result = map.readObject(); if (result.getClass() != ConcurrentHashMap.class) { throw new ClassCastException( - String.format( - "Object inside %s has wrong class %s", - src, - result.getClass() - ) + String.format("Object inside %s has wrong class %s", src, result.getClass()) ); } return (ConcurrentHashMap) result; } catch (final ClassNotFoundException exc) { throw new IllegalArgumentException( - String.format( - "File %s contains invalid data", - src - ), + String.format("File %s contains invalid data", src), exc ); } From 0d5bbfba21c89c00835feda7bf6cfbfff4808553 Mon Sep 17 00:00:00 2001 From: Roman Korostinskiy <70313618+c71n93@users.noreply.github.com> Date: Mon, 11 Mar 2024 11:52:51 +0300 Subject: [PATCH 5/7] #2863 fix documentation --- eo-runtime/src/test/java/EOorg/EOeolang/EOio/EOstdoutTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/eo-runtime/src/test/java/EOorg/EOeolang/EOio/EOstdoutTest.java b/eo-runtime/src/test/java/EOorg/EOeolang/EOio/EOstdoutTest.java index aabd1eb58a..9ce05d69a6 100644 --- a/eo-runtime/src/test/java/EOorg/EOeolang/EOio/EOstdoutTest.java +++ b/eo-runtime/src/test/java/EOorg/EOeolang/EOio/EOstdoutTest.java @@ -143,7 +143,7 @@ public void doesNotPrintTwiceOnFloatComparisonMethods(final String method) { } /** - * Handler implementation for tests. + * PrintWithCmp Phi * * @since 1.0 */ From 80f0434426f48b3c805583ce86d818407c271477 Mon Sep 17 00:00:00 2001 From: Roman Korostinskiy <70313618+c71n93@users.noreply.github.com> Date: Mon, 11 Mar 2024 12:03:18 +0300 Subject: [PATCH 6/7] #2863 fix documentation again --- eo-runtime/src/test/java/EOorg/EOeolang/EOio/EOstdoutTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/eo-runtime/src/test/java/EOorg/EOeolang/EOio/EOstdoutTest.java b/eo-runtime/src/test/java/EOorg/EOeolang/EOio/EOstdoutTest.java index 9ce05d69a6..29068d53f6 100644 --- a/eo-runtime/src/test/java/EOorg/EOeolang/EOio/EOstdoutTest.java +++ b/eo-runtime/src/test/java/EOorg/EOeolang/EOio/EOstdoutTest.java @@ -143,7 +143,7 @@ public void doesNotPrintTwiceOnFloatComparisonMethods(final String method) { } /** - * PrintWithCmp Phi + * PrintWithCmp Phi. * * @since 1.0 */ From 6b317f816313d833a62ab1056f35fa641f9bf68c Mon Sep 17 00:00:00 2001 From: Roman Korostinskiy <70313618+c71n93@users.noreply.github.com> Date: Mon, 11 Mar 2024 16:19:02 +0300 Subject: [PATCH 7/7] #2863 slightly change the messages --- .../src/main/java/org/eolang/maven/rust/Names.java | 11 +++++++++-- eo-runtime/src/main/java/EOorg/EOeolang/EOrust.java | 11 +++++++++-- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/eo-maven-plugin/src/main/java/org/eolang/maven/rust/Names.java b/eo-maven-plugin/src/main/java/org/eolang/maven/rust/Names.java index 2154266e86..ab362fbdd4 100644 --- a/eo-maven-plugin/src/main/java/org/eolang/maven/rust/Names.java +++ b/eo-maven-plugin/src/main/java/org/eolang/maven/rust/Names.java @@ -189,13 +189,20 @@ private static ConcurrentHashMap load(final Path src) throws IOE final Object result = map.readObject(); if (result.getClass() != ConcurrentHashMap.class) { throw new ClassCastException( - String.format("Object inside %s has wrong class %s", src, result.getClass()) + String.format( + "Object inside %s has wrong class %s", + src, + result.getClass() + ) ); } return (ConcurrentHashMap) result; } catch (final ClassNotFoundException exc) { throw new IllegalArgumentException( - String.format("File %s contains invalid data", src), + String.format( + "File %s contains invalid data", + src + ), exc ); } diff --git a/eo-runtime/src/main/java/EOorg/EOeolang/EOrust.java b/eo-runtime/src/main/java/EOorg/EOeolang/EOrust.java index ed9af8e97e..0fa4b543f4 100644 --- a/eo-runtime/src/main/java/EOorg/EOeolang/EOrust.java +++ b/eo-runtime/src/main/java/EOorg/EOeolang/EOrust.java @@ -189,13 +189,20 @@ private static ConcurrentHashMap load(final String src) throws I final Object result = map.readObject(); if (result.getClass() != ConcurrentHashMap.class) { throw new ClassCastException( - String.format("Object inside %s has wrong class %s", src, result.getClass()) + String.format( + "Object inside %s has wrong class %s, a ConcurrentHashMap was expected", + src, + result.getClass() + ) ); } return (ConcurrentHashMap) result; } catch (final ClassNotFoundException exc) { throw new IllegalArgumentException( - String.format("File %s contains invalid data", src), + String.format( + "File %s contains invalid data, a ConcurrentHashMap objects was expected", + src + ), exc ); }