How to use for inside IntelliJ Live Template? - intellij-idea

I am trying to write a simple Groovy script which concatenates strings.
This Groovy script is intended to be used as a variable inside an IntelliJ Live Template and generate some code.
When I set the $variableResolvedWithGroovyScript$ to have the following value:
groovyScript("def elements=[\"firstElem\",\"secondElem\",\"thirdElem\"];
String result=\"\";
for (String element : elements) {
result=result+element;
}
return result;"
, clipboard())
I get below error:
startup failed:
Script1.groovy: 1: expecting EOF, found 'return' # line 1, column 183.
lt+element; } return res
^
1 error
However, if I try and remove the for braces({ and }) it works without any problems:
groovyScript("def elements=[\"firstElem\",\"secondElem\",\"thirdElem\"];
String result=\"\";
for (String element : elements)
result=result+element;
return result;"
, clipboard())
If I take above script and run it inside a Groovy Console it works without problems, so I assume this is a Live Template issue.
I tried to escape the braces in the same way the quotation marks are escaped, but without any luck.
How can I write a for(containing more than one instruction) inside of a Live Template ?

I managed to make it work by loading the Groovy script from the disk. So instead of the $variableResolvedWithGroovyScript$'s value, which was:
groovyScript("def elements=[\"firstElem\",\"secondElem\",\"thirdElem\"];
String result=\"\";
for (String element : elements) {
result=result+element;
}
return result;"
, clipboard())
I simply used:
groovyScript("d:\\Some\\Path\\Concatenate.groovy" , clipboard())
The d:\Some\Path\Concatenate.groovy worked with braces and I was also able to define new methods and so on.

Related

Flutter test - verifying assets exist

I have a flutter app which contains a large list of quotes, each with an associated audio file.
I've written a simple test that verifies all the specified audio files are where they're supposed to be, in case of typos etc:
test('specified audio file should exist for all quotes', () {
ALL_QUOTES.forEach((quote) {
final expectedPath = 'assets/${quote.filename}.wav';
final exists = new File(expectedPath).existsSync();
expect(exists, isTrue, reason: '$expectedPath does not exist');
});
});
This passes fine in IntelliJ, however running from the command line using flutter test it fails on the first thing it looks for.
Is there a way of doing this which will work regardless of how it's run? Why does it pass one way but not the other?
Ok so I got to the bottom of this, and it is something you can do in a unit test.
To diagnose, I added the line print(Directory.current); to the test. Running in IntelliJ, I get /home/project_name. From the command line, it's /home/project_name/test. So just a simple file path thing to resolve.
Edited to include Ovidiu's simpler logic for getting the right asset path
void main() {
test('specified audio file should exist for all quotes', () {
ALL_QUOTES.forEach((quote) {
final expectedPath = 'assets/${quote.filename}.wav';
final exists = _getProjectFile(expectedPath).existsSync();
expect(exists, isTrue, reason: '$expectedPath does not exist');
});
});
}
File _getProjectFile(String path) {
final String assetFolderPath = Platform.environment['UNIT_TEST_ASSETS'];
return File('$assetFolderPath/$path');
}

How to set array property value in code using Carina Test Framework for API tests?

I have the following request json body:
{
...
"attachmentIds": "${attachments}"
...
}
I have a properties file that includes the declaration of the corresponding placeholder
I want to set array of strings in code instead of "attachments" placeholder, but getProperties().setProperty() expects only string value.
How can I achieve it other way or is it possible at all?
Thanks!
As an option you can transform your array into the String in java code. And then pass this String as property value.
Another option, you can pass String array from code and then parse it in your json template.
For example:
String[] arr = { "1", "2", "3" };
apiMethod.addProperty("attachments", arr);
And then in your json:
{
"attachmentIds": [<#list attachments as val>"${val}"<#if val?has_next>,</#if></#list>]
}
Check freemarker documentation to get more details:
https://freemarker.apache.org/docs/ref_builtins_loop_var.html
Also please note that some of freemarker functions (including has_next) are available only in newest versions of library. So make sure to add into your dependencies list. Carina is now in process of migrating to latest freemarker version.

how to pass dynamic arguments into functions , instead of hardcoded strings

We are facing an issue while trying to pass argument stored in var .
The issue is we are storing the dynmically retrived div id in a variable and then passing it to .moveToObject function.
But the function throws back error ;
Error: invalid selector: No selector specified
Wanted to know how we can pass arguments stored in variables into various functions.
For instance in the code below , we are retiring sectionId via execute , storing it in sectionId variable .
then using it later on in moveToObject.
all functions seems to work when we are giving arguments inside "" quotes, but in our case we cannot do this. Please help us out how we can achieve this.
it('drag and drop should work', function() {
var sectionId = "";
// load page, then call function()
return driver
.url('http://localhost:9000')
.pause(7000)
.click('#layoutWizardButton')
.click('#tableWizardBuild')
.pause(3000)
.execute(function() {
sectionId = window.document.getElementById('Section1');
}
.moveToObject('#ribbon-radio-tile')
.buttonDown()
.moveToObject(sectionId) -- -- > errors out here
.buttonUp()
.pause(2000)
.end()
});
Try .moveToObject(sectionId.value)

Linqpad - Outputting into anchor to use title

I have a db that stores exception messages.
I would like to create a query that gets these exceptions but instead of dumping huge amounts of text i would prefer it to be "on demand".
I figured putting the exception into an anchor tag like so and then reading the message when needed by mousing over it would work... apparently not.
var logsForErrors = (from error in Logs
select new {
error = LINQPad.Util.RawHtml("<a title='"+ error.Exception+"'></a>"),
errorDate = error.Date,
errorMessage = error.Message
}).Take(10);
logsForErrors.Dump();
This is throwing an exception (lol) - "Cannot parse custom HTML: "
Encoding the exception message
...RawHtml("<a title='"+ Uri.EscapeDataString(error.Exception)+"'></a>")
Message Could not translate expression 'RawHtml((("h__TransparentIdentifier0.error.Exception)) +
"'>"))' into SQL and could not treat it as a local expression.
will generate a new error
Any ideas? - I am open to alternative solutions to this also.
I just want a container for the message instead of it just dumping right into the output as it it so huge!.
Thanks,
Kohan
Have you tried using the "Results to DataGrids" mode in the recent betas? It might do just what you need without having to write anything else.
Edit: your error was probably due to emitting HTML without escaping the text. The easiest solution is to call Util.RawHtml with an XElement instead of a string. You could write an extension method that does what you want like this:
public static class Extensions
{
public static object Tooltipize (this string data)
{
if (string.IsNullOrEmpty (data) || data.Length < 20) return data;
return Util.RawHtml (new XElement ("span", new XAttribute ("title", data), data.Substring (0, 20)));
}
}
Put this into My Extensions and you can use it from any query.

Unable to resolve this Compilation Error in Flex builder 3

I am new Web App development using Flex Builder 3 and currently I am facing the following problem:
Attached is a code snippet from the mxml file:
<mx:Script>
<![CDATA[
import com.bx.Char10;
import com.bx.A;
[Bindable] private var inputParam:A = new A()
inputParam.CustNumber.char10 = '0123456789'
}
]]>
</mx:Script>
This Gives a compile error
1120 Access of undefined property inputParam
However if I replace
inputParam.CustNumber.char10 = '0123456789'
with
private function set():void
{
inputParam.CustNumber.char10 = '0123456789'
}
The compile error goes away.
My Question is :
How can I remove this Compilation Error without using the workaround I did?
Hmm, I don't believe that one can execute arbitrary statements directly inside a class body. (The "Script" tag's contents are treated as if they were directly inside the class body).
Only function definitions or variable property definitions are allowed.
A different work-around to use is to pass the information through the constructor of the variable property you're interested in.
[Bindable] private var inputParam:A = new A('0123456789')