From ee852879aa6599bfd627b949f0d8d7caa100c457 Mon Sep 17 00:00:00 2001 From: andreoss Date: Thu, 29 Apr 2021 18:29:28 -0400 Subject: [PATCH] (#1589) Add `toString`, `equals` & `hashCode` to `text.Sticky` --- src/main/java/org/cactoos/text/Sticky.java | 18 ++++-------- .../java/org/cactoos/text/StickyTest.java | 29 +++++++++++++++++++ 2 files changed, 35 insertions(+), 12 deletions(-) diff --git a/src/main/java/org/cactoos/text/Sticky.java b/src/main/java/org/cactoos/text/Sticky.java index 59f0f8a1ce..c043886888 100644 --- a/src/main/java/org/cactoos/text/Sticky.java +++ b/src/main/java/org/cactoos/text/Sticky.java @@ -23,7 +23,6 @@ */ package org.cactoos.text; -import org.cactoos.Scalar; import org.cactoos.Text; /** @@ -37,22 +36,17 @@ * @see org.cactoos.scalar.Sticky * @since 0.47 */ -public final class Sticky implements Text { - /** - * Sticky scalar. - */ - private final Scalar scalar; - +public final class Sticky extends TextEnvelope { /** * Ctor. * @param txt Text to cache */ public Sticky(final Text txt) { - this.scalar = new org.cactoos.scalar.Sticky<>(txt::asString); + super( + new TextOfScalar( + new org.cactoos.scalar.Sticky<>(txt::asString) + ) + ); } - @Override - public String asString() throws Exception { - return this.scalar.value(); - } } diff --git a/src/test/java/org/cactoos/text/StickyTest.java b/src/test/java/org/cactoos/text/StickyTest.java index 2a569dd138..f4e51178cb 100644 --- a/src/test/java/org/cactoos/text/StickyTest.java +++ b/src/test/java/org/cactoos/text/StickyTest.java @@ -24,6 +24,10 @@ package org.cactoos.text; import org.cactoos.Text; +import org.hamcrest.core.AllOf; +import org.hamcrest.core.IsEqual; +import org.hamcrest.core.IsNot; +import org.hamcrest.object.HasToString; import org.junit.jupiter.api.Test; import org.llorllale.cactoos.matchers.Assertion; import org.llorllale.cactoos.matchers.IsText; @@ -31,6 +35,7 @@ /** * Test case for {@link Sticky}. * @since 0.47 + * @checkstyle ClassDataAbstractionCouplingCheck (100 lines) */ final class StickyTest { @Test @@ -42,4 +47,28 @@ void cachesResult() { new IsText(sticky) ).affirm(); } + + @Test + void equalsItself() { + final Text random = new Randomized(); + final Text sticky = new Sticky(random); + new Assertion<>( + "must be the same as itself", + sticky, + new AllOf( + new IsEqual<>(sticky), + new IsNot<>(new IsEqual<>(random)) + ) + ).affirm(); + } + + @Test + void hasProperToString() { + final String str = "Hello"; + new Assertion<>( + "must have toString method", + new Sticky(() -> str), + new HasToString<>(new IsEqual<>(str)) + ).affirm(); + } }