Unit test framework for web client-side code

- Also make formatSize nicer and test it.

Now there's no excuse! ;)
This commit is contained in:
Aldo Cortesi 2015-01-02 15:29:51 +13:00
parent b14b4ace25
commit 80339aef93
6 changed files with 91 additions and 55 deletions

View File

@ -127,7 +127,7 @@ class WebMaster(flow.FlowMaster):
self.app = app.Application(self, self.options.wdebug)
if options.rfile:
try:
print(self.load_flows_file(options.rfile))
self.load_flows_file(options.rfile)
except flow.FlowReadError, v:
self.add_event(
"Could not read flow file: %s"%v,

File diff suppressed because one or more lines are too long

View File

@ -1,44 +1,56 @@
{
"name": "mitmproxy",
"private": true,
"dependencies": {
"name": "mitmproxy",
"private": true,
"scripts": {
"test": "jest ./src/js"
},
"jest": {
"testPathDirs": [
"./src/js"
],
"testPathIgnorePatterns": [
"/node_modules/",
"testutils.js"
],
"testDirectoryName": "tests"
},
"dependencies": {
"jquery": "",
"lodash": "",
"react": "",
"react-router": "",
"bootstrap": "",
"flux": ""
},
"devDependencies": {
"bower": "",
"browserify": "^6.3.4",
"gulp": "",
"gulp-concat": "",
"gulp-connect": "",
"gulp-jshint": "",
"gulp-less": "",
"gulp-livereload": "",
"gulp-minify-css": "",
"gulp-notify": "",
"gulp-plumber": "",
"gulp-react": "",
"gulp-rename": "",
"gulp-replace": "",
"gulp-rev": "",
"gulp-sourcemaps": "",
"gulp-uglify": "",
"gulp-util": "",
"jest-cli": "^0.1.18",
"jshint-stylish": "",
"lodash": "",
"map-stream": "",
"react": "",
"react-tools": "",
"reactify": "",
"vinyl-buffer": "",
"vinyl-source-stream": "",
"vinyl-transform": "",
"gulp-peg": ""
}
},
"devDependencies": {
"bower": "",
"browserify": "^6.3.4",
"gulp": "",
"gulp-concat": "",
"gulp-connect": "",
"gulp-jshint": "",
"gulp-less": "",
"gulp-livereload": "",
"gulp-minify-css": "",
"gulp-notify": "",
"gulp-plumber": "",
"gulp-react": "",
"gulp-rename": "",
"gulp-replace": "",
"gulp-rev": "",
"gulp-sourcemaps": "",
"gulp-uglify": "",
"gulp-util": "",
"jest-cli": "^0.1.18",
"jshint-stylish": "",
"lodash": "",
"map-stream": "",
"react": "",
"react-tools": "",
"reactify": "",
"vinyl-buffer": "",
"vinyl-source-stream": "",
"vinyl-transform": "",
"gulp-peg": ""
}
}

View File

@ -1,3 +0,0 @@
QUnit.test("example test", function (assert) {
assert.ok(true);
});

15
web/src/js/tests/utils.js Normal file
View File

@ -0,0 +1,15 @@
jest.dontMock("jquery");
jest.dontMock("../utils");
describe("utils", function () {
var utils = require("../utils");
it("formatSize", function(){
expect(utils.formatSize(1024)).toEqual("1kb");
expect(utils.formatSize(0)).toEqual("0");
expect(utils.formatSize(10)).toEqual("10b");
expect(utils.formatSize(1025)).toEqual("1.0kb");
expect(utils.formatSize(1024*1024)).toEqual("1mb");
expect(utils.formatSize(1024*1024+1)).toEqual("1.0mb");
});
});

View File

@ -23,14 +23,20 @@ for (var i = 65; i <= 90; i++) {
var formatSize = function (bytes) {
var size = bytes;
var prefix = ["B", "KB", "MB", "GB", "TB"];
var i = 0;
while (Math.abs(size) >= 1024 && i < prefix.length - 1) {
i++;
size = size / 1024;
if (bytes === 0)
return "0";
var prefix = ["b", "kb", "mb", "gb", "tb"];
for (var i = 0; i < prefix.length; i++){
if (Math.pow(1024, i + 1) > bytes){
break;
}
}
return (Math.floor(size * 100) / 100.0).toFixed(2) + prefix[i];
var precision;
if (bytes%Math.pow(1024, i) === 0)
precision = 0;
else
precision = 1;
return (bytes/Math.pow(1024, i)).toFixed(precision) + prefix[i];
};