Is there any way I can redefine asBoolean for core class in Groovy? I tried
Number.metaClass.asBoolean = {false}
While it works for non-Groovy classes I can't find how to make the same trick for Number or any other predefined class.
I could reproduce your problem when executing the script in the GroovyConsole. However, when executed as a .groovy file on the command line, the metaClass change works as expected.
I think you're just missing a ->. This works for me
Number.metaClass.asBoolean = {-> false}
assert !2.asBoolean() // Normally 2 evaluates to true
Related
I have a bunch of common groovy functions which I am reusing in different 'Test Plans'. I want to keep them in separate script files. I don't want to create jar. How can I import those files in JMeter JSR223 Assertion, Preprocessor and Postprocessor with minimum fuzz? These are more or less modular functions and I want to keep them separate for obvious reason.
I tried using "Script file" section of JSR223. But it seems that it is only for overriding the script.
How can I use an external groovy script file in JSR 223 assertion/preprocessor/post-processor?
If you have a bunch of functions which you`d like to re-use in different jsr223 elements you can:
In your test-plan create one initial JSR223 preprocessor (or sampler) and define your functions:
def sum(Integer a, Integer b) {
return a + b
}
def isA(Integer x, Integer y) {
}
def isB(Integer x, Integer y) {
}
Then using closure put them as objects:
vars.putObject('sum', this.&sum)
vars.putObject('isA', this.&isA)
vars.putObject('isB', this.&isB)
In any other JSR223 element in the script you can use those functions like that:
def sum= vars.getObject(‘sum’);
println sum(2, 2);
I don't really understand why Script file option doesn't work for you, you can save your Groovy code as separate files on the file system and reference them via "Script file" input.
Whatever.
You can make JSR223 Test Elements modular just like any other Test Element using:
Test Fragment
Module Controller
and Include Controller combination
You can define the Groovy functions in {JMETER_HOME}/bin/utility.groovy script file.
Optionally you can define the function in your own Groovy script and set the property groovy.utilities=bin/utility.groovy in user.properties file.
For example, add the following to the Groovy script
def getRandomRangeOption() {
Random random = new Random()
random.nextBoolean() ? "Between" : "Except"
}
You can call the function with
${__groovy(getRandomRangeOption())}
from anywhere in you JMX.
Aplogies if I have the terminology all wrong; I am still learning the basics of Python. I have been unable to google this issue, probably in large part because I don't know the terminology..
So. I have built a class within a .py script with LOTS of methods/functions. To keep this remotely simple, I want to call these from a commandline argument. I have no idea how to explain it, and I can't find anhy examples, so I will try to demo it:
Take for example mute_on as the function that I want to call. I run the script with the function/method in the argument, like:
python3 ./myscript.py mute_on
I assume we'd import sys(?), define the class and the function, and create the relevant object from the class:
import sys
class TelnetAVR(PioneerDevice):
def mute_on(self, mute):
self.telnet_command("MO")
mypioneer = PioneerDevice('Pioneer AVR', '192.168.2.89', 8102, 10)
...and lastly I would like the commandline argument to call the method/function - instead of calling it explicitly like:
mypioneer.mute_volume()
..I want to use the arg (sys.argv[1]) to dynamically call the function, like:
mypioneer.{sys.argv[1]}()
Any ideas, kind people? I have been auto-referred to What is getattr() exactly and how do I use it? but I have no idea how that information can help me here.
I have tried setting cmnd = 'turn_off' and then the following failed...;
getattr(mypioneer, str(cmnd))
getattr(mypioneer, cmnd)
Thanks!
This answer seems a little basic, but I cannot complain as to its efficacy;
mypioneer = PioneerDevice('Pioneer AVR', '192.168.2.89', 8102, 10)
exp = 'mypioneer.' + sys.argv[1] + '()'
print('Executing: ' + exp )
exec(exp)
I gave up loking for a graceful answer, and simply constructed a string that I wanted to execute (exp) based on the commandline argument. Works great.. Home Assistant can use the same script to call 50 telnet controls over my Pioneer AVR.
I need to obtain JAVA_HOME property from Groovy (Gradle), does anyone know how to achieve this? Only way I can think of is somehow executing this from cmd line via Exec.
Thanks
(I'm running Windows btw :))
System.properties.find { it.key == "java.home" }
A gotcha that bit me. Remember to use curly braces inside a gstring.
println "inside a gstring, java.home=$System.properties.'java.home' will be problematic
//dumps all system properties
but
println "inside a gstring, java.home=${System.properties.'java.home'} will be fine
Result: inside a gstring, java.home=C:\FAST\JDK64\1.7.0.79\jre will be fine
If you run the following Groovy code, the assertion passes
def foo(a, b) {
a + b
}
assert 'aaabbb' == foo(['aaa', 'bbb'])
This suggests that if a method is called with a List parameter that contains X elements, then the List will be spread and a method with X arguments will be invoked.
Of course, this will only happen if there isn't a method defined with a single parameter of type List (or ancestor thereof).
I only discovered this quite recently when reading another SO Groovy answer. I've never seen it mentioned in the Groovy docs, release notes, or books. Is it a hidden feature, a bug, or just something I've missed?
Going to be removed in Groovy 2 apparently:
http://groovy.329449.n5.nabble.com/removing-features-in-Groovy-2-td4422494.html
JT's first on the to-remove list and it seems everyone (with clout) on Groovy User agrees.
When i try execute the following code, which should just print a slashy string in groovy console version 1.7.4 i get a compilation error:
println /slashy string/
if i change this to:
def s = /slashy string/;
println s
everything is fine and the expected string is printed.
Any ideas what i am doing wrong?
The final gotcha (on the linked doc) says, a slashy string cannot be used with assert, because of a grammar limitation. Since println is also a part of the grammar (afaik, since its not a classical java function), I would guess this applies here, too.
It says to use braces around it:
println (/slashy string/)
This worked fine in my groovy shell.