Skip to content

Commit

Permalink
Improve NumberDescription to include the number type, closes #94
Browse files Browse the repository at this point in the history
  • Loading branch information
dmfs committed Jul 27, 2024
1 parent 6501189 commit dcd67ca
Show file tree
Hide file tree
Showing 10 changed files with 79 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,36 @@ public final class NumberDescription extends DescriptionComposition
{
public NumberDescription(Number value)
{
super(new ToStringDescription(value));
super(new ToStringDescription(new NumberToString(value)));
}

private final static class NumberToString
{

private final Number mNumber;

private NumberToString(Number number) {mNumber = number;}

@Override
public String toString()
{
if (mNumber instanceof Integer)
{
return mNumber.toString();
}
if (mNumber instanceof Long)
{
return mNumber.toString() + "l";
}
if (mNumber instanceof Float)
{
return mNumber.toString() + "f";
}
if (mNumber instanceof Double)
{
return mNumber.toString() + "d";
}
return mNumber.toString();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,23 @@ void testInt()
}


@Test
void testLong()
{
assertThat(new NumberDescription(123l), new DescribesAs("<123l>"));
}


@Test
void testFloat()
{
assertThat(new NumberDescription(123.23f), new DescribesAs("<123.23>"));
assertThat(new NumberDescription(123.23f), new DescribesAs("<123.23f>"));
}


@Test
void testDouble()
{
assertThat(new NumberDescription(123.23d), new DescribesAs("<123.23>"));
assertThat(new NumberDescription(123.23d), new DescribesAs("<123.23d>"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ void testWithQuality() throws IOException
assertThat(new HasLength(new EqualTo<>(10L)),
new AllOf<>(
new Passes<>(file10Bytes),
new Fails<>(tempDir, new DescribesAs(new MatchesPattern("had length <\\d+>"))),
new Fails<>(emptyFile, "had length <0>"),
new HasDescription("has length <10>")
new Fails<>(tempDir, new DescribesAs(new MatchesPattern("had length <\\d+l>"))),
new Fails<>(emptyFile, "had length <0l>"),
new HasDescription("has length <10l>")
));
}

Expand All @@ -74,9 +74,9 @@ void testWithPrimitive() throws IOException
assertThat(new HasLength(10),
new AllOf<>(
new Passes<>(file10Bytes),
new Fails<>(tempDir, new DescribesAs(new MatchesPattern("had length <\\d+>"))),
new Fails<>(emptyFile, "had length <0>"),
new HasDescription("has length <10>")
new Fails<>(tempDir, new DescribesAs(new MatchesPattern("had length <\\d+l>"))),
new Fails<>(emptyFile, "had length <0l>"),
new HasDescription("has length <10l>")
));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ void testNumber()
assertThat(new CloseTo(1.0, 0.01),
new AllOf<>(
new Passes<>(1.0, 1f, 1, 1.00999, 0.99000001),
new Fails<>(1.01001d, "<1.01001> differs from <1.0> by <0.01001>, which exceeds the ε of <0.01>"),
new Fails<>(1.01001d, "<1.01001d> differs from <1.0> by <0.01001>, which exceeds the ε of <0.01>"),
new HasDescription("differs from <1.0> by less than <0.01>")
));
}
Expand All @@ -30,7 +30,7 @@ void testDouble1ulp()
assertThat(new CloseTo(1.0),
new AllOf<>(
new Passes<>(1.0000000000000001, 0.9999999999999999),
new Fails<>(1.01001d, "<1.01001> differs from <1.0> by <0.01001>, which exceeds the ε of <2.220446049250313E-16>"),
new Fails<>(1.01001d, "<1.01001d> differs from <1.0> by <0.01001>, which exceeds the ε of <2.220446049250313E-16>"),
new HasDescription("differs from <1.0> by less than <2.220446049250313E-16>")
));
}
Expand All @@ -42,7 +42,7 @@ void testDouble10ulp()
assertThat(new CloseTo(1.0, 10),
new AllOf<>(
new Passes<>(1.000000000000001, 0.999999999999999),
new Fails<>(1.01001d, "<1.01001> differs from <1.0> by <0.01001>, which exceeds the ε of <2.220446049250313E-15>"),
new Fails<>(1.01001d, "<1.01001d> differs from <1.0> by <0.01001>, which exceeds the ε of <2.220446049250313E-15>"),
new HasDescription("differs from <1.0> by less than <2.220446049250313E-15>")
));
}
Expand All @@ -53,7 +53,7 @@ void testFloat1ulp()
assertThat(new CloseTo(1.0f),
new AllOf<>(
new Passes<>(1.0000001, 0.9999999),
new Fails<>(1.01001d, "<1.01001> differs from <1.0> by <0.01001>, which exceeds the ε of <1.1920929E-7>"),
new Fails<>(1.01001d, "<1.01001d> differs from <1.0> by <0.01001>, which exceeds the ε of <1.1920929E-7>"),
new HasDescription("differs from <1.0> by less than <1.1920929E-7>")
));
}
Expand All @@ -65,7 +65,7 @@ void testFloat10ulp()
assertThat(new CloseTo(1.0f, 10),
new AllOf<>(
new Passes<>(1.000001, 0.999999),
new Fails<>(1.01001d, "<1.01001> differs from <1.0> by <0.01001>, which exceeds the ε of <0.0000011920929>"),
new Fails<>(1.01001d, "<1.01001d> differs from <1.0> by <0.01001>, which exceeds the ε of <0.0000011920929>"),
new HasDescription("differs from <1.0> by less than <0.0000011920929>")
));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ void testWithPrimitive()
assertThat(new HasLongValue(10L),
new AllOf<>(
new Passes<>(10, 10L, 10f, 10d),
new Fails<>(11, "had long value <11>"),
new HasDescription("has long value <10>")
new Fails<>(11, "had long value <11l>"),
new HasDescription("has long value <10l>")
));
}

Expand All @@ -46,8 +46,8 @@ void testWithQuality()
assertThat(new HasLongValue(new LessThan<>(11L)),
new AllOf<>(
new Passes<>(10, 10L, 10f, 10d),
new Fails<>(11, "had long value <11>"),
new HasDescription("has long value less than <11>")
new Fails<>(11, "had long value <11l>"),
new HasDescription("has long value less than <11l>")
));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ void testSubClassDelegate()
assertThat(new UnsafeInstanceOf<>(Iterable.class, new That<>(new Iterates<>(1, "abc", true))),
new AllOf<>(
new Passes<>(new Seq<Object>(1, "abc", true), new Seq(1, "abc", true)),
new Fails<>(new Seq<Object>(1.1, "abc", true), "(1) that iterated [ 0: <1.1>\n ... ]"),
new Fails<>(new Seq<Object>(1.1, "abc", true), "(1) that iterated [ 0: <1.1d>\n ... ]"),
new Fails<>(2, "(0) instance of <class java.lang.Integer>"),
new Fails<>("string", "(0) instance of <class java.lang.String>"),
new Fails<>(new Object(), "(0) instance of <class java.lang.Object>"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ void testNumberArray()
new Fails<>(mock("not an array", JsonElementAdapter.class,
with(JsonElementAdapter::asArray, returning(new Absent<>()))),
"array not an array"),
new HasDescription("array { has length <2>\n and\n { <0>: <1.2>\n and\n <1>: <5> } }")
new HasDescription("array { has length <2>\n and\n { <0>: <1.2d>\n and\n <1>: <5> } }")
));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,11 @@ void testWithLong()
with(JsonElementAdapter::asNumber, returning(new Present<>(3L))))),
new Fails<>(mock("mismatching number", JsonElementAdapter.class,
with(JsonElementAdapter::asNumber, returning(new Present<>(4L)))),
"<4>"),
"<4l>"),
new Fails<>(mock("no number", JsonElementAdapter.class,
with(JsonElementAdapter::asNumber, returning(new Absent<>()))),
"not a number"),
new HasDescription("<3>")
new HasDescription("<3l>")
));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@

package org.saynotobugs.confidence.rxjava3.rxexpectation.internal;

import org.saynotobugs.confidence.description.NumberDescription;
import org.saynotobugs.confidence.description.Spaced;
import org.saynotobugs.confidence.description.Text;
import org.saynotobugs.confidence.description.ToStringDescription;
import org.saynotobugs.confidence.quality.composite.QualityComposition;
import org.saynotobugs.confidence.quality.object.Satisfies;
import org.saynotobugs.confidence.rxjava3.RxTestAdapter;
Expand All @@ -31,7 +31,7 @@ public final class IsComplete extends QualityComposition<RxTestAdapter<?>>
public IsComplete()
{
super(new Satisfies<>(actual -> actual.completions() == 1,
ackSubscriber -> new Spaced(new Text("completed"), new NumberDescription(ackSubscriber.completions()), new Text("times")),
ackSubscriber -> new Spaced(new Text("completed"), new ToStringDescription(ackSubscriber.completions()), new Text("times")),
new Text("completes exactly once")));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ void testInstantAndValueQuality()
new AllOf<>(
new Passes<>(new io.reactivex.rxjava3.schedulers.Timed<>(456, 123, TimeUnit.MILLISECONDS)),
new Fails<>(new io.reactivex.rxjava3.schedulers.Timed<>(789, 123, TimeUnit.MILLISECONDS), "{ ...\n had value <789> }"),
new Fails<>(new io.reactivex.rxjava3.schedulers.Timed<>(456, 789, TimeUnit.MILLISECONDS), "{ had time of <789> millis\n ... }"),
new Fails<>(new io.reactivex.rxjava3.schedulers.Timed<>(987, 789, TimeUnit.MILLISECONDS), "{ had time of <789> millis\n and\n had value <987> }"),
new HasDescription("{ has time of <123> millis\n and\n has value <456> }"))
new Fails<>(new io.reactivex.rxjava3.schedulers.Timed<>(456, 789, TimeUnit.MILLISECONDS), "{ had time of <789l> millis\n ... }"),
new Fails<>(new io.reactivex.rxjava3.schedulers.Timed<>(987, 789, TimeUnit.MILLISECONDS), "{ had time of <789l> millis\n and\n had value <987> }"),
new HasDescription("{ has time of <123l> millis\n and\n has value <456> }"))
);
}

Expand All @@ -55,9 +55,9 @@ void testInstantAndValue()
new AllOf<>(
new Passes<>(new io.reactivex.rxjava3.schedulers.Timed<>(456, 123, TimeUnit.MILLISECONDS)),
new Fails<>(new io.reactivex.rxjava3.schedulers.Timed<>(789, 123, TimeUnit.MILLISECONDS), "{ ...\n had value <789> }"),
new Fails<>(new io.reactivex.rxjava3.schedulers.Timed<>(456, 789, TimeUnit.MILLISECONDS), "{ had time of <789> millis\n ... }"),
new Fails<>(new io.reactivex.rxjava3.schedulers.Timed<>(987, 789, TimeUnit.MILLISECONDS), "{ had time of <789> millis\n and\n had value <987> }"),
new HasDescription("{ has time of <123> millis\n and\n has value <456> }"))
new Fails<>(new io.reactivex.rxjava3.schedulers.Timed<>(456, 789, TimeUnit.MILLISECONDS), "{ had time of <789l> millis\n ... }"),
new Fails<>(new io.reactivex.rxjava3.schedulers.Timed<>(987, 789, TimeUnit.MILLISECONDS), "{ had time of <789l> millis\n and\n had value <987> }"),
new HasDescription("{ has time of <123l> millis\n and\n has value <456> }"))
);
}

Expand All @@ -69,9 +69,9 @@ void testDurationAndValueQuality()
new AllOf<>(
new Passes<>(new io.reactivex.rxjava3.schedulers.Timed<>(456, 123, TimeUnit.MILLISECONDS)),
new Fails<>(new io.reactivex.rxjava3.schedulers.Timed<>(789, 123, TimeUnit.MILLISECONDS), "{ ...\n had value <789> }"),
new Fails<>(new io.reactivex.rxjava3.schedulers.Timed<>(456, 789, TimeUnit.MILLISECONDS), "{ had time of <789> millis\n ... }"),
new Fails<>(new io.reactivex.rxjava3.schedulers.Timed<>(987, 789, TimeUnit.MILLISECONDS), "{ had time of <789> millis\n and\n had value <987> }"),
new HasDescription("{ has time of <123> millis\n and\n has value <456> }"))
new Fails<>(new io.reactivex.rxjava3.schedulers.Timed<>(456, 789, TimeUnit.MILLISECONDS), "{ had time of <789l> millis\n ... }"),
new Fails<>(new io.reactivex.rxjava3.schedulers.Timed<>(987, 789, TimeUnit.MILLISECONDS), "{ had time of <789l> millis\n and\n had value <987> }"),
new HasDescription("{ has time of <123l> millis\n and\n has value <456> }"))
);
}

Expand All @@ -83,9 +83,9 @@ void testDurationAndValue()
new AllOf<>(
new Passes<>(new io.reactivex.rxjava3.schedulers.Timed<>(456, 123, TimeUnit.MILLISECONDS)),
new Fails<>(new io.reactivex.rxjava3.schedulers.Timed<>(789, 123, TimeUnit.MILLISECONDS), "{ ...\n had value <789> }"),
new Fails<>(new io.reactivex.rxjava3.schedulers.Timed<>(456, 789, TimeUnit.MILLISECONDS), "{ had time of <789> millis\n ... }"),
new Fails<>(new io.reactivex.rxjava3.schedulers.Timed<>(987, 789, TimeUnit.MILLISECONDS), "{ had time of <789> millis\n and\n had value <987> }"),
new HasDescription("{ has time of <123> millis\n and\n has value <456> }"))
new Fails<>(new io.reactivex.rxjava3.schedulers.Timed<>(456, 789, TimeUnit.MILLISECONDS), "{ had time of <789l> millis\n ... }"),
new Fails<>(new io.reactivex.rxjava3.schedulers.Timed<>(987, 789, TimeUnit.MILLISECONDS), "{ had time of <789l> millis\n and\n had value <987> }"),
new HasDescription("{ has time of <123l> millis\n and\n has value <456> }"))
);
}

Expand All @@ -97,9 +97,9 @@ void testLongAndValueQuality()
new AllOf<>(
new Passes<>(new io.reactivex.rxjava3.schedulers.Timed<>(456, 123, TimeUnit.MILLISECONDS)),
new Fails<>(new io.reactivex.rxjava3.schedulers.Timed<>(789, 123, TimeUnit.MILLISECONDS), "{ ...\n had value <789> }"),
new Fails<>(new io.reactivex.rxjava3.schedulers.Timed<>(456, 789, TimeUnit.MILLISECONDS), "{ had time of <789> millis\n ... }"),
new Fails<>(new io.reactivex.rxjava3.schedulers.Timed<>(987, 789, TimeUnit.MILLISECONDS), "{ had time of <789> millis\n and\n had value <987> }"),
new HasDescription("{ has time of <123> millis\n and\n has value <456> }"))
new Fails<>(new io.reactivex.rxjava3.schedulers.Timed<>(456, 789, TimeUnit.MILLISECONDS), "{ had time of <789l> millis\n ... }"),
new Fails<>(new io.reactivex.rxjava3.schedulers.Timed<>(987, 789, TimeUnit.MILLISECONDS), "{ had time of <789l> millis\n and\n had value <987> }"),
new HasDescription("{ has time of <123l> millis\n and\n has value <456> }"))
);
}

Expand All @@ -111,9 +111,9 @@ void testLongAndValue()
new AllOf<>(
new Passes<>(new io.reactivex.rxjava3.schedulers.Timed<>(456, 123, TimeUnit.MILLISECONDS)),
new Fails<>(new io.reactivex.rxjava3.schedulers.Timed<>(789, 123, TimeUnit.MILLISECONDS), "{ ...\n had value <789> }"),
new Fails<>(new io.reactivex.rxjava3.schedulers.Timed<>(456, 789, TimeUnit.MILLISECONDS), "{ had time of <789> millis\n ... }"),
new Fails<>(new io.reactivex.rxjava3.schedulers.Timed<>(987, 789, TimeUnit.MILLISECONDS), "{ had time of <789> millis\n and\n had value <987> }"),
new HasDescription("{ has time of <123> millis\n and\n has value <456> }"))
new Fails<>(new io.reactivex.rxjava3.schedulers.Timed<>(456, 789, TimeUnit.MILLISECONDS), "{ had time of <789l> millis\n ... }"),
new Fails<>(new io.reactivex.rxjava3.schedulers.Timed<>(987, 789, TimeUnit.MILLISECONDS), "{ had time of <789l> millis\n and\n had value <987> }"),
new HasDescription("{ has time of <123l> millis\n and\n has value <456> }"))
);
}
}

0 comments on commit dcd67ca

Please sign in to comment.