Skip to content

Commit

Permalink
fixing testdata inclusion in browser
Browse files Browse the repository at this point in the history
  • Loading branch information
maraoz committed Mar 10, 2014
1 parent 8f3ae30 commit a2e6e88
Show file tree
Hide file tree
Showing 13 changed files with 65 additions and 111 deletions.
12 changes: 3 additions & 9 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,8 @@ module.exports = function(grunt) {
grunt.initConfig({
shell: {
browserify: {
options: {
stdout: true
},
command: 'node ./browserify.js > browser/bundle.js',
},
browserifyData: {
command: 'browserify -t brfs -s testdata test/testdata.js -o browser/testdata.js'
},
command: 'node ./browserify.js',
}
},
watch: {
readme: {
Expand All @@ -28,7 +22,7 @@ module.exports = function(grunt) {
},
scripts: {
files: ['**/*.js', '**/*.html', '!**/node_modules/**', '!browser/bundle.js', '!browser/testdata.js'],
tasks: ['shell' /*, 'mochaTest'*/ ],
tasks: ['shell'],
},
},
mochaTest: {
Expand Down
10 changes: 3 additions & 7 deletions bitcore.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
module.exports.bignum = require('bignum');
module.exports.base58 = require('base58-native');
module.exports.buffertools = require('buffertools');
module.exports.Buffer = Buffer;

module.exports.config = require('./config');
module.exports.const = require('./const');
Expand All @@ -33,17 +34,12 @@ module.exports.PrivateKey = require('./PrivateKey');
module.exports.RpcClient = require('./RpcClient');
module.exports.Wallet = require('./Wallet');
module.exports.WalletKey = require('./WalletKey');
module.exports.Buffer = Buffer;

if (typeof process.versions === 'undefined') {
// Browser specific
module.exports.bignum.config({EXPONENTIAL_AT: 9999999, DECIMAL_PLACES: 0, ROUNDING_MODE: 1});
// module.exports.PeerManager = function () {
// throw new Error('PeerManager not availabe in browser Bitcore, under .bitcore. Use it with: require(\'PeerManager\');');
// };
}
else {
// Nodejs specific
} else {
// Node specific
module.exports.PeerManager = require('./PeerManager');
}

50 changes: 22 additions & 28 deletions browserify.js → browser/browserify.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict';

/*
* Example for usage of browserify with soop
*
* The key parameter 'pack'
* The supplied 'custom_prelude.js' file is needed for
Expand All @@ -11,22 +10,17 @@
var fs = require('fs');
var browserify = require('browserify');
var browserPack = require('browser-pack');
var opts = {};


var preludePath = 'node_modules/soop/example/custom_prelude.js';

var pack = function (params) {
var preludePath = 'node_modules/soop/example/custom_prelude.js';
params.raw = true;
params.sourceMapPrefix = '//#';
params.prelude= fs.readFileSync(preludePath, 'utf8');
params.preludePath= preludePath;
params.prelude = fs.readFileSync(preludePath, 'utf8');
params.preludePath = preludePath;
return browserPack(params);
};

opts.pack = pack;
opts.debug = true;

var modules = [
'Address',
'Block',
Expand All @@ -51,12 +45,18 @@ var modules = [
'config',
'const',
'networks',
'bitcore',
];

var opts = {};
//opts.pack = pack;
opts.debug = true;
opts.standalone = 'bitcore';
opts.insertGlobals = true;

var b = browserify(opts);
b.require('browserify-bignum/bignumber.js', {expose: 'bignum'} );
b.require('browserify-buffertools/buffertools.js', {expose:'buffertools'});
b.require('./bitcore', {expose: 'bitcore'});
b.require('buffer', {expose: 'buffer'});
b.require('base58-native');
b.require('./Key.js', {expose: 'KeyModule'});
Expand All @@ -65,27 +65,21 @@ b.require('./util/util');
b.require('./util/EncodedData');
b.require('./util/VersionedData');
b.add('./browser/bignum_config.js');
b.require('./test/testdata.js', {expose: './testdata'});
b.transform('brfs');

b.require('./Connection', {expose: './Connection'});

modules.forEach(function(m) {
b.require('./' + m + '.js' ,{expose:m} );
});

var bopts = {
transform: ['brfs']
// detectGlobals: true,
// insertGlobals: 'Buffer',
// insertGlobalVars: {
// Buffer: function () {
// return 'require("buffer").Buffer';
// },
// },
};
b.require('./' + m + '.js' ,{expose: './'+m} );
});
b.require('soop');

b.bundle().pipe(fs.createWriteStream('browser/bundle.js'));


opts.standalone = 'testdata';
var tb = browserify(opts);
tb.require('./test/testdata', {expose: 'testdata'});
tb.transform('brfs');

b.bundle(bopts).pipe(process.stdout);
tb.bundle().pipe(fs.createWriteStream('browser/testdata.js'));



Expand Down
24 changes: 5 additions & 19 deletions test/adapter.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,6 @@
'use strict';

if (typeof require === 'undefined') {
var that = this;
that.require = function(name) {
var split = name.split('/');
if (split.length > 0) {
name = split.pop();
}
var module = that[name];
if (!module) {
if (!bitcore[name])
throw new Error('Cannot find module "' + name + '"');
return bitcore[name];
}
return module;
};
this.Buffer = require('Buffer');

}
if (typeof process === 'undefined') {
var bitcore = require('bitcore');
var testdata = require('testdata');
var Buffer = bitcore.Buffer;
}
12 changes: 2 additions & 10 deletions test/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,8 @@
<script src="../node_modules/chai/chai.js"></script>
<script>mocha.setup('bdd')</script>
<script src="../browser/bundle.js"></script>

<script>
var bitcore = require('bitcore');
this.Buffer = require('buffer').Buffer;
</script>


<!-- <script src="adapter.js"></script> -->
<script src="../browser/testdata.js"></script>
<script src="adapter.js"></script>

<script src="test.Address.js"></script>
<script src="test.basic.js"></script>
Expand All @@ -44,8 +38,6 @@
<script src="test.VersionedData.js"></script>
<script src="test.Wallet.js"></script>
<script src="test.WalletKey.js"></script>
<!--
-->
<script>
mocha.run();
</script>
Expand Down
2 changes: 1 addition & 1 deletion test/test.Address.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

var chai = chai || require('chai');
var bitcore = bitcore || require('../bitcore');
var bitcore = bitcore || require('./bitcore');

var should = chai.should();

Expand Down
2 changes: 1 addition & 1 deletion test/test.PeerManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ var bitcore = bitcore || require('../bitcore');

var should = chai.should();

var PeerManagerModule = bitcore.PeerManager || require('PeerManager');
var PeerManagerModule = bitcore.PeerManager || require('soop').load('PeerManager');

var PeerManager;

Expand Down
4 changes: 2 additions & 2 deletions test/test.Script.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ var ScriptModule = bitcore.Script;
var Address = bitcore.Address;
var networks = bitcore.networks;
var Script;
var test_data = require('./testdata');
var testdata = testdata || require('./testdata');

describe('Script', function() {
it('should initialze the main object', function() {
Expand Down Expand Up @@ -84,7 +84,7 @@ describe('Script', function() {
});
});

test_data.dataScriptAll.forEach(function(datum) {
testdata.dataScriptAll.forEach(function(datum) {
if (datum.length < 2) throw new Error('Invalid test data');
var human = datum[0] + ' ' + datum[1];
it('should parse script from human readable ' + human, function() {
Expand Down
4 changes: 2 additions & 2 deletions test/test.ScriptInterpreter.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ var chai = chai || require('chai');
var bitcore = bitcore || require('../bitcore');

var should = chai.should();
var test_data = require('./testdata');
var testdata = testdata || require('./testdata');

var ScriptInterpreterModule = bitcore.ScriptInterpreter;
var Script = bitcore.Script;
Expand All @@ -23,7 +23,7 @@ describe('ScriptInterpreter', function() {
should.exist(si);
});
var i = 0;
test_data.dataScriptValid.forEach(function(datum) {
testdata.dataScriptValid.forEach(function(datum) {
if (datum.length < 2) throw new Error('Invalid test data');
var scriptSig = datum[0]; // script inputs
var scriptPubKey = datum[1]; // output script
Expand Down
5 changes: 3 additions & 2 deletions test/test.Transaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ var In;
var Out;
var Script = bitcore.Script;
var buffertools = require('buffertools');
var test_data = require('./testdata');
var testdata = testdata || require('./testdata');

describe('Transaction', function() {
it('should initialze the main object', function() {
Expand All @@ -35,7 +35,7 @@ describe('Transaction', function() {
// Inner arrays are either [ "comment" ]
// or [[[prevout hash, prevout index, prevout scriptPubKey], [input 2], ...],"], serializedTransaction, enforceP2SH
// ... where all scripts are stringified scripts.
test_data.dataTxValid.forEach(function(datum) {
testdata.dataTxValid.forEach(function(datum) {
if (datum.length === 3) {
it.skip('valid tx=' + datum[1], function(done) {
var inputs = datum[0];
Expand All @@ -47,6 +47,7 @@ describe('Transaction', function() {
map[[hash, index]] = scriptPubKey; //Script.fromStringContent(scriptPubKey);
console.log(scriptPubKey.getStringContent());
console.log('********************************');
done();

});
var raw = new Buffer(datum[1], 'hex');
Expand Down
17 changes: 3 additions & 14 deletions test/test.basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,7 @@
var chai = chai || require('chai');
var bitcore = bitcore || require('../bitcore');

var test_data;
if (typeof dataValid !== 'undefined' ) {
test_data = {
dataValid: dataValid,
dataInvalid: dataInvalid,
};

}
else {
test_data = require('./testdata');
}

var testdata = testdata || require('./testdata');
var should = chai.should();

var Address = bitcore.Address;
Expand Down Expand Up @@ -122,15 +111,15 @@ function is_invalid(datum) {
}

describe('Valid base58 keys', function() {
test_data.dataValid.forEach(function(datum) {
testdata.dataValid.forEach(function(datum) {
it('valid ' + datum[0], function() {
is_valid(datum);
});
});
});

describe('Invalid base58 keys', function() {
test_data.dataInvalid.forEach(function(datum) {
testdata.dataInvalid.forEach(function(datum) {
it('invalid ' + datum, function() {
is_invalid(datum);
});
Expand Down
10 changes: 3 additions & 7 deletions test/test.misc.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@

var chai = chai || require('chai');
var bitcore = bitcore || require('../bitcore');
var buffertools = require('buffertools');

var should = chai.should();

var test_data = require('./testdata');
var testdata = testdata || require('./testdata');

var bignum = bitcore.bignum;
var base58 = bitcore.base58;
var base58Check = base58.base58Check;
var util = bitcore.util;
var buffertools = require('buffertools');

describe('Miscelaneous stuff', function() {
it('should initialze the config object', function() {
Expand All @@ -20,9 +19,6 @@ describe('Miscelaneous stuff', function() {
it('should initialze the log object', function() {
should.exist(bitcore.log);
});
it('should initialze the util object', function() {
should.exist(bitcore.util);
});
it('should initialze the const object', function() {
should.exist(bitcore.const);
});
Expand Down Expand Up @@ -59,7 +55,7 @@ describe('Miscelaneous stuff', function() {
var m = '1QCJj1gPZKx2EwzGo9Ri8mMBs39STvDYcv';
base58Check.encode(base58Check.decode(m)).should.equal(m);
});
test_data.dataEncodeDecode.forEach(function(datum) {
testdata.dataEncodeDecode.forEach(function(datum) {
it('base58 encode/decode checks ' + datum, function() {
// from bitcoin/bitcoin tests:
// Goal: test low-level base58 encoding functionality
Expand Down

0 comments on commit a2e6e88

Please sign in to comment.