Skip to content

Commit

Permalink
Unit test for adding a a template to a schema.
Browse files Browse the repository at this point in the history
  • Loading branch information
Joe-Jones committed Oct 1, 2014
1 parent 33683a9 commit f2f50cc
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 18 deletions.
2 changes: 1 addition & 1 deletion LogicSystem.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ LogicSystem.prototype.addTemplate = function(template) {
var id = this.addGate(gate[0]);
gates.push(id);
for (var j = 1; j <= 2; j++) {
if (this.gates[id][j]) {
if (template.gates[i][j]) {
this.gates[id][j] = gate[j] + start_gate - 1;
}
}
Expand Down
26 changes: 24 additions & 2 deletions Project.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,8 @@ function Project(project_data) {

this.open_tabs = this.project_data.getData("open_tabs") || [];
this.selected_tab = this.project_data.getData("selected_tab");
this.schema_names = this.selected_tab = this.project_data.getData("schema_names") || {};
this.schema_names = this.project_data.getData("schema_names") || {};
this.schema_infos = this.project_data.getData("schema_infos") || {};

// History and checkpoints
this.checkpoint_position = this.project_data.getData("checkpoint_position") || 0;
Expand All @@ -170,7 +171,7 @@ Project.prototype = {
// private
addSchema: function(id) {
var data = this.project_data.getData(id);
var schema = new SchemaModel(id, this.project_data.project_id, data, this.template_manager);
var schema = new SchemaModel(id, this.project_data.project_id, data, this.template_manager, this);
this.template_manager.addModel(schema);
this.schemas[id] = schema;
return schema;
Expand Down Expand Up @@ -255,6 +256,14 @@ Project.prototype = {

getSchemaName: function(schema_id) {
return this.schema_names[schema_id];
},

getSchemaInfo: function(schema_id) {
return this.schema_infos[schema_id];
},

listSchemas: function() {
return _.keys(this.schemas);
}

};
Expand Down Expand Up @@ -330,6 +339,7 @@ Action.prototype = {
model.trigger("schemaOpened", schema.id);
this.previous_schema = model.selected_tab;
model.selected_tab = schema.id;
model.schema_infos[schema.id] = { input_counter: 0, inputs: [], output_counter: 0, outputs: [] };
break;
case "SELECT_SCHEMA":
if (model.selected_tab == this.schema) {
Expand All @@ -349,6 +359,18 @@ Action.prototype = {
var object = makeGate(this.gate_type);
model.add(object);
object.setPosition(this.position);
if (this.gate_type == "INPUT") {
model.project.schema_infos[this.schema_id]["inputs"].push({
name: "i_" + model.project.schema_infos[this.schema_id]["input_counter"],
number: object.number})
model.project.schema_infos[this.schema_id]["input_counter"]++;
}
if (this.gate_type == "OUTPUT") {
model.project.schema_infos[this.schema_id]["outputs"].push({
name: "0_" + model.project.schema_infos[this.schema_id]["output_counter"],
number: object.number})
model.project.schema_infos[this.schema_id]["output_counter"]++;
}
model.trigger("gateAdded", object.number);
break;
case "REMOVE_GATE":
Expand Down
28 changes: 16 additions & 12 deletions ProjectView.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ var ProjectView = JakeKit.HBox.extend({
this.project = project;
_.bindAll(this, "openTab", "schemaNameChanged", "selectTab");

this.components = new ComponentList();
this.components = new ComponentList(project);
this.addChild(this.components);

this.tabstack = new JakeKit.w2tabstack();
Expand Down Expand Up @@ -76,13 +76,15 @@ var ProjectView = JakeKit.HBox.extend({
var ComponentView = Backbone.View.extend({

attributes: { draggable: "true" },

initialize: function(args) {
this.schema_id = args.schema_id;
this.name = args.name;
},

render: function() {
var html = this.model.get("name");
this.$el.html(html);

this.el.addEventListener("dragstart", makeEventListener("COMPONENT:" + this.model.id), true);

this.$el.html(this.name);
this.el.addEventListener("dragstart", makeEventListener("COMPONENT:" + this.schema_id), true);
return this;
}

Expand All @@ -91,14 +93,16 @@ var ComponentView = Backbone.View.extend({
var ComponentList = Backbone.View.extend({

className: "components",

initialize: function(project) {
this.project = project;
},

render: function() {
if (this.collection) {
this.collection.each(function(component) {
var component_view = new ComponentView({ model: component });
this.$el.append(component_view.render().el);
}, this);
}
_.each(this.project.listSchemas(), function(schema_id) {
var component_view = new ComponentView({ schema_id: schema_id, name: this.project.getSchemaName(schema_id) });
this.$el.append(component_view.render().el);
}, this);
}

});
23 changes: 20 additions & 3 deletions SchemaModel.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
"use strict";

/********************************************************************************************/
function SchemaModel(id, project_id, data, template_manager)
function SchemaModel(id, project_id, data, template_manager, project)
/********************************************************************************************/
{
this.id = id;
this.project_id = project_id;
this.data = data;
this.template_manager = template_manager;
this.loaded = false;
this.project = project;
}

_.extend(SchemaModel.prototype, Backbone.Events);
Expand Down Expand Up @@ -36,6 +37,7 @@ SchemaModel.prototype.add = function(object) {
if (object.type == "SUBCIRCIT") {
var template = this.template_manager.getTemplate(object.schema_id);
object.template_instance = this.logic_system.addTemplate(template);
object.project = this.project;
} else {
object.logic_id = this.logic_system.addGate(object.type);
}
Expand Down Expand Up @@ -145,8 +147,23 @@ SchemaModel.prototype.addConnection = function(input_item_num, input_num, output
this.next_connection_number ++;

// Add to the LogicSystem
this.logic_system.makeConnection(connection.output_item.logic_id, connection.input_item.logic_id, connection.input_num);
this.logic_system.injectTransient(connection.input_item.logic_id);

var logic_output;
if (output_item.type == "SUBCIRCIT") {
logic_output = this.logic_system.gateNumber(output_item.template_instance,
this.project.getSchemaInfo(output_item.schema_id)["outputs"][output_num]["number"]);
} else {
logic_output = output_item.logic_id;
}

if (input_item.type == "SUBCIRCIT") {
this.logic_system.connectToTemplateInstance(logic_output, input_item.template_instance,
this.project.getSchemaInfo(input_item.schema_id)["inputs"][input_num]["number"]);
// Todo need a way to inject a transient.
} else {
this.logic_system.makeConnection(logic_output, connection.input_item.logic_id, connection.input_num);
this.logic_system.injectTransient(connection.input_item.logic_id);
}
this.logic_system.run();
};

Expand Down
79 changes: 79 additions & 0 deletions tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -576,3 +576,82 @@ QUnit.test("test undo", function(assert) {
deleteDatabase("testdatabase10");
});

function buildHalfAdder(project, schema) {
var new_gate;
var schema_id = schema.id;
schema.on("gateAdded", function(id) { new_gate = id; });
project.dispatchAction(new Action({type: "ADD_GATE", schema_id: schema_id, gate_type: "INPUT", position: new Point(0, 0)}));
var input1 = new_gate;
project.dispatchAction(new Action({type: "ADD_GATE", schema_id: schema_id, gate_type: "INPUT", position: new Point(0, 0)}));
var input2 = new_gate;
project.dispatchAction(new Action({type: "ADD_GATE", schema_id: schema_id, gate_type: "AND", position: new Point(0, 0)}));
var input_and = new_gate;
project.dispatchAction(new Action({type: "ADD_GATE", schema_id: schema_id, gate_type: "OR", position: new Point(0, 0)}));
var or = new_gate;
project.dispatchAction(new Action({type: "ADD_GATE", schema_id: schema_id, gate_type: "NOT", position: new Point(0, 0)}));
var not = new_gate;
project.dispatchAction(new Action({type: "ADD_GATE", schema_id: schema_id, gate_type: "AND", position: new Point(0, 0)}));
var output_and = new_gate;
project.dispatchAction(new Action({type: "ADD_GATE", schema_id: schema_id, gate_type: "OUTPUT", position: new Point(0, 0)}));
var carry = new_gate
project.dispatchAction(new Action({type: "ADD_GATE", schema_id: schema_id, gate_type: "OUTPUT", position: new Point(0, 0)}));
var sum = new_gate;
project.dispatchAction(new Action({type: "ADD_CONNECTION", schema_id: schema_id,
output_item: input1, output_num: 0, input_item: input_and, input_num: 0}));
project.dispatchAction(new Action({type: "ADD_CONNECTION", schema_id: schema_id,
output_item: input1, output_num: 0, input_item: or, input_num: 0}));
project.dispatchAction(new Action({type: "ADD_CONNECTION", schema_id: schema_id,
output_item: input2, output_num: 0, input_item: input_and, input_num: 1}));
project.dispatchAction(new Action({type: "ADD_CONNECTION", schema_id: schema_id,
output_item: input2, output_num: 0, input_item: or, input_num: 1}));
project.dispatchAction(new Action({type: "ADD_CONNECTION", schema_id: schema_id,
output_item: input_and, output_num: 0, input_item: carry, input_num: 0}));
project.dispatchAction(new Action({type: "ADD_CONNECTION", schema_id: schema_id,
output_item: input_and, output_num: 0, input_item: not, input_num: 0}));
project.dispatchAction(new Action({type: "ADD_CONNECTION", schema_id: schema_id,
output_item: not, output_num: 0, input_item: output_and, input_num: 0}));
project.dispatchAction(new Action({type: "ADD_CONNECTION", schema_id: schema_id,
output_item: or, output_num: 0, input_item: output_and, input_num: 1}));
project.dispatchAction(new Action({type: "ADD_CONNECTION", schema_id: schema_id,
output_item: output_and, output_num: 0, input_item: sum, input_num: 0}));
}

QUnit.test("embed a template in a schema", function(assert) {
var project = getAProject("testdatabase11", "test_project");
var component = addSchema(project);
buildHalfAdder(project, component);

var schema = addSchema(project);
var new_gate;
var schema_id = schema.id;
schema.on("gateAdded", function(id) { new_gate = id; });
project.dispatchAction(new Action({type: "ADD_GATE", schema_id: schema_id, gate_type: "SWITCH", position: new Point(0, 0)}));
var input1 = new_gate;
project.dispatchAction(new Action({type: "ADD_GATE", schema_id: schema_id, gate_type: "SWITCH", position: new Point(0, 0)}));
var input2 = new_gate;
project.dispatchAction(new Action({type: "ADD_GATE", schema_id: schema_id, gate_type: "COMPONENT:" + component.id, position: new Point(0, 0)}));
var adder_instance = new_gate;
project.dispatchAction(new Action({type: "ADD_GATE", schema_id: schema_id, gate_type: "BULB", position: new Point(0, 0)}));
var carry = new_gate
project.dispatchAction(new Action({type: "ADD_GATE", schema_id: schema_id, gate_type: "BULB", position: new Point(0, 0)}));
var sum = new_gate;
project.dispatchAction(new Action({type: "ADD_CONNECTION", schema_id: schema_id,
output_item: input1, output_num: 0, input_item: adder_instance, input_num: 0}));
project.dispatchAction(new Action({type: "ADD_CONNECTION", schema_id: schema_id,
output_item: input2, output_num: 0, input_item: adder_instance, input_num: 1}));
project.dispatchAction(new Action({type: "ADD_CONNECTION", schema_id: schema_id,
output_item: adder_instance, output_num: 0, input_item: carry, input_num: 0}));
project.dispatchAction(new Action({type: "ADD_CONNECTION", schema_id: schema_id,
output_item: adder_instance, output_num: 1, input_item: sum, input_num: 0}));

var truth_table = [
[0,0,0,0],
[0,1,0,1],
[1,0,0,1],
[1,1,1,0],
];

checkTruthTable2(truth_table, "test a subsircuit", schema, [input1, input2], [carry, sum], assert);
deleteDatabase("testdatabase11");
});

0 comments on commit f2f50cc

Please sign in to comment.