I write on Groovy and use Spock framework for testing.
Some time ago, IDEA completely supports Spock.
When I've writing the code inside feature (test method) in where-block something like this:
with(someObject) {
intField == 1
...
}
IDEA correctly recognize the someObject and understand that intField is field of the object, also, it was offering me other fields of the object. So, inside Spock's with() block I've felt like inside any closure from DefaultGroovyMethods (with{}, each{}, find{}, etc.), but haven't need to use explicit it param.
(So, I's writing with(someObject) { intField == 1 } what is the same is someObject.with { assert it.intField == 1 }.
After a moment IDEA missed Spock's with() method support.
Now, it don't recognize the class of the parameter (ex. someObject) in think that it's object of Object class. Any fields inside closure don't be recognized. Explicit it usage didn't help.
After some IDEA update everything have repaired, but after reboot the problem has come back.
Does anybody know how to fix it??
I'm using the last version of the IDE - 2018.2
I just tested this out with 2018.1 and have the same issue as you. I didn't run the code but I am fairly certain that the code would be recognized and executed. This to me looks like a bug with Intellij, and it might be worth it to submit a bug report in their ticket tracking system.
Related
I'd love to create a new library for my purposes and am currently struggling with the technical approach needed to fulfill the requirements.
I want to have something like this:
class MyFoo {
#Populate("myText")
fun giveMeAValue(bar: Bar) {
LOG.info(bar.message) // "myText"
}
}
I'd like to hook on the function call of giveMeAValue or better said I want to hook logic on every function that is annotated with #Populate. If a call is being registered I'd like to execute some logic and pass a value for the bar parameter. Please ignore the fact that I might overwrite a value that has been passed already.
I was reading up for Reflection using Kotling (i.e. https://medium.com/tompee/kotlin-annotation-processor-and-code-generation-58bd7d0d333b) but could not find the thing I'd like to have here.
The hooking/proxy mechanism before the "real" function is being called. That's what I want to achieve.
Can anybody tell me what I am actually looking for? Is there a word for that concept or an article/guide describing this? I was not able to find anything. A code snippet/example would be awesome, too!
I want to create a Perl 6 module that would "export" a LEAVE phaser to the scope in which the use statement is placed. I have not found a way to do that.
I assume this would need to be done inside an EXPORT sub, but how? The default functionality of an EXPORT sub is to just return a Map with name => object mapping of things to export. As far as I know, there's no way to introspect what the outer scope is. Or am I missing something?
Thanks to Zoffix++ for pointing out a very hacky way of doing this.
sub EXPORT() {
$*W.add_phaser: $*LANG, 'LEAVE', { code you want to run }
{} # need to show that we're not exporting anything
}
This hack depends on various Rakudo internals, and is therefore not recommended to be used "in the wild". And it's quite likely that a better, more supportable way will be implemented for this functionality in the near future.
This hack was needed for a module that supports a sort of timely destruction other than from the direct scope in which an object is created (aka LEAVE phaser). This is typically handled in Perl 5 by using reference counting and calling DESTROY if the reference count of an object goes to 0.
This module can now be found in the Perl 6 ecosystem: FINALIZER. This module allows module developers to mark created objects for finalization: by default on program exit. Or from a scope indicated by the client program.
Not sure this is possible, but other people might know more. But what are you after anyway? I had a similar desire a while ago, I wanted to do something like a RAII lock. I solved it by wrapping the block rather than putting the LEAVE into it directly:
sub mtest($block) { LEAVE { say "hoo" }; $block() }
mtest { say "woo"; }
Perhaps that works for you as well...
I'm wondering if there is a way to let IDE suggest methods before variables?
When writing code like this it's a pain to select methods yourself (builder class example)
Variable example:
var message = "message"
private set
Thank you
Sorry, it appears you can't reconfigure the IDE or Kotlin plugin in this way. You can file a feature suggestion for Kotlin plugin, but I believe there are cases when variables are preferred over same-named methods, so just reordering them would break someone else's habits, so it's tricky.
You can consider making the variables private (so they won't be shown in the completion list at all) or selecting them and then typing an opening parenthesis manually.
In PHPUnit, I want to use methods like verifyText() with an optional message as the last parameter, like I do with assertStringEquals($expected, $actual, $message). It doesn't seem to work. Am I missing something?
I would tell myself to read the code, but I tried and I can't even figure out how any of the verify() methods get called. It must be some __call() function but I don't see it. So that's my follow-up question, how do the verify() methods get called? Then I could override them if I want.
I'm exploring the same question, albeit in the context of Selenium.
I found, grepping the source, an array $autoGeneratedCommands, which is set up in SeleniumTestCase/Driver. The mechanism here implements/maps verifyTextPresent() by a call to verifyCommand(), which calls assertCommand(). Subsequently one of the family assert*() is called... omitting the message in the call. This seems like an inadvertant feature to me. Well, coded bug.
I'm hoping someone can help me see the real value in these tests because I read a lot about them and even worked with them on a recent project. But I never really saw the real value in the tests. Sure I can understand why they are written and see how they could sort of be useful. But it just seems that there is little ROI on these types of tests. Here is an example of some class in our code base:
class ServiceObject : IServiceObject
{
Dependency _dependency;
ServiceObject(Dependency dependency)
{
this._dependency = dependency
}
bool SomeMethod()
{
dependency.SomePublicMethod();
}
}
Then in our behavioral tests we would write test like so (pseudo code):
void ServiceObject_SomeMethod_Uses_Dependency_SomePublicMethod()
{
create mock of IServiceObject;
stub out dependency object
add expectation of call to dependency.SomePublicMethod for IServiceObject.SomeMethod
call mockserviceobject.SomeMethod
check whether expectation was satisfied
}
Obviously there are some details missing but if you are familiar with this type of testing you will understand.
So my question really derives from the fact that I can't see how it is valuable to know that my ServiceObject calls into the dependency method. I understand the reasoning behind it because you want to make sure that the logic of the method is hitting the parts that it is supposed to. But what I can't see is how this is a sustainable testing pattern?
I wrote the logic and know how the code should work so why would it ever change after I test it once to make sure that it is working? Now you can say that if you work in a team environment you might want to make sure that someone doesn't come along and change the code so that the dependency is accidentally skipped and thus would want to make sure they are aware of it. But what if it was skipped for a valid reason. Then that whole test and maybe any others would all have to be scrapped.
Anyways I am just hoping for someone to turn the light on as to the true potential of these types of tests.
The objective is to test the class in isolation, truly Unit test it. Verify that it performs exactly its responsibilities.
In this case the code is rather trivial. The value of this kind of testing may become more clear when there is conditionality in the processing path, parameters are passed to the dependency and processing is performed on the results from the dependency.
For example, suppose instead a method was like this:
bool someMethod(int paramX, int param Y ){
if ( (paramX / paramY) > 5 ){
return dependency.doOneThing(paramX);
} else {
return dependency.doSomethingElse(paramY);
}
}
Now we have quite a few tests to write, and I think the value becomes more obvious. Especially when we write a test with paramY set to zero.