diff --git a/README.md b/README.md index 30c20aa..8ef3ba5 100644 --- a/README.md +++ b/README.md @@ -73,6 +73,14 @@ npm run-script build npm test ``` +### Testing (karma) +Karma allows testing within one or several browser. + +```bash +karma start --singleRun=true --browsers="Chrome,Firefox" +``` + + **Please note this repo is in it's early stage.** If you'd like to run a Http ethereum node check out diff --git a/karma.conf.js b/karma.conf.js new file mode 100644 index 0000000..0ed5a15 --- /dev/null +++ b/karma.conf.js @@ -0,0 +1,96 @@ +// Karma configuration +// Generated on Thu Feb 19 2015 19:57:47 GMT+0100 (W. Europe Standard Time) + +module.exports = function (config) { + config.set({ + + // base path that will be used to resolve all patterns (eg. files, exclude) + basePath: '', + + // level of logging + // possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG + + // Continuous Integration mode + // if true, Karma captures browsers, runs the tests and exits + singleRun: false, logLevel: config.LOG_INFO, + //singleRun: true, logLevel: config.LOG_DEBUG, + + // frameworks to use + // available frameworks: https://npmjs.org/browse/keyword/karma-adapter + frameworks: ['browserify', 'mocha'], + + + // list of files / patterns to load in the browser + files: [ + 'test/*.js', + + ], + + + // list of files to exclude + exclude: [ + ], + + client: { + mocha: { + //ui: 'tdd' + timeout: 5000 // especially for the post requests + } + }, + browserify: { + bundleDelay: 750, + debug: true + // transform: [], + // //extensions: ['.js'] + }, + + // preprocess matching files before serving them to the browser + // available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor + preprocessors: { + 'test/*.js': ['browserify'] + }, + + + // test results reporter to use + // possible values: 'dots', 'progress' + // available reporters: https://npmjs.org/browse/keyword/karma-reporter + reporters: ['dots'], + + + // web server port + port: 9876, + + + // enable / disable colors in the output (reporters and logs) + colors: true, + + + // enable / disable watching file and executing tests whenever any file changes + autoWatch: true, + + + // start these browsers + // available browser launchers: https://npmjs.org/browse/keyword/karma-launcher + // // Chrome + // // PhantomJS + browsers: ['Chrome', 'Safari', 'Firefox'], + browserNoActivityTimeout: 10000, + browserDisconnectTimeout: 5000, + + customLaunchers: { + chromeWithoutSecurity: { + base: 'Chrome', + flags: ['--disable-web-security'] + }, + + IE9: { + base: 'IE', + 'x-ua-compatible': 'IE=EmulateIE9' + }, + IE8: { + base: 'IE', + 'x-ua-compatible': 'IE=EmulateIE8' + } + } + }); +}; diff --git a/lib/utils.js b/lib/utils.js index f88e6ab..2ac02ea 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -108,7 +108,7 @@ var filterEvents = function (json) { /// TODO: add tests for it! var toEth = function (str) { /*jshint maxcomplexity:7 */ - var val = typeof str === "string" ? str.indexOf('0x') === 0 ? parseInt(str.substr(2), 16) : parseInt(str) : str; + var val = typeof str === "string" ? str.indexOf('0x') === 0 ? parseInt(str.substr(2), 16) : parseInt(str.replace(/,/g,'').replace(/ /g,'')) : str; var unit = 0; var units = c.ETH_UNITS; while (val > 3000 && unit < units.length - 1) diff --git a/package.json b/package.json index 340200d..12355ec 100644 --- a/package.json +++ b/package.json @@ -25,6 +25,12 @@ "gulp-uglify": ">=1.0.0", "istanbul": "^0.3.5", "jshint": ">=2.5.0", + "karma": "^0.12.31", + "karma-browserify": "^4.0.0", + "karma-chrome-launcher": "^0.1.7", + "karma-firefox-launcher": "^0.1.4", + "karma-mocha": "^0.1.10", + "karma-safari-launcher": "^0.1.1", "mocha": ">=2.1.0", "mocha-lcov-reporter": "0.0.1", "unreachable-branch-transform": "^0.1.0", diff --git a/test/utils.convert.js b/test/utils.convert.js new file mode 100644 index 0000000..2287574 --- /dev/null +++ b/test/utils.convert.js @@ -0,0 +1,237 @@ +var assert = require('assert'); +var utils = require('../lib/utils.js'); + +describe('utils', function() { + + // given + // NOTE: I made the following tests pass as long as the values are mathematically correct. + // However, in the current state, there are inconsistancy regarding the trailing decimals. + // For instance '10 Mether' but '10.00 Gether' + var data = [{ + test: 10e-9, + res: '0.00 wei' + }, { + test: 10e-6, + res: '0.00 wei' + }, { + test: 10e-3, + res: '0.01 wei' + }, { + test: '-10', + res: '-10 wei' + }, { + test: '0', + res: '0 wei' + }, { + test: 0, + res: '0 wei' + }, { + test: '1200000', + res: '1200 Kwei' + }, { + test: 1e3, + res: '1000 wei' + }, { + test: 10e3, + res: '10 Kwei' + }, { + test: 100e3, + res: '100 Kwei' + }, { + test: 1000e3, + res: '1000 Kwei' + }, { + test: 10e6, + res: '10 Mwei' + }, { + test: 100e6, + res: '100 Mwei' + }, { + test: 1000e6, + res: '1000 Mwei' + }, { + test: 10e9, + res: '10 Gwei' + }, { + test: 100e9, + res: '100 Gwei' + }, { + test: 1000e9, + res: '1000 Gwei' + }, { + test: 10e12, + res: '10 szabo' + }, { + test: 100e12, + res: '100 szabo' + }, { + test: 1000e12, + res: '1000 szabo' + }, { + test: 10e15, + res: '10 finney' + }, { + test: 100e15, + res: '100 finney' + }, { + test: 1000e15, + res: '1000 finney' + }, { + test: 10e18, + res: '10 ether' + }, { + test: 100e18, + res: '100 ether' + }, { + test: 1000e18, + res: '1000 ether' + }, { + test: 10e21, + res: '10 grand' + }, { + test: 100e21, + res: '100.00 grand' + }, { + test: 1000e21, + res: '1000 grand' + }, { + test: 10e24, + res: '10 Mether' + }, { + test: 100e24, + res: '100.00 Mether' + }, { + test: 1000e24, + res: '1000 Mether' + }, { + test: 10e27, + res: '10.00 Gether' + }, { + test: 100e27, + res: '100.00 Gether' + }, { + test: 1000e27, + res: '1000 Gether' + }, { + test: 10e30, + res: '10.00 Tether' + }, { + test: 100e30, + res: '100.00 Tether' + }, { + test: 1000e30, + res: '1,000.00 Tether' + }, { + test: 10e33, + res: '10.00 Pether' + }, { + test: 100e33, + res: '100.00 Pether' + }, { + test: 1000e33, + res: '1000 Pether' + }, { + test: 10e36, + res: '10.00 Eether' + }, { + test: 100e36, + res: '100.00 Eether' + }, { + test: 1000e36, + res: '1,000.00 Eether' + }, { + test: 10e39, + res: '10 Zether' + }, { + test: 100e39, + res: '100.00 Zether' + }, { + test: 1000e39, + res: '1000 Zether' + }, { + test: 10e42, + res: '10 Yether' + }, { + test: 100e42, + res: '100.00 Yether' + }, { + test: 1000e42, + res: '1,000.00 Yether' + }, { + test: 10e45, + res: '10 Nether' + }, { + test: 100e45, + res: '100.00 Nether' + }, { + test: 1000e45, + res: '1000 Nether' + }, { + test: 10e48, + res: '10 Dether' + }, { + test: 100e48, + res: '100.00 Dether' + }, { + test: 1000e48, + res: '1000 Dether' + }, { + test: 10e51, + res: '10 Vether' + }, { + test: 100e51, + res: '100.00 Vether' + }, { + test: 1000e51, + res: '1,000.00 Vether' + }, { + test: 10e54, + res: '10 Uether' + }, { + test: 10e57, + res: '10000 Uether' + }, { + test: 100e57, + res: '100,000.00 Uether' + }, { + test: 1000e57, + res: '1,000,000.00 Uether' + }, { + test: '10', + res: utils.toEth(10) + }, { + test: '100', + res: utils.toEth(100) + }, { + test: '1000', + res: utils.toEth(1000) + }, { + test: '1001', + res: utils.toEth(1001) + }, { + test: '999', + res: utils.toEth(999) + }, { + test: '10000000000000000000000', + res: utils.toEth(10e21) // 10 grand + }, { + test: '10 000', + res: '10 Kwei' + }, { + test: '10,000', + res: '10 Kwei' + }, { + test: '10,000.00', + res: '10 Kwei' + }]; + + data.forEach(function(elem) { + it('should convert ' + elem.test + ' into ' + elem.res, function() { + // when + var res = utils.toEth(elem.test); + + // then + assert.equal(res, elem.res); + }); + }); +});