Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add tests for procedure serialization #6574

Merged
merged 9 commits into from
Oct 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
305 changes: 305 additions & 0 deletions tests/mocha/jso_deserialization_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -713,4 +713,309 @@ suite('JSO Deserialization', function() {
chai.assert.equal(block.someProperty, 'some value');
});
});

// TODO(#6522): Unskip tests.
suite.skip('Procedures', function() {
class MockProcedureModel {
constructor(id) {
this.id = id ?? Blockly.utils.idGenerator.genUid();
this.name = '';
this.parameters = [];
this.returnTypes = null;
this.enabled = true;
}

setName(name) {
this.name = name;
return this;
}

insertParameter(parameterModel, index) {
this.parameters.splice(index, 0, parameterModel);
return this;
}

deleteParameter(index) {
this.parameters.splice(index, 1);
return this;
}

setReturnTypes(types) {
this.returnTypes = types;
return this;
}

setEnabled(enabled) {
this.enabled = enabled;
return this;
}

getId() {
return this.id;
}

getName() {
return this.name;
}

getParameter(index) {
return this.parameters[index];
}

getParameters() {
return [...this.parameters];
}

getReturnTypes() {
return this.returnTypes;
}

getEnabled() {
return this.enabled;
}
}

class MockParameterModel {
constructor(name, id) {
this.id = id ?? Blockly.utils.idGenerator.genUid();
this.name = name;
this.types = [];
}

setName(name) {
this.name = name;
return this;
}

setTypes(types) {
this.types = types;
return this;
}

getName() {
return this.name;
}

getTypes() {
return this.types;
}

getId() {
return this.id;
}
}

setup(function() {
this.procedureSerializer = new
Blockly.serialization.procedures.ProcedureSerializer(
MockProcedureModel, MockParameterModel);
this.procedureMap = this.workspace.getProcedureMap();
});

teardown(function() {
this.procedureSerializer = null;
this.procedureMap = null;
});

suite('invariant properties', function() {
test('the id property is assigned', function() {
const jso = {
'id': 'test id',
'name': 'test name',
'returnTypes': [],
};

this.procedureSerializer.load([jso], this.workspace);

const procedureModel = this.procedureMap.getProcedures()[0];
chai.assert.isNotNull(
procedureModel, 'Expected a procedure model to exist');
chai.assert.equal(
procedureModel.getId(),
'test id',
'Expected the procedure model ID to match the serialized ID');
});

test('the name property is assigned', function() {
const jso = {
'id': 'test id',
'name': 'test name',
'returnTypes': [],
};

this.procedureSerializer.load([jso], this.workspace);

const procedureModel = this.procedureMap.getProcedures()[0];
chai.assert.isNotNull(
procedureModel, 'Expected a procedure model to exist');
chai.assert.equal(
procedureModel.getName(),
'test name',
'Expected the procedure model name to match the serialized name');
});
});

suite('return types', function() {
test('if the return type property is null it is assigned', function() {
const jso = {
'id': 'test id',
'name': 'test name',
'returnTypes': null,
};

this.procedureSerializer.load([jso], this.workspace);

const procedureModel = this.procedureMap.getProcedures()[0];
chai.assert.isNotNull(
procedureModel, 'Expected a procedure model to exist');
chai.assert.isNull(
procedureModel.getReturnTypes(),
'Expected the procedure model types to be null');
});

test('if the return type property is an empty array it is assigned', function() {
const jso = {
'id': 'test id',
'name': 'test name',
'returnTypes': [],
};

this.procedureSerializer.load([jso], this.workspace);

const procedureModel = this.procedureMap.getProcedures()[0];
chai.assert.isNotNull(
procedureModel, 'Expected a procedure model to exist');
chai.assert.isArray(
procedureModel.getReturnTypes(),
'Expected the procedure model types to be an array');
chai.assert.isEmpty(
procedureModel.getReturnTypes(),
'Expected the procedure model types array to be empty');
});

test('if the return type property is a string array it is assigned', function() {
const jso = {
'id': 'test id',
'name': 'test name',
'returnTypes': ['test type 1', 'test type 2'],
};

this.procedureSerializer.load([jso], this.workspace);

const procedureModel = this.procedureMap.getProcedures()[0];
chai.assert.isNotNull(
procedureModel, 'Expected a procedure model to exist');
chai.assert.isArray(
procedureModel.getReturnTypes(),
'Expected the procedure model types to be an array');
chai.assert.deepEqual(
procedureModel.getReturnTypes(),
['test type 1', 'test type 2'],
'Expected the procedure model types array to be match the ' +
'serialized array');
});
});

suite('parameters', function() {
suite('invariant properties', function() {
test('the id property is assigned', function() {
const jso = {
'id': 'test id',
'name': 'test name',
'returnTypes': [],
'parameters': [
{
'id': 'test id',
'name': 'test name',
},
],
};

this.procedureSerializer.load([jso], this.workspace);

const parameterModel =
this.procedureMap.getProcedures()[0].getParameters()[0];
chai.assert.isNotNull(
parameterModel, 'Expected a parameter model to exist');
chai.assert.equal(
parameterModel.getId(),
'test id',
'Expected the parameter model ID to match the serialized ID');
});

test('the name property is assigned', function() {
const jso = {
'id': 'test id',
'name': 'test name',
'returnTypes': [],
'parameters': [
{
'id': 'test id',
'name': 'test name',
},
],
};

this.procedureSerializer.load([jso], this.workspace);

const parameterModel =
this.procedureMap.getProcedures()[0].getParameters()[0];
chai.assert.isNotNull(
parameterModel, 'Expected a parameter model to exist');
chai.assert.equal(
parameterModel.getName(),
'test name',
'Expected the parameter model name to match the serialized name');
});
});

suite('types', function() {
test('if the type property does not exist, nothing is assigned', function() {
const jso = {
'id': 'test id',
'name': 'test name',
'returnTypes': [],
'parameters': [
{
'id': 'test id',
'name': 'test name',
},
],
};

chai.assert.doesNotThrow(
() => {
this.procedureMap.getProcedures()[0].getParameters()[0];
},
'Expected the deserializer to skip the non-existant type property');
});

test('if the type property exists, it is assigned', function() {
const jso = {
'id': 'test id',
'name': 'test name',
'returnTypes': [],
'parameters': [
{
'id': 'test id',
'name': 'test name',
'types': ['test type 1', 'test type 2'],
},
],
};

this.procedureSerializer.load([jso], this.workspace);

const parameterModel =
this.procedureMap.getProcedures()[0].getParameters()[0];
chai.assert.isNotNull(
parameterModel, 'Expected a parameter model to exist');
chai.assert.deepEqual(
parameterModel.getTypes(),
['test type 1', 'test type 2'],
'Expected the parameter model types to match the serialized types');
});
});
});
});
});
Loading