Skip to content

Commit

Permalink
more cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
dmfs committed Dec 8, 2024
1 parent e615ae0 commit a5efc48
Show file tree
Hide file tree
Showing 22 changed files with 86 additions and 89 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@

/**
* A {@link Assessment} that prepends any mismatch {@link Description}.
*
* @deprecated use {@link DescriptionUpdated} to update pass and fail description.
*/
@Deprecated
public final class FailPrepended extends AssessmentComposition
{
public FailPrepended(Description prefix, Assessment delegate)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import org.saynotobugs.confidence.Description;
import org.saynotobugs.confidence.Quality;
import org.saynotobugs.confidence.assessment.DescriptionUpdated;
import org.saynotobugs.confidence.assessment.FailUpdated;


@StaticFactories(
Expand All @@ -34,33 +33,19 @@
deprecates = @DeprecatedFactories(value = "Core", packageName = "org.saynotobugs.confidence.quality"))
public final class DescribedAs<T> extends QualityComposition<T>
{
/**
* A {@link Quality} like the delegate {@link Quality} but with updated descriptions.
* <p>
* This constructor applies the same function to all three descriptions.
*/
public DescribedAs(
Function<Description, ? extends Description> descriptionUpdate,
Quality<T> delegate)
{
this(descriptionUpdate, descriptionUpdate, delegate);
}


public DescribedAs(
Function<Description, ? extends Description> mismatchUpdate,
Function<Description, ? extends Description> expectationUpdate,
Quality<T> delegate)
{
super(
actual -> new FailUpdated(mismatchUpdate, delegate.assessmentOf(actual)),
expectationUpdate.value(delegate.description()));
}


public DescribedAs(
BiFunction<? super T, Description, ? extends Description> mismatchUpdate,
Function<Description, ? extends Description> expectationUpdate,
Quality<T> delegate)
{
super(
actual -> new FailUpdated(description -> mismatchUpdate.value(actual, description), delegate.assessmentOf(actual)),
expectationUpdate.value(delegate.description()));
this((value, description) -> descriptionUpdate.value(description),
(value, description) -> descriptionUpdate.value(description),
descriptionUpdate,
delegate);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,12 @@
import org.saynotobugs.confidence.Assessment;
import org.saynotobugs.confidence.Description;
import org.saynotobugs.confidence.Quality;
import org.saynotobugs.confidence.assessment.DescriptionUpdated;
import org.saynotobugs.confidence.assessment.Fail;
import org.saynotobugs.confidence.assessment.FailPrepended;
import org.saynotobugs.confidence.description.Spaced;
import org.saynotobugs.confidence.description.Text;
import org.saynotobugs.confidence.description.Value;
import org.saynotobugs.confidence.description.bifunction.TextAndOriginal;
import org.saynotobugs.confidence.quality.object.EqualTo;

import java.io.DataInputStream;
Expand Down Expand Up @@ -60,9 +61,9 @@ public Assessment assessmentOf(File candidate)
{
byte[] buffer = new byte[(int) candidate.length()];
reader.readFully(buffer);
return new FailPrepended(
new Spaced(
new Text("contained data")), mDelegate.assessmentOf(buffer));
return new DescriptionUpdated(
new TextAndOriginal<>(new Text("contained data")),
mDelegate.assessmentOf(buffer));
}
catch (IOException exception)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,10 @@
import org.saynotobugs.confidence.Assessment;
import org.saynotobugs.confidence.Quality;
import org.saynotobugs.confidence.assessment.AllPassed;
import org.saynotobugs.confidence.assessment.FailPrepended;
import org.saynotobugs.confidence.assessment.DescriptionUpdated;
import org.saynotobugs.confidence.description.Spaced;
import org.saynotobugs.confidence.description.Text;
import org.saynotobugs.confidence.description.bifunction.TextAndOriginal;
import org.saynotobugs.confidence.quality.composite.AllOf;
import org.saynotobugs.confidence.quality.composite.QualityComposition;

Expand Down Expand Up @@ -64,7 +65,9 @@ public Each(Quality<? super T> delegate)
super(actual -> new AllPassed(new Text("elements ["), EMPTY, new Text("]"),
new Collected<>(ArrayList::new,
new Mapped<Pair<Integer, Assessment>, Assessment>(
numberedVerdict -> new FailPrepended(new Text(numberedVerdict.left() + ":"), numberedVerdict.right()),
numberedVerdict -> new DescriptionUpdated(
new TextAndOriginal<>(numberedVerdict.left() + ":"),
numberedVerdict.right()),
new Numbered<>(new Mapped<>(delegate::assessmentOf, actual)))).value()),
new Spaced(new Text("each element"), delegate.description()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,12 @@
import org.dmfs.srcless.annotations.staticfactory.DeprecatedFactories;
import org.dmfs.srcless.annotations.staticfactory.StaticFactories;
import org.saynotobugs.confidence.Quality;
import org.saynotobugs.confidence.assessment.FailPrepended;
import org.saynotobugs.confidence.assessment.DescriptionUpdated;
import org.saynotobugs.confidence.assessment.PassIf;
import org.saynotobugs.confidence.description.Block;
import org.saynotobugs.confidence.description.Text;
import org.saynotobugs.confidence.description.Value;
import org.saynotobugs.confidence.description.bifunction.TextAndOriginal;
import org.saynotobugs.confidence.quality.composite.QualityComposition;
import org.saynotobugs.confidence.quality.iterable.Iterates;
import org.saynotobugs.confidence.utils.ArrayIterable;
Expand All @@ -46,8 +47,8 @@ public final class EqualTo<T> extends QualityComposition<T>
public EqualTo(T expected)
{
super(actual -> expected.getClass().isArray() && actual.getClass().isArray()
? new FailPrepended(
new Text("array that"),
? new DescriptionUpdated(
new TextAndOriginal<>("array that"),
new Iterates<>(new Mapped<>(EqualTo::new, new ArrayIterable(expected))).assessmentOf(new ArrayIterable(actual)))
: new PassIf(expected.equals(actual), new Value(actual), new Value(actual)),
expected.getClass().isArray()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.saynotobugs.confidence.description.Spaced;
import org.saynotobugs.confidence.description.Text;
import org.saynotobugs.confidence.description.Value;
import org.saynotobugs.confidence.description.bifunction.TextAndOriginal;
import org.saynotobugs.confidence.quality.composite.DescribedAs;
import org.saynotobugs.confidence.quality.composite.Has;
import org.saynotobugs.confidence.quality.composite.QualityComposition;
Expand Down Expand Up @@ -54,6 +55,8 @@ public ContainsText(Charset charset, Quality<? super CharSequence> delegate)
new Text("contains"),
new Text("contained"),
path -> new String(Files.readAllBytes(path), charset),
new DescribedAs<>(orig -> new Spaced(new Value(charset.name()), new Text("text"), orig), delegate)));
new DescribedAs<>(
new TextAndOriginal<>(new Spaced(new Value(charset.name()), new Text("text"))),
delegate)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ void testSuppliedValue()
@Test
void testSuppliedValueWithDescription()
{
assertThat(new ConsumerThatAccepts<>(new Text("Pass"), new Text("Fail"),new Text("Consumes"), () -> "a"),
assertThat(new ConsumerThatAccepts<>(new Text("Pass"), new Text("Fail"), new Text("Consumes"), () -> "a"),
new AllOf<>(
new Passes<>(x -> {}, "Pass"),
new Fails<>(x -> {throw new RuntimeException();}, "Fail <java.lang.RuntimeException>"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ void test() throws IOException

assertThat(new ContainsData(new byte[] { 1, 2, 3 }),
new AllOf<>(
new Passes<>(passingFile, "iterated [\n 0: 1\n 1: 2\n 2: 3\n]"),
new Passes<>(passingFile, "contained data array that iterated [\n 0: 1\n 1: 2\n 2: 3\n]"),
new Fails<>(emptyFile, "contained data array that iterated [\n 0: missing 1\n 1: missing 2\n 2: missing 3\n]"),
new Fails<>(new File(dir, "nonexistent"), new DescribesAs(new MatchesPattern("threw <java.io.FileNotFoundException: .*> while reading"))),
new HasDescription("contains data [\n 1\n 2\n 3\n]")));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ void testDirWithDelegate() throws IOException
dir.mkdir();
assertThat(new ContainsFile("someDir", new Is<>(new ADirectory())),
new AllOf<>(
new Passes<>(tempDir, new DescribesAs(new MatchesPattern("contained \"someDir\" was directory </.*>"))),
new Passes<>(tempDir, new DescribesAs(new MatchesPattern("contained \"someDir\" was directory </.*>"))),
new Fails<>(file, "contained \"someDir\" was not a directory"),
new Fails<>(dir, "contained \"someDir\" was not a directory"),
new Fails<>(new File(tempDir, "nonExistentFile"), "contained \"someDir\" was not a directory"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,15 @@ void testLiteral()
new AllOf<>(
new Passes<>(new Seq<String>(), "elements []"),
new Passes<>(new Seq<>("1", "2", "3"), "elements [\n" +
" was\n" +
" 0: was\n" +
" \"1\"\n" +
" \"1\"\n" +
" \"1\"\n" +
" was\n" +
" 1: was\n" +
" \"2\"\n" +
" \"2\"\n" +
" \"2\"\n" +
" was\n" +
" 2: was\n" +
" \"3\"\n" +
" \"3\"\n" +
" \"3\"\n" +
Expand All @@ -72,13 +72,13 @@ void testQuality()
new AllOf<>(
new Passes<Iterable<String>>(new Seq<>(), "elements []"),
new Passes<>(new Seq<>("1", "2", "3"), "elements [\n" +
" was\n" +
" 0: was\n" +
" \"1\" mismatched pattern /[abc]/\n" +
" \"1\"\n" +
" was\n" +
" 1: was\n" +
" \"2\" mismatched pattern /[abc]/\n" +
" \"2\"\n" +
" was\n" +
" 2: was\n" +
" \"3\" mismatched pattern /[abc]/\n" +
" \"3\"\n" +
"]"),
Expand All @@ -103,13 +103,13 @@ void testQualityIterable()
new AllOf<>(
new Passes<Iterable<String>>(new Seq<>(), "elements []"),
new Passes<>(new Seq<>("1", "2", "3"), "elements [\n" +
" was\n" +
" 0: was\n" +
" \"1\" mismatched pattern /[abc]/\n" +
" \"1\"\n" +
" was\n" +
" 1: was\n" +
" \"2\" mismatched pattern /[abc]/\n" +
" \"2\"\n" +
" was\n" +
" 2: was\n" +
" \"3\" mismatched pattern /[abc]/\n" +
" \"3\"\n" +
"]"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ void testSimpleCtor()
assertThat(new Each<>(new LessThan<>(3)),
new AllOf<>(
new Passes<>(asList(0, 1, 2), "elements [\n" +
" 0\n" +
" 1\n" +
" 2\n" +
" 0: 0\n" +
" 1: 1\n" +
" 2: 2\n" +
"]"),
new Passes<>(asList(0, 0, 0), "elements [\n" +
" 0\n" +
" 0\n" +
" 0\n" +
" 0: 0\n" +
" 1: 0\n" +
" 2: 0\n" +
"]"),
new Passes<List<Integer>>(emptyList(), "elements []"),
new Fails<>(asList(1, 4, 2), "elements [\n ...\n 1: 4\n ...\n]"),
Expand All @@ -50,24 +50,24 @@ void testMultipleCtor()
assertThat(new Each<>(new LessThan<>(3), new GreaterThan<>(0)),
new AllOf<>(
new Passes<Iterable<Integer>>(asList(1, 1, 2), "elements [\n" +
" all of\n" +
" 0: all of\n" +
" 0: 1\n" +
" 1: 1\n" +
" all of\n" +
" 1: all of\n" +
" 0: 1\n" +
" 1: 1\n" +
" all of\n" +
" 2: all of\n" +
" 0: 2\n" +
" 1: 2\n" +
"]"),
new Passes<>(asList(2, 2, 2), "elements [\n" +
" all of\n" +
" 0: all of\n" +
" 0: 2\n" +
" 1: 2\n" +
" all of\n" +
" 1: all of\n" +
" 0: 2\n" +
" 1: 2\n" +
" all of\n" +
" 2: all of\n" +
" 0: 2\n" +
" 1: 2\n" +
"]"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ void testKeyOnly()
assertThat(new ContainsEntry<>("k1"),
new AllOf<>(
new Passes<>(map1, "contained Entry (\"k1\": <anything>)"),
new Fails<>(new HashMap<String, String>(), "{} did not contain Entry (\"k1\": <anything>)"),
new Fails<>(map2, "{ \"k2\": \"v2\" } did not contain Entry (\"k1\": <anything>)"),
new Fails<>(new HashMap<String, String>(), "{} did not contain Entry (\"k1\": <anything>)"),
new Fails<>(map2, "{ \"k2\": \"v2\" } did not contain Entry (\"k1\": <anything>)"),
// new Fails<>(Map.of("k2", "v2"), new DescribesAs("")),
new HasDescription( "contains Entry (\"k1\": <anything>)")
new HasDescription("contains Entry (\"k1\": <anything>)")
));
}

Expand All @@ -45,10 +45,10 @@ void testKeyQualityOnly()
assertThat(new ContainsEntry<>(new EqualTo<>("k1")),
new AllOf<>(
new Passes<>(map1, "contained Entry (\"k1\": <anything>)"),
new Fails<>(new HashMap<String, String>(), "{} did not contain Entry (\"k1\": <anything>)"),
new Fails<>(map2, "{ \"k2\": \"v2\" } did not contain Entry (\"k1\": <anything>)"),
new Fails<>(new HashMap<String, String>(), "{} did not contain Entry (\"k1\": <anything>)"),
new Fails<>(map2, "{ \"k2\": \"v2\" } did not contain Entry (\"k1\": <anything>)"),
// new Fails<>(Map.of("k2", "v2"), new DescribesAs("")),
new HasDescription( "contains Entry (\"k1\": <anything>)")
new HasDescription("contains Entry (\"k1\": <anything>)")
));
}

Expand All @@ -64,10 +64,10 @@ void testKeyAndValue()
assertThat(new ContainsEntry<>("k1", "v1"),
new AllOf<>(
new Passes<>(map1, "contained Entry (\"k1\": \"v1\")"),
new Fails<>(new HashMap<String, String>(), "{} did not contain Entry (\"k1\": \"v1\")"),
new Fails<>(map2, "{ \"k2\": \"v2\" } did not contain Entry (\"k1\": \"v1\")"),
new Fails<>(new HashMap<String, String>(), "{} did not contain Entry (\"k1\": \"v1\")"),
new Fails<>(map2, "{ \"k2\": \"v2\" } did not contain Entry (\"k1\": \"v1\")"),
// new Fails<>(Map.of("k2", "v2"), new DescribesAs("")),
new HasDescription( "contains Entry (\"k1\": \"v1\")")
new HasDescription("contains Entry (\"k1\": \"v1\")")
));
}

Expand Down Expand Up @@ -102,10 +102,10 @@ void testKeyQuality()
assertThat(new ContainsEntry<>(new EqualTo<>("k1")),
new AllOf<>(
new Passes<>(map1, "contained Entry (\"k1\": <anything>)"),
new Fails<>(new HashMap<String, String>(), "{} did not contain Entry (\"k1\": <anything>)"),
new Fails<>(map2, "{ \"k2\": \"v2\" } did not contain Entry (\"k1\": <anything>)"),
new Fails<>(new HashMap<String, String>(), "{} did not contain Entry (\"k1\": <anything>)"),
new Fails<>(map2, "{ \"k2\": \"v2\" } did not contain Entry (\"k1\": <anything>)"),
// new Fails<>(Map.of("k2", "v2"), new DescribesAs("")),
new HasDescription( "contains Entry (\"k1\": <anything>)")
new HasDescription("contains Entry (\"k1\": <anything>)")
));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ void testDouble1ulp()
new AllOf<>(
new Passes<>(1.0000000000000001, "1.0d differed from 1.0 by 0.0, which was less than 2.220446049250313E-16"),
new Passes<>(0.9999999999999999, "0.9999999999999999d differed from 1.0 by 1E-16, which was less than 2.220446049250313E-16"),
new Fails<>(1.01001d, "1.01001d differed from 1.0 by 0.01001, which exceeded the ε of 2.220446049250313E-16"),
new Fails<>(1.01001d, "1.01001d differed from 1.0 by 0.01001, which exceeded the ε of 2.220446049250313E-16"),
new HasDescription("differs from 1.0 by less than 2.220446049250313E-16")
));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ void testArray()
{
assertThat(new EqualTo<>(new String[] { "1", "2", "3" }),
new AllOf<>(
new Passes<Object>(new String[] { "1", "2", "3" }, "iterated [\n 0: \"1\"\n 1: \"2\"\n 2: \"3\"\n]"),
new Passes<Object>(new String[] { "1", "2", "3" }, "array that iterated [\n 0: \"1\"\n 1: \"2\"\n 2: \"3\"\n]"),
new Fails<Object>("123", "\"123\""),
new Fails<>(new String[] { "1", "2" }, "array that iterated [\n ...\n 2: missing \"3\"\n]"),
new HasDescription("[\n \"1\"\n \"2\"\n \"3\"\n]")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class SuccessfullyTest
@Test
void test()
{
assertThat(new Successfully<Fragile<Object, Exception>>(new Text("passed"), new Text("failed with"),new Text("passes"), Fragile::value),
assertThat(new Successfully<Fragile<Object, Exception>>(new Text("passed"), new Text("failed with"), new Text("passes"), Fragile::value),
new AllOf<>(
new Passes<Fragile<Object, Exception>>(() -> "", "passed"),
new Fails<>(() -> {throw new RuntimeException();}, "failed with <java.lang.RuntimeException>"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ void testWithQuality() throws IOException
}
assertThat(new ContainsText(UTF_8, new EqualTo<>("0123456789")),
new AllOf<>(
new Passes<>(file10Bytes, "contained \"0123456789\""),
new Passes<>(file10Bytes, "contained \"UTF-8\" text \"0123456789\""),
new Fails<>(tempDir, new DescribesAs(new MatchesPattern("threw <java.io.IOException: [^>]+>"))),
new Fails<>(emptyFile, "contained \"UTF-8\" text \"\""),
new HasDescription("contains \"UTF-8\" text \"0123456789\"")
Expand All @@ -75,7 +75,7 @@ void testWithPrimitive() throws IOException
}
assertThat(new ContainsText("0123456789"),
new AllOf<>(
new Passes<>(file10Bytes, "contained \"0123456789\""),
new Passes<>(file10Bytes, "contained \"UTF-8\" text \"0123456789\""),
new Fails<>(tempDir, new DescribesAs(new MatchesPattern("threw <java.io.IOException: [^>]+>"))),
new Fails<>(emptyFile, "contained \"UTF-8\" text \"\""),
new HasDescription("contains \"UTF-8\" text \"0123456789\"")
Expand Down
Loading

0 comments on commit a5efc48

Please sign in to comment.