Exemplo n.º 1
0
joe.describe('extract-opts', function (describe, it) {
	// Prepare
	function cb () {}
	const fixtures = {
		empty: {
			in:  [],
			out: [{}, null]
		},
		opts: {
			in:  [{a:1}],
			out: [{a:1}, null]
		},
		cb: {
			in:  [null, cb],
			out: [{}, cb]
		},
		both: {
			in:  [{a:1}, cb],
			out: [{a:1}, cb]
		},
		mixed: {
			in:  [{a:1, next:cb}],
			out: [{a:1}, cb]
		},
		rename: {
			in:  [{a:1, asd:cb}, null, {completionCallbackNames: ['asd']}],
			out: [{a:1}, cb]
		}
	}

	// Test
	eachr(fixtures, function (value, key) {
		it(key, function () {
			const actual = extractOpts.apply(extractOpts, value.in)
			deepEqual(actual, value.out, 'result comparison')
		})
	})
})
Exemplo n.º 2
0
// Import
var expect = require('chai').expect;
var joe = require('joe');
var domain = require('./');

// =====================================
// Tests

joe.describe('domain-browser', function(describe,it){
	it('should work on throws', function(done){
		var d = domain.create();
		d.on('error', function(err){
			expect(err && err.message).to.eql('a thrown error');
			done();
		});
		d.run(function(){
			throw new Error('a thrown error');
		});
	});
});
Exemplo n.º 3
0
joe.describe('domain-browser', function(describe,it){
	it('should work on throws', function(done){
		var d = domain.create()
		d.on('error', function(err){
			expect(err && err.message).to.eql('a thrown error')
			done()
		})
		d.run(function(){
			throw new Error('a thrown error')
		})
	})

	it('should be able to add emitters', function(done){
		var d = domain.create()
		var emitter = new events.EventEmitter()

		d.add(emitter)
		d.on('error', function (err) {
			expect(err && err.message).to.eql('an emitted error')
			done()
		})

		emitter.emit('error', new Error('an emitted error'))
	})

	it('should be able to remove emitters', function (done){
		var emitter = new events.EventEmitter()
		var d = domain.create()

		d.add(emitter)
		var domainGotError = false
		d.on('error', function (err) {
			domainGotError = true
		})

		emitter.on('error', function (err) {
			expect(err && err.message).to.eql('This error should not go to the domain')

			// Make sure nothing race condition-y is happening
			setTimeout(function () {
				expect(domainGotError).to.eql(false)
				done()
			}, 0)
		})

		d.remove(emitter)
		emitter.emit('error', new Error('This error should not go to the domain'))
	})

	it('bind should work', function(done){
		var d = domain.create()
		d.on('error', function(err){
			expect(err && err.message).to.eql('a thrown error')
			done()
		})
		d.bind(function(err, a, b){
			expect(err && err.message).to.equal('a passed error')
			expect(a).to.equal(2)
			expect(b).to.equal(3)
			throw new Error('a thrown error')
		})(new Error('a passed error'), 2, 3)
	})

	it('intercept should work', function(done){
		var d = domain.create()
		var count = 0
		d.on('error', function(err){
			if ( count === 0 ) {
				expect(err && err.message).to.eql('a thrown error')
			} else if ( count === 1 ) {
				expect(err && err.message).to.eql('a passed error')
				done()
			}
			count++
		})

		d.intercept(function(a, b){
			expect(a).to.equal(2)
			expect(b).to.equal(3)
			throw new Error('a thrown error')
		})(null, 2, 3)

		d.intercept(function(a, b){
			throw new Error('should never reach here')
		})(new Error('a passed error'), 2, 3)
	})

})
Exemplo n.º 4
0
joe.describe('domain-browser', function (describe, it) {
	it('should work on throws', function (done) {
		const d = domain.create();
		d.on('error', function (err) {
			equal(err && err.message, 'a thrown error', 'error message');
			done()
		});
		d.run(function () {
			throw new Error('a thrown error')
		})
	});

	it('should be able to add emitters', function (done) {
		const d = domain.create();
		const emitter = new events.EventEmitter();

		d.add(emitter);
		d.on('error', function (err) {
			equal(err && err.message, 'an emitted error', 'error message');
			done()
		});

		emitter.emit('error', new Error('an emitted error'))
	});

	it('should be able to remove emitters', function (done) {
		const emitter = new events.EventEmitter();
		const d = domain.create();
		let domainGotError = false;

		d.add(emitter);
		d.on('error', function (err) {
			domainGotError = true
		});

		emitter.on('error', function (err) {
			equal(err && err.message, 'This error should not go to the domain', 'error message');

			// Make sure nothing race condition-y is happening
			setTimeout(function () {
				equal(domainGotError, false, 'no domain error');
				done()
			}, 0)
		});

		d.remove(emitter);
		emitter.emit('error', new Error('This error should not go to the domain'))
	});

	it('bind should work', function (done) {
		const d = domain.create();
		d.on('error', function (err) {
			equal(err && err.message, 'a thrown error', 'error message');
			done()
		});
		d.bind(function (err, a, b) {
			equal(err && err.message, 'a passed error', 'error message');
			equal(a, 2, 'value of a');
			equal(b, 3, 'value of b');
			throw new Error('a thrown error')
		})(new Error('a passed error'), 2, 3)
	});

	it('intercept should work', function (done) {
		const d = domain.create();
		let count = 0;
		d.on('error', function (err) {
			if ( count === 0 ) {
				equal(err && err.message, 'a thrown error', 'error message')
			}
			else if ( count === 1 ) {
				equal(err && err.message, 'a passed error', 'error message');
				done()
			}
			count++
		});

		d.intercept(function (a, b) {
			equal(a, 2, 'value of a');
			equal(b, 3, 'value of b');
			throw new Error('a thrown error')
		})(null, 2, 3);

		d.intercept(function (a, b) {
			throw new Error('should never reach here')
		})(new Error('a passed error'), 2, 3)
	})
});
Exemplo n.º 5
0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

var joe = require('joe');
var should = require('chai').should();
var sfmt = require('..');

joe.describe('indexed string format', function (describe, it) {
  it('should default to strings when no format is specified', function () {
    sfmt('a %{2} b %{0} c%{1} d%{3}', 'a', 'b', 'c', 'd').should.equal('a c b a cb dd');
  });

  it('should use formats when specified', function () {
    sfmt('a %{2:s} b %{0} c%{1:d} d%{3:j}', 'a', 12, 'c', 'd').should.equal('a c b a c12 d"d"');
  });

  it('should ignore unknown type format', function () {
    sfmt('a %{0:q}', 'hello').should.equal('a hello');
  });

  it('should be escaped with double percents', function () {
  	sfmt('%%{0} bc', 'one').should.equal('%{0} bc');
  })
});
Exemplo n.º 6
0
"use strict";
// Import
var expect = require('chai').expect,
	joe = require('joe')

// Test
joe.describe('feed plugin', function(describe,it){
	var Chainy = require('chainy-core').subclass().require('set').addExtension('feed', require('../'))
	it("should work", function(next){
		Chainy.create()
			.set('https://raw.githubusercontent.com/chainy-plugins/set/master/package.json')
			.feed()
			.done(function(err, result){
				if (err)  return next(err)
				expect(result && result.name).to.equal('chainy-plugin-set')
				return next()
			})
	})
})
Exemplo n.º 7
0
joe.describe('Object', function (describe, it) {
  describe('construction', function (describe, it) {
    it('should construct empty object', function () {
      var obj = mjo.o();
      expect(obj).to.be.an('object');
      expect(Object.keys(obj)).to.have.length(0);
    });

    it('should construct object with given properties', function () {
      var obj = mjo.o({a: 1, b: '2'});
      expect(obj).to.be.an('object');
      expect(obj.a).to.equal(1);
      expect(obj.b).to.equal('2');
      expect(Object.keys(obj)).to.have.length(2);
    });

    it('should construct from multiple source objects', function () {
      var obj = mjo.o({a: 1}, {b: '2'});
      expect(obj).to.be.an('object');
      expect(obj.a).to.equal(1);
      expect(obj.b).to.equal('2');
      expect(Object.keys(obj)).to.have.length(2);
    });

    it('should allow null in parameter list', function () {
      var obj = mjo.o({a: 1}, null, {b: '2'});
      expect(obj).to.be.an('object');
      expect(obj.a).to.equal(1);
      expect(obj.b).to.equal('2');
      expect(Object.keys(obj)).to.have.length(2);
    });

    it('should flatten array in parameter list', function () {
      var obj = mjo.o({a: 1}, [{b: '2'}, {c: 3}], {d: 4});
      expect(obj).to.be.an('object');
      expect(obj).to.have.all.keys({a:0, b:0, c:0, d:0});
    });

    it('should flatten arbitrarily deep arrays and ignore nulls and empty arrays', function () {
      var obj = mjo.o([[[{a:1}], [[null, {b: '2'}], {c: 3}], null], [{d: 4}], []]);
      expect(obj).to.be.an('object');
      expect(obj).to.have.all.keys({a:0, b:0, c:0, d:0});
      expect(obj).to.have.property('a', 1);
      expect(obj).to.have.property('b', '2');
      expect(obj).to.have.property('c', 3);
      expect(obj).to.have.property('d', 4);
    });
  });

  describe('extension', function (describe, it) {
    it('should return original object', function () {
      var obj = {a: 1};
      var o2 = mjo.extend(obj, {});
      expect(o2).equals(obj);
    });

    it('should add properties to original object', function () {
      var obj = {a: 1};
      mjo.extend(obj, {b: '2'}, [{c: 3, d: 4}]);
      expect(obj).to.have.all.keys(['a', 'b', 'c', 'd']);
      expect(obj).to.have.property('a', 1);
      expect(obj).to.have.property('b', '2');
      expect(obj).to.have.property('c', 3);
      expect(obj).to.have.property('d', 4);
    });
  });
});
Exemplo n.º 8
0
joe.describe('chainy', function(describe,it){
	var Chainy = require('../')

	it("should fail when attempting to extend the base class", function(){
		var err = null
		try {
			Chainy.addExtension('test', 'action', function(){})
		}
		catch (_err) {
			err = _err
		}
		expect(err && err.message).to.contain('frozen')
	})

	it("should pass when attempting to extend a child class", function(){
		var extension = function(){}
		var MyChainy = Chainy.subclass().addExtension('test', 'utility', extension)
		expect(MyChainy.prototype.test).to.eql(extension)
	})

	it("it should have autoloaded the set plugin", function(next){
		var a = [1,[2,3]]
		Chainy.create()
			.set(a)
			.done(function(err, result){
				if (err)  return next(err)
				expect(result).to.equal(a)
				return next()
			})
	})

	/*
	test currently disabled until we figure out how to prevent this from adding flatten to the dependencies in package.json
	it("it should autoinstall the flatten plugin on node v0.11+", function(next){
		var a = [1,[2,3]]
		try {
			Chainy.create().require('flatten')
				.set(a)
				.flatten()
				.done(function(err, result){
					if (err)  return next(err)
					expect(result).to.deep.equal([1,2,3])
					return next()
				})
		} catch ( err ) {
			if ( require('semver').satisfies(process.version, '>=0.11') === false ) {
				console.log('error expected as we are on node <0.11')
				next()
			} else {
				next(err)
			}
		}
	})
	*/

})
joe.describe('domain-browser', function(describe,it){
	it('should work on throws', function(done){
		var d = domain.create();
		d.on('error', function(err){
			expect(err && err.message).to.eql('a thrown error');
			done();
		});
		d.run(function(){
			throw new Error('a thrown error');
		});
	});

	it('should be able to add emitters', function(done){
		var d = domain.create();
		var emitter = new events.EventEmitter();

		d.add(emitter);
		d.on('error', function (err) {
			expect(err && err.message).to.eql('an emitted error');
			done();
		});

		emitter.emit('error', new Error('an emitted error'));
	});

	it('should be able to remove emitters', function (done){
		var emitter = new events.EventEmitter();
		var d = domain.create();

		d.add(emitter);
		var domainGotError = false;
		d.on('error', function (err) {
			domainGotError = true
		});

		emitter.on('error', function (err) {
			expect(err && err.message).to.eql('This error should not go to the domain')

			// Make sure nothing race condition-y is happening
			setTimeout(function () {
				expect(domainGotError).to.eql(false)
				done()
			}, 0)
		})

		d.remove(emitter);
		emitter.emit('error', new Error('This error should not go to the domain'));
	})
});
Exemplo n.º 10
0
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*   http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

var joe = require('joe');
var should = require('chai').should();
var sfmt = require('..');

joe.describe('named string format', function (describe, it) {
  it('should get values from object by name', function () {
    sfmt('a: %{name} b: %{value}', { name: 'chris', value: 'dev'})
      .should.equal('a: chris b: dev');
  });

  it('should return undefined in string for missing values', function () {
    sfmt('a: %{something} b: %{nothing}', { something: 5})
      .should.equal('a: 5 b: undefined');
  });
});
Exemplo n.º 11
0
// Test
joe.describe('pipe plugin', function(describe,it){
	var Chainy = require('chainy-core').subclass().require('set').addExtension('pipe', require('../'))
	var file = __dirname+'/.log'
	var cleanup = function(task, next){
		fsUtil.exists(file, function(exists){
			if ( exists ) {
				fsUtil.unlink(file, next)
			} else {
				next()
			}
		})
	}
	
	this.on('test.before', cleanup).on('test.after', cleanup)

	it("should work", function(next){
		var outputStream = fsUtil.createWriteStream(file)
		Chainy.create()
			.set('some data')
			.pipe(outputStream)
			.done(function(err, result){
				if (err)  return next(err)
				expect(result).to.equal('some data')
				fsUtil.readFile(file, function(err, data){
					expect(data.toString()).to.equal('some data')
					return next()
				})
			})
	})
})
Exemplo n.º 12
0
joe.describe('Maybeish', function (describe, it) {
  describe('dealing with null at root', function (describe, it) {
    it('should return null when wrapping null', function () {
      expect(maybe(null)()).to.be.null;
    });

    it('should return value when wrapping value', function () {
      expect(maybe(5)()).to.equal(5);
    });

    it('should return null when wrapping undefined', function () {
      expect(maybe(undefined)()).to.be.null;
    });

    it('should return null when accessing nested properties', function () {
      expect(maybe(null, 'no.properties.on.null')()).to.be.null;
    });

    it('should return null when accessing nested properties via calls', function () {
      expect(maybe(null)('no')('properties')()).to.be.null;
    });

    it('should not invoke value callback', function () {
      var spy = sinon.spy();
      maybe(null)(spy);
      spy.called.should.be.false;
    });

    it('should invoke null callback and return its result', function () {
      var valueSpy = sinon.stub().returns('should not be called');
      var nullSpy = sinon.stub().returns('called');

      var result = maybe(null)(valueSpy, nullSpy);

      valueSpy.called.should.be.false;
      nullSpy.callCount.should.equal(1);
      expect(result).to.equal('called');
    });
  });

  describe('dealing with an object', function () {
    // Sample object to use
    var obj = {
      one: 1,
      nested: {
        a: 2,
        b: 'three'
      },
      two: 2
    };

    it('should return obj when wrapping obj', function () {
      expect(maybe(obj)()).to.equal(obj);
    });

    it('should return property when accessing nested properties', function () {
      expect(maybe(obj, 'nested.b')()).to.equal(obj.nested.b);
    });

    it('should return property when accessing nested properties via calls', function () {
      expect(maybe(obj)('nested')('a')()).to.equal(obj.nested.a);
    });

    it('should not invoke null callback', function () {
      var valueSpy = sinon.spy();
      var nullSpy = sinon.spy();
      maybe(obj)(valueSpy, nullSpy);
      nullSpy.called.should.be.false;
    });

    it('should invoke value callback passing value and return its result', function () {
      var valueSpy = sinon.stub().returns('called');
      var nullSpy = sinon.stub().returns('not called');

      var result = maybe(obj, 'two')(valueSpy, nullSpy);

      nullSpy.called.should.be.false;
      valueSpy.callCount.should.equal(1);
      expect(result).to.equal('called');
      valueSpy.firstCall.calledWith(obj.two).should.be.true;
    });

    it('should be null if accessing property that does not exist', function () {
      expect(maybe(obj, 'notReal.b')()).to.be.null;
    });
  });
});