started to implement unit test. started to get rid of composition step.

This commit is contained in:
Borys Levytskyi
2015-04-11 18:24:40 +03:00
parent 604ea0918b
commit 400f742fef
9 changed files with 131 additions and 106 deletions

View File

@@ -6,9 +6,12 @@ module.exports = function(config) {
'src/js/core/core.js',
'src/js/core/is.js',
'src/js/core/di.js',
'src/js/core/should.js',
'src/js/core/appShell.js',
'src/js/core/observable.js',
'src/js/app.js',
'src/js/components/*.js',
'src/js/app/**/*.js',
'tests/*.js'
]
});

View File

@@ -13,9 +13,11 @@
app.debugMode = false;
app.bootstrap = function(rootViewElement) {
console.group('Bootstrap');
this.rootViewElement = rootViewElement;
this.set('rootView', rootViewElement)
this.initialize();
console.groupEnd();
};

View File

@@ -2,6 +2,7 @@ app.compose(function() {
"use strict";
var should = app.get('should')
app.set('calc', {
numberOfBits: function (num) {
@@ -18,7 +19,10 @@ app.compose(function() {
}
return Math.max.apply(null, counts);
},
calcExpression: function (expr) {
return eval(expr.string);
}
});
});

View File

@@ -1,10 +1,10 @@
app.compose(function() {
app.set('expression', function() {
"use strict";
var twoOperandsRegex = /^(\d+)\s*(<<|>>|\||\&|\^)\s*(\d+)$/;
var numbersList = /^((\d*)+\s?)+$/;
app.set('expression', {
return {
canParse: function(string) {
return twoOperandsRegex.test(string) || numbersList.test(string);
},
@@ -21,7 +21,7 @@ app.compose(function() {
return createListOfNumbersExpression(string)
}
}
});
};
function createCalculableExpression(matches) {
@@ -33,7 +33,7 @@ app.compose(function() {
m.operand2 = o2;
m.sign = matches[2];
m.string = matches.input;
m.result = eval(matches.input);
//m.result = eval(matches.input);
return m;
}

View File

@@ -1,99 +1,95 @@
app.compose(function() {
"use strict";
"use strict";
app.controller('expressionInputCtrl', function (){
var cmd = app.get('cmd');
app.controller('expressionInputCtrl', function (){
var cmd = app.get('cmd');
return {
onViewAttached: function () {
var self = this;
self.history =[];
self.historyIndex = 0;
this.viewElement.focus();
this.viewElement.addEventListener('keyup', function (args) {
var inpt = args.target;
if (args.keyCode != 13 || inpt.value.trim().length == 0) {
return;
}
// Enter
cmd.execute(inpt.value);
self.history.unshift(inpt.value);
self.historyIndex = 0;
inpt.value = '';
});
this.viewElement.addEventListener('keydown', function(args){
if(args.keyCode == 38) {
if (self.history.length > self.historyIndex) { // up
args.target.value = self.history[self.historyIndex++];
}
args.preventDefault();
return;
}
if(args.keyCode == 40) {
if(self.historyIndex > 0) { // up
args.target.value = self.history[--self.historyIndex];
}
args.preventDefault();
}
})
}
}
});
app.controller('cmdController', function() {
var html = app.get('html');
var rootView = app.get('rootView');
return {
clear: function () {
this.viewElement.innerHTML = '';
},
display: function ( model) {
var view = app.buildViewFor(model);
var vw = this.viewElement;
if(vw.childNodes.length == 0) {
vw.appendChild(view);
}
else {
vw.insertBefore(view, vw.childNodes[0]);
}
}
};
});
app.controller('configPanelCtrl', {
onViewAttached: function (){
return {
onViewAttached: function () {
var self = this;
var cfg = app.get('cmdConfig');
self.update(cfg);
self.history =[];
self.historyIndex = 0;
cfg.observe(function(){
self.update(cfg);
this.viewElement.focus();
this.viewElement.addEventListener('keyup', function (args) {
var inpt = args.target;
if (args.keyCode != 13 || inpt.value.trim().length == 0) {
return;
}
// Enter
cmd.execute(inpt.value);
self.history.unshift(inpt.value);
self.historyIndex = 0;
inpt.value = '';
});
},
update: function (cfg) {
var emIndicator = this.viewElement.querySelector('#emphasizeBytes');
var reg = /\son/g;
if(cfg.emphasizeBytes) {
emIndicator.classList.add("on");
} else {
emIndicator.classList.remove("on");
}
this.viewElement.addEventListener('keydown', function(args){
if(args.keyCode == 38) {
if (self.history.length > self.historyIndex) { // up
args.target.value = self.history[self.historyIndex++];
}
args.preventDefault();
return;
}
if(args.keyCode == 40) {
if(self.historyIndex > 0) { // up
args.target.value = self.history[--self.historyIndex];
}
args.preventDefault();
}
})
}
});
}
});
app.controller('cmdController', function() {
var html = app.get('html');
var rootView = app.get('rootView');
return {
clear: function () {
this.viewElement.innerHTML = '';
},
display: function ( model) {
var view = app.buildViewFor(model);
var vw = this.viewElement;
if(vw.childNodes.length == 0) {
vw.appendChild(view);
}
else {
vw.insertBefore(view, vw.childNodes[0]);
}
}
};
});
app.controller('configPanelCtrl', {
onViewAttached: function (){
var self = this;
var cfg = app.get('cmdConfig');
self.update(cfg);
cfg.observe(function(){
self.update(cfg);
});
},
update: function (cfg) {
var emIndicator = this.viewElement.querySelector('#emphasizeBytes');
var reg = /\son/g;
if(cfg.emphasizeBytes) {
emIndicator.classList.add("on");
} else {
emIndicator.classList.remove("on");
}
}
});

View File

@@ -9,16 +9,19 @@ app.compose(function () {
app.modelView(app.models.BitwiseOperation, {
renderView: function(expr) {
var maxLen = getBinaryLength([expr.operand1, expr.operand2, expr.result]);
var result = calc.calcExpression(expr);
var maxLen = getBinaryLength([expr.operand1, expr.operand2, result]);
expr.operand1Binary = formatter.toBinaryString(expr.operand1, maxLen);
expr.operand2Binary = formatter.toBinaryString(expr.operand2, maxLen);
expr.resultBinary = formatter.toBinaryString(expr.result, maxLen);
var model = Object.create(expr);
model.result = result;
model.operand1Binary = formatter.toBinaryString(expr.operand1, maxLen);
model.operand2Binary = formatter.toBinaryString(expr.operand2, maxLen);
model.resultBinary = formatter.toBinaryString(result, maxLen);
var templateId = /<<|>>/.test(expr.sign) ? 'shiftExpressionView' : 'binaryExpressionView';
var templateId = /<<|>>/.test(model.sign) ? 'shiftExpressionView' : 'binaryExpressionView';
var template = app.template(templateId)
var el = template.render(expr);
var el = template.render(model);
colorizeBits(el);
return el;
}

View File

@@ -74,6 +74,8 @@
if(is.aFunction(this.onFirstTimeResolve)){
this.onFirstTimeResolve(this.resolved);
}
console.log('resolved:', this.name);
};
Container.Registration = Registration;

21
tests/expressionSpec.js Normal file
View File

@@ -0,0 +1,21 @@
describe("expression parse", function() {
var app = window.app;
var expression = app.get('expression');
var expressionsCases = {
"1 2 3": { numbers: [1,2,3] },
"1": { numbers: [1] },
"2>>1": { operand1: 2, operand2:1, "sign":">>", string:"2>>1" },
"123|111": { operand1: 123, operand2:111, "sign":"|", string: "123|111" },
"23^1": { operand1: 23, operand2:1, "sign":"^", string: "23^1" }
};
it("should parse decimal expressions", function() {
var input, expr;
for(input in expressionsCases) {
expect(expression.canParse(input)).toBe(true);
expr = expression.parse(input);
expect(JSON.stringify(expr)).toEqual(JSON.stringify(expressionsCases[input]));
}
});
});

View File

@@ -1,6 +0,0 @@
describe("A for app", function() {
var app = window.app;
it("may there be app", function() {
expect(app).not.toBe(null);
});
});