Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions src/main/java/org/cactoos/scalar/package-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,5 @@
* Scalars.
*
* @since 0.12
* @todo #1569:30min Create tests for the semantics of relaxed wildcards
* in changed classes of {@link org.cactoos.scalar} package in #1569,
* which is a child of #1533.
*/
package org.cactoos.scalar;
147 changes: 147 additions & 0 deletions src/test/java/org/cactoos/scalar/WildcardSemanticsTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
/*
* SPDX-FileCopyrightText: Copyright (c) 2017-2025 Yegor Bugayenko
* SPDX-License-Identifier: MIT
*/
package org.cactoos.scalar;

import java.util.ArrayList;
import java.util.List;
import org.cactoos.Func;
import org.cactoos.Proc;
import org.cactoos.Scalar;
import org.cactoos.Text;
import org.cactoos.iterable.IterableOf;
import org.cactoos.text.TextOf;
import org.cactoos.text.Upper;
import org.junit.jupiter.api.Test;
import org.llorllale.cactoos.matchers.Assertion;
import org.llorllale.cactoos.matchers.HasValue;
import org.llorllale.cactoos.matchers.IsText;

/**
* Tests for wildcard semantics in scalar classes.
* This test class verifies the proper behavior of relaxed wildcards
* (? extends T, ? super T) in the scalar package as requested in #1569.
*
* @since 1.0.0
* @checkstyle JavadocMethodCheck (500 lines)
*/
final class WildcardSemanticsTest {

/**
* Test string constant.
*/
private static final String HELLO = "hello";

@Test
void scalarOfAcceptsCovariantFunc() {
final Func<Object, String> func = input -> input.toString();
final String input = "test";
new Assertion<>(
"ScalarOf must accept covariant function",
new ScalarOf<>(func, input),
new HasValue<>("test")
).affirm();
}

@Test
void scalarOfAcceptsContravariantProc() {
final List<String> result = new ArrayList<>(1);
final Proc<Object> proc = input -> result.add(input.toString());
final String input = WildcardSemanticsTest.HELLO;
final String expected = "world";
new Assertion<>(
"ScalarOf must accept contravariant processor",
new ScalarOf<>(proc, input, expected),
new HasValue<>(expected)
).affirm();
}

@Test
void mappedAcceptsCovariantScalar() {
final Scalar<String> scalar = () -> WildcardSemanticsTest.HELLO;
final Func<Object, Text> func = input -> new Upper(new TextOf(input.toString()));
new Assertion<>(
"Mapped must accept covariant scalar",
new Mapped<>(func, scalar),
new HasValue<>(new IsText("HELLO"))
).affirm();
}

@Test
void mappedAcceptsContravariantFunc() {
final Scalar<String> scalar = () -> WildcardSemanticsTest.HELLO;
final Func<CharSequence, Text> func = input -> new Upper(new TextOf(input.toString()));
new Assertion<>(
"Mapped must accept contravariant function",
new Mapped<>(func, scalar),
new HasValue<>(new IsText("HELLO"))
).affirm();
}

@Test
void andAcceptsContravariantFunc() {
final Func<CharSequence, Boolean> func = input -> input.length() > 0;
final String[] inputs = {WildcardSemanticsTest.HELLO, "world"};
new Assertion<>(
"And must accept contravariant function",
new And(func, inputs),
new HasValue<>(true)
).affirm();
}

@Test
void andAcceptsCovariantIterable() {
final Func<CharSequence, Boolean> func = input -> input.length() > 0;
final IterableOf<String> inputs = new IterableOf<>(WildcardSemanticsTest.HELLO, "world");
new Assertion<>(
"And must accept covariant iterable",
new And(func, inputs),
new HasValue<>(true)
).affirm();
}

@Test
void orAcceptsContravariantFunc() {
final Func<CharSequence, Boolean> func = input -> input.length() > 5;
final String[] inputs = {"hi", WildcardSemanticsTest.HELLO};
new Assertion<>(
"Or must accept contravariant function",
new Or(func, inputs),
new HasValue<>(false)
).affirm();
}

@Test
void orAcceptsCovariantIterable() {
final Func<CharSequence, Boolean> func = input -> input.length() > 3;
final IterableOf<String> inputs = new IterableOf<>("hi", WildcardSemanticsTest.HELLO);
new Assertion<>(
"Or must accept covariant iterable",
new Or(func, inputs),
new HasValue<>(true)
).affirm();
}

@Test
void flattenedAcceptsNestedWildcards() {
final Scalar<String> inner = () -> "test";
final Scalar<Scalar<String>> outer = () -> inner;
new Assertion<>(
"Flattened must accept nested wildcards",
new Flattened<>(outer),
new HasValue<>("test")
).affirm();
}

@Test
void scalarWithFallbackAcceptsCovariantScalar() {
final Scalar<String> scalar = () -> "success";
new Assertion<>(
"ScalarWithFallback must accept covariant scalar",
new ScalarWithFallback<>(scalar),
new HasValue<>("success")
).affirm();
}

}