From 400f742fef33656bf1e6c1d3ad771f11913dc661 Mon Sep 17 00:00:00 2001 From: Borys Levytskyi Date: Sat, 11 Apr 2015 18:24:40 +0300 Subject: [PATCH] started to implement unit test. started to get rid of composition step. --- karma.conf.js | 3 + src/js/app.js | 2 + src/js/app/bitwise/calc.js | 6 +- src/js/app/bitwise/expression.js | 8 +- src/js/app/controllers.js | 174 +++++++++++++++---------------- src/js/app/modelViews.js | 15 +-- src/js/core/di.js | 2 + tests/expressionSpec.js | 21 ++++ tests/stubSpec.js | 6 -- 9 files changed, 131 insertions(+), 106 deletions(-) create mode 100644 tests/expressionSpec.js delete mode 100644 tests/stubSpec.js diff --git a/karma.conf.js b/karma.conf.js index fee7859..5ee46a5 100644 --- a/karma.conf.js +++ b/karma.conf.js @@ -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' ] }); diff --git a/src/js/app.js b/src/js/app.js index 13b2226..096f3d1 100644 --- a/src/js/app.js +++ b/src/js/app.js @@ -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(); }; diff --git a/src/js/app/bitwise/calc.js b/src/js/app/bitwise/calc.js index 4be9450..7133ccd 100644 --- a/src/js/app/bitwise/calc.js +++ b/src/js/app/bitwise/calc.js @@ -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); } }); - }); diff --git a/src/js/app/bitwise/expression.js b/src/js/app/bitwise/expression.js index 0040fbe..7f53f6a 100644 --- a/src/js/app/bitwise/expression.js +++ b/src/js/app/bitwise/expression.js @@ -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; } diff --git a/src/js/app/controllers.js b/src/js/app/controllers.js index 6eb59f5..0f45645 100644 --- a/src/js/app/controllers.js +++ b/src/js/app/controllers.js @@ -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"); + } + } +}); \ No newline at end of file diff --git a/src/js/app/modelViews.js b/src/js/app/modelViews.js index f32c44a..406e557 100644 --- a/src/js/app/modelViews.js +++ b/src/js/app/modelViews.js @@ -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; } diff --git a/src/js/core/di.js b/src/js/core/di.js index 7c9557d..56708b7 100644 --- a/src/js/core/di.js +++ b/src/js/core/di.js @@ -74,6 +74,8 @@ if(is.aFunction(this.onFirstTimeResolve)){ this.onFirstTimeResolve(this.resolved); } + + console.log('resolved:', this.name); }; Container.Registration = Registration; diff --git a/tests/expressionSpec.js b/tests/expressionSpec.js new file mode 100644 index 0000000..6648ab4 --- /dev/null +++ b/tests/expressionSpec.js @@ -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])); + } + }); +}); \ No newline at end of file diff --git a/tests/stubSpec.js b/tests/stubSpec.js deleted file mode 100644 index d0dc2ff..0000000 --- a/tests/stubSpec.js +++ /dev/null @@ -1,6 +0,0 @@ -describe("A for app", function() { - var app = window.app; - it("may there be app", function() { - expect(app).not.toBe(null); - }); -}); \ No newline at end of file