almost can do tests

This commit is contained in:
Peter Cottle 2012-12-17 11:26:50 -08:00
parent 565142048c
commit ddf399e099
10 changed files with 288 additions and 163 deletions

View file

@ -52,9 +52,14 @@ function UI() {
exports.getEvents = function() {
return events;
};
exports.getUI = function() {
return ui;
};
exports.getMainVis = function() {
return mainVis;
};
exports.init = init;

View file

@ -1,23 +1,24 @@
var _;
var _ = require('underscore');
var Backbone;
// horrible hack to get localStorage Backbone plugin
if (!require('../util').isBrowser()) {
_ = require('underscore');
Backbone = require('backbone');
} else {
Backbone = window.Backbone;
_ = window._;
}
var GitEngine = require('../git').GitEngine;
var AnimationFactory = require('../visuals/animation/animationFactory').AnimationFactory;
var GitVisuals = require('../visuals').GitVisuals;
var TreeCompare = require('../git/treeCompare').TreeCompare;
var Collections = require('../models/collections');
var CommitCollection = Collections.CommitCollection;
var BranchCollection = Collections.BranchCollection;
var Command = require('../models/commandModel').Command;
var mock = require('../util/mock').mock;
var util = require('../util');
var HeadlessGit = function() {
this.init();
@ -26,6 +27,7 @@ var HeadlessGit = function() {
HeadlessGit.prototype.init = function() {
this.commitCollection = new CommitCollection();
this.branchCollection = new BranchCollection();
this.treeCompare = new TreeCompare();
// here we mock visuals and animation factory so the git engine
// is headless
@ -42,5 +44,16 @@ HeadlessGit.prototype.init = function() {
this.gitEngine.init();
};
HeadlessGit.prototype.sendCommand = function(value) {
util.splitTextCommand(value, function(commandStr) {
var commandObj = new Command({
rawStr: commandStr
});
console.log('dispatching command', value);
var done = function() {};
this.gitEngine.dispatch(commandObj, done);
}, this);
};
exports.HeadlessGit = HeadlessGit;

View file

@ -11,6 +11,7 @@ if (!require('../util').isBrowser()) {
var AnimationFactoryModule = require('../visuals/animation/animationFactory');
var AnimationQueue = require('../visuals/animation').AnimationQueue;
var TreeCompare = require('./treeCompare').TreeCompare;
var Errors = require('../util/errors');
var GitError = Errors.GitError;
@ -107,8 +108,15 @@ GitEngine.prototype.exportTree = function() {
return totalExport;
};
GitEngine.prototype.printTree = function() {
var str = escape(JSON.stringify(this.exportTree()));
GitEngine.prototype.printTree = function(tree) {
tree = tree || this.exportTree();
TreeCompare.prototype.stripTreeFields([tree]);
var str = JSON.stringify(tree);
if (/'/.test(str)) {
// escape it to make it more copy paste friendly
str = escape(str);
}
return str;
};

View file

@ -1,3 +1,5 @@
var _ = require('underscore');
// static class...
function TreeCompare() {
@ -80,7 +82,10 @@ TreeCompare.prototype.compareTrees = function(treeA, treeB) {
// like createTime, message, author
this.stripTreeFields([treeA, treeB]);
console.log('comparing tree A', treeA, 'to', treeB);
return _.isEqual(treeA, treeB);
};
exports.TreeCompare = TreeCompare;

View file

@ -16,5 +16,9 @@ _.each(toGlobalize, function(module) {
_.extend(window, module);
});
window.events = toGlobalize.Main.getEvents();
$(document).ready(function() {
window.events = toGlobalize.Main.getEvents();
window.mainVis = toGlobalize.Main.getMainVis();
window.ui = toGlobalize.Main.getMainVis();
});

View file

@ -1,5 +1,24 @@
var _ = require('underscore');
exports.isBrowser = function() {
var inBrowser = String(typeof window) !== 'undefined';
return inBrowser;
};
exports.splitTextCommand = function(value, func, context) {
func = _.bind(func, context);
_.each(value.split(';'), function(command, index) {
command = _.escape(command);
command = command
.replace(/^(\s+)/, '')
.replace(/(\s+)$/, '')
.replace(/"/g, '"')
.replace(/'/g, "'");
if (index > 0 && !command.length) {
return;
}
func(command);
});
};

View file

@ -6,6 +6,8 @@ var CommandEntry = require('../models/commandModel').CommandEntry;
var Errors = require('../util/errors');
var Warning = Errors.Warning;
var util = require('../util');
var CommandPromptView = Backbone.View.extend({
initialize: function(options) {
this.collection = options.collection;
@ -232,22 +234,9 @@ var CommandPromptView = Backbone.View.extend({
}
this.index = -1;
// split commands on semicolon
_.each(value.split(';'), _.bind(function(command, index) {
command = _.escape(command);
command = command
.replace(/^(\s+)/, '')
.replace(/(\s+)$/, '')
.replace(/"/g, '"')
.replace(/'/g, "'");
if (index > 0 && !command.length) {
return;
}
util.splitTextCommand(value, function(command) {
this.addToCollection(command);
}, this));
}, this);
},
addToCollection: function(value) {

View file

@ -1,7 +1,7 @@
var _;
var Backbone;
// horrible hack to get localStorage Backbone plugin
if (!require('../util').isBrowser()) {
if (!require('../../util').isBrowser()) {
_ = require('underscore');
Backbone = require('backbone');
} else {

View file

@ -3,7 +3,7 @@ var GLOBAL = require('../../util/constants').GLOBAL;
var _;
var Backbone;
// horrible hack to get localStorage Backbone plugin
if (!require('../util').isBrowser()) {
if (!require('../../util').isBrowser()) {
_ = require('underscore');
Backbone = require('backbone');
} else {