Added support for bin, dec, hex modes to formatter. Unit tests support

This commit is contained in:
Borys Levytskyi
2015-04-11 19:16:36 +03:00
parent 62c62933d7
commit 81a9c9ba77
7 changed files with 56 additions and 19 deletions

View File

@@ -7,7 +7,8 @@
app.set('cmdConfig', core.ObservableObject.create({
emphasizeBytes: true,
theme: 'dark'
theme: 'dark',
mode: 'dec'
}));
app.debugMode = false;

View File

@@ -2,27 +2,38 @@ app.set("formatter", function() {
"use strict";
var should = app.get('should');
var is = app.get('is');
return {
toBinaryString: function(num, totalLength) {
formatString: function(num, mode) {
mode = mode || "bin";
var binaryStr = num.toString(2),
formatted = [],
i;
var convertedString = num.toString(getBase(mode));
return convertedString;
if(totalLength != null) {
should.bePositiveInteger(totalLength);
},
padLeft: function (str, length, symbol) {
var sb = Array.prototype.slice.call(str), symbol = symbol || "0";
if(length == null) {
return str;
}
for(i = 0; i<binaryStr.length; i++) {
formatted.push(binaryStr[i]);
while(length > sb.length) {
sb.unshift(symbol);
}
while(totalLength > formatted.length) {
formatted.unshift('0');
}
return sb.join('');
}
};
return formatted.join('');
function getBase(mode) {
switch (mode){
case 'bin': return 2;
case 'hex': return 16;
case 'dec': return 10;
}
}
});

View File

@@ -14,9 +14,9 @@ app.compose(function () {
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);
model.operand1Binary = formatter.padLeft(formatter.formatString(expr.operand1), maxLen);
model.operand2Binary = formatter.padLeft(formatter.formatString(expr.operand2), maxLen);
model.resultBinary = formatter.padLeft(formatter.formatString(result), maxLen);
var templateId = /<<|>>/.test(model.sign) ? 'shiftExpressionView' : 'binaryExpressionView';
var template = app.template(templateId)
@@ -43,7 +43,7 @@ app.compose(function () {
binCell.className = 'bin';
decCell.textContent = o;
binCell.textContent = formatter.toBinaryString(o, maxLen);
binCell.textContent = formatter.formatString(o, maxLen);
});
colorizeBits(table);

View File

@@ -28,6 +28,10 @@
array: function(obj) {
return obj instanceof Array;
},
number: function(num) {
return typeof num == "number" && !isNaN(num)
}
};
})();

View File

@@ -1,9 +1,10 @@
(function(){
"use strict";
var is = window.core.is;
window.core.should = {
beNumber: function (num, name) {
this.check(typeof num == "number" && !isNaN(num), num + " is not a number");
this.check(is.number(num), num + " is not a number");
this.check(isFinite(num), append(name, "is an infinite number"));
},

View File

@@ -0,0 +1,20 @@
describe('expression formatter', function () {
var di = app.di.clone();
var formatter = di.resolve('formatter');
it('should format number to binary by default', function() {
expect(formatter.formatString(10)).toBe("1010");
});
it('should format number hexadecimal', function() {
expect(formatter.formatString(15, 'hex')).toBe("f");
});
it('should format number decimal', function() {
expect(formatter.formatString(16, 'dec')).toBe('16');
});
it('should respect padding', function() {
expect(formatter.padLeft("a", 6)).toBe("00000a");
});
});