Last active
December 26, 2015 12:39
-
-
Save dhruvasagar/660fb401ea7f6dcfa52c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Describe(title, testFunc, parent) { | |
this._title = title; | |
this._parent = parent; | |
this._testFunc = testFunc; | |
if ( ! this._parent ) { // root | |
this.run(); | |
} | |
} | |
Describe.prototype.title = function() { | |
var title = ''; | |
if ( this._parent ) { | |
title = this._parent.title() + ' ' + this._title; | |
} else { | |
title = this._title; | |
} | |
return title; | |
}; | |
Describe.prototype.setup = function(cb) { | |
this._setup = cb; | |
}; | |
Describe.prototype.teardown = function(cb) { | |
this._teardown = cb; | |
}; | |
Describe.prototype._describe = function(title, testFunc, verbose) { | |
var describe = new Describe(title, testFunc, this); | |
describe.setup(this._setup); | |
describe.teardown(this._teardown); | |
if ( this._setup ) { | |
this._setup.call(describe); | |
} | |
describe.run(verbose); | |
if ( this._teardown ) { | |
this._teardown.call(describe); | |
} | |
}; | |
Describe.prototype.it = function(title, testFunc) { | |
this._describe(title, testFunc, true); | |
}; | |
Describe.prototype.describe = function(title, testFunc) { | |
this._describe(title, testFunc, false); | |
}; | |
Describe.prototype.run = function(verbose) { | |
try { | |
this._testFunc.call(this); | |
if ( verbose ) { | |
console.log('"' + this.title() + ' : Pass"\n'); | |
} | |
} catch(e) { | |
if ( verbose ) { | |
console.log('"' + this.title() + ' : Fail"'); | |
console.log(e.message + "\n"); | |
} | |
} | |
}; | |
module.exports = function(title, testFunc) { | |
return new Describe(title, testFunc); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var assert = require('assert'); | |
var describe = require('./describe'); | |
function UriBuilder(baseUri) { | |
var self = this; | |
this.params = {}; | |
if ( baseUri.indexOf('?') >= 0 ) { | |
var params = baseUri.match(/\?(.*)/); | |
params[1].split('&').forEach(function(p) { | |
var key_val = p.split('='); | |
var key = key_val[0], val = key_val[1]; | |
self.params[key] = val; | |
}); | |
this.baseUri = baseUri.substring(0, baseUri.indexOf('?')); | |
} else { | |
this.baseUri = baseUri; | |
} | |
} | |
UriBuilder.prototype.build = function() { | |
var params = this.params; | |
var paramsArray = []; | |
for (key in params) { | |
paramsArray.push(key + '=' + encodeURIComponent(params[key])); | |
} | |
if( paramsArray.length > 0 ) { | |
return this.baseUri + '?' + paramsArray.join('&'); | |
} else { | |
return this.baseUri; | |
} | |
}; | |
describe('UriBuilder without initial params', function() { | |
this.setup(function() { | |
this.builder = new UriBuilder('http://www.codewars.com'); | |
}); | |
this.it('should build correct uri with set parameters', function(test) { | |
this.builder.params.page = 1; | |
this.builder.params.language = 'javascript'; | |
assert.equal(this.builder.build(), 'http://www.codewars.com?page=1&language=javascript'); | |
}); | |
}); | |
describe('UriBuilder with pre-existing params', function(){ | |
this.setup(function() { | |
this.builder = new UriBuilder('http://www.codewars.com?page=1'); | |
}); | |
this.describe('should build correct uri', function() { | |
this.it('without changes', function() { | |
assert.equal(this.builder.build(), 'http://www.codewars.com?page=1'); | |
}); | |
this.describe('after', function() { | |
this.it('updating params', function() { | |
this.builder.params.page = 2; | |
assert.equal(this.builder.build(), 'http://www.codewars.com?page=2'); | |
}); | |
this.it('deleting a param', function() { | |
delete this.builder.params.page; | |
assert.equal(this.builder.build(), 'http://www.codewars.com'); | |
}); | |
this.it('adding param again', function() { | |
this.builder.params.page = 1; | |
assert.equal(this.builder.build(), 'http://www.codewars.com?page=1'); | |
}); | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment