Resharper jasmine loop tests always aborted - jasmine

i'm searching for a while to find a resharper test integration of looped jasmine tests.
See the little example (working excelent when run directly with karma test runner) BUT Resharper always aborted this test without any hint whats wrong:
describe('this is my looping test!', function() {
var input = [1,2,3];
var output = [10, 20, 30];
function test_my_times_ten(input, output) {
it('should multiply ' + input + ' by 10 to give ' + output, function() {
expect(input * 10).toEqual(output)
});
}
for(var x = 0; x < input.length; x++) {
test_my_times_ten(input[x], output[x]);
}
});
Is there any possibility to run loop tests?
Thanks, Jana

Related

For loop printing out the same result

Why does this javascript code output the same result?
var myAlerts = [];
for (var i = 0; i < 5; i++) {
myAlerts.push(
function inner() {
alert(i);
}
);
}
myAlerts[0](); // 5
myAlerts[1](); // 5
myAlerts[2](); // 5
myAlerts[3](); // 5
myAlerts[4](); // 5
I'd expect to see in 1, 2, 3, 4. It feels like it's something related to lexical scoping, but what's the real reason behind?
Can someone explain exactly how this piece of code work behind the scenes?
This will produce the expected results.
var myAlerts = [];
for (var i = 0; i < 5; i++) {
myAlerts.push(alert(i));
}
myAlerts[0](); // 5
myAlerts[1](); // 5
myAlerts[2](); // 5
myAlerts[3](); // 5
myAlerts[4]();
By using the function inner() i is set to the same variable once it is called outside of your loop
let allows you to declare variables that are limited in scope to the
block, statement, or expression on which it is used. This is unlike
the var keyword, which defines a variable globally, or locally to an
entire function regardless of block scope. An explanation of why the
name "let" was chosen can be found here.
You can also use:
var myAlerts = [];
for (let i = 0; i < 5; i++) {
myAlerts.push(
function inner(){
alert(i)
});
}
myAlerts[0](); // 5
myAlerts[1](); // 5
myAlerts[2](); // 5
myAlerts[3](); // 5
myAlerts[4]();

Apps Script Exceeded MAXIMUM_RUNNING_TIME Workaround

First of all, I am continuing an old thread at this link that I am unable to comment on due to being a newbie.
I have a situation that an answer in that thread given by user Br. Sayan would really improve my Spreadsheet Google App Script. I am making calls to Google Url Shortener API, which puts quotas at 1 call per user per second. I have slowed my script down enough to accommodate this quota, but I then I run over the MAX_RUNNING_TIME for App Scripts execution due to the extended number of calls I need to make, so I need to break the loop when the execution time is exceeded and pick up where I left off.
Here is the code of his answer:
function runMe() {
var startTime= (new Date()).getTime();
//do some work here
var scriptProperties = PropertiesService.getScriptProperties();
var startRow= scriptProperties.getProperty('start_row');
for(var ii = startRow; ii <= size; ii++) {
var currTime = (new Date()).getTime();
if(currTime - startTime >= MAX_RUNNING_TIME) {
scriptProperties.setProperty("start_row", ii);
ScriptApp.newTrigger("runMe")
.timeBased()
.at(new Date(currTime+REASONABLE_TIME_TO_WAIT))
.create();
break;
} else {
doSomeWork();
}
}
//do some more work here
}
My Questions:
Is MAX_RUNNING_TIME a global variable with a value set by Apps Script that I can leave that reference as-is, or must I replace it with a value equalling the 6 minutes listed as the quota for run time on the Google API Console?
How can I place the bulk of my function within this script so that a loop that runs inside my function (say var i = 0; i < data.length; i++) will be synchronized with the loop in the portion given in the above code?
Clarification: when i is incremented up by 1, I need ii to increment by 1.
Does this happen automatically? Do I need one loop nested inside the other? Does the bulk of my function go in the first '//do some work here' or the second '//do some work here' or possibly even doSomeWork()?
#tehhowch agreed! However, HOW I need to adapt my code depends on where I need to put it in the above snippet.
Here is what I have so far:
'function short() {
var = startTime = (new Date()).getTime();
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var run = 0;
var finc = 50;
var istart = run * finc;
var iLen = (run + 1) * finc;
var startRow = 2 + istart;
var endRow = startRow + finc;
var data = sheet.getSheetValues(startRow,2,endRow,1);
var shortUrl = new Array();
for (var i=istart; i < iLen; i++) {
Utilities.sleep(1100);
var url = UrlShortener.Url.insert({longUrl: data[i][0]});
shortUrl.push([url.id]);
Logger.log([url.id]);
}
var t = ss.setActiveSheet(ss.getSheets()[0]);
t.getRange(startRow,4,finc,data[0].length).clearContent();
t.getRange(startRow,4,finc,data[0].length).setValues(shortUrl);'
So if I update the code after each subsequent run to manually increase the variable 'run' by 1, and manually run the code again, this works.
I have also tried break it down into multiple functions by updating the i= and i < parts for each subsequent function, which also works, but requires much more manual work.
I have also tried, unsuccessfully, to use a prompt with a button press that continues the function, which would be better than the other attempts, but would still require a button press to resume the code after each run.
I want to automate the function as much as possible.

p5.play counter not working

I'm trying to write a program in which the end screen of the game only shows up after the last animation finishes. I'm using a counter that's implemented after each object is removed (which is only after it finishes its animation), and when that counter gets to zero, it should show the end screen. Unfortunately, from what I can tell, the counter statement isn't registering at all. I've inserted a print statement that isn't functioning.
var star;
var score;
var counter;
function setup() {
createCanvas(600,400);
score = 0;
counter = 20;
for (var s = 0; s < 20; s++) {
star = createSprite(random(width), random(height));
star.addAnimation("idle", idleAnim);
star.addAnimation("explode", explAnim);
star.changeAnimation("idle");
star.onMousePressed = function() {
this.changeAnimation("explode");
this.animation.looping = false;
score +=1
if (this.getAnimationLabel() == "explode" && this.animation.getFrame() == this.animation.getLastFrame()) {
this.remove();
counter -= 1;
print(counter);
}
}
}
}
function draw() {
if (score == 20 && counter == 0) {
background(255,222,51)
textSize(90);
fill(0)
text("YOU WIN!",95,225)
} else {
drawSprites();
}
}
You need to take a step back and debug your program. For example, are you sure the star.onMousePressed() function is firing? Are you sure the if statement is working the way you expected? Are you sure the player.dir() function is being called?
It sounds like your if statement is not being entered. can you find out the value of everything on that line? Which thing has a different value from what you expected?
Use console.log() statements, or use the JavaScript debugger, to answer all of the above. Figure out exactly which line of code is behaving differently from what you expected, and then isolate that problem in a MCVE. Good luck.

ExtendScript Toolkit CCC - Adding a leading zero if only a single digit is found

I have a script that changes the layer name of my illustrator file to "Test 1, Test 2, etc..." All I want to accomplish is to add leading zero to single digits. "Test 01, Test 02 ... Test 10, Test 11, etc..."
var doc = app.activeDocument;
idLayers("Test "); // Rename visible layers
// Hidden layers will be skipped and not counted
function idLayers(prefix){
var counter = 1;
for(i=0;doc.layers.length>i;i++){
var currentLayer = doc.layers[i];
// if layer is visible...
if (currentLayer.visible) {
currentLayer.name= prefix + counter;
counter++;
}
}
}
I found the following that would help but I'm not sure where to add it to the above code.
function pad(n) {
return (n < 10) ? ("0" + n) : n;
}
Total noob here so any help would be greatly appreciated. Thank you in advance!
You simply need to add the function that you already found at the end of your script (or at the beginning, it does not really matter) and then call it in the line, where the layer is named. So the whole script would look like this:
var doc = app.activeDocument;
idLayers("Test "); // Rename visible layers
// Hidden layers will be skipped and not counted
function idLayers(prefix){
var counter = 1;
for(i=0;doc.layers.length>i;i++){
var currentLayer = doc.layers[i];
// if layer is visible...
if (currentLayer.visible) {
currentLayer.name= prefix + pad(counter);
counter++;
}
}
}
function pad(n) {
return (n < 10) ? ("0" + n) : n;
}

Function.createCallback doesn't pass context correctly in FireFox

I've discovered what seems to be a bug in how the MS AJAX library interacts with FireFox -- but maybe I'm just doing it wrong. I've got a script that looks something like this:
dowork({ value: "some value", currentRetry: 0 });
// Try to connect at least 10 times, with a second in-between retries..
function dowork(request) {
if (request.currentRetry < 10) {
logMessage('currentRetry = ' + request.currentRetry + '; trying again in 1 second.');
request.currentRetry++;
var callback = Function.createCallback(dowork, { value: request.context, currentRetry: request.currentRetry });
setTimeout(callback, 1000);
}
else {
logMessage('Exceeded retries; currentRetry = ' + request.currentRetry);
}
}
In other words, I'm trying to do something that's likely to fail periodically, so I want to retry, say, 10 times, with a second in-between. The only way I can figure out how to do this is by using something like the Function.createCallback bit from the MS Ajax library.
And this works correctly in, say, IE 8 and Chrome 2, i.e., it produces the following output:
currentRetry = 0; trying again in 1 second.
currentRetry = 1; trying again in 1 second.
currentRetry = 2; trying again in 1 second.
currentRetry = 3; trying again in 1 second.
currentRetry = 4; trying again in 1 second.
currentRetry = 5; trying again in 1 second.
currentRetry = 6; trying again in 1 second.
currentRetry = 7; trying again in 1 second.
currentRetry = 8; trying again in 1 second.
currentRetry = 9; trying again in 1 second.
Exceeded retries; currentRetry = 10
However, in FireFox (3.5 Preview, haven't tested it in other flavors), the output looks like this:
currentRetry = 0; trying again in 1 second.
Exceeded retries; currentRetry = undefined
Any thoughts either on a workaround, or on what I'm doing wrong?
Well, I don't know what the problem is with Function.createCallback, but I was able to fix it by using an anonymous method instead:
var callback = function () { dowork(request) };
setTimeout(callback, 1000);
Close enough for government work.

Resources