Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce code duplication down to 16 lines per piece #2920

Merged
merged 7 commits into from Mar 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/simian.yml
Expand Up @@ -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
4 changes: 2 additions & 2 deletions eo-runtime/src/main/java/EOorg/EOeolang/EOrust.java
Expand Up @@ -190,7 +190,7 @@ private static ConcurrentHashMap<String, String> load(final String src) throws I
if (result.getClass() != ConcurrentHashMap.class) {
throw new ClassCastException(
String.format(
"Object inside %s has wrong class %s",
"Object inside %s has wrong class %s, a ConcurrentHashMap was expected",
src,
result.getClass()
)
Expand All @@ -200,7 +200,7 @@ private static ConcurrentHashMap<String, String> load(final String src) throws I
} catch (final ClassNotFoundException exc) {
throw new IllegalArgumentException(
String.format(
"File %s contains invalid data",
"File %s contains invalid data, a ConcurrentHashMap objects was expected",
src
),
exc
Expand Down
91 changes: 59 additions & 32 deletions eo-runtime/src/test/java/EOorg/EOeolang/EOio/EOstdoutTest.java
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand All @@ -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();
Expand All @@ -161,4 +141,51 @@ public void doesNotPrintTwiceOnFloatComparisonMethods(final String method) {
Matchers.equalTo(str)
);
}

/**
* PrintWithCmp Phi.
*
* @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()
)
)
);
}
}
}
105 changes: 41 additions & 64 deletions eo-runtime/src/test/java/org/eolang/DataizedTest.java
Expand Up @@ -48,22 +48,7 @@ void logsCorrectly() {
final Level before = log.getLevel();
log.setLevel(Level.ALL);
final List<LogRecord> 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 Hnd(logs);
log.addHandler(hnd);
new Dataized(new Data.ToPhi(1L), log).take();
log.setLevel(before);
Expand All @@ -83,22 +68,7 @@ void logsWhenException() {
final Level before = log.getLevel();
log.setLevel(Level.ALL);
final List<LogRecord> 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 Hnd(logs);
log.addHandler(hnd);
final Phi wrong = new PhIncorrect(Phi.Φ);
IntStream.range(0, 5).forEach(
Expand All @@ -125,22 +95,7 @@ void printsShortLogs() throws InterruptedException {
final Level before = log.getLevel();
log.setLevel(Level.ALL);
final List<LogRecord> 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 Hnd(logs);
log.addHandler(hnd);
final Thread thread = new Thread(
() -> {
Expand Down Expand Up @@ -170,22 +125,7 @@ void printsLongLogs() throws InterruptedException {
final Level before = log.getLevel();
log.setLevel(Level.ALL);
final List<LogRecord> 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 Hnd(logs);
log.addHandler(hnd);
final Thread thread = new Thread(
() -> {
Expand Down Expand Up @@ -261,4 +201,41 @@ public static class PhiDec extends PhDefault {
}
}

/**
* Handler implementation for tests.
*
* @since 1.0
*/
private static class Hnd extends Handler {
/**
* Logs.
*/
private final List<LogRecord> logs;

/**
* Ctor.
*
* @param logs Logs
*/
Hnd(final List<LogRecord> logs) {
this.logs = logs;
}

@Override
public void publish(final LogRecord record) {
this.logs.add(record);
}

@Override
public void flush() {
throw new UnsupportedOperationException("#flush()");
}

@Override
public void close() throws SecurityException {
throw new UnsupportedOperationException("#close()");
}

}

}