Repository navigation
v2.0.1 #16
bitspittle
announced in
Announcements
v2.0.1
#16
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Note
2.0.1 is 2.0.0 with a bit of API cleanup.
A handful of minor modifier extension functions moved from their old 1.0.4 location to a new package for a clearer
import experience. The old imports still work but are deprecated.
We also made a few properties on the
Reportableclassinternal. They could have cluttered autocomplete while you werebuilding an assertion chain, and there should never be a reason to use them directly. We meant to clean up all cases
like this in 2.0.0 but missed a spot. Technically this is a backwards incompatible change, so if it causes you any
trouble, please file an issue!
The rest of these release notes are copied from 2.0.0 as is.
Using Truthish 2.0 in your tests should look exactly the same as any 1.x release, but under the hood, we completely refactored the APIs from an inheritance class system to an extension function system.
If you update your project's truthish dependency, hopefully nothing needs to change except the addition of:
at the top of your tests.
In addition to this change, this new version lands with a ton of new assertions, and support for features called Correspondences (for looser collection matching) and Mismatches (for clearer reports when related items differ). These (and more) are discussed below.
Kotlin versions
Requirements are now:
Before, Truthish aimed to support as wide a userbase as possible, but this required staying on very old versions of Kotlin.
We have since realized this was costing us useful API updates and language features, and when we looked at state-of-the-art libraries in the ecosystem, we saw that no one else was doing this (including official Kotlin libraries). So, we started following a new policy -- use the latest compiler available to us and support back to as many versions as that allows.
Breaking changes
Assertions are now extension functions, in the
com.varabyte.truthish.assertionspackage.assertThat<Animal>(dog).isEqualTo(animal)or else reverse the assertion order:assertThat(animal).isEqualTo(dog)APIs around Sequences, Arrays and Maps have been made stricter:
assertThat(sequence).isEqualTo(...)only takes another sequence.containsExactly(list).inOrder().IntArraywithArray<Int>).hasNoDuplicates().Some assertions are stricter:
containsAnyIn()with nothing to look for now fails instead of passing. Passing used to hide tests whose expected list was accidentally empty. (This was a mistake before. Now, this behavior matches Kotlin'sany {}.)containsAllIn()now counts duplicates, soassertThat(listOf(1)).containsAllIn(1, 1)fails, reporting themissing
1.The
macosX64target is gone. Kotlin is deprecating it, since Apple has moved off Intel Macs.Deprecations
isGreaterThanEqual()andisLessThanEqual()are renamed toisAtLeast()andisAtMost().New features
IDE diffs
If you are running tests inside IntelliJ IDEA and using JUnit 4+ as your test runner, you should start seeing a "Click to see difference" link, which opens up a very helpful view in the IDE's diff viewer.
Much better failure reports
inOrder()now shows where the order broke.hasSize(),hasLength()andisNotBlank()show the value that failed, not just its size or length.class java.lang.Integer (Kotlin reflection is not available){ "a" to 1 }instead of{ a=1 }[]for empty collections,<anonymous>for anonymous classes"a\tb").New assertions
hasMessageThat()andhasCauseThat()for throwablesisInOrder(), to check that a collection is sortedisIn()andisNotIn(), for rangesisAnyOf()andisNoneOf(), on every subjectisNaN(),isFinite(),isZero()and friends for floats and doublescontainsEntry()anddoesNotContainEntry()for mapsignoreCaseforisEqualTo(),contains(),startsWith(),endsWith()and friends on stringsinOrder(consecutive = true)aftercontainsAllIn(), to check for an unbroken run of elementsCorrespondences
Sometimes you want to compare two collections more loosely than exact equality.
For example, say you have a list of error Strings which you generate that are very long. Their text may change over time, but the strings will always contain a general category of the error (e.g. the
"Unresolved reference"part of"/very/long/path/to/file.kt[123]: Unresolved reference -- The value xyz could not be resolved; please check your imports.")Instead of writing
assertThat(errors) .containsExactly( "/very/long/path/to/File.kt[123]: Unresolved reference -- The value xyz could not be resolved; please check your imports.", "/very/long/path/to/another/DifferentFile.kt[92]: Type mismatch -- Variable is type Cat but expected type Dog" )you can use a correspondence:
assertThat(errors) .comparingElementsUsing(Correspondence.contains) .containsExactly("Unresolved reference", "Type mismatch")comparingValuesUsing.Mismatches
When a collection check fails, and if there's some clear way to identify related elements based on some common value, then you can tell Truthish how expected and actual elements can pair up. Paired elements move out of the Missing and Extraneous rows into a Mismatch row, which can make the change much easier to see at a glance, especially in a test failure resulting in multiple differences.
Above, we can tell at a glance that both
actualandexpectedcollections had a "Bob" and a "Chuck", while onlyexpectedhad a "Diana". Without mismatches, you would have roughly gotten "Missing: Bob, Chuck, Diana; Extraneous: Bob, Chuck".If you are comparing two maps to each other, elements will automatically be paired by key for you.
If you need to add additional information to the mismatch rows, you can
explainthem (where returningnullmeans you have nothing to say):.pairingMismatchesBy { it.id } .explainingMismatchesWith { actual, expected -> if (actual.age != expected.age) "age is off by ${abs(actual.age - expected.age)}" else null } .containsExactly(expectedUsers)pairingMismatchesWhen { actual, expected -> ... }if you need more flexibility thanpairingMismatchesBy.displayingDiffsPairedByandformattingDiffsUsing.Unused return value warnings
Truthish supports Kotlin's unused return value checker.
It can be very easy to write an unfinished assertion chain unintentionally! For example:
Before, that line silently did nothing, potentially allowing a test to pass when it should have failed. Now, the compiler and IDE will highlight a warning:
Unused return value of 'assertThat'The checker is on by default starting in Kotlin 2.5. In Kotlin 2.3 and 2.4, opt in with
-Xreturn-value-checker=check(see the README).Custom subjects
In 1.x, custom subjects couldn't do everything the built-in ones could. 2.0 fixes that.
This will be much clearer with a concrete example. Let's imagine your popular library has a
Userclass, and you want to support Truthish users by letting them test against it with its own subject type.To do this:
Subject<T>, whereTis your class.thatextension function (receivingAssertionEntryPoint) which just callsprepareon your subject.assertThatfunction which delegates tothat.Bringing it all together:
At this point, you can pass a
userinstance intoassertThat(user),assertWithMessage("...").that(user), andassertAll { that(user) }.Subjects for collection-like types (e.g. a multimap) can extend
ElementsSubjectorKeyedSubjectinstead, and get the built-in collection checks for free, including mismatches and correspondences.Documentation
Every public API is now documented, so your IDE's quick documentation works everywhere.
Fixes
isWithinreported expected and actual values swapped, and didn't catchNaN.assertThrowsdidn't properly handle the case if a different exception than what it was expecting was thrown.assertAllreports, the "At" entry now points at the right line, even for custom subjects (before, this only worked for assertions defined inside Truthish itself).Full Changelog: v1.0.4...v2.0.1
This discussion was created from the release v2.0.1.
All reactions