-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Dedicated Hamcrest Adapter module, closes #109
Provide a separate module containing adapters between Hamcrest and Confidence. This deprecates the `Hamcrest` `Quality` in confidence-core.
- Loading branch information
Showing
8 changed files
with
256 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
plugins { | ||
id 'java-library' | ||
} | ||
apply from: '../jacoco.gradle' | ||
apply from: '../publish.gradle' | ||
|
||
sourceCompatibility = JavaVersion.VERSION_1_8 | ||
targetCompatibility = JavaVersion.VERSION_1_8 | ||
|
||
dependencies { | ||
compileOnly libs.eclipse.jdt.anntation | ||
compileOnly libs.srcless.annotations | ||
annotationProcessor libs.bundles.srcless.processors | ||
|
||
api libs.hamcrest | ||
api project(':confidence-core') | ||
implementation libs.jems2 | ||
|
||
testImplementation project(':confidence-test') | ||
testImplementation libs.jems2.testing | ||
testImplementation libs.junit.jupiter.api | ||
testRuntimeOnly libs.junit.jupiter.engine | ||
} | ||
|
||
test { | ||
useJUnitPlatform() | ||
} |
59 changes: 59 additions & 0 deletions
59
...dence-hamcrest/src/main/java/org/saynotobugs/confidence/hamcrest/matcher/QualifiesAs.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/* | ||
* Copyright 2024 dmfs GmbH | ||
* | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
|
||
package org.saynotobugs.confidence.hamcrest.matcher; | ||
|
||
import org.dmfs.srcless.annotations.staticfactory.StaticFactories; | ||
import org.hamcrest.Description; | ||
import org.hamcrest.TypeSafeDiagnosingMatcher; | ||
import org.saynotobugs.confidence.Assessment; | ||
import org.saynotobugs.confidence.Quality; | ||
import org.saynotobugs.confidence.Scribe; | ||
import org.saynotobugs.confidence.scribe.StringBuilderScribe; | ||
|
||
/** | ||
* A Hamcrest {@link org.hamcrest.Matcher} of {@code T} that delegates to a {@link Quality}. | ||
*/ | ||
@StaticFactories(value = "Hamcrest", packageName = "org.saynotobugs.confidence.hamcrest") | ||
public final class QualifiesAs<T> extends TypeSafeDiagnosingMatcher<T> | ||
{ | ||
private final Quality<? super T> mQuality; | ||
|
||
public QualifiesAs(Quality<? super T> quality) | ||
{ | ||
mQuality = quality; | ||
} | ||
|
||
@Override | ||
protected boolean matchesSafely(T item, Description mismatchDescription) | ||
{ | ||
Scribe scribe = new StringBuilderScribe(""); | ||
Assessment assessment = mQuality.assessmentOf(item); | ||
assessment.description().describeTo(scribe); | ||
mismatchDescription.appendText(scribe.toString()); | ||
return assessment.isSuccess(); | ||
} | ||
|
||
@Override | ||
public void describeTo(Description description) | ||
{ | ||
Scribe scribe = new StringBuilderScribe(""); | ||
mQuality.description().describeTo(scribe); | ||
description.appendText(scribe.toString()); | ||
} | ||
} |
67 changes: 67 additions & 0 deletions
67
confidence-hamcrest/src/main/java/org/saynotobugs/confidence/hamcrest/quality/Matches.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* | ||
* Copyright 2024 dmfs GmbH | ||
* | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
|
||
package org.saynotobugs.confidence.hamcrest.quality; | ||
|
||
import org.dmfs.srcless.annotations.staticfactory.StaticFactories; | ||
import org.hamcrest.StringDescription; | ||
import org.saynotobugs.confidence.Assessment; | ||
import org.saynotobugs.confidence.Description; | ||
import org.saynotobugs.confidence.Quality; | ||
import org.saynotobugs.confidence.assessment.Fail; | ||
import org.saynotobugs.confidence.assessment.Pass; | ||
import org.saynotobugs.confidence.description.Text; | ||
|
||
|
||
/** | ||
* A {@link Quality} of {@code T} that delegates to a Hamcrest {@link org.hamcrest.Matcher}. | ||
*/ | ||
@StaticFactories(value = "Hamcrest", packageName = "org.saynotobugs.confidence.hamcrest") | ||
public final class Matches<T> implements Quality<T> | ||
{ | ||
private final org.hamcrest.Matcher<? super T> mDelegate; | ||
|
||
|
||
public Matches(org.hamcrest.Matcher<? super T> delegate) | ||
{ | ||
mDelegate = delegate; | ||
} | ||
|
||
|
||
@Override | ||
public Assessment assessmentOf(T candidate) | ||
{ | ||
if (mDelegate.matches(candidate)) | ||
{ | ||
return new Pass(); | ||
} | ||
org.hamcrest.Description mismatch = new StringDescription(); | ||
mDelegate.describeMismatch(candidate, mismatch); | ||
return new Fail(new Text(mismatch.toString())); | ||
} | ||
|
||
|
||
@Override | ||
public Description description() | ||
{ | ||
org.hamcrest.Description expected = new StringDescription(); | ||
mDelegate.describeTo(expected); | ||
return new Text(expected.toString()); | ||
} | ||
|
||
} |
41 changes: 41 additions & 0 deletions
41
...e-hamcrest/src/test/java/org/saynotobugs/confidence/hamcrest/matcher/QualifiesAsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* | ||
* Copyright 2024 dmfs GmbH | ||
* | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
|
||
package org.saynotobugs.confidence.hamcrest.matcher; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.saynotobugs.confidence.quality.object.EqualTo; | ||
|
||
import static org.dmfs.jems2.hamcrest.matchers.matcher.MatcherMatcher.*; | ||
import static org.hamcrest.CoreMatchers.allOf; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
|
||
class QualifiesAsTest | ||
{ | ||
@Test | ||
void test() | ||
{ | ||
assertThat(new QualifiesAs<>(new EqualTo<>("123")), | ||
allOf( | ||
matches("123"), | ||
mismatches("12", "\"12\""), | ||
describesAs("\"123\"") | ||
)); | ||
} | ||
|
||
} |
42 changes: 42 additions & 0 deletions
42
...dence-hamcrest/src/test/java/org/saynotobugs/confidence/hamcrest/quality/MatchesTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* | ||
* Copyright 2024 dmfs GmbH | ||
* | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
|
||
package org.saynotobugs.confidence.hamcrest.quality; | ||
|
||
import org.hamcrest.Matchers; | ||
import org.junit.jupiter.api.Test; | ||
import org.saynotobugs.confidence.quality.composite.AllOf; | ||
import org.saynotobugs.confidence.test.quality.Fails; | ||
import org.saynotobugs.confidence.test.quality.HasDescription; | ||
import org.saynotobugs.confidence.test.quality.Passes; | ||
|
||
import static org.saynotobugs.confidence.Assertion.assertThat; | ||
|
||
class MatchesTest | ||
{ | ||
@Test | ||
void test() | ||
{ | ||
assertThat(new Matches<>(Matchers.equalTo(123)), | ||
new AllOf<>( | ||
new Passes<>(123), | ||
new Fails<>(12, "was <12>"), | ||
new HasDescription("<123>") | ||
)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters