Add error; python and php tests pass.

This commit is contained in:
Rachel Fenichel
2017-12-20 16:28:14 -08:00
parent fced496d28
commit 0a0b6f3848
5 changed files with 33 additions and 6 deletions

View File

@@ -76,6 +76,11 @@ Blockly.Names.prototype.setVariableMap = function(map) {
Blockly.Names.prototype.getNameForVariable = function(id) {
if (!this.variableMap_) {
console.log('Deprecated call to Blockly.Names.prototype.getName without ' +
'defining a variable map. To fix, add the folowing code in your ' +
'generator\'s init() function:\n' +
'Blockly.YourGeneratorName.variableDB_.setVariableMap(' +
'workspace.getVariableMap());');
return null;
}
var variable = this.variableMap_.getVariableById(id);

View File

@@ -149,14 +149,27 @@ Blockly.PHP.init = function(workspace) {
Blockly.PHP.variableDB_.reset();
}
Blockly.PHP.variableDB_.setVariableMap(workspace.getVariableMap());
var defvars = [];
var varName;
// TODO (fenichel): Why does this use Variables.allVariables when the rest use
// workspace.getAllVariables()?
var variables = Blockly.Variables.allVariables(workspace);
for (var i = 0, variable; variable = variables[i]; i++) {
varName = variable.name;
defvars[i] = Blockly.PHP.variableDB_.getName(varName,
Blockly.Variables.NAME_TYPE) + ';';
}
// Add developer variables (not created or named by the user).
var devVarList = Blockly.Variables.allDeveloperVariables(workspace);
for (var i = 0; i < devVarList.length; i++) {
defvars.push(Blockly.PHP.variableDB_.getName(devVarList[i],
Blockly.Names.DEVELOPER_VARIABLE_TYPE) + ';');
}
// Declare all of the variables.
Blockly.PHP.definitions_['variables'] = defvars.join('\n');
};

View File

@@ -160,12 +160,21 @@ Blockly.Python.init = function(workspace) {
Blockly.Python.variableDB_.reset();
}
Blockly.Python.variableDB_.setVariableMap(workspace.getVariableMap());
var defvars = [];
var variables = workspace.getAllVariables();
for (var i = 0; i < variables.length; i++) {
defvars[i] = Blockly.Python.variableDB_.getName(variables[i].name,
Blockly.Variables.NAME_TYPE) + ' = None';
}
// Add developer variables (not created or named by the user).
var devVarList = Blockly.Variables.allDeveloperVariables(workspace);
for (var i = 0; i < devVarList.length; i++) {
defvars.push(Blockly.Python.variableDB_.getName(devVarList[i],
Blockly.Names.DEVELOPER_VARIABLE_TYPE) + ' = None');
}
Blockly.Python.definitions_['variables'] = defvars.join('\n');
};

View File

@@ -27,7 +27,7 @@
Blockly.PHP['unittest_main'] = function(block) {
// Container for unit tests.
var resultsVar = Blockly.PHP.variableDB_.getName('unittestResults',
Blockly.Variables.NAME_TYPE);
Blockly.Names.DEVELOPER_VARIABLE_TYPE);
var functionName = Blockly.PHP.provideFunction_(
'unittest_report',
[ 'function ' + Blockly.PHP.FUNCTION_NAME_PLACEHOLDER_ + '() {',
@@ -72,7 +72,7 @@ Blockly.PHP['unittest_main'] = function(block) {
Blockly.PHP['unittest_main'].defineAssert_ = function(block) {
var resultsVar = Blockly.PHP.variableDB_.getName('unittestResults',
Blockly.Variables.NAME_TYPE);
Blockly.Names.DEVELOPER_VARIABLE_TYPE);
var functionName = Blockly.PHP.provideFunction_(
'assertEquals',
['function ' + Blockly.PHP.FUNCTION_NAME_PLACEHOLDER_ +
@@ -129,7 +129,7 @@ Blockly.PHP['unittest_assertvalue'] = function(block) {
Blockly.PHP['unittest_fail'] = function(block) {
// Always assert an error.
var resultsVar = Blockly.PHP.variableDB_.getName('unittestResults',
Blockly.Variables.NAME_TYPE);
Blockly.Names.DEVELOPER_VARIABLE_TYPE);
var message = Blockly.PHP.quote_(block.getFieldValue('MESSAGE'));
var functionName = Blockly.PHP.provideFunction_(
'unittest_fail',

View File

@@ -27,7 +27,7 @@
Blockly.Python['unittest_main'] = function(block) {
// Container for unit tests.
var resultsVar = Blockly.Python.variableDB_.getName('unittestResults',
Blockly.Variables.NAME_TYPE);
Blockly.Names.DEVELOPER_VARIABLE_TYPE);
var functionName = Blockly.Python.provideFunction_(
'unittest_report',
['def ' + Blockly.Python.FUNCTION_NAME_PLACEHOLDER_ + '():',
@@ -68,7 +68,7 @@ Blockly.Python['unittest_main'] = function(block) {
Blockly.Python['unittest_main'].defineAssert_ = function() {
var resultsVar = Blockly.Python.variableDB_.getName('unittestResults',
Blockly.Variables.NAME_TYPE);
Blockly.Names.DEVELOPER_VARIABLE_TYPE);
var functionName = Blockly.Python.provideFunction_(
'assertEquals',
['def ' + Blockly.Python.FUNCTION_NAME_PLACEHOLDER_ +
@@ -117,7 +117,7 @@ Blockly.Python['unittest_assertvalue'] = function(block) {
Blockly.Python['unittest_fail'] = function(block) {
// Always assert an error.
var resultsVar = Blockly.Python.variableDB_.getName('unittestResults',
Blockly.Variables.NAME_TYPE);
Blockly.Names.DEVELOPER_VARIABLE_TYPE);
var message = Blockly.Python.quote_(block.getFieldValue('MESSAGE'));
var functionName = Blockly.Python.provideFunction_(
'fail',