-
Notifications
You must be signed in to change notification settings - Fork 512
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Adds shared examples to Kiwi. #600
Adds shared examples to Kiwi. #600
Conversation
Shared examples allow developers to reuse a set of describe, context, and it nodes to test one or more classes. See the functional tests in `Tests/Shared Examples/KWSharedExampleFunctionalTest.m` for sample usage. Addresses issues kiwi-bdd#175, kiwi-bdd#138. - `KWSharedExampleRegistry` is a singleton that maintains a mapping of shared example names to `KWSharedExample` instances. - `KWSharedExample` is a data structure composed of a name and context block. - `sharedExamplesFor` creates a `KWSharedExample` and registers it. - `itBehavesLike` looks up the `KWSharedExample` with the given name and inserts its context block into the current example group. - `SHARED_EXAMPLES_BEGIN`, `SHARED_EXAMPLES_END` macros are used to ensure `sharedExamplesFor` are evaluated prior to any examples. - In order to assertion macros like `should` could be compiled outside of the implementation block of a `KWSpec`--like within a `sharedExamplesFor`, for example--changed macro to reference global state referenced by `KWSpec` class method. - Changed all pragma marks referencing internal/private methods to `Internal Methods`. - Removed unused `KWEncodingForVoidMethod` function.
👍 😍 |
I'll defer to @sharplet, since he's more familiar with modern-day Kiwi, but I'm excited to see this released!! When a failure occurs, are the correct lines highlighted in Xcode? |
@modocache So am I! Since it is a very useful feature 🤘 About highlighting the failures, unfortunately the implementation is still the one you originally did. I have already started working on how to fix it 👷 (suggestions on how to do it are very much welcome), but since I really want this functionality to be available 🙏, I was hoping to get this one merged first and then open an issue to fix the highlighting of failures when running a shared example. Does it make sense? |
I think it's possible to have the correct lines highlighted by using macros for #define sharedExamplesFor(name, block) kiwi_sharedExamplesFor(name, block, __FILE__, __LINE__) ...and define functions that take file name and line number parameters: void kiwi_sharedExamplesFor(NSString *name,
KWSharedExampleBlock block,
NSString *file,
NSUInteger line); Then, use the file and line arguments when reporting failures via That should work, provided you can fit it into Kiwi's framework of using |
@modocache Thanks for the hints! Let's see what I achieve. On the meanwhile, let's hope @sharplet can get this one out soon 😊 |
+1 |
Ah sorry everyone, really want to take a look at this! @ecaselles really appreciate you taking the time and hopefully I can get a chance this weekend! |
Just one thought while I'm thinking it: @modocache what are your thoughts on passing through an |
@sharplet Would the |
It's totally open. So here's how you could pass a class in: itBehavesLike(@"a foobar", @{
@"class": [Foobar class],
});
sharedExamplesFor(@"a foobar", ^(NSDictionary *data) {
__block id foobar;
beforeEach(^{
foobar = [data[@"class"] new];
});
// ...
}); But you could pass anything, for example a block that lazily instantiates your object. |
@sharplet Done! Now you can use the Please 🙏 review the changes and let me know your thoughts 💭 |
BTW, I was thinking on renaming the folder/group with the source files to |
pinging @sharplet and @modocache for feedback 🙇 |
can someone review and merge this one, please? |
Very sorry for the wait everyone—this is great. Thanks @ecaselles for doing the work to bring this into the future! 😉 In the interest of expediency, I went ahead and implemented your suggestion to move 🎉 |
@modocache a side note about failure highlighting 📝 After spending a few hours understanding how to highlight the line where the IMHO, it is common to write shared examples containing multiple tests and in that case, the current implementation of highlighting the failing line in the test is more useful, because it is very easy to see which tests are not passing and the class/subject invoking the shared examples appears first in the description (e.g. Otherwise, if we highlight the Finally, I have checked Specta's behavior on these situations and it also highlights the failing tests inside the shared example 👏. Therefore, I believe there is no need to modify the current implementation 🎉 |
This PR adds shared examples to the latest version of
Kiwi
(2.3.1). It is based on the original implementation proposed by @modocache in #442.Shared examples allow developers to reuse a set of describe, context, and it nodes to test one or more classes. See the functional tests in
Tests/Shared Examples/KWSharedExampleFunctionalTest.m
for sample usage.Addresses issues #175, #138.
Implementation Details
KWSharedExampleRegistry
is a singleton that maintains a mapping of shared example names toKWSharedExample
instances.KWSharedExample
is a data structure composed of a name and context block.sharedExamplesFor
creates aKWSharedExample
and registers it.itBehavesLike
looks up theKWSharedExample
with the given name and inserts its context block into the current example group.SHARED_EXAMPLES_BEGIN
,SHARED_EXAMPLES_END
macros are used to ensuresharedExamplesFor
are evaluated prior to any examples.KWSpec
--like withinsharedExamplesFor
, for example--changed macro to reference global state by usingKWSpec
class method.KWEncodingForVoidMethod
function.Sample Usage