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

Implement AssertAll #248

Closed
wants to merge 2 commits into from
Closed
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
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,12 @@
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest</artifactId>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.6.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
Expand Down
48 changes: 48 additions & 0 deletions src/main/java/org/llorllale/cactoos/matchers/Assert.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* The MIT License (MIT)
*
* Copyright (c) for portions of project cactoos-matchers are held by
* Yegor Bugayenko, 2017-2018, as part of project cactoos.
* All other copyright for project cactoos-matchers are held by
* George Aristy, 2018-2020.
*
* 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.llorllale.cactoos.matchers;

import org.cactoos.Text;

/**
* Assertion with textual description.
*
* @since 1.0.0
*/
public interface Assert {
/**
* Description.
* @return Message of assert.
*/
Text description();

/**
* Affirm this assertion.
* @throws AssertionError if this assertion is refuted
*/
void affirm() throws AssertionError;
}
138 changes: 138 additions & 0 deletions src/main/java/org/llorllale/cactoos/matchers/AssertAll.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
/*
* The MIT License (MIT)
*
* Copyright (c) for portions of project cactoos-matchers are held by
* Yegor Bugayenko, 2017-2018, as part of project cactoos.
* All other copyright for project cactoos-matchers are held by
* George Aristy, 2018-2020.
*
* 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.llorllale.cactoos.matchers;

import org.cactoos.Text;
import org.cactoos.iterable.IterableOf;
import org.cactoos.iterable.MappedWithIndex;
import org.cactoos.scalar.Folded;
import org.cactoos.scalar.Mapped;
import org.cactoos.scalar.Unchecked;
import org.cactoos.text.FormattedText;
import org.cactoos.text.Joined;
import org.cactoos.text.TextOf;
import org.cactoos.text.UncheckedText;

/**
* Group assert.
* Fails if one of assertions fails.
*
* @since 1.0.0
* @checkstyle ClassDataAbstractionCoupling (100 lines)
*/
public final class AssertAll implements Assert {

/**
* Assertions.
*/
private final Iterable<? extends Assert> assertions;

/**
* Description.
*/
private final Text desc;

/**
* Ctor.
* @param description Description.
* @param assertions Assertions.
*/
public AssertAll(
final String description,
final Assert... assertions
) {
this(
description,
new IterableOf<>(assertions)
);
}

/**
* Ctor.
* @param description Description.
* @param assertions Assertions.
*/
public AssertAll(
final String description,
final Iterable<? extends Assert> assertions
) {
this.desc = new FormattedText(
"%s: %s",
new TextOf(description),
new Joined(
new TextOf(", "),
new MappedWithIndex<>(
(check, num) -> new FormattedText(
"%d) %s",
num + 1,
check.description().asString()
),
assertions
)
)
);
this.assertions = assertions;
}

@Override
public Text description() {
return this.desc;
}

@SuppressWarnings(
{ "PMD.AvoidCatchingGenericException", "PMD.AvoidCatchingThrowable" }
)
@Override
public void affirm() throws AssertionError {
new Unchecked<>(
new Mapped<Void>(
res -> {
if (res.getSuppressed().length > 0) {
throw res;
}
return null;
},
new Folded<>(
new AssertionError(
new UncheckedText(this.desc).asString()
),
(res, check) -> {
// @checkstyle IllegalCatchCheck (5 lines)
try {
check.affirm();
} catch (final Throwable ex) {
res.addSuppressed(ex);
}
return res;
},
this.assertions
)
)
).value();
}

}
74 changes: 74 additions & 0 deletions src/main/java/org/llorllale/cactoos/matchers/AssertWith.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* The MIT License (MIT)
*
* Copyright (c) for portions of project cactoos-matchers are held by
* Yegor Bugayenko, 2017-2018, as part of project cactoos.
* All other copyright for project cactoos-matchers are held by
* George Aristy, 2018-2020.
*
* 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.llorllale.cactoos.matchers;

import org.cactoos.Func;
import org.cactoos.Text;
import org.cactoos.func.UncheckedFunc;

/**
* Assert with value.
* @param <T> Type of value
*
* @since 1.0.0
*/
public final class AssertWith<T> implements Assert {
/**
* Value.
*/
private final T value;

/**
* Assertion.
*/
private final Func<? super T, ? extends Assert> func;

/**
* Ctor.
* @param value Value.
* @param func Function to produce assertion.
*/
public AssertWith(
final T value,
final Func<? super T, ? extends Assert> func
) {
this.value = value;
this.func = func;
}

@Override
public Text description() {
return new UncheckedFunc<>(this.func)
.apply(this.value)
.description();
}

@Override
public void affirm() throws AssertionError {
new UncheckedFunc<>(this.func).apply(this.value).affirm();
}
}
16 changes: 10 additions & 6 deletions src/main/java/org/llorllale/cactoos/matchers/Assertion.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
*/
package org.llorllale.cactoos.matchers;

import org.cactoos.Text;
import org.cactoos.text.TextOf;
import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.hamcrest.StringDescription;
Expand All @@ -42,7 +44,7 @@
* new Assertion<>(
* "must match the string",
* new TextOf("string"),
* new TextIs("string")
* new IsText("string")
* ).affirm(); // value is affirmed
* }
* }
Expand All @@ -67,7 +69,7 @@
* Assertion, and ban all overloads of the former in forbidden-apis.txt.
* We should also look into banning common matchers like Matchers.is(), etc.
*/
public final class Assertion<T> {
public final class Assertion<T> implements Assert {

/**
* Message.
Expand Down Expand Up @@ -98,10 +100,7 @@ public Assertion(
this.matcher = matcher;
}

/**
* Affirm this assertion.
* @throws AssertionError if this assertion is refuted
*/
@Override
public void affirm() throws AssertionError {
if (!this.matcher.matches(this.test)) {
final Description text = new StringDescription();
Expand All @@ -116,4 +115,9 @@ public void affirm() throws AssertionError {
throw new AssertionError(text.toString());
}
}

@Override
public Text description() {
return new TextOf(this.msg);
}
}
Loading