The right order for Functions

106 views
Skip to first unread message

Bennet Preimess

unread,
May 19, 2018, 6:53:43 AM5/19/18
to Blockly
Hello Community,

i am building a blockly-application to make testcases in nodejs. When I connect the Blocks like on the screenshot the order is wrong and i become the following error message:
TypeError: a.replace is not a function

This is the code wich is generated:
describe('Beschreibung', function () {
  it
('Testcasename', () => {
 
});
});
logout
();

This is what i want to generate:
describe('Beschreibung', function () {
  it
('Testcasename', () => {
   
logout ();
 
});
});

The "Vorlage"- and Testcase-Blocks only returns the code without order.
The login-Block-Codegenerator:

let code = 'login (';
        let initialLength
= code.length;
        let methodEnded
= false;
       
JSONblock.args0.forEach(argument => {
         
if(argument.type === 'input_value' && !methodEnded) {
            let value
= Blockly.JavaScript.valueToCode(block, argument.name, Blockly.JavaScript.ORDER_FUNCTION_CALL)
           
if (value) {
              code
+= value + ', ';
           
} else {
              methodEnded
= true;
           
}
         
}
       
})
       
return (code.length === initialLength)
       
? [code + ');\n', Blockly.JavaScript.ORDER_FUNCTION_CALL]             // arguments are added
       
: [code.slice(0,-2) + ');\n', Blockly.JavaScript.ORDER_FUNCTION_CALL] // no arguments are added
     
}

Best wishes
login_testcase.jpg

Andrew n marshall

unread,
May 21, 2018, 7:00:08 PM5/21/18
to blo...@googlegroups.com
Hi Bennet,

Can you send the full generator functions for all three blocks. I'm guessing the bug is actually in the Vorlage and/or Testcase generators.

--
You received this message because you are subscribed to the Google Groups "Blockly" group.
To unsubscribe from this group and stop receiving emails from it, send an email to blockly+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Bennet Preimess

unread,
May 22, 2018, 3:46:49 AM5/22/18
to Blockly
This is my Testcase genetrator-function:
    Blockly.JavaScript['testcase'] = function(block) {
     
var statements_name = Blockly.JavaScript.statementToCode(block, 'Methoden');
     
var text_tcn = block.getFieldValue('TCN');
     
var code = 'it(\''+ text_tcn +'\', () => {\n'
      code
+= statements_name;
      code
+= '});\n'
     
return code;

Generator-function for Vorlage:
    Blockly.JavaScript['defaultblock'] = function(block) {
     
var statements_name = Blockly.JavaScript.statementToCode(block, 'Testcases');
     
var text_description = block.getFieldValue('description');
     
var code = 'describe(\''+ text_description +'\', function () {\n'
      code
+= statements_name;
      code
+= '});'
     
return code;
   
};

I changed the generator-function for the login-block:
      Blockly.JavaScript[JSONblock.type] = function(block) {
       
var values = [];
       
JSONblock.args0.forEach(argument => {
         
if (Blockly.JavaScript.valueToCode(block, argument.name, Blockly.JavaScript.ORDER_FUNCTION_CALL)) {
            values
.push(Blockly.JavaScript.valueToCode(block, argument.name, Blockly.JavaScript.ORDER_FUNCTION_CALL))
         
}
       
})
       
var code = JSONblock.type + '(' + values + ')\n'
       
return code;
     
}

Now it works. But is this the right way? Thank you :)

Andrew n marshall

unread,
May 22, 2018, 2:03:41 PM5/22/18
to blo...@googlegroups.com
Glad you got it working.  This looks right to me.

--
Reply all
Reply to author
Forward
0 new messages