Comments (5)
My solution Inspired by ThingyThing
.
class ExpectExtension : BeforeEachCallback, AfterEachCallback, InvocationInterceptor,
ParameterResolver {
private var _expect: Expect? = null
val expect: Expect
get() = _expect!!
override fun beforeEach(context: ExtensionContext) {
_expect = Expect.create()
}
override fun interceptTestMethod(
invocation: InvocationInterceptor.Invocation<Void>,
invocationContext: ReflectiveInvocationContext<Method>,
extensionContext: ExtensionContext
) {
_expect?.apply(
object : Statement() {
override fun evaluate() {
invocation.proceed()
}
},
Description.EMPTY,
)?.evaluate()
}
override fun afterEach(context: ExtensionContext) {
_expect = null
}
override fun supportsParameter(
parameterContext: ParameterContext,
extensionContext: ExtensionContext
): Boolean {
val paramType = parameterContext.parameter.type
return paramType is Type && paramType == Expect::class.java
}
override fun resolveParameter(
parameterContext: ParameterContext,
extensionContext: ExtensionContext
): Any {
return expect
}
}
A.
@Test
@ExtendWith(ExpectExtension::class)
fun test(expect: Expect) {
expect.that<Int>(1).isEqualTo(2)
expect.that<Int>(1).isEqualTo(3)
expect.that<Int>(1).isEqualTo(4)
}
B.
@JvmField
@RegisterExtension
val extension = ExpectExtension()
@Test
fun test2() {
extension.expect.that<Int>(1).isEqualTo(2)
extension.expect.that<Int>(1).isEqualTo(3)
extension.expect.that<Int>(1).isEqualTo(4)
}
@Test
fun test3(expect: Expect) {
expect.that<Int>(1).isEqualTo(2)
expect.that<Int>(1).isEqualTo(3)
expect.that<Int>(1).isEqualTo(4)
}
I look forward to any form of Expect Extension for JUnit5 being added in the future!
from truth.
Thanks. As you've seen, JUnit 5 is a giant TODO for us, one that we're unlikely to carve off time for in the near future :(
I can say one slightly encouraging thing, which is that your proposal looks useful even to non-JUnit-5 users. (Maybe we've even heard requests for it from such users? I can't seem to find any offhand, though.) Then I can say one more discouraging thing, which is that we have had a proposal open inside Google for something like this for a couple years, and we haven't acted on it. Now, that proposal was more about adding context to a group of assertions:
try (TruthContext ctx = Truth.addFailureMessage("foo %s", bar)) {
...
}
If we take into account that such a feature could support all of...
- context to add to failure messages
Expect
-style failure collection for...- users of JUnit 5 (and other non-JUnit-4 frameworks)
- users of any framework who simply want to group assertions at a smaller scope than a whole test
...then that provides at least a little more motivation.
Still probably not a big priority for us, given that Truth itself continues not to be a big priority for us, sadly.
from truth.
I created PR #706 over a year ago which enables
@Test
public void test(StandardSubjectBuilder expect) {
expect.that(1).isEqualTo(2);
expect.that(-1).isGreaterThan(0);
}
in JUnit 5. I think that makes sense if you're actually using JUnit 5, but if you want to use it in any test framework, then your ThingyThing seems reasonable.
from truth.
@ephemient that seems great! Perhaps the two could be merged to enable both options...
from truth.
As belatedly noted on #894, anything that we label as P3 has no timeline for being reviewed :( We hope to eventually schedule some more time for Truth (especially for features that could work well in Kotlin, like this one), but there are no plans yet.
(Another thing we should do: Figure out if this feature request and #266 represent different requests or if they could be merged.)
from truth.
Related Issues (20)
- Consider reducing ImmutableList array copying HOT 1
- Wanted: Truth8Junit to support Java 8 types with assume() HOT 3
- Compatibility with gradle 7.6 HOT 2
- UsingCorrespondence should fail on use of equals(), like Subject does
- 1.1.4, Guava update leads to java.lang.UnsupportedOperationException at WindowsSecurityDescriptor.java:358 HOT 2
- Add StringSubject isNotBlank or hasText HOT 2
- Idk
- com.google.guava:guava library version update HOT 2
- Matching on exact class type, not a subtype HOT 2
- Can Truth assert a method throw exception? HOT 1
- unknown enum constant ElementType.MODULE with Truth 1.4.3 HOT 16
- Truth 1.1+ depends on Java 8 bytecode HOT 3
- FAQ entry: Why we don't support multiple chained calls to assertion methods on the same Subject HOT 3
- Port a core set of unit tests to kotlin HOT 2
- Document when arrays are compared by their contents vs. their object identity HOT 3
- New extension for Gradle's TestKit HOT 1
- Asymmetric matchers like Jest's expect.stringContaining("") ? HOT 3
- Add MapSubject#containsAnyOf HOT 2
- Allow logical operations on assertions HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from truth.