mirror of
https://github.com/google/blockly.git
synced 2026-01-09 18:10:08 +01:00
chore: revert procedure events (#6795)
* chore: delete procedure events * chore: delete event related tests
This commit is contained in:
@@ -28,16 +28,6 @@ import {CommentCreate, CommentCreateJson} from './events_comment_create.js';
|
||||
import {CommentDelete} from './events_comment_delete.js';
|
||||
import {CommentMove, CommentMoveJson} from './events_comment_move.js';
|
||||
import {MarkerMove, MarkerMoveJson} from './events_marker_move.js';
|
||||
import {ProcedureBase} from './events_procedure_base.js';
|
||||
import {ProcedureChangeReturn} from './events_procedure_change_return.js';
|
||||
import {ProcedureCreate} from './events_procedure_create.js';
|
||||
import {ProcedureDelete} from './events_procedure_delete.js';
|
||||
import {ProcedureEnable} from './events_procedure_enable.js';
|
||||
import {ProcedureRename} from './events_procedure_rename.js';
|
||||
import {ProcedureParameterBase} from './events_procedure_parameter_base.js';
|
||||
import {ProcedureParameterCreate} from './events_procedure_parameter_create.js';
|
||||
import {ProcedureParameterDelete} from './events_procedure_parameter_delete.js';
|
||||
import {ProcedureParameterRename} from './events_procedure_parameter_rename.js';
|
||||
import {Selected, SelectedJson} from './events_selected.js';
|
||||
import {ThemeChange, ThemeChangeJson} from './events_theme_change.js';
|
||||
import {ToolboxItemSelect, ToolboxItemSelectJson} from './events_toolbox_item_select.js';
|
||||
@@ -87,16 +77,6 @@ export {FinishedLoading};
|
||||
export {FinishedLoadingJson};
|
||||
export {MarkerMove};
|
||||
export {MarkerMoveJson};
|
||||
export {ProcedureBase};
|
||||
export {ProcedureChangeReturn};
|
||||
export {ProcedureCreate};
|
||||
export {ProcedureDelete};
|
||||
export {ProcedureEnable};
|
||||
export {ProcedureRename};
|
||||
export {ProcedureParameterBase};
|
||||
export {ProcedureParameterCreate};
|
||||
export {ProcedureParameterDelete};
|
||||
export {ProcedureParameterRename};
|
||||
export {Selected};
|
||||
export {SelectedJson};
|
||||
export {ThemeChange};
|
||||
|
||||
@@ -1,41 +0,0 @@
|
||||
/**
|
||||
* @license
|
||||
* Copyright 2022 Google LLC
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
import {Abstract as AbstractEvent, AbstractEventJson} from './events_abstract.js';
|
||||
import type {IProcedureModel} from '../interfaces/i_procedure_model.js';
|
||||
import type {Workspace} from '../workspace.js';
|
||||
|
||||
|
||||
/**
|
||||
* The base event for an event associated with a procedure.
|
||||
*/
|
||||
export abstract class ProcedureBase extends AbstractEvent {
|
||||
isBlank = false;
|
||||
|
||||
/**
|
||||
* @param workspace The workspace the procedure model exists in.
|
||||
* @param model The procedure model associated with this event.
|
||||
*/
|
||||
constructor(workspace: Workspace, public readonly model: IProcedureModel) {
|
||||
super();
|
||||
this.workspaceId = workspace.id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Encode the event as JSON.
|
||||
*
|
||||
* @returns JSON representation.
|
||||
*/
|
||||
toJson(): ProcedureBaseJson {
|
||||
const json = super.toJson() as ProcedureBaseJson;
|
||||
json['procedureId'] = this.model.getId();
|
||||
return json;
|
||||
}
|
||||
}
|
||||
|
||||
export interface ProcedureBaseJson extends AbstractEventJson {
|
||||
procedureId: string;
|
||||
}
|
||||
@@ -1,87 +0,0 @@
|
||||
|
||||
/**
|
||||
* @license
|
||||
* Copyright 2022 Google LLC
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
import type {IProcedureModel} from '../interfaces/i_procedure_model.js';
|
||||
import * as registry from '../registry.js';
|
||||
import type {Workspace} from '../workspace.js';
|
||||
|
||||
import {ProcedureBase, ProcedureBaseJson} from './events_procedure_base.js';
|
||||
import * as eventUtils from './utils.js';
|
||||
|
||||
|
||||
/**
|
||||
* Notifies listeners that a procedure's return type/status has changed.
|
||||
*/
|
||||
export class ProcedureChangeReturn extends ProcedureBase {
|
||||
/** A string used to check the type of the event. */
|
||||
type = eventUtils.PROCEDURE_CHANGE_RETURN;
|
||||
|
||||
/** The new type(s) the procedure's return has been set to. */
|
||||
private newTypes: string[]|null;
|
||||
|
||||
/**
|
||||
* @param oldTypes The type(s) the procedure's return was set to before it
|
||||
* changed.
|
||||
*/
|
||||
constructor(
|
||||
workpace: Workspace, model: IProcedureModel,
|
||||
public readonly oldTypes: string[]|null) {
|
||||
super(workpace, model);
|
||||
|
||||
this.newTypes = model.getReturnTypes();
|
||||
}
|
||||
|
||||
run(forward: boolean) {
|
||||
const procedureModel =
|
||||
this.getEventWorkspace_().getProcedureMap().get(this.model.getId());
|
||||
if (!procedureModel) {
|
||||
throw new Error(
|
||||
'Cannot change the type of a procedure that does not exist ' +
|
||||
'in the procedure map');
|
||||
}
|
||||
if (forward) {
|
||||
procedureModel.setReturnTypes(this.newTypes);
|
||||
} else {
|
||||
procedureModel.setReturnTypes(this.oldTypes);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Encode the event as JSON.
|
||||
*
|
||||
* @returns JSON representation.
|
||||
*/
|
||||
toJson(): ProcedureChangeReturnJson {
|
||||
const json = super.toJson() as ProcedureChangeReturnJson;
|
||||
json['oldTypes'] = this.oldTypes;
|
||||
return json;
|
||||
}
|
||||
|
||||
/**
|
||||
* Deserializes the JSON event.
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
static fromJson(json: ProcedureChangeReturnJson, workspace: Workspace):
|
||||
ProcedureChangeReturn {
|
||||
const model = workspace.getProcedureMap().get(json['procedureId']);
|
||||
if (!model) {
|
||||
throw new Error(
|
||||
'Cannot deserialize procedure change return event because the ' +
|
||||
'target procedure does not exist');
|
||||
}
|
||||
return new ProcedureChangeReturn(workspace, model, json['oldTypes']);
|
||||
}
|
||||
}
|
||||
|
||||
export interface ProcedureChangeReturnJson extends ProcedureBaseJson {
|
||||
oldTypes: string[]|null;
|
||||
}
|
||||
|
||||
registry.register(
|
||||
registry.Type.EVENT, eventUtils.PROCEDURE_CHANGE_RETURN,
|
||||
ProcedureChangeReturn);
|
||||
@@ -1,74 +0,0 @@
|
||||
/**
|
||||
* @license
|
||||
* Copyright 2022 Google LLC
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
import type {IProcedureModel} from '../interfaces/i_procedure_model.js';
|
||||
import {ObservableParameterModel, ObservableProcedureModel} from '../procedures.js';
|
||||
import * as registry from '../registry.js';
|
||||
import {loadProcedure, saveProcedure, State as ProcedureState} from '../serialization/procedures.js';
|
||||
import type {Workspace} from '../workspace.js';
|
||||
|
||||
import {ProcedureBase, ProcedureBaseJson} from './events_procedure_base.js';
|
||||
import * as eventUtils from './utils.js';
|
||||
|
||||
|
||||
/**
|
||||
* Notifies listeners that a procedure data model has been created.
|
||||
*/
|
||||
export class ProcedureCreate extends ProcedureBase {
|
||||
/** A string used to check the type of the event. */
|
||||
type = eventUtils.PROCEDURE_CREATE;
|
||||
|
||||
constructor(workspace: Workspace, model: IProcedureModel) {
|
||||
super(workspace, model);
|
||||
}
|
||||
|
||||
run(forward: boolean) {
|
||||
const workspace = this.getEventWorkspace_();
|
||||
const procedureMap = workspace.getProcedureMap();
|
||||
const procedureModel = procedureMap.get(this.model.getId());
|
||||
if (forward) {
|
||||
if (procedureModel) return;
|
||||
// TODO: This should add the model to the map instead of creating a dupe.
|
||||
procedureMap.add(new ObservableProcedureModel(
|
||||
workspace, this.model.getName(), this.model.getId()));
|
||||
} else {
|
||||
if (!procedureModel) return;
|
||||
procedureMap.delete(this.model.getId());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Encode the event as JSON.
|
||||
*
|
||||
* @returns JSON representation.
|
||||
*/
|
||||
toJson(): ProcedureCreateJson {
|
||||
const json = super.toJson() as ProcedureCreateJson;
|
||||
json['model'] = saveProcedure(this.model);
|
||||
return json;
|
||||
}
|
||||
|
||||
/**
|
||||
* Deserializes the JSON event.
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
static fromJson(json: ProcedureCreateJson, workspace: Workspace):
|
||||
ProcedureCreate {
|
||||
return new ProcedureCreate(
|
||||
workspace,
|
||||
loadProcedure(
|
||||
ObservableProcedureModel, ObservableParameterModel, json['model'],
|
||||
workspace));
|
||||
}
|
||||
}
|
||||
|
||||
export interface ProcedureCreateJson extends ProcedureBaseJson {
|
||||
model: ProcedureState,
|
||||
}
|
||||
|
||||
registry.register(
|
||||
registry.Type.EVENT, eventUtils.PROCEDURE_CREATE, ProcedureCreate);
|
||||
@@ -1,71 +0,0 @@
|
||||
|
||||
/**
|
||||
* @license
|
||||
* Copyright 2022 Google LLC
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
import type {IProcedureModel} from '../interfaces/i_procedure_model.js';
|
||||
import {ObservableProcedureModel} from '../procedures.js';
|
||||
import * as registry from '../registry.js';
|
||||
import type {Workspace} from '../workspace.js';
|
||||
|
||||
import {ProcedureBase, ProcedureBaseJson} from './events_procedure_base.js';
|
||||
import * as eventUtils from './utils.js';
|
||||
|
||||
|
||||
/**
|
||||
* Notifies listeners that a procedure data model has been deleted.
|
||||
*/
|
||||
export class ProcedureDelete extends ProcedureBase {
|
||||
/** A string used to check the type of the event. */
|
||||
type = eventUtils.PROCEDURE_DELETE;
|
||||
|
||||
constructor(workspace: Workspace, model: IProcedureModel) {
|
||||
super(workspace, model);
|
||||
}
|
||||
|
||||
run(forward: boolean) {
|
||||
const workspace = this.getEventWorkspace_();
|
||||
const procedureMap = workspace.getProcedureMap();
|
||||
const procedureModel = procedureMap.get(this.model.getId());
|
||||
if (forward) {
|
||||
if (!procedureModel) return;
|
||||
procedureMap.delete(this.model.getId());
|
||||
} else {
|
||||
if (procedureModel) return;
|
||||
procedureMap.add(new ObservableProcedureModel(
|
||||
workspace, this.model.getName(), this.model.getId()));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Encode the event as JSON.
|
||||
*
|
||||
* @returns JSON representation.
|
||||
*/
|
||||
toJson(): ProcedureDeleteJson {
|
||||
return super.toJson() as ProcedureDeleteJson;
|
||||
}
|
||||
|
||||
/**
|
||||
* Deserializes the JSON event.
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
static fromJson(json: ProcedureDeleteJson, workspace: Workspace):
|
||||
ProcedureDelete {
|
||||
const model = workspace.getProcedureMap().get(json['procedureId']);
|
||||
if (!model) {
|
||||
throw new Error(
|
||||
'Cannot deserialize procedure delete event because the ' +
|
||||
'target procedure does not exist');
|
||||
}
|
||||
return new ProcedureDelete(workspace, model);
|
||||
}
|
||||
}
|
||||
|
||||
export interface ProcedureDeleteJson extends ProcedureBaseJson {}
|
||||
|
||||
registry.register(
|
||||
registry.Type.EVENT, eventUtils.PROCEDURE_DELETE, ProcedureDelete);
|
||||
@@ -1,77 +0,0 @@
|
||||
|
||||
/**
|
||||
* @license
|
||||
* Copyright 2022 Google LLC
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
import type {IProcedureModel} from '../interfaces/i_procedure_model.js';
|
||||
import * as registry from '../registry.js';
|
||||
import type {Workspace} from '../workspace.js';
|
||||
|
||||
import {ProcedureBase, ProcedureBaseJson} from './events_procedure_base.js';
|
||||
import * as eventUtils from './utils.js';
|
||||
|
||||
/**
|
||||
* Notifies listeners that the procedure data model has been enabled or
|
||||
* disabled.
|
||||
*/
|
||||
export class ProcedureEnable extends ProcedureBase {
|
||||
/** A string used to check the type of the event. */
|
||||
type = eventUtils.PROCEDURE_ENABLE;
|
||||
|
||||
private oldState: boolean;
|
||||
private newState: boolean;
|
||||
|
||||
constructor(workspace: Workspace, model: IProcedureModel) {
|
||||
super(workspace, model);
|
||||
|
||||
this.oldState = !model.getEnabled();
|
||||
this.newState = model.getEnabled();
|
||||
}
|
||||
|
||||
run(forward: boolean) {
|
||||
const procedureModel =
|
||||
this.getEventWorkspace_().getProcedureMap().get(this.model.getId());
|
||||
if (!procedureModel) {
|
||||
throw new Error(
|
||||
'Cannot change the enabled state of a procedure that does not ' +
|
||||
'exist in the procedure map');
|
||||
}
|
||||
if (forward) {
|
||||
procedureModel.setEnabled(this.newState);
|
||||
} else {
|
||||
procedureModel.setEnabled(this.oldState);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Encode the event as JSON.
|
||||
*
|
||||
* @returns JSON representation.
|
||||
*/
|
||||
toJson(): ProcedureEnableJson {
|
||||
return super.toJson() as ProcedureEnableJson;
|
||||
}
|
||||
|
||||
/**
|
||||
* Deserializes the JSON event.
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
static fromJson(json: ProcedureEnableJson, workspace: Workspace):
|
||||
ProcedureEnable {
|
||||
const model = workspace.getProcedureMap().get(json['procedureId']);
|
||||
if (!model) {
|
||||
throw new Error(
|
||||
'Cannot deserialize procedure enable event because the ' +
|
||||
'target procedure does not exist');
|
||||
}
|
||||
return new ProcedureEnable(workspace, model);
|
||||
}
|
||||
}
|
||||
|
||||
export interface ProcedureEnableJson extends ProcedureBaseJson {}
|
||||
|
||||
registry.register(
|
||||
registry.Type.EVENT, eventUtils.PROCEDURE_ENABLE, ProcedureEnable);
|
||||
@@ -1,44 +0,0 @@
|
||||
|
||||
/**
|
||||
* @license
|
||||
* Copyright 2022 Google LLC
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
import {IParameterModel} from '../interfaces/i_parameter_model.js';
|
||||
import {IProcedureModel} from '../interfaces/i_procedure_model.js';
|
||||
|
||||
import {ProcedureBase, ProcedureBaseJson} from './events_procedure_base.js';
|
||||
|
||||
import type {Workspace} from '../workspace.js';
|
||||
|
||||
/**
|
||||
* The base event for an event associated with a procedure parameter.
|
||||
*/
|
||||
export abstract class ProcedureParameterBase extends ProcedureBase {
|
||||
/**
|
||||
* @param workspace The workspace the parameter model exists in.
|
||||
* @param model The procedure model the parameter model belongs to.
|
||||
* @param parameter The parameter model associated with this event.
|
||||
*/
|
||||
constructor(
|
||||
workspace: Workspace, model: IProcedureModel,
|
||||
public readonly parameter: IParameterModel) {
|
||||
super(workspace, model);
|
||||
}
|
||||
|
||||
/**
|
||||
* Encode the event as JSON.
|
||||
*
|
||||
* @returns JSON representation.
|
||||
*/
|
||||
toJson(): ProcedureParameterBaseJson {
|
||||
const json = super.toJson() as ProcedureParameterBaseJson;
|
||||
json['parameterId'] = this.model.getId();
|
||||
return json;
|
||||
}
|
||||
}
|
||||
|
||||
export interface ProcedureParameterBaseJson extends ProcedureBaseJson {
|
||||
parameterId: string,
|
||||
}
|
||||
@@ -1,101 +0,0 @@
|
||||
/**
|
||||
* @license
|
||||
* Copyright 2022 Google LLC
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
import type {IParameterModel} from '../interfaces/i_parameter_model.js';
|
||||
import type {IProcedureModel} from '../interfaces/i_procedure_model.js';
|
||||
import {ObservableParameterModel} from '../procedures/observable_parameter_model.js';
|
||||
import * as registry from '../registry.js';
|
||||
import {loadParameter, ParameterState, saveParameter} from '../serialization/procedures.js';
|
||||
import type {Workspace} from '../workspace.js';
|
||||
|
||||
import {ProcedureParameterBase, ProcedureParameterBaseJson} from './events_procedure_parameter_base.js';
|
||||
import * as eventUtils from './utils.js';
|
||||
|
||||
|
||||
/**
|
||||
* Notifies listeners that a parameter has been added to a procedure model.
|
||||
*/
|
||||
export class ProcedureParameterCreate extends ProcedureParameterBase {
|
||||
/** A string used to check the type of the event. */
|
||||
type = eventUtils.PROCEDURE_PARAMETER_CREATE;
|
||||
|
||||
/**
|
||||
* @param parameter The parameter model that was just added to the procedure.
|
||||
* @param index The index the parameter was inserted at.
|
||||
*/
|
||||
constructor(
|
||||
workspace: Workspace, procedure: IProcedureModel,
|
||||
parameter: IParameterModel, public readonly index: number) {
|
||||
super(workspace, procedure, parameter);
|
||||
}
|
||||
|
||||
run(forward: boolean) {
|
||||
const workspace = this.getEventWorkspace_();
|
||||
const procedureMap = workspace.getProcedureMap();
|
||||
const procedureModel = procedureMap.get(this.model.getId());
|
||||
if (!procedureModel) {
|
||||
throw new Error(
|
||||
'Cannot add a parameter to a procedure that does not exist ' +
|
||||
'in the procedure map');
|
||||
}
|
||||
const parameterModel = procedureModel.getParameter(this.index);
|
||||
if (forward) {
|
||||
if (this.parameterMatches(parameterModel)) return;
|
||||
// TODO: This should just add the parameter instead of creating a dupe.
|
||||
procedureModel.insertParameter(
|
||||
new ObservableParameterModel(
|
||||
workspace, this.parameter.getName(), this.parameter.getId()),
|
||||
this.index);
|
||||
} else {
|
||||
if (!this.parameterMatches(parameterModel)) return;
|
||||
procedureModel.deleteParameter(this.index);
|
||||
}
|
||||
}
|
||||
|
||||
parameterMatches(param: IParameterModel) {
|
||||
return param && param.getId() === this.parameter.getId();
|
||||
}
|
||||
|
||||
/**
|
||||
* Encode the event as JSON.
|
||||
*
|
||||
* @returns JSON representation.
|
||||
*/
|
||||
toJson(): ProcedureParameterCreateJson {
|
||||
const json = super.toJson() as ProcedureParameterCreateJson;
|
||||
json['parameter'] = saveParameter(this.parameter);
|
||||
json['index'] = this.index;
|
||||
return json;
|
||||
}
|
||||
|
||||
/**
|
||||
* Deserializes the JSON event.
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
static fromJson(json: ProcedureParameterCreateJson, workspace: Workspace):
|
||||
ProcedureParameterCreate {
|
||||
const procedure = workspace.getProcedureMap().get(json['procedureId']);
|
||||
if (!procedure) {
|
||||
throw new Error(
|
||||
'Cannot deserialize parameter create event because the ' +
|
||||
'target procedure does not exist');
|
||||
}
|
||||
return new ProcedureParameterCreate(
|
||||
workspace, procedure,
|
||||
loadParameter(ObservableParameterModel, json['parameter'], workspace),
|
||||
json['index']);
|
||||
}
|
||||
}
|
||||
|
||||
export interface ProcedureParameterCreateJson extends
|
||||
ProcedureParameterBaseJson {
|
||||
parameter: ParameterState, index: number,
|
||||
}
|
||||
|
||||
registry.register(
|
||||
registry.Type.EVENT, eventUtils.PROCEDURE_PARAMETER_CREATE,
|
||||
ProcedureParameterCreate);
|
||||
@@ -1,97 +0,0 @@
|
||||
/**
|
||||
* @license
|
||||
* Copyright 2022 Google LLC
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
import type {IParameterModel} from '../interfaces/i_parameter_model.js';
|
||||
import type {IProcedureModel} from '../interfaces/i_procedure_model.js';
|
||||
import {ObservableParameterModel} from '../procedures/observable_parameter_model.js';
|
||||
import * as registry from '../registry.js';
|
||||
import type {Workspace} from '../workspace.js';
|
||||
|
||||
import {ProcedureParameterBase, ProcedureParameterBaseJson} from './events_procedure_parameter_base.js';
|
||||
import * as eventUtils from './utils.js';
|
||||
|
||||
/**
|
||||
* Notifies listeners that a parameter has been removed from a procedure.
|
||||
*/
|
||||
export class ProcedureParameterDelete extends ProcedureParameterBase {
|
||||
/** A string used to check the type of the event. */
|
||||
type = eventUtils.PROCEDURE_PARAMETER_DELETE;
|
||||
|
||||
/**
|
||||
* @param parameter The parameter model that was just removed from the
|
||||
* procedure.
|
||||
* @param index The index the parameter was at before it was removed.
|
||||
*/
|
||||
constructor(
|
||||
workspace: Workspace, procedure: IProcedureModel,
|
||||
parameter: IParameterModel, public readonly index: number) {
|
||||
super(workspace, procedure, parameter);
|
||||
}
|
||||
|
||||
run(forward: boolean) {
|
||||
const workspace = this.getEventWorkspace_();
|
||||
const procedureMap = workspace.getProcedureMap();
|
||||
const procedureModel = procedureMap.get(this.model.getId());
|
||||
if (!procedureModel) {
|
||||
throw new Error(
|
||||
'Cannot add a parameter to a procedure that does not exist ' +
|
||||
'in the procedure map');
|
||||
}
|
||||
const parameterModel = procedureModel.getParameter(this.index);
|
||||
if (forward) {
|
||||
if (!this.parameterMatches(parameterModel)) return;
|
||||
procedureModel.deleteParameter(this.index);
|
||||
} else {
|
||||
if (this.parameterMatches(parameterModel)) return;
|
||||
// TODO: this should just insert the model instead of creating a dupe.
|
||||
procedureModel.insertParameter(
|
||||
new ObservableParameterModel(
|
||||
workspace, this.parameter.getName(), this.parameter.getId()),
|
||||
this.index);
|
||||
}
|
||||
}
|
||||
|
||||
parameterMatches(param: IParameterModel) {
|
||||
return param && param.getId() === this.parameter.getId();
|
||||
}
|
||||
|
||||
/**
|
||||
* Encode the event as JSON.
|
||||
*
|
||||
* @returns JSON representation.
|
||||
*/
|
||||
toJson(): ProcedureParameterDeleteJson {
|
||||
const json = super.toJson() as ProcedureParameterDeleteJson;
|
||||
json['index'] = this.index;
|
||||
return json;
|
||||
}
|
||||
|
||||
/**
|
||||
* Deserializes the JSON event.
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
static fromJson(json: ProcedureParameterDeleteJson, workspace: Workspace):
|
||||
ProcedureParameterDelete {
|
||||
const model = workspace.getProcedureMap().get(json['procedureId']);
|
||||
if (!model) {
|
||||
throw new Error(
|
||||
'Cannot deserialize procedure delete event because the ' +
|
||||
'target procedure does not exist');
|
||||
}
|
||||
const param = model.getParameter(json['index']);
|
||||
return new ProcedureParameterDelete(workspace, model, param, json['index']);
|
||||
}
|
||||
}
|
||||
|
||||
export interface ProcedureParameterDeleteJson extends
|
||||
ProcedureParameterBaseJson {
|
||||
index: number;
|
||||
}
|
||||
|
||||
registry.register(
|
||||
registry.Type.EVENT, eventUtils.PROCEDURE_PARAMETER_DELETE,
|
||||
ProcedureParameterDelete);
|
||||
@@ -1,105 +0,0 @@
|
||||
/**
|
||||
* @license
|
||||
* Copyright 2022 Google LLC
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
import type {IParameterModel} from '../interfaces/i_parameter_model.js';
|
||||
import type {IProcedureModel} from '../interfaces/i_procedure_model.js';
|
||||
import * as registry from '../registry.js';
|
||||
import type {Workspace} from '../workspace.js';
|
||||
|
||||
import {ProcedureParameterBase, ProcedureParameterBaseJson} from './events_procedure_parameter_base.js';
|
||||
import * as eventUtils from './utils.js';
|
||||
|
||||
/**
|
||||
* Notifies listeners that a procedure parameter was renamed.
|
||||
*/
|
||||
export class ProcedureParameterRename extends ProcedureParameterBase {
|
||||
/** A string used to check the type of the event. */
|
||||
type = eventUtils.PROCEDURE_PARAMETER_RENAME;
|
||||
|
||||
/** The new name of the procedure parameter. */
|
||||
private readonly newName: string;
|
||||
|
||||
/** @param oldName The old name of the procedure parameter. */
|
||||
constructor(
|
||||
workspace: Workspace, procedure: IProcedureModel,
|
||||
parameter: IParameterModel, public readonly oldName: string) {
|
||||
super(workspace, procedure, parameter);
|
||||
|
||||
this.newName = parameter.getName();
|
||||
}
|
||||
|
||||
run(forward: boolean) {
|
||||
const parameterModel = findMatchingParameter(
|
||||
this.getEventWorkspace_(), this.model.getId(), this.parameter.getId());
|
||||
if (!parameterModel) {
|
||||
throw new Error(
|
||||
'Cannot rename a parameter that does not exist ' +
|
||||
'in the procedure map');
|
||||
}
|
||||
if (forward) {
|
||||
parameterModel.setName(this.newName);
|
||||
} else {
|
||||
parameterModel.setName(this.oldName);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Encode the event as JSON.
|
||||
*
|
||||
* @returns JSON representation.
|
||||
*/
|
||||
toJson(): ProcedureParameterRenameJson {
|
||||
const json = super.toJson() as ProcedureParameterRenameJson;
|
||||
json['oldName'] = this.oldName;
|
||||
return json;
|
||||
}
|
||||
|
||||
/**
|
||||
* Deserializes the JSON event.
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
static fromJson(json: ProcedureParameterRenameJson, workspace: Workspace):
|
||||
ProcedureParameterRename {
|
||||
const model = workspace.getProcedureMap().get(json['procedureId']);
|
||||
if (!model) {
|
||||
throw new Error(
|
||||
'Cannot deserialize procedure delete event because the ' +
|
||||
'target procedure does not exist');
|
||||
}
|
||||
const param = findMatchingParameter(
|
||||
workspace, json['procedureId'], json['parameterId']);
|
||||
if (!param) {
|
||||
throw new Error(
|
||||
'Cannot deserialize parameter rename event because the ' +
|
||||
'target parameter does not exist');
|
||||
}
|
||||
return new ProcedureParameterRename(
|
||||
workspace, model, param, json['oldName']);
|
||||
}
|
||||
}
|
||||
|
||||
function findMatchingParameter(
|
||||
workspace: Workspace, modelId: string, paramId: string): IParameterModel|
|
||||
undefined {
|
||||
const procedureModel = workspace.getProcedureMap().get(modelId);
|
||||
if (!procedureModel) {
|
||||
throw new Error(
|
||||
'Cannot rename the parameter of a procedure that does not exist ' +
|
||||
'in the procedure map');
|
||||
}
|
||||
return procedureModel.getParameters().find((p) => p.getId() === paramId);
|
||||
}
|
||||
|
||||
|
||||
export interface ProcedureParameterRenameJson extends
|
||||
ProcedureParameterBaseJson {
|
||||
oldName: string;
|
||||
}
|
||||
|
||||
registry.register(
|
||||
registry.Type.EVENT, eventUtils.PROCEDURE_PARAMETER_RENAME,
|
||||
ProcedureParameterRename);
|
||||
@@ -1,78 +0,0 @@
|
||||
/**
|
||||
* @license
|
||||
* Copyright 2022 Google LLC
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
import type {IProcedureModel} from '../interfaces/i_procedure_model.js';
|
||||
import * as registry from '../registry.js';
|
||||
import type {Workspace} from '../workspace.js';
|
||||
|
||||
import {ProcedureBase, ProcedureBaseJson} from './events_procedure_base.js';
|
||||
import * as eventUtils from './utils.js';
|
||||
|
||||
/** Notifies listeners that a procedure model has been renamed. */
|
||||
export class ProcedureRename extends ProcedureBase {
|
||||
/** A string used to check the type of the event. */
|
||||
type = eventUtils.PROCEDURE_RENAME;
|
||||
|
||||
private newName: string;
|
||||
|
||||
/** @param oldName The old name of the procedure model. */
|
||||
constructor(
|
||||
workspace: Workspace, model: IProcedureModel,
|
||||
public readonly oldName: string) {
|
||||
super(workspace, model);
|
||||
|
||||
this.newName = model.getName();
|
||||
}
|
||||
|
||||
run(forward: boolean) {
|
||||
const procedureModel =
|
||||
this.getEventWorkspace_().getProcedureMap().get(this.model.getId());
|
||||
if (!procedureModel) {
|
||||
throw new Error(
|
||||
'Cannot change the type of a procedure that does not exist ' +
|
||||
'in the procedure map');
|
||||
}
|
||||
if (forward) {
|
||||
procedureModel.setName(this.newName);
|
||||
} else {
|
||||
procedureModel.setName(this.oldName);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Encode the event as JSON.
|
||||
*
|
||||
* @returns JSON representation.
|
||||
*/
|
||||
toJson(): ProcedureRenameJson {
|
||||
const json = super.toJson() as ProcedureRenameJson;
|
||||
json['oldName'] = this.oldName;
|
||||
return json;
|
||||
}
|
||||
|
||||
/**
|
||||
* Deserializes the JSON event.
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
static fromJson(json: ProcedureRenameJson, workspace: Workspace):
|
||||
ProcedureRename {
|
||||
const model = workspace.getProcedureMap().get(json['procedureId']);
|
||||
if (!model) {
|
||||
throw new Error(
|
||||
'Cannot deserialize procedure rename event because the ' +
|
||||
'target procedure does not exist');
|
||||
}
|
||||
return new ProcedureRename(workspace, model, json['oldName']);
|
||||
}
|
||||
}
|
||||
|
||||
export interface ProcedureRenameJson extends ProcedureBaseJson {
|
||||
oldName: string;
|
||||
}
|
||||
|
||||
registry.register(
|
||||
registry.Type.EVENT, eventUtils.PROCEDURE_RENAME, ProcedureRename);
|
||||
@@ -240,30 +240,6 @@ export const COMMENT_MOVE = 'comment_move';
|
||||
*/
|
||||
export const FINISHED_LOADING = 'finished_loading';
|
||||
|
||||
/** Name of event that creates a procedure model. */
|
||||
export const PROCEDURE_CREATE = 'procedure_create';
|
||||
|
||||
/** Name of event that deletes a procedure model. */
|
||||
export const PROCEDURE_DELETE = 'procedure_delete';
|
||||
|
||||
/** Name of event that renames a procedure model. */
|
||||
export const PROCEDURE_RENAME = 'procedure_rename';
|
||||
|
||||
/** Name of event that enables/disables a procedure model. */
|
||||
export const PROCEDURE_ENABLE = 'procedure_enable';
|
||||
|
||||
/** Name of event that changes the returntype of a procedure model. */
|
||||
export const PROCEDURE_CHANGE_RETURN = 'procedure_change_return';
|
||||
|
||||
/** Name of event that creates a procedure parameter. */
|
||||
export const PROCEDURE_PARAMETER_CREATE = 'procedure_parameter_create';
|
||||
|
||||
/** Name of event that deletes a procedure parameter. */
|
||||
export const PROCEDURE_PARAMETER_DELETE = 'procedure_parameter_delete';
|
||||
|
||||
/** Name of event that renames a procedure parameter. */
|
||||
export const PROCEDURE_PARAMETER_RENAME = 'procedure_parameter_rename';
|
||||
|
||||
/**
|
||||
* Type of events that cause objects to be bumped back into the visible
|
||||
* portion of the workspace.
|
||||
|
||||
@@ -38,11 +38,6 @@ export class ObservableParameterModel implements IParameterModel {
|
||||
this.variable =
|
||||
this.workspace.getVariable(name) ?? this.workspace.createVariable(name);
|
||||
triggerProceduresUpdate(this.workspace);
|
||||
if (this.shouldFireEvents) {
|
||||
eventUtils.fire(
|
||||
new (eventUtils.get(eventUtils.PROCEDURE_PARAMETER_RENAME))(
|
||||
this.workspace, this.procedureModel, this, oldName));
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
@@ -24,8 +24,6 @@ export class ObservableProcedureMap extends
|
||||
override set(id: string, proc: IProcedureModel): this {
|
||||
if (this.get(id) === proc) return this;
|
||||
super.set(id, proc);
|
||||
eventUtils.fire(new (eventUtils.get(eventUtils.PROCEDURE_CREATE))(
|
||||
this.workspace, proc));
|
||||
if (isObservable(proc)) proc.startPublishing();
|
||||
return this;
|
||||
}
|
||||
@@ -39,8 +37,6 @@ export class ObservableProcedureMap extends
|
||||
const existed = super.delete(id);
|
||||
if (!existed) return existed;
|
||||
triggerProceduresUpdate(this.workspace);
|
||||
eventUtils.fire(new (eventUtils.get(eventUtils.PROCEDURE_DELETE))(
|
||||
this.workspace, proc));
|
||||
if (isObservable(proc)) proc.stopPublishing();
|
||||
return existed;
|
||||
}
|
||||
@@ -53,8 +49,6 @@ export class ObservableProcedureMap extends
|
||||
for (const id of this.keys()) {
|
||||
const proc = this.get(id);
|
||||
super.delete(id);
|
||||
eventUtils.fire(new (eventUtils.get(eventUtils.PROCEDURE_DELETE))(
|
||||
this.workspace, proc));
|
||||
}
|
||||
triggerProceduresUpdate(this.workspace);
|
||||
}
|
||||
|
||||
@@ -36,10 +36,6 @@ export class ObservableProcedureModel implements IProcedureModel {
|
||||
const prevName = this.name;
|
||||
this.name = name;
|
||||
if (this.shouldTriggerUpdates) triggerProceduresUpdate(this.workspace);
|
||||
if (this.shouldFireEvents) {
|
||||
eventUtils.fire(new (eventUtils.get(eventUtils.PROCEDURE_RENAME))(
|
||||
this.workspace, this, prevName));
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
@@ -65,11 +61,6 @@ export class ObservableProcedureModel implements IProcedureModel {
|
||||
}
|
||||
|
||||
if (this.shouldTriggerUpdates) triggerProceduresUpdate(this.workspace);
|
||||
if (this.shouldFireEvents) {
|
||||
eventUtils.fire(
|
||||
new (eventUtils.get(eventUtils.PROCEDURE_PARAMETER_CREATE))(
|
||||
this.workspace, this, parameterModel, index));
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
@@ -83,12 +74,6 @@ export class ObservableProcedureModel implements IProcedureModel {
|
||||
if (isObservable(oldParam)) {
|
||||
oldParam.stopPublishing();
|
||||
}
|
||||
|
||||
if (this.shouldFireEvents) {
|
||||
eventUtils.fire(
|
||||
new (eventUtils.get(eventUtils.PROCEDURE_PARAMETER_DELETE))(
|
||||
this.workspace, this, oldParam, index));
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
@@ -112,10 +97,6 @@ export class ObservableProcedureModel implements IProcedureModel {
|
||||
const oldReturnTypes = this.returnTypes;
|
||||
this.returnTypes = types;
|
||||
if (this.shouldTriggerUpdates) triggerProceduresUpdate(this.workspace);
|
||||
if (this.shouldFireEvents) {
|
||||
eventUtils.fire(new (eventUtils.get(eventUtils.PROCEDURE_CHANGE_RETURN))(
|
||||
this.workspace, this, oldReturnTypes));
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
@@ -127,10 +108,6 @@ export class ObservableProcedureModel implements IProcedureModel {
|
||||
if (enabled === this.enabled) return this;
|
||||
this.enabled = enabled;
|
||||
if (this.shouldTriggerUpdates) triggerProceduresUpdate(this.workspace);
|
||||
if (this.shouldFireEvents) {
|
||||
eventUtils.fire(new (eventUtils.get(eventUtils.PROCEDURE_ENABLE))(
|
||||
this.workspace, this));
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,198 +0,0 @@
|
||||
/**
|
||||
* @license
|
||||
* Copyright 2022 Google LLC
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
goog.declareModuleId('Blockly.test.eventProcedureChangeReturn');
|
||||
|
||||
import {assertEventFiredShallow, assertEventNotFired, createChangeListenerSpy} from './test_helpers/events.js';
|
||||
import {sharedTestSetup, sharedTestTeardown} from './test_helpers/setup_teardown.js';
|
||||
|
||||
|
||||
suite('Procedure Change Return Event', function() {
|
||||
const DEFAULT_TYPES = null;
|
||||
const NON_DEFAULT_TYPES = [];
|
||||
|
||||
setup(function() {
|
||||
sharedTestSetup.call(this);
|
||||
this.workspace = new Blockly.Workspace();
|
||||
this.procedureMap = this.workspace.getProcedureMap();
|
||||
this.eventSpy = createChangeListenerSpy(this.workspace);
|
||||
});
|
||||
|
||||
teardown(function() {
|
||||
sharedTestTeardown.call(this);
|
||||
});
|
||||
|
||||
suite('running', function() {
|
||||
setup(function() {
|
||||
this.createProcedureModel = (id) => {
|
||||
return new Blockly.procedures.ObservableProcedureModel(
|
||||
this.workspace, 'test name', id);
|
||||
};
|
||||
|
||||
this.createEventToState = (procedureModel) => {
|
||||
const event = new Blockly.Events.ProcedureChangeReturn(
|
||||
this.workspace,
|
||||
procedureModel,
|
||||
procedureModel.getReturnTypes() === DEFAULT_TYPES ?
|
||||
NON_DEFAULT_TYPES :
|
||||
DEFAULT_TYPES);
|
||||
return event;
|
||||
};
|
||||
});
|
||||
|
||||
suite('forward (redo)', function() {
|
||||
test('the procedure with the matching ID has its return set', function() {
|
||||
const initial = this.createProcedureModel('test id');
|
||||
const final = this.createProcedureModel('test id');
|
||||
final.setReturnTypes(NON_DEFAULT_TYPES);
|
||||
const event = this.createEventToState(final);
|
||||
this.procedureMap.add(initial);
|
||||
|
||||
event.run(/* forward= */ true);
|
||||
|
||||
chai.assert.equal(
|
||||
initial.getReturnTypes(),
|
||||
final.getReturnTypes(),
|
||||
"Expected the procedure's return type to be toggled");
|
||||
});
|
||||
|
||||
test('changing the return fires a change return event', function() {
|
||||
const initial = this.createProcedureModel('test id');
|
||||
const final = this.createProcedureModel('test id');
|
||||
final.setReturnTypes(NON_DEFAULT_TYPES);
|
||||
const event = this.createEventToState(final);
|
||||
this.procedureMap.add(initial);
|
||||
|
||||
this.eventSpy.resetHistory();
|
||||
event.run(/* forward= */ true);
|
||||
|
||||
assertEventFiredShallow(
|
||||
this.eventSpy,
|
||||
Blockly.Events.ProcedureChangeReturn,
|
||||
{
|
||||
model: initial,
|
||||
oldTypes: DEFAULT_TYPES,
|
||||
},
|
||||
this.workspace.id);
|
||||
});
|
||||
|
||||
test('noop return changes do not fire change return events', function() {
|
||||
const initial = this.createProcedureModel('test id');
|
||||
const final = this.createProcedureModel('test id');
|
||||
const event = this.createEventToState(final);
|
||||
this.procedureMap.add(initial);
|
||||
|
||||
this.eventSpy.resetHistory();
|
||||
event.run(/* forward= */ true);
|
||||
|
||||
assertEventNotFired(
|
||||
this.eventSpy,
|
||||
Blockly.Events.ProcedureChangeReturn,
|
||||
{},
|
||||
this.workspace.id);
|
||||
});
|
||||
|
||||
test(
|
||||
'attempting to change the return of a procedure that ' +
|
||||
'does not exist in the map throws',
|
||||
function() {
|
||||
const initial = this.createProcedureModel('test id');
|
||||
const final = this.createProcedureModel('test id');
|
||||
const event = this.createEventToState(final);
|
||||
|
||||
chai.assert.throws(() => {
|
||||
event.run(/* forward= */ true);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
suite('backward (undo)', function() {
|
||||
test('the procedure with the matching ID has its return set', function() {
|
||||
const initial = this.createProcedureModel('test id');
|
||||
const undoable = this.createProcedureModel('test id');
|
||||
initial.setReturnTypes(NON_DEFAULT_TYPES);
|
||||
undoable.setReturnTypes(NON_DEFAULT_TYPES);
|
||||
const event = this.createEventToState(undoable);
|
||||
this.procedureMap.add(initial);
|
||||
|
||||
event.run(/* forward= */ false);
|
||||
|
||||
chai.assert.equal(
|
||||
initial.getReturnTypes(),
|
||||
DEFAULT_TYPES,
|
||||
"Expected the procedure's return type to be toggled");
|
||||
});
|
||||
|
||||
test('changing the return fires a change return event', function() {
|
||||
const initial = this.createProcedureModel('test id');
|
||||
const undoable = this.createProcedureModel('test id');
|
||||
initial.setReturnTypes(NON_DEFAULT_TYPES);
|
||||
undoable.setReturnTypes(NON_DEFAULT_TYPES);
|
||||
const event = this.createEventToState(undoable);
|
||||
this.procedureMap.add(initial);
|
||||
|
||||
this.eventSpy.resetHistory();
|
||||
event.run(/* forward= */ false);
|
||||
|
||||
assertEventFiredShallow(
|
||||
this.eventSpy,
|
||||
Blockly.Events.ProcedureChangeReturn,
|
||||
{
|
||||
model: initial,
|
||||
oldTypes: NON_DEFAULT_TYPES,
|
||||
},
|
||||
this.workspace.id);
|
||||
});
|
||||
|
||||
test('noop return changes do not fire change return events', function() {
|
||||
const initial = this.createProcedureModel('test id');
|
||||
const undoable = this.createProcedureModel('test id');
|
||||
undoable.setReturnTypes(NON_DEFAULT_TYPES);
|
||||
const event = this.createEventToState(undoable);
|
||||
this.procedureMap.add(initial);
|
||||
|
||||
this.eventSpy.resetHistory();
|
||||
event.run(/* forward= */ false);
|
||||
|
||||
assertEventNotFired(
|
||||
this.eventSpy,
|
||||
Blockly.Events.ProcedureChangeReturn,
|
||||
{},
|
||||
this.workspace.id);
|
||||
});
|
||||
|
||||
test(
|
||||
'attempting to change the return of a procedure that ' +
|
||||
'does not exist throws',
|
||||
function() {
|
||||
const initial = this.createProcedureModel('test id');
|
||||
const undoable = this.createProcedureModel('test id');
|
||||
initial.setReturnTypes(NON_DEFAULT_TYPES);
|
||||
undoable.setReturnTypes(NON_DEFAULT_TYPES);
|
||||
const event = this.createEventToState(undoable);
|
||||
|
||||
chai.assert.throws(() => {
|
||||
event.run(/* forward= */ false);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
suite('serialization', function() {
|
||||
test('events round-trip through JSON', function() {
|
||||
const model = new Blockly.procedures.ObservableProcedureModel(
|
||||
this.workspace, 'test name', 'test id');
|
||||
this.procedureMap.add(model);
|
||||
const origEvent = new Blockly.Events.ProcedureChangeReturn(
|
||||
this.workspace, model, NON_DEFAULT_TYPES);
|
||||
|
||||
const json = origEvent.toJson();
|
||||
const newEvent = new Blockly.Events.fromJson(json, this.workspace);
|
||||
|
||||
chai.assert.deepEqual(newEvent, origEvent);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -1,161 +0,0 @@
|
||||
/**
|
||||
* @license
|
||||
* Copyright 2022 Google LLC
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
goog.declareModuleId('Blockly.test.eventProcedureCreate');
|
||||
|
||||
import {assertEventFiredShallow, assertEventNotFired, createChangeListenerSpy} from './test_helpers/events.js';
|
||||
import {sharedTestSetup, sharedTestTeardown} from './test_helpers/setup_teardown.js';
|
||||
|
||||
|
||||
suite('Procedure Create Event', function() {
|
||||
setup(function() {
|
||||
sharedTestSetup.call(this);
|
||||
this.workspace = new Blockly.Workspace();
|
||||
this.procedureMap = this.workspace.getProcedureMap();
|
||||
this.eventSpy = createChangeListenerSpy(this.workspace);
|
||||
});
|
||||
|
||||
teardown(function() {
|
||||
sharedTestTeardown.call(this);
|
||||
});
|
||||
|
||||
suite('running', function() {
|
||||
setup(function() {
|
||||
this.createProcedureModel = (name, id) => {
|
||||
return new Blockly.procedures.ObservableProcedureModel(
|
||||
this.workspace, name, id);
|
||||
};
|
||||
|
||||
this.createEventToState = (procedureModel) => {
|
||||
return new Blockly.Events.ProcedureCreate(this.workspace, procedureModel);
|
||||
};
|
||||
});
|
||||
|
||||
suite('forward', function() {
|
||||
test('a procedure model is created if it does not exist', function() {
|
||||
const model = this.createProcedureModel('test name', 'test id');
|
||||
const event = this.createEventToState(model);
|
||||
|
||||
event.run(/* forward= */ true);
|
||||
|
||||
const createdProc = this.procedureMap.get('test id');
|
||||
chai.assert.isDefined(createdProc, 'Expected the procedure to exist');
|
||||
chai.assert.equal(
|
||||
createdProc.getName(),
|
||||
model.getName(),
|
||||
"Expected the procedure's name to match the model");
|
||||
chai.assert.equal(
|
||||
createdProc.getId(),
|
||||
model.getId(),
|
||||
"Expected the procedure's id to match the model");
|
||||
});
|
||||
|
||||
test('creating a procedure model fires a create event', function() {
|
||||
const model = this.createProcedureModel('test name', 'test id');
|
||||
const event = this.createEventToState(model);
|
||||
|
||||
this.eventSpy.resetHistory();
|
||||
event.run(/* forward= */ true);
|
||||
|
||||
assertEventFiredShallow(
|
||||
this.eventSpy,
|
||||
Blockly.Events.ProcedureCreate,
|
||||
{model: this.procedureMap.get('test id')},
|
||||
this.workspace.id);
|
||||
});
|
||||
|
||||
test(
|
||||
'a procedure model is not created if a model with a ' +
|
||||
'matching ID exists',
|
||||
function() {
|
||||
const model = this.createProcedureModel('test name', 'test id');
|
||||
const event = this.createEventToState(model);
|
||||
this.procedureMap.add(model);
|
||||
|
||||
event.run(/* forward= */ true);
|
||||
|
||||
chai.assert.equal(
|
||||
this.procedureMap.get('test id'),
|
||||
model,
|
||||
'Expected the model in the procedure map to be the same ' +
|
||||
'as the original model');
|
||||
});
|
||||
|
||||
test('not creating a model does not fire a create event', function() {
|
||||
const model = this.createProcedureModel('test name', 'test id');
|
||||
const event = this.createEventToState(model);
|
||||
this.procedureMap.add(model);
|
||||
|
||||
this.eventSpy.resetHistory();
|
||||
event.run(/* forward= */ true);
|
||||
|
||||
assertEventNotFired(
|
||||
this.eventSpy,
|
||||
Blockly.Events.ProcedureCreate,
|
||||
{},
|
||||
this.workspace.id);
|
||||
});
|
||||
});
|
||||
|
||||
suite('backward', function() {
|
||||
test(
|
||||
'a procedure model is deleted if a model with a matching ID exists',
|
||||
function() {
|
||||
const model = this.createProcedureModel('test name', 'test id');
|
||||
const event = this.createEventToState(model);
|
||||
this.procedureMap.add(model);
|
||||
|
||||
event.run(/* forward= */ false);
|
||||
|
||||
chai.assert.isUndefined(
|
||||
this.procedureMap.get('test id'),
|
||||
'Expected the procedure to be deleted');
|
||||
});
|
||||
|
||||
test('deleting a model fires a delete event', function() {
|
||||
const model = this.createProcedureModel('test name', 'test id');
|
||||
const event = this.createEventToState(model);
|
||||
this.procedureMap.add(model);
|
||||
|
||||
this.eventSpy.resetHistory();
|
||||
event.run(/* forward= */ false);
|
||||
|
||||
assertEventFiredShallow(
|
||||
this.eventSpy,
|
||||
Blockly.Events.ProcedureDelete,
|
||||
{model},
|
||||
this.workspace.id);
|
||||
});
|
||||
|
||||
test('not deleting a model does not fire a delete event', function() {
|
||||
const model = this.createProcedureModel('test name', 'test id');
|
||||
const event = this.createEventToState(model);
|
||||
|
||||
this.eventSpy.resetHistory();
|
||||
event.run(/* forward= */ false);
|
||||
|
||||
assertEventNotFired(
|
||||
this.eventSpy,
|
||||
Blockly.Events.ProcedureDelete,
|
||||
{},
|
||||
this.workspace.id);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
suite('serialization', function() {
|
||||
test('events round-trip through JSON', function() {
|
||||
const model = new Blockly.procedures.ObservableProcedureModel(
|
||||
this.workspace, 'test name', 'test id');
|
||||
const origEvent = new Blockly.Events.ProcedureCreate(this.workspace, model);
|
||||
|
||||
const json = origEvent.toJson();
|
||||
const newEvent = new Blockly.Events.fromJson(json, this.workspace);
|
||||
|
||||
chai.assert.deepEqual(newEvent, origEvent);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -1,162 +0,0 @@
|
||||
/**
|
||||
* @license
|
||||
* Copyright 2022 Google LLC
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
goog.declareModuleId('Blockly.test.eventProcedureDelete');
|
||||
|
||||
import {assertEventFiredShallow, assertEventNotFired, createChangeListenerSpy} from './test_helpers/events.js';
|
||||
import {sharedTestSetup, sharedTestTeardown} from './test_helpers/setup_teardown.js';
|
||||
|
||||
|
||||
suite('Procedure Delete Event', function() {
|
||||
setup(function() {
|
||||
sharedTestSetup.call(this);
|
||||
this.workspace = new Blockly.Workspace();
|
||||
this.procedureMap = this.workspace.getProcedureMap();
|
||||
this.eventSpy = createChangeListenerSpy(this.workspace);
|
||||
});
|
||||
|
||||
teardown(function() {
|
||||
sharedTestTeardown.call(this);
|
||||
});
|
||||
|
||||
suite('running', function() {
|
||||
setup(function() {
|
||||
this.createProcedureModel = (name, id) => {
|
||||
return new Blockly.procedures.ObservableProcedureModel(
|
||||
this.workspace, name, id);
|
||||
};
|
||||
|
||||
this.createEventToState = (procedureModel) => {
|
||||
return new Blockly.Events.ProcedureDelete(this.workspace, procedureModel);
|
||||
};
|
||||
});
|
||||
|
||||
suite('forward', function() {
|
||||
test(
|
||||
'a procedure model is deleted if a model with a matching ID exists',
|
||||
function() {
|
||||
const model = this.createProcedureModel('test name', 'test id');
|
||||
const event = this.createEventToState(model);
|
||||
this.procedureMap.add(model);
|
||||
|
||||
event.run(/* forward= */ true);
|
||||
|
||||
chai.assert.isUndefined(
|
||||
this.procedureMap.get('test id'),
|
||||
'Expected the procedure to be deleted');
|
||||
});
|
||||
|
||||
test('deleting a model fires a delete event', function() {
|
||||
const model = this.createProcedureModel('test name', 'test id');
|
||||
const event = this.createEventToState(model);
|
||||
this.procedureMap.add(model);
|
||||
|
||||
this.eventSpy.resetHistory();
|
||||
event.run(/* forward= */ true);
|
||||
|
||||
assertEventFiredShallow(
|
||||
this.eventSpy,
|
||||
Blockly.Events.ProcedureDelete,
|
||||
{model},
|
||||
this.workspace.id);
|
||||
});
|
||||
|
||||
test('not deleting a model does not fire a delete event', function() {
|
||||
const model = this.createProcedureModel('test name', 'test id');
|
||||
const event = this.createEventToState(model);
|
||||
|
||||
this.eventSpy.resetHistory();
|
||||
event.run(/* forward= */ true);
|
||||
|
||||
assertEventNotFired(
|
||||
this.eventSpy,
|
||||
Blockly.Events.ProcedureDelete,
|
||||
{},
|
||||
this.workspace.id);
|
||||
});
|
||||
});
|
||||
|
||||
suite('backward', function() {
|
||||
test('a procedure model is created if it does not exist', function() {
|
||||
const model = this.createProcedureModel('test name', 'test id');
|
||||
const event = this.createEventToState(model);
|
||||
|
||||
event.run(/* forward= */ false);
|
||||
|
||||
const createdProc = this.procedureMap.get('test id');
|
||||
chai.assert.isDefined(createdProc, 'Expected the procedure to exist');
|
||||
chai.assert.equal(
|
||||
createdProc.getName(),
|
||||
model.getName(),
|
||||
"Expected the procedure's name to match the model");
|
||||
chai.assert.equal(
|
||||
createdProc.getId(),
|
||||
model.getId(),
|
||||
"Expected the procedure's id to match the model");
|
||||
});
|
||||
|
||||
test('creating a procedure model fires a create event', function() {
|
||||
const model = this.createProcedureModel('test name', 'test id');
|
||||
const event = this.createEventToState(model);
|
||||
|
||||
this.eventSpy.resetHistory();
|
||||
event.run(/* forward= */ false);
|
||||
|
||||
assertEventFiredShallow(
|
||||
this.eventSpy,
|
||||
Blockly.Events.ProcedureCreate,
|
||||
{model: this.procedureMap.get('test id')},
|
||||
this.workspace.id);
|
||||
});
|
||||
|
||||
test(
|
||||
'a procedure model is not created if a model with a ' +
|
||||
'matching ID exists',
|
||||
function() {
|
||||
const model = this.createProcedureModel('test name', 'test id');
|
||||
const event = this.createEventToState(model);
|
||||
this.procedureMap.add(model);
|
||||
|
||||
event.run(/* forward= */ false);
|
||||
|
||||
chai.assert.equal(
|
||||
this.procedureMap.get('test id'),
|
||||
model,
|
||||
'Expected the model in the procedure map to be the same ' +
|
||||
'as the original model');
|
||||
});
|
||||
|
||||
test('not creating a model does not fire a create event', function() {
|
||||
const model = this.createProcedureModel('test name', 'test id');
|
||||
const event = this.createEventToState(model);
|
||||
this.procedureMap.add(model);
|
||||
|
||||
this.eventSpy.resetHistory();
|
||||
event.run(/* forward= */ true);
|
||||
|
||||
assertEventNotFired(
|
||||
this.eventSpy,
|
||||
Blockly.Events.ProcedureCreate,
|
||||
{},
|
||||
this.workspace.id);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
suite('serialization', function() {
|
||||
test('events round-trip through JSON', function() {
|
||||
const model = new Blockly.procedures.ObservableProcedureModel(
|
||||
this.workspace, 'test name', 'test id');
|
||||
this.procedureMap.add(model);
|
||||
const origEvent = new Blockly.Events.ProcedureDelete(this.workspace, model);
|
||||
|
||||
const json = origEvent.toJson();
|
||||
const newEvent = new Blockly.Events.fromJson(json, this.workspace);
|
||||
|
||||
chai.assert.deepEqual(newEvent, origEvent);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -1,188 +0,0 @@
|
||||
/**
|
||||
* @license
|
||||
* Copyright 2022 Google LLC
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
goog.declareModuleId('Blockly.test.eventProcedureEnable');
|
||||
|
||||
import {assertEventFiredShallow, assertEventNotFired, createChangeListenerSpy} from './test_helpers/events.js';
|
||||
import {sharedTestSetup, sharedTestTeardown} from './test_helpers/setup_teardown.js';
|
||||
|
||||
|
||||
suite('Procedure Enable Event', function() {
|
||||
setup(function() {
|
||||
sharedTestSetup.call(this);
|
||||
this.workspace = new Blockly.Workspace();
|
||||
this.procedureMap = this.workspace.getProcedureMap();
|
||||
this.eventSpy = createChangeListenerSpy(this.workspace);
|
||||
});
|
||||
|
||||
teardown(function() {
|
||||
sharedTestTeardown.call(this);
|
||||
});
|
||||
|
||||
suite('running', function() {
|
||||
setup(function() {
|
||||
this.createProcedureModel = (id) => {
|
||||
return new Blockly.procedures.ObservableProcedureModel(
|
||||
this.workspace, 'test name', id);
|
||||
};
|
||||
|
||||
this.createEventToState = (procedureModel) => {
|
||||
return new Blockly.Events.ProcedureEnable(this.workspace, procedureModel);
|
||||
};
|
||||
});
|
||||
|
||||
suite('forward', function() {
|
||||
test('the procedure with the matching ID is toggled', function() {
|
||||
const initial = this.createProcedureModel('test id');
|
||||
const final = this.createProcedureModel('test id');
|
||||
final.setEnabled(!final.getEnabled()); // Set it to the non-default.
|
||||
const event = this.createEventToState(final);
|
||||
this.procedureMap.add(initial);
|
||||
|
||||
event.run(/* forward= */ true);
|
||||
|
||||
chai.assert.equal(
|
||||
initial.getEnabled(),
|
||||
final.getEnabled(),
|
||||
"Expected the procedure's enabled state to be flipped");
|
||||
});
|
||||
|
||||
test('toggling a procedure fires an enable event', function() {
|
||||
const initial = this.createProcedureModel('test id');
|
||||
const final = this.createProcedureModel('test id');
|
||||
final.setEnabled(!final.getEnabled()); // Set it to the non-default.
|
||||
const event = this.createEventToState(final);
|
||||
this.procedureMap.add(initial);
|
||||
|
||||
this.eventSpy.resetHistory();
|
||||
event.run(/* forward= */ true);
|
||||
|
||||
assertEventFiredShallow(
|
||||
this.eventSpy,
|
||||
Blockly.Events.ProcedureEnable,
|
||||
{model: initial},
|
||||
this.workspace.id);
|
||||
});
|
||||
|
||||
test('noop toggles do not fire enable events', function() {
|
||||
const initial = this.createProcedureModel('test id');
|
||||
const final = this.createProcedureModel('test id');
|
||||
const event = this.createEventToState(final);
|
||||
this.procedureMap.add(initial);
|
||||
|
||||
this.eventSpy.resetHistory();
|
||||
event.run(/* forward= */ true);
|
||||
|
||||
assertEventNotFired(
|
||||
this.eventSpy,
|
||||
Blockly.Events.ProcedureEnable,
|
||||
this.workspace.id);
|
||||
});
|
||||
|
||||
test(
|
||||
'attempting to toggle a procedure that does not exist throws',
|
||||
function() {
|
||||
const initial = this.createProcedureModel('test id');
|
||||
const final = this.createProcedureModel('test id');
|
||||
final.setEnabled(!final.getEnabled()); // Set it to the non-default.
|
||||
const event = this.createEventToState(final);
|
||||
|
||||
chai.assert.throws(() => {
|
||||
event.run(/* forward= */ true);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
suite('backward', function() {
|
||||
test('the procedure with the matching ID is toggled', function() {
|
||||
const initial = this.createProcedureModel('test id');
|
||||
const undoable = this.createProcedureModel('test id');
|
||||
// Set them to be non-default.
|
||||
const defaultEnabled = initial.getEnabled();
|
||||
initial.setEnabled(!defaultEnabled);
|
||||
undoable.setEnabled(!defaultEnabled);
|
||||
const event = this.createEventToState(undoable);
|
||||
this.procedureMap.add(initial);
|
||||
|
||||
event.run(/* forward= */ false);
|
||||
|
||||
chai.assert.equal(
|
||||
initial.getEnabled(),
|
||||
defaultEnabled,
|
||||
"Expected the procedure's enabled state to be flipped");
|
||||
});
|
||||
|
||||
test('toggling a procedure fires an enable event', function() {
|
||||
const initial = this.createProcedureModel('test id');
|
||||
const undoable = this.createProcedureModel('test id');
|
||||
// Set them to be non-default.
|
||||
const defaultEnabled = initial.getEnabled();
|
||||
initial.setEnabled(!defaultEnabled);
|
||||
undoable.setEnabled(!defaultEnabled);
|
||||
const event = this.createEventToState(undoable);
|
||||
this.procedureMap.add(initial);
|
||||
|
||||
this.eventSpy.resetHistory();
|
||||
event.run(/* forward= */ false);
|
||||
|
||||
assertEventFiredShallow(
|
||||
this.eventSpy,
|
||||
Blockly.Events.ProcedureEnable,
|
||||
{model: initial},
|
||||
this.workspace.id);
|
||||
});
|
||||
|
||||
test('noop toggles do not fire enable events', function() {
|
||||
const initial = this.createProcedureModel('test id');
|
||||
const undoable = this.createProcedureModel('test id');
|
||||
// Set them to be non-default.
|
||||
const defaultEnabled = initial.getEnabled();
|
||||
undoable.setEnabled(!defaultEnabled);
|
||||
const event = this.createEventToState(undoable);
|
||||
this.procedureMap.add(initial);
|
||||
|
||||
this.eventSpy.resetHistory();
|
||||
event.run(/* forward= */ false);
|
||||
|
||||
assertEventNotFired(
|
||||
this.eventSpy,
|
||||
Blockly.Events.ProcedureEnable,
|
||||
{},
|
||||
this.workspace.id);
|
||||
});
|
||||
|
||||
test(
|
||||
'attempting to toggle a procedure that does not exist throws',
|
||||
function() {
|
||||
const initial = this.createProcedureModel('test id');
|
||||
const undoable = this.createProcedureModel('test id');
|
||||
// Set them to be non-default.
|
||||
const defaultEnabled = initial.getEnabled();
|
||||
initial.setEnabled(!defaultEnabled);
|
||||
undoable.setEnabled(!defaultEnabled);
|
||||
const event = this.createEventToState(undoable);
|
||||
|
||||
chai.assert.throws(() => {
|
||||
event.run(/* forward= */ false);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
suite('serialization', function() {
|
||||
test('events round-trip through JSON', function() {
|
||||
const model = new Blockly.procedures.ObservableProcedureModel(
|
||||
this.workspace, 'test name', 'test id');
|
||||
this.procedureMap.add(model);
|
||||
const origEvent = new Blockly.Events.ProcedureEnable(this.workspace, model);
|
||||
|
||||
const json = origEvent.toJson();
|
||||
const newEvent = new Blockly.Events.fromJson(json, this.workspace);
|
||||
|
||||
chai.assert.deepEqual(newEvent, origEvent);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -1,217 +0,0 @@
|
||||
/**
|
||||
* @license
|
||||
* Copyright 2022 Google LLC
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
goog.declareModuleId('Blockly.test.eventProcedureParameterCreate');
|
||||
|
||||
import {assertEventFiredShallow, assertEventNotFired, createChangeListenerSpy} from './test_helpers/events.js';
|
||||
import {sharedTestSetup, sharedTestTeardown} from './test_helpers/setup_teardown.js';
|
||||
|
||||
|
||||
suite('Procedure Parameter Create Event', function() {
|
||||
setup(function() {
|
||||
sharedTestSetup.call(this);
|
||||
this.workspace = new Blockly.Workspace();
|
||||
this.procedureMap = this.workspace.getProcedureMap();
|
||||
this.eventSpy = createChangeListenerSpy(this.workspace);
|
||||
});
|
||||
|
||||
teardown(function() {
|
||||
sharedTestTeardown.call(this);
|
||||
});
|
||||
|
||||
suite('running', function() {
|
||||
setup(function() {
|
||||
this.createProcedureModel = (name, id) => {
|
||||
return new Blockly.procedures.ObservableProcedureModel(
|
||||
this.workspace, name, id);
|
||||
};
|
||||
|
||||
this.createProcedureAndParameter =
|
||||
(procName, procId, paramName, paramId) => {
|
||||
const param = new Blockly.procedures.ObservableParameterModel(
|
||||
this.workspace, paramName, paramId);
|
||||
const proc = new Blockly.procedures.ObservableProcedureModel(
|
||||
this.workspace, procName, procId)
|
||||
.insertParameter(param, 0);
|
||||
return {param, proc};
|
||||
};
|
||||
|
||||
this.createEventToState = (procedureModel, parameterModel) => {
|
||||
return new Blockly.Events.ProcedureParameterCreate(
|
||||
this.workspace, procedureModel, parameterModel, 0);
|
||||
};
|
||||
});
|
||||
|
||||
suite('forward', function() {
|
||||
test('a parameter is inserted if it does not exist', function() {
|
||||
const {param: modelParam, proc: modelProc} =
|
||||
this.createProcedureAndParameter(
|
||||
'test name', 'test id', 'test param name', 'test param id');
|
||||
const event = this.createEventToState(modelProc, modelParam);
|
||||
const actualProc = this.createProcedureModel('test name', 'test id');
|
||||
this.procedureMap.add(actualProc);
|
||||
|
||||
event.run(/* forward= */ true);
|
||||
|
||||
const createdParam = actualProc.getParameter(0);
|
||||
chai.assert.isDefined(createdParam, 'Expected the parameter to exist');
|
||||
chai.assert.equal(
|
||||
createdParam.getName(),
|
||||
modelParam.getName(),
|
||||
"Expected the parameter's name to match the model");
|
||||
chai.assert.equal(
|
||||
createdParam.getId(),
|
||||
modelParam.getId(),
|
||||
"Expected the parameter's id to match the model");
|
||||
});
|
||||
|
||||
test('inserting a parameter fires a create event', function() {
|
||||
const {param: modelParam, proc: modelProc} =
|
||||
this.createProcedureAndParameter(
|
||||
'test name', 'test id', 'test param name', 'test param id');
|
||||
const event = this.createEventToState(modelProc, modelParam);
|
||||
const actualProc = this.createProcedureModel('test name', 'test id');
|
||||
this.procedureMap.add(actualProc);
|
||||
|
||||
this.eventSpy.resetHistory();
|
||||
event.run(/* forward= */ true);
|
||||
|
||||
assertEventFiredShallow(
|
||||
this.eventSpy,
|
||||
Blockly.Events.ProcedureParameterCreate,
|
||||
{
|
||||
model: actualProc,
|
||||
parameter: actualProc.getParameter(0),
|
||||
index: 0,
|
||||
},
|
||||
this.workspace.id);
|
||||
});
|
||||
|
||||
test(
|
||||
'a parameter is not created if a parameter with a ' +
|
||||
'matching ID and index already exists',
|
||||
function() {
|
||||
const {param: modelParam, proc: modelProc} =
|
||||
this.createProcedureAndParameter(
|
||||
'test name', 'test id', 'test param name', 'test param id');
|
||||
const event = this.createEventToState(modelProc, modelParam);
|
||||
this.procedureMap.add(modelProc);
|
||||
|
||||
this.eventSpy.resetHistory();
|
||||
event.run(/* forward= */ true);
|
||||
|
||||
const actualProc = this.procedureMap.get('test id');
|
||||
chai.assert.equal(
|
||||
actualProc,
|
||||
modelProc,
|
||||
'Expected the procedure in the procedure map to not have changed');
|
||||
chai.assert.equal(
|
||||
actualProc.getParameter(0),
|
||||
modelParam,
|
||||
'Expected the parameter to not have changed');
|
||||
});
|
||||
|
||||
test(
|
||||
'not creating a parameter model does not fire a create event',
|
||||
function() {
|
||||
const {param: modelParam, proc: modelProc} =
|
||||
this.createProcedureAndParameter(
|
||||
'test name', 'test id', 'test param name', 'test param id');
|
||||
const event = this.createEventToState(modelProc, modelParam);
|
||||
this.procedureMap.add(modelProc);
|
||||
|
||||
this.eventSpy.resetHistory();
|
||||
event.run(/* forward= */ true);
|
||||
|
||||
assertEventNotFired(
|
||||
this.eventSpy,
|
||||
Blockly.Events.ProcedureParameterCreate,
|
||||
{},
|
||||
this.workspace.id);
|
||||
});
|
||||
});
|
||||
|
||||
suite('backward', function() {
|
||||
test('a parameter is removed if it exists', function() {
|
||||
const {param, proc} =
|
||||
this.createProcedureAndParameter(
|
||||
'test name', 'test id', 'test param name', 'test param id');
|
||||
const event = this.createEventToState(proc, param);
|
||||
this.procedureMap.add(proc);
|
||||
|
||||
event.run(/* forward= */ false);
|
||||
|
||||
chai.assert.isUndefined(
|
||||
proc.getParameter(0),
|
||||
'Expected the parameter to be deleted');
|
||||
});
|
||||
|
||||
test('removing a parameter fires a delete event', function() {
|
||||
const {param, proc} =
|
||||
this.createProcedureAndParameter(
|
||||
'test name', 'test id', 'test param name', 'test param id');
|
||||
const event = this.createEventToState(proc, param);
|
||||
this.procedureMap.add(proc);
|
||||
|
||||
this.eventSpy.resetHistory();
|
||||
event.run(/* forward= */ false);
|
||||
|
||||
assertEventFiredShallow(
|
||||
this.eventSpy,
|
||||
Blockly.Events.ProcedureParameterDelete,
|
||||
{
|
||||
model: proc,
|
||||
parameter: param,
|
||||
index: 0,
|
||||
},
|
||||
this.workspace.id);
|
||||
});
|
||||
|
||||
test(
|
||||
'a parameter is not deleted if a parameter with a ' +
|
||||
'matching ID and index does not exist',
|
||||
function() {
|
||||
// TODO: Figure out what we want to do in this case.
|
||||
});
|
||||
|
||||
test('not removing a parameter does not fire a delete event', function() {
|
||||
const {param, proc} =
|
||||
this.createProcedureAndParameter(
|
||||
'test name', 'test id', 'test param name', 'test param id');
|
||||
const event = this.createEventToState(proc, param);
|
||||
this.procedureMap.add(proc);
|
||||
proc.deleteParameter(0);
|
||||
|
||||
this.eventSpy.resetHistory();
|
||||
event.run(/* forward= */ false);
|
||||
|
||||
assertEventNotFired(
|
||||
this.eventSpy,
|
||||
Blockly.Events.ProcedureParameterDelete,
|
||||
{},
|
||||
this.workspace.id);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
suite('serialization', function() {
|
||||
test('events round-trip through JSON', function() {
|
||||
const param = new Blockly.procedures.ObservableParameterModel(
|
||||
this.workspace, 'test param name', 'test param id');
|
||||
const model =
|
||||
new Blockly.procedures.ObservableProcedureModel(
|
||||
this.workspace, 'test name', 'test id');
|
||||
this.procedureMap.add(model);
|
||||
const origEvent = new Blockly.Events.ProcedureParameterCreate(
|
||||
this.workspace, model, param, 0);
|
||||
|
||||
const json = origEvent.toJson();
|
||||
const newEvent = new Blockly.Events.fromJson(json, this.workspace);
|
||||
|
||||
chai.assert.deepEqual(newEvent, origEvent);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -1,218 +0,0 @@
|
||||
/**
|
||||
* @license
|
||||
* Copyright 2022 Google LLC
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
goog.declareModuleId('Blockly.test.eventProcedureParameterDelete');
|
||||
|
||||
import {assertEventFiredShallow, assertEventNotFired, createChangeListenerSpy} from './test_helpers/events.js';
|
||||
import {sharedTestSetup, sharedTestTeardown} from './test_helpers/setup_teardown.js';
|
||||
|
||||
|
||||
suite('Procedure Parameter Delete Event', function() {
|
||||
setup(function() {
|
||||
sharedTestSetup.call(this);
|
||||
this.workspace = new Blockly.Workspace();
|
||||
this.procedureMap = this.workspace.getProcedureMap();
|
||||
this.eventSpy = createChangeListenerSpy(this.workspace);
|
||||
});
|
||||
|
||||
teardown(function() {
|
||||
sharedTestTeardown.call(this);
|
||||
});
|
||||
|
||||
suite('running', function() {
|
||||
setup(function() {
|
||||
this.createProcedureModel = (name, id) => {
|
||||
return new Blockly.procedures.ObservableProcedureModel(
|
||||
this.workspace, name, id);
|
||||
};
|
||||
|
||||
this.createProcedureAndParameter =
|
||||
(procName, procId, paramName, paramId) => {
|
||||
const param = new Blockly.procedures.ObservableParameterModel(
|
||||
this.workspace, procName, paramId);
|
||||
const proc = new Blockly.procedures.ObservableProcedureModel(
|
||||
this.workspace, paramName, procId)
|
||||
.insertParameter(param, 0);
|
||||
return {param, proc};
|
||||
};
|
||||
|
||||
this.createEventToState = (procedureModel, parameterModel) => {
|
||||
return new Blockly.Events.ProcedureParameterDelete(
|
||||
this.workspace, procedureModel, parameterModel, 0);
|
||||
};
|
||||
});
|
||||
|
||||
suite('forward', function() {
|
||||
test('a parameter is removed if it exists', function() {
|
||||
const {param, proc} =
|
||||
this.createProcedureAndParameter(
|
||||
'test name', 'test id', 'test param name', 'test param id');
|
||||
const event = this.createEventToState(proc, param);
|
||||
this.procedureMap.add(proc);
|
||||
|
||||
event.run(/* forward= */ true);
|
||||
|
||||
chai.assert.isUndefined(
|
||||
proc.getParameter(0),
|
||||
'Expected the parameter to be deleted');
|
||||
});
|
||||
|
||||
test('removing a parameter fires a delete event', function() {
|
||||
const {param, proc} =
|
||||
this.createProcedureAndParameter(
|
||||
'test name', 'test id', 'test param name', 'test param id');
|
||||
const event = this.createEventToState(proc, param);
|
||||
this.procedureMap.add(proc);
|
||||
|
||||
this.eventSpy.resetHistory();
|
||||
event.run(/* forward= */ true);
|
||||
|
||||
assertEventFiredShallow(
|
||||
this.eventSpy,
|
||||
Blockly.Events.ProcedureParameterDelete,
|
||||
{
|
||||
model: proc,
|
||||
parameter: param,
|
||||
index: 0,
|
||||
},
|
||||
this.workspace.id);
|
||||
});
|
||||
|
||||
test(
|
||||
'a parameter is not deleted if a parameter with a ' +
|
||||
'matching ID and index does not exist',
|
||||
function() {
|
||||
// TODO: Figure out what we want to do in this case.
|
||||
});
|
||||
|
||||
test('not removing a parameter does not fire a delete event', function() {
|
||||
const {param, proc} =
|
||||
this.createProcedureAndParameter(
|
||||
'test name', 'test id', 'test param name', 'test param id');
|
||||
const event = this.createEventToState(proc, param);
|
||||
this.procedureMap.add(proc);
|
||||
proc.deleteParameter(0);
|
||||
|
||||
this.eventSpy.resetHistory();
|
||||
event.run(/* forward= */ false);
|
||||
|
||||
assertEventNotFired(
|
||||
this.eventSpy,
|
||||
Blockly.Events.ProcedureParameterDelete,
|
||||
{},
|
||||
this.workspace.id);
|
||||
});
|
||||
});
|
||||
|
||||
suite('backward', function() {
|
||||
test('a parameter is inserted if it does not exist', function() {
|
||||
const {param: modelParam, proc: modelProc} =
|
||||
this.createProcedureAndParameter(
|
||||
'test name', 'test id', 'test param name', 'test param id');
|
||||
const event = this.createEventToState(modelProc, modelParam);
|
||||
const actualProc = this.createProcedureModel('test name', 'test id');
|
||||
this.procedureMap.add(actualProc);
|
||||
|
||||
event.run(/* forward= */ false);
|
||||
|
||||
const createdParam = actualProc.getParameter(0);
|
||||
chai.assert.isDefined(createdParam, 'Expected the parameter to exist');
|
||||
chai.assert.equal(
|
||||
createdParam.getName(),
|
||||
modelParam.getName(),
|
||||
"Expected the parameter's name to match the model");
|
||||
chai.assert.equal(
|
||||
createdParam.getId(),
|
||||
modelParam.getId(),
|
||||
"Expected the parameter's id to match the model");
|
||||
});
|
||||
|
||||
test('inserting a parameter fires a create event', function() {
|
||||
const {param: modelParam, proc: modelProc} =
|
||||
this.createProcedureAndParameter(
|
||||
'test name', 'test id', 'test param name', 'test param id');
|
||||
const event = this.createEventToState(modelProc, modelParam);
|
||||
const actualProc = this.createProcedureModel('test name', 'test id');
|
||||
this.procedureMap.add(actualProc);
|
||||
|
||||
this.eventSpy.resetHistory();
|
||||
event.run(/* forward= */ false);
|
||||
|
||||
assertEventFiredShallow(
|
||||
this.eventSpy,
|
||||
Blockly.Events.ProcedureParameterCreate,
|
||||
{
|
||||
model: actualProc,
|
||||
parameter: actualProc.getParameter(0),
|
||||
index: 0,
|
||||
},
|
||||
this.workspace.id);
|
||||
});
|
||||
|
||||
test(
|
||||
'a parameter is not created if a parameter with a ' +
|
||||
'matching ID and index already exists',
|
||||
function() {
|
||||
const {param: modelParam, proc: modelProc} =
|
||||
this.createProcedureAndParameter(
|
||||
'test name', 'test id', 'test param name', 'test param id');
|
||||
const event = this.createEventToState(modelProc, modelParam);
|
||||
this.procedureMap.add(modelProc);
|
||||
|
||||
this.eventSpy.resetHistory();
|
||||
event.run(/* forward= */ false);
|
||||
|
||||
const actualProc = this.procedureMap.get('test id');
|
||||
chai.assert.equal(
|
||||
actualProc,
|
||||
modelProc,
|
||||
'Expected the procedure in the procedure map to not have changed');
|
||||
chai.assert.equal(
|
||||
actualProc.getParameter(0),
|
||||
modelParam,
|
||||
'Expected the parameter to not have changed');
|
||||
});
|
||||
|
||||
test(
|
||||
'not creating a parameter model does not fire a create event',
|
||||
function() {
|
||||
const {param: modelParam, proc: modelProc} =
|
||||
this.createProcedureAndParameter(
|
||||
'test name', 'test id', 'test param name', 'test param id');
|
||||
const event = this.createEventToState(modelProc, modelParam);
|
||||
this.procedureMap.add(modelProc);
|
||||
|
||||
this.eventSpy.resetHistory();
|
||||
event.run(/* forward= */ false);
|
||||
|
||||
assertEventNotFired(
|
||||
this.eventSpy,
|
||||
Blockly.Events.ProcedureParameterCreate,
|
||||
{},
|
||||
this.workspace.id);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
suite('serialization', function() {
|
||||
test('events round-trip through JSON', function() {
|
||||
const param = new Blockly.procedures.ObservableParameterModel(
|
||||
this.workspace, 'test param name', 'test param id');
|
||||
const model =
|
||||
new Blockly.procedures.ObservableProcedureModel(
|
||||
this.workspace, 'test name', 'test id')
|
||||
.insertParameter(param, 0);
|
||||
this.procedureMap.add(model);
|
||||
const origEvent = new Blockly.Events.ProcedureParameterDelete(
|
||||
this.workspace, model);
|
||||
|
||||
const json = origEvent.toJson();
|
||||
const newEvent = new Blockly.Events.fromJson(json, this.workspace);
|
||||
|
||||
chai.assert.deepEqual(newEvent, origEvent);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -1,225 +0,0 @@
|
||||
/**
|
||||
* @license
|
||||
* Copyright 2022 Google LLC
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
goog.declareModuleId('Blockly.test.eventProcedureParameterRename');
|
||||
|
||||
import {assertEventFiredShallow, assertEventNotFired, createChangeListenerSpy} from './test_helpers/events.js';
|
||||
import {sharedTestSetup, sharedTestTeardown} from './test_helpers/setup_teardown.js';
|
||||
|
||||
|
||||
suite('Procedure Parameter Rename Event', function() {
|
||||
setup(function() {
|
||||
sharedTestSetup.call(this);
|
||||
this.workspace = new Blockly.Workspace();
|
||||
this.procedureMap = this.workspace.getProcedureMap();
|
||||
this.eventSpy = createChangeListenerSpy(this.workspace);
|
||||
});
|
||||
|
||||
teardown(function() {
|
||||
sharedTestTeardown.call(this);
|
||||
});
|
||||
|
||||
suite('running', function() {
|
||||
const DEFAULT_NAME = 'default';
|
||||
const NON_DEFAULT_NAME = 'non-default';
|
||||
|
||||
setup(function() {
|
||||
this.createProcedureAndParameter = (procId, paramId) => {
|
||||
const param = new Blockly.procedures.ObservableParameterModel(
|
||||
this.workspace, DEFAULT_NAME, paramId);
|
||||
const proc = new Blockly.procedures.ObservableProcedureModel(
|
||||
this.workspace, 'test name', procId)
|
||||
.insertParameter(param, 0);
|
||||
return {param, proc};
|
||||
};
|
||||
|
||||
this.createEventToState = (procedureModel, parameterModel) => {
|
||||
return new Blockly.Events.ProcedureParameterRename(
|
||||
this.workspace,
|
||||
procedureModel,
|
||||
parameterModel,
|
||||
parameterModel.getName() === DEFAULT_NAME ?
|
||||
NON_DEFAULT_NAME :
|
||||
DEFAULT_NAME);
|
||||
};
|
||||
});
|
||||
|
||||
suite('forward', function() {
|
||||
test('the parameter with the matching ID and index is renamed', function() {
|
||||
const {param: initialParam, proc: initialProc} =
|
||||
this.createProcedureAndParameter('test procId', 'test paramId');
|
||||
const {param: finalParam, proc: finalProc} =
|
||||
this.createProcedureAndParameter('test procId', 'test paramId');
|
||||
finalParam.setName(NON_DEFAULT_NAME);
|
||||
const event = this.createEventToState(finalProc, finalParam);
|
||||
this.procedureMap.add(initialProc);
|
||||
|
||||
event.run(/* forward= */ true);
|
||||
|
||||
chai.assert.equal(
|
||||
initialParam.getName(),
|
||||
finalParam.getName(),
|
||||
"Expected the procedure parameter's name to be changed");
|
||||
});
|
||||
|
||||
test('renaming a parameter fires a rename event', function() {
|
||||
const {param: initialParam, proc: initialProc} =
|
||||
this.createProcedureAndParameter('test procId', 'test paramId');
|
||||
const {param: finalParam, proc: finalProc} =
|
||||
this.createProcedureAndParameter('test procId', 'test paramId');
|
||||
finalParam.setName(NON_DEFAULT_NAME);
|
||||
const event = this.createEventToState(finalProc, finalParam);
|
||||
this.procedureMap.add(initialProc);
|
||||
|
||||
this.eventSpy.resetHistory();
|
||||
event.run(/* forward= */ true);
|
||||
|
||||
assertEventFiredShallow(
|
||||
this.eventSpy,
|
||||
Blockly.Events.ProcedureParameterRename,
|
||||
{
|
||||
model: initialProc,
|
||||
parameter: initialParam,
|
||||
oldName: DEFAULT_NAME,
|
||||
},
|
||||
this.workspace.id);
|
||||
});
|
||||
|
||||
test('noop renames do not fire rename events', function() {
|
||||
const {param: initialParam, proc: initialProc} =
|
||||
this.createProcedureAndParameter('test procId', 'test paramId');
|
||||
const {param: finalParam, proc: finalProc} =
|
||||
this.createProcedureAndParameter('test procId', 'test paramId');
|
||||
const event = this.createEventToState(finalProc, finalParam);
|
||||
this.procedureMap.add(initialProc);
|
||||
|
||||
this.eventSpy.resetHistory();
|
||||
event.run(/* forward= */ true);
|
||||
|
||||
assertEventNotFired(
|
||||
this.eventSpy,
|
||||
Blockly.Events.ProcedureParameterRename,
|
||||
{},
|
||||
this.workspace.id);
|
||||
});
|
||||
|
||||
test(
|
||||
'attempting to rename a parameter that does not exist throws',
|
||||
function() {
|
||||
const {param: initialParam, proc: initialProc} =
|
||||
this.createProcedureAndParameter('test procId', 'test paramId');
|
||||
const {param: finalParam, proc: finalProc} =
|
||||
this.createProcedureAndParameter('test procId', 'test paramId');
|
||||
finalParam.setName(NON_DEFAULT_NAME);
|
||||
const event = this.createEventToState(finalProc, finalParam);
|
||||
|
||||
chai.assert.throws(() => {
|
||||
event.run(/* forward= */ true);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
suite('backward', function() {
|
||||
test('the parameter with the matching ID and index is renamed', function() {
|
||||
const {param: initialParam, proc: initialProc} =
|
||||
this.createProcedureAndParameter('test procId', 'test paramId');
|
||||
const {param: undoableParam, proc: undoableProc} =
|
||||
this.createProcedureAndParameter('test procId', 'test paramId');
|
||||
initialParam.setName(NON_DEFAULT_NAME);
|
||||
undoableParam.setName(NON_DEFAULT_NAME);
|
||||
const event = this.createEventToState(undoableProc, undoableParam);
|
||||
this.procedureMap.add(initialProc);
|
||||
|
||||
this.eventSpy.resetHistory();
|
||||
event.run(/* forward= */ false);
|
||||
|
||||
chai.assert.equal(
|
||||
initialParam.getName(),
|
||||
DEFAULT_NAME,
|
||||
"Expected the procedure parameter's name to be changed");
|
||||
});
|
||||
|
||||
test('renaming a parameter fires a rename event', function() {
|
||||
const {param: initialParam, proc: initialProc} =
|
||||
this.createProcedureAndParameter('test procId', 'test paramId');
|
||||
const {param: undoableParam, proc: undoableProc} =
|
||||
this.createProcedureAndParameter('test procId', 'test paramId');
|
||||
initialParam.setName(NON_DEFAULT_NAME);
|
||||
undoableParam.setName(NON_DEFAULT_NAME);
|
||||
const event = this.createEventToState(undoableProc, undoableParam);
|
||||
this.procedureMap.add(initialProc);
|
||||
|
||||
this.eventSpy.resetHistory();
|
||||
event.run(/* forward= */ false);
|
||||
|
||||
|
||||
assertEventFiredShallow(
|
||||
this.eventSpy,
|
||||
Blockly.Events.ProcedureParameterRename,
|
||||
{
|
||||
model: initialProc,
|
||||
parameter: initialParam,
|
||||
oldName: NON_DEFAULT_NAME,
|
||||
},
|
||||
this.workspace.id);
|
||||
});
|
||||
|
||||
test('noop renames do not fire rename events', function() {
|
||||
const {param: initialParam, proc: initialProc} =
|
||||
this.createProcedureAndParameter('test procId', 'test paramId');
|
||||
const {param: undoableParam, proc: undoableProc} =
|
||||
this.createProcedureAndParameter('test procId', 'test paramId');
|
||||
undoableParam.setName(NON_DEFAULT_NAME);
|
||||
const event = this.createEventToState(undoableProc, undoableParam);
|
||||
this.procedureMap.add(initialProc);
|
||||
|
||||
event.run(/* forward= */ false);
|
||||
|
||||
|
||||
assertEventNotFired(
|
||||
this.eventSpy,
|
||||
Blockly.Events.ProcedureParameterRename,
|
||||
{},
|
||||
this.workspace.id);
|
||||
});
|
||||
|
||||
test(
|
||||
'attempting to rename a parameter that does not exist throws',
|
||||
function() {
|
||||
const {param: initialParam, proc: initialProc} =
|
||||
this.createProcedureAndParameter('test procId', 'test paramId');
|
||||
const {param: undoableParam, proc: undoableProc} =
|
||||
this.createProcedureAndParameter('test procId', 'test paramId');
|
||||
initialParam.setName(NON_DEFAULT_NAME);
|
||||
undoableParam.setName(NON_DEFAULT_NAME);
|
||||
const event = this.createEventToState(undoableProc, undoableParam);
|
||||
|
||||
chai.assert.throws(() => {
|
||||
event.run(/* forward= */ false);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
suite('serialization', function() {
|
||||
test('events round-trip through JSON', function() {
|
||||
const param = new Blockly.procedures.ObservableParameterModel(
|
||||
this.workspace, 'test param name', 'test param id');
|
||||
const model =
|
||||
new Blockly.procedures.ObservableProcedureModel(
|
||||
this.workspace, 'test name', 'test id')
|
||||
.insertParameter(param, 0);
|
||||
this.procedureMap.add(model);
|
||||
const origEvent = new Blockly.Events.ProcedureParameterDelete(
|
||||
this.workspace, model);
|
||||
|
||||
const json = origEvent.toJson();
|
||||
const newEvent = new Blockly.Events.fromJson(json, this.workspace);
|
||||
|
||||
chai.assert.deepEqual(newEvent, origEvent);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -1,186 +0,0 @@
|
||||
/**
|
||||
* @license
|
||||
* Copyright 2022 Google LLC
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
goog.declareModuleId('Blockly.test.eventProcedureRename');
|
||||
|
||||
import {assertEventFiredShallow, assertEventNotFired, createChangeListenerSpy} from './test_helpers/events.js';
|
||||
import {sharedTestSetup, sharedTestTeardown} from './test_helpers/setup_teardown.js';
|
||||
|
||||
|
||||
suite('Procedure Rename Event', function() {
|
||||
const DEFAULT_NAME = 'default';
|
||||
const NON_DEFAULT_NAME = 'non-default';
|
||||
|
||||
setup(function() {
|
||||
sharedTestSetup.call(this);
|
||||
this.workspace = new Blockly.Workspace();
|
||||
this.procedureMap = this.workspace.getProcedureMap();
|
||||
this.eventSpy = createChangeListenerSpy(this.workspace);
|
||||
});
|
||||
|
||||
teardown(function() {
|
||||
sharedTestTeardown.call(this);
|
||||
});
|
||||
|
||||
suite('running', function() {
|
||||
setup(function() {
|
||||
this.createProcedureModel = (id) => {
|
||||
return new Blockly.procedures.ObservableProcedureModel(
|
||||
this.workspace, DEFAULT_NAME, id);
|
||||
};
|
||||
|
||||
this.createEventToState = (procedureModel) => {
|
||||
return new Blockly.Events.ProcedureRename(
|
||||
this.workspace,
|
||||
procedureModel,
|
||||
procedureModel.getName() === DEFAULT_NAME ?
|
||||
NON_DEFAULT_NAME :
|
||||
DEFAULT_NAME);
|
||||
};
|
||||
});
|
||||
|
||||
suite('forward', function() {
|
||||
test('the procedure with the matching ID is renamed', function() {
|
||||
const initial = this.createProcedureModel('test id');
|
||||
const final = this.createProcedureModel('test id');
|
||||
final.setName(NON_DEFAULT_NAME);
|
||||
const event = this.createEventToState(final);
|
||||
this.procedureMap.add(initial);
|
||||
|
||||
event.run(/* forward= */ true);
|
||||
|
||||
chai.assert.equal(
|
||||
initial.getName(),
|
||||
final.getName(),
|
||||
"Expected the procedure's name to be changed");
|
||||
});
|
||||
|
||||
test('renaming a procedure fires a rename event', function() {
|
||||
const initial = this.createProcedureModel('test id');
|
||||
const final = this.createProcedureModel('test id');
|
||||
final.setName(NON_DEFAULT_NAME);
|
||||
const event = this.createEventToState(final);
|
||||
this.procedureMap.add(initial);
|
||||
|
||||
event.run(/* forward= */ true);
|
||||
|
||||
assertEventFiredShallow(
|
||||
this.eventSpy,
|
||||
Blockly.Events.ProcedureRename,
|
||||
{model: initial, oldName: DEFAULT_NAME},
|
||||
this.workspace.id);
|
||||
});
|
||||
|
||||
test('noop renames do not fire rename events', function() {
|
||||
const initial = this.createProcedureModel('test id');
|
||||
const final = this.createProcedureModel('test id');
|
||||
const event = this.createEventToState(final);
|
||||
this.procedureMap.add(initial);
|
||||
|
||||
event.run(/* forward= */ true);
|
||||
|
||||
assertEventNotFired(
|
||||
this.eventSpy,
|
||||
Blockly.Events.ProcedureRename,
|
||||
{},
|
||||
this.workspace.id);
|
||||
});
|
||||
|
||||
test(
|
||||
'attempting to rename a procedure that does not exist throws',
|
||||
function() {
|
||||
const initial = this.createProcedureModel('test id');
|
||||
const final = this.createProcedureModel('test id');
|
||||
final.setName(NON_DEFAULT_NAME);
|
||||
const event = this.createEventToState(final);
|
||||
|
||||
chai.assert.throws(() => {
|
||||
event.run(/* forward= */ true);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
suite('backward', function() {
|
||||
test('the procedure with the matching ID is renamed', function() {
|
||||
const initial = this.createProcedureModel('test id');
|
||||
const undoable = this.createProcedureModel('test id');
|
||||
initial.setName(NON_DEFAULT_NAME);
|
||||
undoable.setName(NON_DEFAULT_NAME);
|
||||
const event = this.createEventToState(undoable);
|
||||
this.procedureMap.add(initial);
|
||||
|
||||
event.run(/* forward= */ false);
|
||||
|
||||
chai.assert.equal(
|
||||
initial.getName(),
|
||||
DEFAULT_NAME,
|
||||
"Expected the procedure's name to be changed");
|
||||
});
|
||||
|
||||
test('renaming a procedure fires a rename event', function() {
|
||||
const initial = this.createProcedureModel('test id');
|
||||
const undoable = this.createProcedureModel('test id');
|
||||
initial.setName(NON_DEFAULT_NAME);
|
||||
undoable.setName(NON_DEFAULT_NAME);
|
||||
const event = this.createEventToState(undoable);
|
||||
this.procedureMap.add(initial);
|
||||
|
||||
event.run(/* forward= */ false);
|
||||
|
||||
assertEventFiredShallow(
|
||||
this.eventSpy,
|
||||
Blockly.Events.ProcedureRename,
|
||||
{model: initial, oldName: NON_DEFAULT_NAME},
|
||||
this.workspace.id);
|
||||
});
|
||||
|
||||
test('noop renames do not fire rename events', function() {
|
||||
const initial = this.createProcedureModel('test id');
|
||||
const undoable = this.createProcedureModel('test id');
|
||||
initial.setName(NON_DEFAULT_NAME);
|
||||
const event = this.createEventToState(undoable);
|
||||
this.procedureMap.add(initial);
|
||||
|
||||
event.run(/* forward= */ false);
|
||||
|
||||
assertEventNotFired(
|
||||
this.eventSpy,
|
||||
Blockly.Events.ProcedureRename,
|
||||
{},
|
||||
this.workspace.id);
|
||||
});
|
||||
|
||||
test(
|
||||
'attempting to rename a procedure that does not exist throws',
|
||||
function() {
|
||||
const initial = this.createProcedureModel('test id');
|
||||
const undoable = this.createProcedureModel('test id');
|
||||
initial.setName(NON_DEFAULT_NAME);
|
||||
undoable.setName(NON_DEFAULT_NAME);
|
||||
const event = this.createEventToState(undoable);
|
||||
|
||||
chai.assert.throws(() => {
|
||||
event.run(/* forward= */ false);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
suite('serialization', function() {
|
||||
test('events round-trip through JSON', function() {
|
||||
const model = new Blockly.procedures.ObservableProcedureModel(
|
||||
this.workspace, 'test name', 'test id');
|
||||
this.procedureMap.add(model);
|
||||
const origEvent = new Blockly.Events.ProcedureRename(
|
||||
this.workspace, model, NON_DEFAULT_NAME);
|
||||
|
||||
const json = origEvent.toJson();
|
||||
const newEvent = new Blockly.Events.fromJson(json, this.workspace);
|
||||
|
||||
chai.assert.deepEqual(newEvent, origEvent);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -73,14 +73,6 @@
|
||||
'Blockly.test.eventCommentDelete',
|
||||
'Blockly.test.eventCommentMove',
|
||||
'Blockly.test.eventMarkerMove',
|
||||
'Blockly.test.eventProcedureCreate',
|
||||
'Blockly.test.eventProcedureDelete',
|
||||
'Blockly.test.eventProcedureRename',
|
||||
'Blockly.test.eventProcedureEnable',
|
||||
'Blockly.test.eventProcedureChangeReturn',
|
||||
'Blockly.test.eventProcedureParameterCreate',
|
||||
'Blockly.test.eventProcedureParameterDelete',
|
||||
'Blockly.test.eventProcedureParameterRename',
|
||||
'Blockly.test.eventSelected',
|
||||
'Blockly.test.eventThemeChange',
|
||||
'Blockly.test.eventToolboxItemSelect',
|
||||
|
||||
@@ -203,550 +203,6 @@ suite('Procedure Map', function() {
|
||||
});
|
||||
});
|
||||
|
||||
suite('event firing', function() {
|
||||
setup(function() {
|
||||
this.eventSpy = createChangeListenerSpy(this.workspace);
|
||||
});
|
||||
|
||||
teardown(function() {
|
||||
this.workspace.removeChangeListener(this.eventSpy);
|
||||
});
|
||||
|
||||
suite('procedure create', function() {
|
||||
test('create events are fired when a procedure is inserted', function() {
|
||||
const procedureModel =
|
||||
new Blockly.procedures.ObservableProcedureModel(this.workspace);
|
||||
this.procedureMap.set(procedureModel.getId(), procedureModel);
|
||||
|
||||
assertEventFiredShallow(
|
||||
this.eventSpy,
|
||||
Blockly.Events.ProcedureCreate,
|
||||
{model: procedureModel},
|
||||
this.workspace.id);
|
||||
});
|
||||
|
||||
test(
|
||||
'create events are not fired if a procedure is already inserted',
|
||||
function() {
|
||||
const procedureModel =
|
||||
new Blockly.procedures.ObservableProcedureModel(this.workspace);
|
||||
this.procedureMap.set(procedureModel.getId(), procedureModel);
|
||||
|
||||
this.eventSpy.resetHistory();
|
||||
this.procedureMap.set(procedureModel.getId(), procedureModel);
|
||||
|
||||
assertEventNotFired(
|
||||
this.eventSpy,
|
||||
Blockly.Events.ProcedureCreate,
|
||||
{},
|
||||
this.workspace.id);
|
||||
});
|
||||
|
||||
test('create events are fired when a procedure is added', function() {
|
||||
const procedureModel =
|
||||
new Blockly.procedures.ObservableProcedureModel(this.workspace);
|
||||
this.procedureMap.add(procedureModel);
|
||||
|
||||
assertEventFiredShallow(
|
||||
this.eventSpy,
|
||||
Blockly.Events.ProcedureCreate,
|
||||
{model: procedureModel},
|
||||
this.workspace.id);
|
||||
});
|
||||
|
||||
test(
|
||||
'create events are not fired if a procedure is already added',
|
||||
function() {
|
||||
const procedureModel =
|
||||
new Blockly.procedures.ObservableProcedureModel(this.workspace);
|
||||
this.procedureMap.add(procedureModel);
|
||||
|
||||
this.eventSpy.resetHistory();
|
||||
this.procedureMap.add(procedureModel);
|
||||
|
||||
assertEventNotFired(
|
||||
this.eventSpy,
|
||||
Blockly.Events.ProcedureCreate,
|
||||
{},
|
||||
this.workspace.id);
|
||||
});
|
||||
});
|
||||
|
||||
suite('procedure delete', function() {
|
||||
test('delete events are fired when a procedure is deleted', function() {
|
||||
const procedureModel =
|
||||
new Blockly.procedures.ObservableProcedureModel(this.workspace);
|
||||
this.procedureMap.add(procedureModel);
|
||||
this.procedureMap.delete(procedureModel.getId());
|
||||
|
||||
assertEventFiredShallow(
|
||||
this.eventSpy,
|
||||
Blockly.Events.ProcedureDelete,
|
||||
{model: procedureModel},
|
||||
this.workspace.id);
|
||||
});
|
||||
|
||||
test(
|
||||
'delete events are not fired if a procedure does not exist',
|
||||
function() {
|
||||
const procedureModel =
|
||||
new Blockly.procedures.ObservableProcedureModel(this.workspace);
|
||||
this.procedureMap.delete(procedureModel.getId());
|
||||
|
||||
assertEventNotFired(
|
||||
this.eventSpy,
|
||||
Blockly.Events.ProcedureDelete,
|
||||
{},
|
||||
this.workspace.id);
|
||||
});
|
||||
|
||||
test(
|
||||
'delete events are fired when the procedure map is cleared',
|
||||
function() {
|
||||
const procedureModel1 =
|
||||
new Blockly.procedures.ObservableProcedureModel(this.workspace);
|
||||
const procedureModel2 =
|
||||
new Blockly.procedures.ObservableProcedureModel(this.workspace);
|
||||
const procedureModel3 =
|
||||
new Blockly.procedures.ObservableProcedureModel(this.workspace);
|
||||
this.procedureMap.add(procedureModel1);
|
||||
this.procedureMap.add(procedureModel2);
|
||||
this.procedureMap.add(procedureModel3);
|
||||
this.procedureMap.clear();
|
||||
|
||||
assertEventFiredShallow(
|
||||
this.eventSpy,
|
||||
Blockly.Events.ProcedureDelete,
|
||||
{model: procedureModel1},
|
||||
this.workspace.id);
|
||||
assertEventFiredShallow(
|
||||
this.eventSpy,
|
||||
Blockly.Events.ProcedureDelete,
|
||||
{model: procedureModel2},
|
||||
this.workspace.id);
|
||||
assertEventFiredShallow(
|
||||
this.eventSpy,
|
||||
Blockly.Events.ProcedureDelete,
|
||||
{model: procedureModel3},
|
||||
this.workspace.id);
|
||||
});
|
||||
});
|
||||
|
||||
suite('procedure rename', function() {
|
||||
test('rename events are fired when a procedure is renamed', function() {
|
||||
const procedureModel =
|
||||
new Blockly.procedures.ObservableProcedureModel(this.workspace)
|
||||
.setName('test name');
|
||||
this.procedureMap.add(procedureModel);
|
||||
procedureModel.setName('new name');
|
||||
|
||||
assertEventFiredShallow(
|
||||
this.eventSpy,
|
||||
Blockly.Events.ProcedureRename,
|
||||
{
|
||||
model: procedureModel,
|
||||
oldName: 'test name',
|
||||
},
|
||||
this.workspace.id);
|
||||
});
|
||||
|
||||
test('rename events are not fired if the rename is noop', function() {
|
||||
const procedureModel =
|
||||
new Blockly.procedures.ObservableProcedureModel(this.workspace)
|
||||
.setName('test name');
|
||||
this.procedureMap.add(procedureModel);
|
||||
procedureModel.setName('test name');
|
||||
|
||||
assertEventNotFired(
|
||||
this.eventSpy,
|
||||
Blockly.Events.ProcedureRename,
|
||||
{},
|
||||
this.workspace.id);
|
||||
});
|
||||
|
||||
test(
|
||||
'rename events are not fired if the procedure is not in the map',
|
||||
function() {
|
||||
const procedureModel =
|
||||
new Blockly.procedures.ObservableProcedureModel(this.workspace)
|
||||
.setName('test name');
|
||||
procedureModel.setName('new name');
|
||||
|
||||
assertEventNotFired(
|
||||
this.eventSpy,
|
||||
Blockly.Events.ProcedureRename,
|
||||
{},
|
||||
this.workspace.id);
|
||||
});
|
||||
});
|
||||
|
||||
suite('procedure enable', function() {
|
||||
test('enable events are fired when a procedure is enabled', function() {
|
||||
const procedureModel =
|
||||
new Blockly.procedures.ObservableProcedureModel(this.workspace)
|
||||
.setEnabled(false);
|
||||
this.procedureMap.add(procedureModel);
|
||||
procedureModel.setEnabled(true);
|
||||
|
||||
assertEventFiredShallow(
|
||||
this.eventSpy,
|
||||
Blockly.Events.ProcedureEnable,
|
||||
{model: procedureModel},
|
||||
this.workspace.id);
|
||||
});
|
||||
|
||||
test('enable events are fired when a procedure is disabled', function() {
|
||||
const procedureModel =
|
||||
new Blockly.procedures.ObservableProcedureModel(this.workspace);
|
||||
this.procedureMap.add(procedureModel);
|
||||
procedureModel.setEnabled(false);
|
||||
|
||||
assertEventFiredShallow(
|
||||
this.eventSpy,
|
||||
Blockly.Events.ProcedureEnable,
|
||||
{model: procedureModel},
|
||||
this.workspace.id);
|
||||
});
|
||||
|
||||
test('enable events are not fired if enabling is noop', function() {
|
||||
const procedureModel =
|
||||
new Blockly.procedures.ObservableProcedureModel(this.workspace);
|
||||
this.procedureMap.add(procedureModel);
|
||||
procedureModel.setEnabled(true);
|
||||
|
||||
assertEventNotFired(
|
||||
this.eventSpy,
|
||||
Blockly.Events.ProcedureEnable,
|
||||
{},
|
||||
this.workspace.id);
|
||||
});
|
||||
|
||||
test('enable events are not fired if disabling is noop', function() {
|
||||
const procedureModel =
|
||||
new Blockly.procedures.ObservableProcedureModel(this.workspace)
|
||||
.setEnabled(false);
|
||||
this.procedureMap.add(procedureModel);
|
||||
procedureModel.setEnabled(false);
|
||||
|
||||
assertEventNotFired(
|
||||
this.eventSpy,
|
||||
Blockly.Events.ProcedureEnable,
|
||||
{},
|
||||
this.workspace.id);
|
||||
});
|
||||
|
||||
test(
|
||||
'enable events are not fired if the procedure is not in the map',
|
||||
function() {
|
||||
const procedureModel =
|
||||
new Blockly.procedures.ObservableProcedureModel(this.workspace)
|
||||
.setEnabled(false);
|
||||
procedureModel.setEnabled(true);
|
||||
|
||||
assertEventNotFired(
|
||||
this.eventSpy,
|
||||
Blockly.Events.ProcedureEnable,
|
||||
{},
|
||||
this.workspace.id);
|
||||
});
|
||||
});
|
||||
|
||||
suite('parameter create', function() {
|
||||
test(
|
||||
'parameter create events are fired when a parameter is inserted',
|
||||
function() {
|
||||
const procedureModel =
|
||||
new Blockly.procedures.ObservableProcedureModel(this.workspace);
|
||||
this.procedureMap.add(procedureModel);
|
||||
const parameterModel =
|
||||
new Blockly.procedures.ObservableParameterModel(
|
||||
this.workspace, 'test name');
|
||||
procedureModel.insertParameter(parameterModel, 0);
|
||||
|
||||
assertEventFiredShallow(
|
||||
this.eventSpy,
|
||||
Blockly.Events.ProcedureParameterCreate,
|
||||
{
|
||||
model: procedureModel,
|
||||
parameter: parameterModel,
|
||||
index: 0,
|
||||
},
|
||||
this.workspace.id);
|
||||
});
|
||||
|
||||
test(
|
||||
'parameter create events are not fired if the parameter is ' +
|
||||
'already inserted',
|
||||
function() {
|
||||
const procedureModel =
|
||||
new Blockly.procedures.ObservableProcedureModel(this.workspace);
|
||||
this.procedureMap.add(procedureModel);
|
||||
const parameterModel =
|
||||
new Blockly.procedures.ObservableParameterModel(
|
||||
this.workspace, 'test name');
|
||||
procedureModel.insertParameter(parameterModel, 0);
|
||||
|
||||
this.eventSpy.resetHistory();
|
||||
procedureModel.insertParameter(parameterModel, 0);
|
||||
|
||||
assertEventNotFired(
|
||||
this.eventSpy,
|
||||
Blockly.Events.ProcedureParameterCreate,
|
||||
{},
|
||||
this.workspace.id);
|
||||
});
|
||||
|
||||
test(
|
||||
'parameter create events are not fired if the procedure is ' +
|
||||
'not in the map',
|
||||
function() {
|
||||
const procedureModel =
|
||||
new Blockly.procedures.ObservableProcedureModel(this.workspace);
|
||||
procedureModel.insertParameter(
|
||||
new Blockly.procedures.ObservableParameterModel(
|
||||
this.workspace, 'test name'),
|
||||
0);
|
||||
|
||||
assertEventNotFired(
|
||||
this.eventSpy,
|
||||
Blockly.Events.ProcedureParameterCreate,
|
||||
{},
|
||||
this.workspace.id);
|
||||
});
|
||||
});
|
||||
|
||||
suite('parameter delete', function() {
|
||||
test(
|
||||
'parameter delete events are fired when a parameter is deleted',
|
||||
function() {
|
||||
const procedureModel =
|
||||
new Blockly.procedures.ObservableProcedureModel(this.workspace);
|
||||
this.procedureMap.add(procedureModel);
|
||||
const parameterModel =
|
||||
new Blockly.procedures.ObservableParameterModel(
|
||||
this.workspace, 'test name');
|
||||
procedureModel.insertParameter(parameterModel, 0);
|
||||
procedureModel.deleteParameter(0);
|
||||
|
||||
assertEventFiredShallow(
|
||||
this.eventSpy,
|
||||
Blockly.Events.ProcedureParameterDelete,
|
||||
{
|
||||
model: procedureModel,
|
||||
parameter: parameterModel,
|
||||
index: 0,
|
||||
},
|
||||
this.workspace.id);
|
||||
});
|
||||
|
||||
test(
|
||||
'parameter delete events are not fired if the parameter does not exist',
|
||||
function() {
|
||||
const procedureModel =
|
||||
new Blockly.procedures.ObservableProcedureModel(this.workspace);
|
||||
this.procedureMap.add(procedureModel);
|
||||
procedureModel.deleteParameter(0);
|
||||
|
||||
assertEventNotFired(
|
||||
this.eventSpy,
|
||||
Blockly.Events.ProcedureParameterDelete,
|
||||
{},
|
||||
this.workspace.id);
|
||||
});
|
||||
|
||||
test(
|
||||
'parameter delete events are not fired if the procedure is ' +
|
||||
'not in the map',
|
||||
function() {
|
||||
const procedureModel =
|
||||
new Blockly.procedures.ObservableProcedureModel(this.workspace)
|
||||
.insertParameter(
|
||||
new Blockly.procedures.ObservableParameterModel(
|
||||
this.workspace, 'test name'),
|
||||
0);
|
||||
procedureModel.deleteParameter(0);
|
||||
|
||||
assertEventNotFired(
|
||||
this.eventSpy,
|
||||
Blockly.Events.ProcedureParameterDelete,
|
||||
{},
|
||||
this.workspace.id);
|
||||
});
|
||||
});
|
||||
|
||||
suite('parameter rename', function() {
|
||||
test(
|
||||
'parameter rename events are fired when a parameter is renamed',
|
||||
function() {
|
||||
const procedureModel =
|
||||
new Blockly.procedures.ObservableProcedureModel(this.workspace);
|
||||
this.procedureMap.add(procedureModel);
|
||||
const parameterModel =
|
||||
new Blockly.procedures.ObservableParameterModel(
|
||||
this.workspace, 'test name');
|
||||
procedureModel.insertParameter(parameterModel, 0);
|
||||
|
||||
parameterModel.setName('new name');
|
||||
|
||||
assertEventFiredShallow(
|
||||
this.eventSpy,
|
||||
Blockly.Events.ProcedureParameterRename,
|
||||
{
|
||||
model: procedureModel,
|
||||
parameter: parameterModel,
|
||||
oldName: 'test name',
|
||||
},
|
||||
this.workspace.id);
|
||||
});
|
||||
|
||||
test(
|
||||
'parameter rename events are not fired if the rename is noop',
|
||||
function() {
|
||||
const procedureModel =
|
||||
new Blockly.procedures.ObservableProcedureModel(this.workspace);
|
||||
this.procedureMap.add(procedureModel);
|
||||
const parameterModel =
|
||||
new Blockly.procedures.ObservableParameterModel(
|
||||
this.workspace, 'test name');
|
||||
procedureModel.insertParameter(parameterModel, 0);
|
||||
|
||||
parameterModel.setName('test name');
|
||||
|
||||
assertEventNotFired(
|
||||
this.eventSpy,
|
||||
Blockly.Events.ProcedureParameterRename,
|
||||
{},
|
||||
this.workspace.id);
|
||||
});
|
||||
|
||||
test(
|
||||
'parameter rename events are not fired if the procedure is ' +
|
||||
'not in the map',
|
||||
function() {
|
||||
const parameterModel =
|
||||
new Blockly.procedures.ObservableParameterModel(
|
||||
this.workspace, 'test name');
|
||||
new Blockly.procedures.ObservableProcedureModel(this.workspace)
|
||||
.insertParameter(parameterModel, 0);
|
||||
|
||||
parameterModel.setName('new name');
|
||||
|
||||
assertEventNotFired(
|
||||
this.eventSpy,
|
||||
Blockly.Events.ProcedureParameterRename,
|
||||
{},
|
||||
this.workspace.id);
|
||||
});
|
||||
|
||||
test(
|
||||
'parameter rename events are not fired if the parameter is ' +
|
||||
'not in a procedure',
|
||||
function() {
|
||||
const parameterModel =
|
||||
new Blockly.procedures.ObservableParameterModel(
|
||||
this.workspace, 'test name');
|
||||
|
||||
parameterModel.setName('new name');
|
||||
|
||||
assertEventNotFired(
|
||||
this.eventSpy,
|
||||
Blockly.Events.ProcedureParameterRename,
|
||||
{},
|
||||
this.workspace.id);
|
||||
});
|
||||
});
|
||||
|
||||
suite('procedure change return', function() {
|
||||
test(
|
||||
'return type change events are fired when the return is added',
|
||||
function() {
|
||||
const procedureModel =
|
||||
new Blockly.procedures.ObservableProcedureModel(this.workspace)
|
||||
.setReturnTypes(null);
|
||||
this.procedureMap.add(procedureModel);
|
||||
procedureModel.setReturnTypes([]);
|
||||
|
||||
assertEventFiredShallow(
|
||||
this.eventSpy,
|
||||
Blockly.Events.ProcedureChangeReturn,
|
||||
{
|
||||
model: procedureModel,
|
||||
oldTypes: null,
|
||||
},
|
||||
this.workspace.id);
|
||||
});
|
||||
|
||||
test(
|
||||
'return type change events are fired when the return is removed',
|
||||
function() {
|
||||
const types = [];
|
||||
const procedureModel =
|
||||
new Blockly.procedures.ObservableProcedureModel(this.workspace)
|
||||
.setReturnTypes(types);
|
||||
this.procedureMap.add(procedureModel);
|
||||
procedureModel.setReturnTypes(null);
|
||||
|
||||
assertEventFiredShallow(
|
||||
this.eventSpy,
|
||||
Blockly.Events.ProcedureChangeReturn,
|
||||
{
|
||||
model: procedureModel,
|
||||
oldTypes: types,
|
||||
},
|
||||
this.workspace.id);
|
||||
});
|
||||
|
||||
test(
|
||||
'return type change events are not fired if adding is noop',
|
||||
function() {
|
||||
const procedureModel =
|
||||
new Blockly.procedures.ObservableProcedureModel(this.workspace)
|
||||
.setReturnTypes([]);
|
||||
this.procedureMap.add(procedureModel);
|
||||
procedureModel.setReturnTypes([]);
|
||||
|
||||
assertEventNotFired(
|
||||
this.eventSpy,
|
||||
Blockly.Events.ProcedureChangeReturn,
|
||||
{},
|
||||
this.workspace.id);
|
||||
});
|
||||
|
||||
test(
|
||||
'return type change events are not fired if removing is noop',
|
||||
function() {
|
||||
const procedureModel =
|
||||
new Blockly.procedures.ObservableProcedureModel(this.workspace)
|
||||
.setReturnTypes(null);
|
||||
this.procedureMap.add(procedureModel);
|
||||
procedureModel.setReturnTypes(null);
|
||||
|
||||
assertEventNotFired(
|
||||
this.eventSpy,
|
||||
Blockly.Events.ProcedureChangeReturn,
|
||||
{},
|
||||
this.workspace.id);
|
||||
});
|
||||
|
||||
test(
|
||||
'return type change events are not fired if the procedure is ' +
|
||||
'not in the map',
|
||||
function() {
|
||||
const procedureModel =
|
||||
new Blockly.procedures.ObservableProcedureModel(this.workspace)
|
||||
.setReturnTypes(null);
|
||||
|
||||
procedureModel.setReturnTypes([]);
|
||||
|
||||
assertEventNotFired(
|
||||
this.eventSpy,
|
||||
Blockly.Events.ProcedureChangeReturn,
|
||||
{},
|
||||
this.workspace.id);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
suite('backing variable to parameters', function() {
|
||||
test(
|
||||
'construction references an existing variable if available',
|
||||
|
||||
Reference in New Issue
Block a user