Skip to content

Commit

Permalink
Merge branch '__rultor'
Browse files Browse the repository at this point in the history
  • Loading branch information
rultor committed Jan 10, 2023
2 parents 51394a6 + 4f111d3 commit 3b60ce1
Show file tree
Hide file tree
Showing 3 changed files with 157 additions and 10 deletions.
2 changes: 1 addition & 1 deletion eo-runtime/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ SOFTWARE.
<limit>
<counter>COMPLEXITY</counter>
<value>COVEREDRATIO</value>
<minimum>0.52</minimum>
<minimum>0.53</minimum>
</limit>
<limit>
<counter>METHOD</counter>
Expand Down
35 changes: 26 additions & 9 deletions eo-runtime/src/main/java/org/eolang/AtLogged.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,15 @@

package org.eolang;

import java.io.PrintStream;

/**
* An attribute that logs all its operations to the console (very
* convenient for debugging).
*
* @since 0.24
* @todo #1614:30min This class don't have enough tests. We need to add more, at least for
* the next methods: toString(), 蠁Term(), copy(), put(), get().
* @todo #1617:30min Use logger instead of System.out.println. It's much better to use standard
* logger in that class. Examples of using logger are inside {@link PhDefault} or {@link Dataized}.
*/
final class AtLogged implements Attr {

Expand All @@ -44,14 +46,30 @@ final class AtLogged implements Attr {
*/
private final String owner;

/**
* Output stream.
*/
private final PrintStream out;

/**
* Ctor.
* @param attr Attribute
* @param label Label
*/
AtLogged(final Attr attr, final String label) {
this(attr, label, System.out);
}

/**
* Ctor.
* @param attr Attribute
* @param label Label
* @param stream Output stream
*/
AtLogged(final Attr attr, final String label, final PrintStream stream) {
this.origin = attr;
this.owner = label;
this.out = stream;
}

@Override
Expand All @@ -66,25 +84,24 @@ public String toString() {

@Override
public Attr copy(final Phi self) {
System.out.printf(" %s.copy()...\n", this.owner);
this.out.printf(" %s.copy()...\n", this.owner);
final Attr ret = this.origin.copy(self);
System.out.printf(" %s.copy()!\n", this.owner);
this.out.printf(" %s.copy()!\n", this.owner);
return ret;
}

@Override
public Phi get() {
System.out.printf(" %s.get()...\n", this.owner);
this.out.printf(" %s.get()...\n", this.owner);
final Phi ret = this.origin.get();
System.out.printf(" %s.get()! -> %d\n", this.owner, ret.hashCode());
this.out.printf(" %s.get()! -> %d\n", this.owner, ret.hashCode());
return ret;
}

@Override
public void put(final Phi src) {
System.out.printf(" %s.put()...\n", this.owner);
this.out.printf(" %s.put()...\n", this.owner);
this.origin.put(src);
System.out.printf(" %s.put()!\n", this.owner);
this.out.printf(" %s.put()!\n", this.owner);
}

}
130 changes: 130 additions & 0 deletions eo-runtime/src/test/java/org/eolang/AtLoggedTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2016-2022 Objectionary.com
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package org.eolang;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.nio.charset.StandardCharsets;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

/**
* Test case for {@link AtLogged}.
*
* @since 0.29
*/
class AtLoggedTest {

/**
* Testable object.
*/
private AtLogged logged;

/**
* Delegate.
*/
private AtSimple origin;

/**
* Output stream.
*/
private ByteArrayOutputStream out;

/**
* Label for logging.
*/
private String label;

@BeforeEach
void setUp() {
this.out = new ByteArrayOutputStream();
this.origin = new AtSimple();
this.label = "test";
this.logged = new AtLogged(this.origin, this.label, new PrintStream(this.out));
}

@Test
void convertsToStringAsOrigin() {
MatcherAssert.assertThat(
this.logged.toString(),
Matchers.equalTo(this.origin.toString())
);
}

@Test
void convertsToPhiTermAsOrigin() {
MatcherAssert.assertThat(
this.logged.蠁Term(),
Matchers.equalTo(this.origin.蠁Term())
);
}

@Test
void copiesWithLogging() {
this.logged.copy(Phi.);
final String log = new String(this.out.toByteArray(), StandardCharsets.UTF_8);
MatcherAssert.assertThat(
log,
Matchers.containsString(String.format(" %s.copy()...", this.label))
);
MatcherAssert.assertThat(
log,
Matchers.containsString(String.format(" %s.copy()!", this.label))
);
}

@Test
void getsWithLogging() {
MatcherAssert.assertThat(
this.logged.get(),
Matchers.equalTo(this.origin.get())
);
final String log = new String(this.out.toByteArray(), StandardCharsets.UTF_8);
MatcherAssert.assertThat(
log,
Matchers.containsString(String.format(" %s.get()...", this.label))
);
MatcherAssert.assertThat(
log,
Matchers.containsString(String.format(" %s.get()!", this.label))
);
}

@Test
void putsWithLogging() {
this.logged.put(Phi.);
final String log = new String(this.out.toByteArray(), StandardCharsets.UTF_8);
MatcherAssert.assertThat(
log,
Matchers.containsString(String.format(" %s.put()...", this.label))
);
MatcherAssert.assertThat(
log,
Matchers.containsString(String.format(" %s.put()!", this.label))
);
}
}

2 comments on commit 3b60ce1

@0pdd
Copy link

@0pdd 0pdd commented on 3b60ce1 Jan 10, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Puzzle 1614-2eb2b1c3 disappeared from eo-runtime/src/main/java/org/eolang/AtLogged.java), that's why I closed #1617. Please, remember that the puzzle was not necessarily removed in this particular commit. Maybe it happened earlier, but we discovered this fact only now.

@0pdd
Copy link

@0pdd 0pdd commented on 3b60ce1 Jan 10, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Puzzle 1617-eb1a6d1b discovered in eo-runtime/src/main/java/org/eolang/AtLogged.java) and submitted as #1664. Please, remember that the puzzle was not necessarily added in this particular commit. Maybe it was added earlier, but we discovered it only now.

Please sign in to comment.