Skip to content

Commit

Permalink
chore: add deserialization tests
Browse files Browse the repository at this point in the history
  • Loading branch information
BeksOmega committed Oct 25, 2022
1 parent 7319f1e commit 66e998a
Show file tree
Hide file tree
Showing 2 changed files with 294 additions and 3 deletions.
293 changes: 292 additions & 1 deletion tests/mocha/jso_deserialization_test.js
Expand Up @@ -714,17 +714,308 @@ suite('JSO Deserialization', function() {
});
});

suite('Procedures', function() {
// 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');
});
});
});
});
});
4 changes: 2 additions & 2 deletions tests/mocha/jso_serialization_test.js
Expand Up @@ -795,6 +795,7 @@ suite('JSO Serialization', function() {
});
});

// TODO(#6522): Unskip serialization tests.
suite.skip('Procedures', function() {
class MockProcedureModel {
constructor() {
Expand Down Expand Up @@ -893,8 +894,7 @@ suite('JSO Serialization', function() {
this.procedureMap = null;
});

// TODO(#6522): Unskip serialization tests.
suite.skip('invariant properties', function() {
suite('invariant properties', function() {
test('the state always has an id property', function() {
const procedureModel = new MockProcedureModel();
this.procedureMap.add(procedureModel);
Expand Down

0 comments on commit 66e998a

Please sign in to comment.